Skip to content

Commit 54c5884

Browse files
authored
Merge pull request rails#55446 from skipkayhil/hm-lsmkvnurqqruoswp
[rail_inspector] Fix recognizing `self.` config
2 parents ff15b03 + 1f8dc08 commit 54c5884

File tree

4 files changed

+107
-60
lines changed

4 files changed

+107
-60
lines changed

tools/rail_inspector/lib/rail_inspector/configuring.rb

Lines changed: 45 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,42 @@
66

77
module RailInspector
88
class Configuring
9-
class CachedParser
10-
def initialize
11-
@cache = {}
9+
class Files
10+
class Proxy
11+
def initialize(pathname)
12+
@pathname = pathname
13+
end
14+
15+
def parse
16+
@parse ||= Prism.parse_file(@pathname.to_s).value
17+
end
18+
19+
def read
20+
@pathname.read
21+
end
22+
23+
def write(string)
24+
@pathname.write(string)
25+
end
26+
27+
def to_s
28+
@pathname.to_s
29+
end
1230
end
1331

14-
def call(path)
15-
@cache[path] ||= Prism.parse_file(path.to_s).value
32+
def initialize(root)
33+
@root = Pathname.new(root)
34+
@files = {}
1635
end
17-
end
1836

19-
DOC_PATH = "guides/source/configuring.md"
20-
APPLICATION_CONFIGURATION_PATH =
21-
"railties/lib/rails/application/configuration.rb"
22-
NEW_FRAMEWORK_DEFAULTS_PATH =
23-
"railties/lib/rails/generators/rails/app/templates/config/initializers/new_framework_defaults_%{version}.rb.tt"
37+
def []=(name, path)
38+
@files[name] = Proxy.new(@root.join(path))
39+
end
40+
41+
def method_missing(name, ...)
42+
@files[name] || super
43+
end
44+
end
2445

2546
class Doc
2647
attr_accessor :general_config, :versioned_defaults
@@ -45,12 +66,19 @@ def to_s
4566
end
4667
end
4768

48-
attr_reader :errors, :parser
69+
attr_reader :errors, :files
4970

5071
def initialize(rails_path)
5172
@errors = []
52-
@parser = CachedParser.new
53-
@rails_path = Pathname.new(rails_path)
73+
@files = Files.new(rails_path)
74+
75+
@files[:application_configuration] = "railties/lib/rails/application/configuration.rb"
76+
@files[:doc_path] = "guides/source/configuring.md"
77+
@files[:rails_version] = "RAILS_VERSION"
78+
79+
@files[:new_framework_defaults] = "railties/lib/rails/generators/rails/app/templates/config/initializers/new_framework_defaults_%{version}.rb.tt" % {
80+
version: rails_version.tr(".", "_")
81+
}
5482
end
5583

5684
def check
@@ -60,27 +88,15 @@ def check
6088
end
6189

6290
def doc
63-
@doc ||=
64-
begin
65-
content = File.read(doc_path)
66-
Configuring::Doc.new(content)
67-
end
68-
end
69-
70-
def parse(relative_path)
71-
parser.call(@rails_path.join(relative_path))
72-
end
73-
74-
def read(relative_path)
75-
File.read(@rails_path.join(relative_path))
91+
@doc ||= Configuring::Doc.new(files.doc_path.read)
7692
end
7793

7894
def rails_version
79-
@rails_version ||= File.read(@rails_path.join("RAILS_VERSION")).to_f.to_s
95+
@rails_version ||= files.rails_version.read.to_f.to_s
8096
end
8197

8298
def write!
83-
File.write(doc_path, doc.to_s)
99+
files.doc_path.write(doc.to_s)
84100
end
85101

86102
def error_message
@@ -90,10 +106,5 @@ def error_message
90106
"Make sure new configurations are added to configuring.md#rails-general-configuration in alphabetical order.\n" +
91107
"Errors may be autocorrectable with the --autocorrect flag"
92108
end
93-
94-
private
95-
def doc_path
96-
@rails_path.join(DOC_PATH)
97-
end
98109
end
99110
end

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

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,40 +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.read(new_framework_defaults_path)
41-
end
42-
43-
def new_framework_defaults_path
44-
NEW_FRAMEWORK_DEFAULTS_PATH %
45-
{ version: checker.rails_version.tr(".", "_") }
46-
end
39+
end
4740
end
4841

4942
attr_reader :checker
@@ -55,7 +48,11 @@ def initialize(checker)
5548
def check
5649
header, *defaults_by_version = documented_defaults
5750

58-
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
5956

6057
checker.doc.versioned_defaults =
6158
header +
@@ -66,7 +63,7 @@ def check
6663

6764
private
6865
def app_config_tree
69-
checker.parse(APPLICATION_CONFIGURATION_PATH)
66+
checker.files.application_configuration.parse
7067
end
7168

7269
def check_defaults(defaults)
@@ -106,7 +103,7 @@ def check_defaults(defaults)
106103
end
107104

108105
checker.errors << <<~MESSAGE unless config_diff.empty?
109-
#{APPLICATION_CONFIGURATION_PATH}: Incorrect load_defaults docs
106+
#{checker.files.application_configuration}: Incorrect load_defaults docs
110107
--- Expected
111108
+++ Actual
112109
#{config_diff.split("\n")[5..].join("\n")}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def call
2121
APP_CONFIG_CONST = "Rails::Application::Configuration"
2222

2323
def app_config_tree
24-
@checker.parse(APPLICATION_CONFIGURATION_PATH)
24+
@checker.files.application_configuration.parse
2525
end
2626
end
2727

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)