Skip to content

Commit daaa87c

Browse files
authored
Merge pull request rails#52665 from Shopify/config-file-bootsnap
Optimize ConfigurationFile when ERB is not used
2 parents 888d284 + b8924f6 commit daaa87c

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

activesupport/lib/active_support/configuration_file.rb

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,18 @@ def self.parse(content_path, **options)
2020

2121
def parse(context: nil, **options)
2222
source = render(context)
23-
if YAML.respond_to?(:unsafe_load)
24-
YAML.unsafe_load(source, **options) || {}
23+
if source == @content
24+
if YAML.respond_to?(:unsafe_load)
25+
YAML.unsafe_load_file(@content_path, **options) || {}
26+
else
27+
YAML.load_file(@content_path, **options) || {}
28+
end
2529
else
26-
YAML.load(source, **options) || {}
30+
if YAML.respond_to?(:unsafe_load)
31+
YAML.unsafe_load(source, **options) || {}
32+
else
33+
YAML.load(source, **options) || {}
34+
end
2735
end
2836
rescue Psych::SyntaxError => error
2937
raise "YAML syntax error occurred while parsing #{@content_path}. " \

activesupport/test/configuration_file_test.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class ConfigurationFileTest < ActiveSupport::TestCase
66
test "backtrace contains YAML path" do
77
Tempfile.create do |file|
88
file.write("wrong: <%= foo %>")
9-
file.rewind
9+
file.flush
1010

1111
error = assert_raises do
1212
ActiveSupport::ConfigurationFile.parse(file.path)
@@ -19,7 +19,7 @@ class ConfigurationFileTest < ActiveSupport::TestCase
1919
test "backtrace contains YAML path (when Pathname given)" do
2020
Tempfile.create do |file|
2121
file.write("wrong: <%= foo %>")
22-
file.rewind
22+
file.flush
2323

2424
error = assert_raises do
2525
ActiveSupport::ConfigurationFile.parse(Pathname(file.path))
@@ -28,4 +28,14 @@ class ConfigurationFileTest < ActiveSupport::TestCase
2828
assert_match file.path, error.backtrace.first
2929
end
3030
end
31+
32+
test "load raw YAML" do
33+
Tempfile.create do |file|
34+
file.write("ok: 42")
35+
file.flush
36+
37+
data = ActiveSupport::ConfigurationFile.parse(Pathname(file.path))
38+
assert_equal({ "ok" => 42 }, data)
39+
end
40+
end
3141
end

0 commit comments

Comments
 (0)