Skip to content

Commit 1f8dc08

Browse files
committed
[rail_inspector] Fix recognizing self. config
It previously would only find `self.` configs as `Rails.application.config`, but `Rails.configuration` should also be found.
1 parent 1fec9c9 commit 1f8dc08

File tree

2 files changed

+59
-18
lines changed

2 files changed

+59
-18
lines changed

tools/rail_inspector/lib/rail_inspector/configuring/check/framework_defaults.rb

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,35 +10,33 @@ class FrameworkDefaults
1010
class NewFrameworkDefaultsFile
1111
attr_reader :checker, :visitor
1212

13-
def initialize(checker, visitor)
13+
# Defaults are strings like:
14+
# self.yjit
15+
# action_controller.escape_json_responses
16+
def initialize(checker, defaults, file_content)
1417
@checker = checker
15-
@visitor = visitor
18+
@defaults = defaults
19+
@file_content = file_content
1620
end
1721

1822
def check
19-
visitor.config_map[checker.rails_version].each_key do |config|
20-
app_config = config.gsub(/^self/, "config")
23+
@defaults.each do |config|
24+
if config.start_with? "self"
25+
next if @file_content.include? config.gsub(/^self/, "config")
26+
next if @file_content.include? config.gsub(/^self/, "configuration")
27+
end
2128

22-
next if defaults_file_content.include? app_config
29+
next if @file_content.include? config
2330

2431
next if config == "self.yjit"
2532

26-
add_error(config)
27-
end
28-
end
29-
30-
private
31-
def add_error(config)
3233
checker.errors << <<~MESSAGE
33-
#{new_framework_defaults_path}
34-
Missing: #{config}
34+
#{checker.files.new_framework_defaults}: Missing new default
35+
#{config}
3536
3637
MESSAGE
3738
end
38-
39-
def defaults_file_content
40-
@defaults_file_content ||= checker.files.new_framework_defaults.read
41-
end
39+
end
4240
end
4341

4442
attr_reader :checker
@@ -50,7 +48,11 @@ def initialize(checker)
5048
def check
5149
header, *defaults_by_version = documented_defaults
5250

53-
NewFrameworkDefaultsFile.new(checker, visitor).check
51+
NewFrameworkDefaultsFile.new(
52+
checker,
53+
visitor.config_map[checker.rails_version].keys,
54+
checker.files.new_framework_defaults.read
55+
).check
5456

5557
checker.doc.versioned_defaults =
5658
header +
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# frozen_string_literal: true
2+
3+
require "test_helper"
4+
require "rail_inspector/configuring"
5+
6+
class TestFrameworkDefaults < ActiveSupport::TestCase
7+
def test_identifies_self_when_file_uses_config
8+
defaults = ["self.log_file_size"]
9+
10+
check(defaults, <<~FILE).check
11+
###
12+
# You must apply this in config/application.rb
13+
# config.log_file_size = 100 * 1024 * 1024
14+
FILE
15+
16+
assert_empty checker.errors
17+
end
18+
19+
def test_identifies_self_when_file_uses_configuration
20+
defaults = ["self.log_file_size"]
21+
22+
check(defaults, <<~FILE).check
23+
###
24+
# You must apply this in config/application.rb
25+
# Rails.configuration.log_file_size = 100 * 1024 * 1024
26+
FILE
27+
28+
assert_empty checker.errors
29+
end
30+
31+
private
32+
def check(defaults, file_content)
33+
@check ||= RailInspector::Configuring::Check::FrameworkDefaults::NewFrameworkDefaultsFile.new(checker, defaults, file_content)
34+
end
35+
36+
def checker
37+
@checker ||= RailInspector::Configuring.new("../..")
38+
end
39+
end

0 commit comments

Comments
 (0)