From 2df827fcc514736c6b4a3b39da6282bd3b9477c3 Mon Sep 17 00:00:00 2001 From: Nathaniel Watts Date: Sat, 4 Mar 2023 19:47:44 -0600 Subject: [PATCH 1/2] 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](https://github.com/rails/rails/commit/179d0a1f474ada02e0030ac3bd062fc653765dbe), but extracts into a module, given `YAML.load_file` is being called in multiple places. --- lib/test_data/config.rb | 4 +++- lib/test_data/configurators/cable_yaml.rb | 4 +++- lib/test_data/configurators/secrets_yaml.rb | 4 +++- lib/test_data/wrap/webpacker_config.rb | 4 +++- lib/test_data/yaml_loader.rb | 11 +++++++++++ 5 files changed, 23 insertions(+), 4 deletions(-) create mode 100644 lib/test_data/yaml_loader.rb diff --git a/lib/test_data/config.rb b/lib/test_data/config.rb index 9522638..f335549 100644 --- a/lib/test_data/config.rb +++ b/lib/test_data/config.rb @@ -1,3 +1,5 @@ +require_relative "./yaml_loader" + module TestData def self.config(pwd: Rails.root, &blk) @configuration ||= Configuration.new(pwd: pwd) @@ -95,7 +97,7 @@ def after_rails_fixture_load(callable = nil, &blk) end def database_yaml - YAML.load_file(database_yaml_full_path) + YAMLLoader.load_file(database_yaml_full_path) end def database_name diff --git a/lib/test_data/configurators/cable_yaml.rb b/lib/test_data/configurators/cable_yaml.rb index 836d0c4..3a0ceaf 100644 --- a/lib/test_data/configurators/cable_yaml.rb +++ b/lib/test_data/configurators/cable_yaml.rb @@ -1,3 +1,5 @@ +require_relative "../yaml_loader" + module TestData module Configurators class CableYaml @@ -8,7 +10,7 @@ def initialize def verify if !File.exist?(@config.cable_yaml_full_path) || - YAML.load_file(@config.cable_yaml_full_path).key?("test_data") + YAMLLoader.load_file(@config.cable_yaml_full_path).key?("test_data") ConfigurationVerification.new(looks_good?: true) else ConfigurationVerification.new(problems: [ diff --git a/lib/test_data/configurators/secrets_yaml.rb b/lib/test_data/configurators/secrets_yaml.rb index 86cbd24..56e1a41 100644 --- a/lib/test_data/configurators/secrets_yaml.rb +++ b/lib/test_data/configurators/secrets_yaml.rb @@ -1,3 +1,5 @@ +require_relative "../yaml_loader" + module TestData module Configurators class SecretsYaml @@ -8,7 +10,7 @@ def initialize def verify if !File.exist?(@config.secrets_yaml_full_path) || - YAML.load_file(@config.secrets_yaml_full_path).key?("test_data") + YAMLLoader.load_file(@config.secrets_yaml_full_path).key?("test_data") ConfigurationVerification.new(looks_good?: true) else ConfigurationVerification.new(problems: [ diff --git a/lib/test_data/wrap/webpacker_config.rb b/lib/test_data/wrap/webpacker_config.rb index 593e49a..4efe577 100644 --- a/lib/test_data/wrap/webpacker_config.rb +++ b/lib/test_data/wrap/webpacker_config.rb @@ -1,3 +1,5 @@ +require_relative "../yaml_loader" + module TestData module Wrap class WebpackerConfig @@ -34,7 +36,7 @@ def required_entries_missing_from_test_data_config private def load_yaml(path) - YAML.load_file(path) + YAMLLoader.load_file(path) rescue end end diff --git a/lib/test_data/yaml_loader.rb b/lib/test_data/yaml_loader.rb new file mode 100644 index 0000000..cd2ad37 --- /dev/null +++ b/lib/test_data/yaml_loader.rb @@ -0,0 +1,11 @@ +module TestData + module YAMLLoader + def self.load_file(path) + begin + YAML.load_file(path, aliases: true) + rescue ArgumentError + YAML.load_file(path) + end + end + end +end From 8200c85cdebda1ea76f1bee530d1eb9ece961f47 Mon Sep 17 00:00:00 2001 From: Nathaniel Watts Date: Mon, 6 Mar 2023 07:03:42 -0600 Subject: [PATCH 2/2] Lint --- lib/test_data/dumps_database.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/test_data/dumps_database.rb b/lib/test_data/dumps_database.rb index b9b1313..63a8961 100644 --- a/lib/test_data/dumps_database.rb +++ b/lib/test_data/dumps_database.rb @@ -80,7 +80,7 @@ def prepend_set_replication_role!(data_dump_path) def log_size_info_and_warnings(before_size:, after_size:) percent_change = percent_change(before_size, after_size) - TestData.log.info " Size: #{to_size(after_size)}#{" (#{percent_change}% #{before_size > after_size ? "decrease" : "increase"})" if percent_change}" + TestData.log.info " Size: #{to_size(after_size)}#{" (#{percent_change}% #{(before_size > after_size) ? "decrease" : "increase"})" if percent_change}" if after_size > 5242880 TestData.log.warn " WARNING: file size exceeds 5MB. Be sure to only persist what data you need to sufficiently test your application" end