-
Notifications
You must be signed in to change notification settings - Fork 476
86 lines (71 loc) · 2.27 KB
/
regular_expressions.yml
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
82
83
84
85
86
name: Validate regular Expressions
on:
pull_request:
push:
branches: [ master ]
permissions:
contents: read
jobs:
regex:
name: Validate regular expressions
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Ruby
uses: ruby/[email protected]
with:
ruby-version: '3.1'
bundler-cache: true
- name: Validate regular expressions
run: |
cat <<- EOF > validate-regular-expressions.rb
require "yaml"
RegexSyntaxError = Class.new(StandardError)
module Warning
extend self
def warn(message)
raise RegexSyntaxError, message.gsub(/\A.+\.rb:\d+: warning: /, '').gsub(/\n$/, "")
end
end
def find_regexes(object)
result = []
object.each { |key, value|
if key == "regex"
result.push(value)
else
if key.is_a?(Hash) || key.is_a?(Array)
result.push(*find_regexes(key))
end
if value.is_a?(Hash) || value.is_a?(Array)
result.push(*find_regexes(value))
end
end
}
return result
end
success = true
Dir["regexes/**/*.yml"].each do |file|
data = YAML.load_file(file)
print "Checking file #{file}... "
warnings = []
regexes = 0
find_regexes(data).each do |regex_string|
regexes += 1
Regexp.new(regex_string, Regexp::IGNORECASE)
rescue RegexSyntaxError => e
success = false
warnings << "'#{regex_string}', warning: #{e.message}"
end
if warnings.any?
print "err\n\n"
warnings.each { puts " " + _1 }
puts "\n"
else
print "found #{regexes} regular expressions, all ok\n"
end
rescue
print "could not find regular expressions\n"
end
success || exit(1)
EOF
ruby validate-regular-expressions.rb