Skip to content

Commit 2df827f

Browse files
committed
Add YAMLLoader module to account for Psych v4
Reference: https://stackoverflow.com/a/71192990 Calling `YAML.load_file` on the Rails `database.yaml` file (which has a `&default` alias in it) breaks given recent changes in Psych v4 in Ruby 3.1. This adjustment follows [a patch that can be found in Rails](rails/rails@179d0a1), but extracts into a module, given `YAML.load_file` is being called in multiple places.
1 parent 23a5900 commit 2df827f

5 files changed

Lines changed: 23 additions & 4 deletions

File tree

lib/test_data/config.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require_relative "./yaml_loader"
2+
13
module TestData
24
def self.config(pwd: Rails.root, &blk)
35
@configuration ||= Configuration.new(pwd: pwd)
@@ -95,7 +97,7 @@ def after_rails_fixture_load(callable = nil, &blk)
9597
end
9698

9799
def database_yaml
98-
YAML.load_file(database_yaml_full_path)
100+
YAMLLoader.load_file(database_yaml_full_path)
99101
end
100102

101103
def database_name

lib/test_data/configurators/cable_yaml.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require_relative "../yaml_loader"
2+
13
module TestData
24
module Configurators
35
class CableYaml
@@ -8,7 +10,7 @@ def initialize
810

911
def verify
1012
if !File.exist?(@config.cable_yaml_full_path) ||
11-
YAML.load_file(@config.cable_yaml_full_path).key?("test_data")
13+
YAMLLoader.load_file(@config.cable_yaml_full_path).key?("test_data")
1214
ConfigurationVerification.new(looks_good?: true)
1315
else
1416
ConfigurationVerification.new(problems: [

lib/test_data/configurators/secrets_yaml.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require_relative "../yaml_loader"
2+
13
module TestData
24
module Configurators
35
class SecretsYaml
@@ -8,7 +10,7 @@ def initialize
810

911
def verify
1012
if !File.exist?(@config.secrets_yaml_full_path) ||
11-
YAML.load_file(@config.secrets_yaml_full_path).key?("test_data")
13+
YAMLLoader.load_file(@config.secrets_yaml_full_path).key?("test_data")
1214
ConfigurationVerification.new(looks_good?: true)
1315
else
1416
ConfigurationVerification.new(problems: [

lib/test_data/wrap/webpacker_config.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require_relative "../yaml_loader"
2+
13
module TestData
24
module Wrap
35
class WebpackerConfig
@@ -34,7 +36,7 @@ def required_entries_missing_from_test_data_config
3436
private
3537

3638
def load_yaml(path)
37-
YAML.load_file(path)
39+
YAMLLoader.load_file(path)
3840
rescue
3941
end
4042
end

lib/test_data/yaml_loader.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module TestData
2+
module YAMLLoader
3+
def self.load_file(path)
4+
begin
5+
YAML.load_file(path, aliases: true)
6+
rescue ArgumentError
7+
YAML.load_file(path)
8+
end
9+
end
10+
end
11+
end

0 commit comments

Comments
 (0)