Skip to content

Commit a1771c3

Browse files
committed
ci: add config linter
1 parent 97f12fa commit a1771c3

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

ci/bin/build-and-test

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ require "fileutils"
44
require "pathname"
55
require "yaml"
66

7+
require_relative "lint-configs"
8+
79
def ensure_everything_is_committed
810
out = `git status -s`
911

@@ -31,7 +33,7 @@ config_path = if ENV.fetch("CONFIG_PATH", "") == ""
3133
end
3234

3335
config_env_var = %Q(CONFIG_PATH="#{config_path}") if config_path
34-
skip_flags = ENV.fetch("SKIPS", "--skip-javascript --skip-docker --skip-kamal --skip-solid")
36+
skip_flags = ENV.fetch("SKIPS", "--skip-javascript --skip-docker")
3537

3638
puts "=" * 80
3739
puts <<~EO_CONFIG_SUMMARY
@@ -90,6 +92,9 @@ Dir.chdir(app_path) do |cwd|
9092
puts "Checking everything is committed and there are no un-staged files"
9193
ensure_everything_is_committed
9294

95+
puts "Checking that no config properties have been duplicated in environment files"
96+
ensure_no_duplicate_config_lines_in_environment_files
97+
9398
puts "Running test suite"
9499
system("./bin/ci-run", exception: true)
95100
end

ci/bin/lint-configs.rb

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/usr/bin/env ruby
2+
3+
# Checks the given file for repeated "config.xyz = " lines
4+
#
5+
# @param [String] file
6+
#
7+
# @return [Array<String>]
8+
def check_file_for_repeated_config_properties(file)
9+
# @type [Array<String>]
10+
seen = []
11+
# @type [Array<String>]
12+
duplicates = []
13+
in_condition = false
14+
15+
File.readlines(file).each do |line|
16+
in_condition = true if line.strip.start_with?("if")
17+
in_condition = false if line.strip == "end"
18+
19+
# ignore conditions for now
20+
next if in_condition
21+
22+
line.match(/(config\.[a-zA-Z_]+) = /) do |match|
23+
duplicates << match[1] if seen.include?(match[1])
24+
25+
seen << match[1]
26+
end
27+
end
28+
29+
duplicates
30+
end
31+
32+
# Checks each environment config file for duplicate config properties,
33+
# as we should be reusing lines where possible to avoid confusion
34+
# and reduce the diff when doing Rails upgrades on applications
35+
def ensure_no_duplicate_config_lines_in_environment_files
36+
# @type [Hash<String, Array<String>>]
37+
results = {}
38+
39+
Dir.glob("**/config/environments/*.rb").each do |file|
40+
results[file] = check_file_for_repeated_config_properties(file)
41+
end
42+
43+
results.reject! { |_, duplicates| duplicates.empty? }
44+
45+
results.each do |file, duplicates|
46+
puts "#{file} has duplicate config properties:"
47+
duplicates.each { |line| puts " - #{line}" }
48+
end
49+
50+
raise "one or more environment files had duplicate configs" unless results.empty?
51+
end
52+
53+
if __FILE__ == $PROGRAM_NAME
54+
Dir.chdir(ARGV[0]) do
55+
ensure_no_duplicate_config_lines_in_environment_files
56+
end
57+
end

0 commit comments

Comments
 (0)