-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
json_syntax_check
executable file
·81 lines (67 loc) · 1.76 KB
/
json_syntax_check
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'find'
require 'json'
# JSON checker calling module
module JSONChecker
def self.run(argv)
patterns = argv
checker = Checker.new(patterns)
failures = checker.run
File.open(ENV.fetch('GITHUB_OUTPUT', './test_output'), 'a') do |f|
f.puts "failed_files=#{JSON.dump(failures.map(&:file))}"
end
if failures.empty?
puts "No file has JSON syntax error (of #{checker.files.length} files)"
else
puts "Files below has/have JSON syntax error (of #{checker.files.length} files)"
failures.each do |f|
puts "- #{f.file}: #{f.hint}"
end
end
failures.length
end
end
# Syntax failure
class Failure
attr_reader :file, :hint
def initialize(file, hint)
@file = file
@hint = hint
end
end
# JSON checker class
class Checker
def initialize(patterns)
@patterns = patterns.map { |pat| Regexp.compile(pat) }
@files = nil
@base = ENV.fetch('BASE', ENV.fetch('GITHUB_WORKSPACE'))
@debug = ENV.fetch('DEBUG', '0') != '0'
end
def files
return @files unless @files.nil?
@files = []
Find.find(@base) do |file|
next unless File.file?(file)
next unless @patterns.any? { |pat| pat.match?(file) }
@files << file
end
puts "base=#{@base} ptns=#{@patterns} files=#{@files}" if @debug
@files
end
def run
failures = files.map { |f| validate_json(f) }
failures.compact
end
def validate_json(file)
JSON.parse(File.read(file))
rescue JSON::ParserError => e
hint = e.message.lines.first.chomp unless e.message.lines.nil?
hint += "' ..." if e.message.lines.length > 1
hint ||= 'Unknown JSON parse error'
Failure.new(file, hint)
else
nil
end
end
exit JSONChecker.run(ARGV)