From 97b69430d7b3b16ab330dca6b1e0d627e3849c4d Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Thu, 1 Jul 2021 19:49:27 +0900 Subject: [PATCH] [Fix #515] Fix an error for `Rails/BulkChangeTable` Fixes #515 Follow up https://github.com/ruby/psych/pull/487 and https://github.com/rails/rails/commit/255b5ff This PR fixes an error for `Rails/BulkChangeTable` when using Psych 4.0. --- CHANGELOG.md | 4 ++++ lib/rubocop/cop/rails/bulk_change_table.rb | 6 +++++- spec/rubocop/cop/rails/bulk_change_table_spec.rb | 12 ++++++------ 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 21013b4d91..e2a0f75f46 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## master (unreleased) +### Bug fixes + +* [#515](https://github.com/rubocop/rubocop-rails/issues/515): Fix an error for `Rails/BulkChangeTable` when using Psych 4.0. ([@koic][]) + ## 2.11.1 (2021-06-25) ### Bug fixes diff --git a/lib/rubocop/cop/rails/bulk_change_table.rb b/lib/rubocop/cop/rails/bulk_change_table.rb index 0d31f62ef5..fab0fa5796 100644 --- a/lib/rubocop/cop/rails/bulk_change_table.rb +++ b/lib/rubocop/cop/rails/bulk_change_table.rb @@ -194,7 +194,11 @@ def database_from_yaml def database_yaml return nil unless File.exist?('config/database.yml') - yaml = YAML.load_file('config/database.yml') + yaml = if YAML.respond_to?(:unsafe_load_file) + YAML.unsafe_load_file('config/database.yml') + else + YAML.load_file('config/database.yml') + end return nil unless yaml.is_a? Hash config = yaml['development'] diff --git a/spec/rubocop/cop/rails/bulk_change_table_spec.rb b/spec/rubocop/cop/rails/bulk_change_table_spec.rb index b4e239aadc..b344900662 100644 --- a/spec/rubocop/cop/rails/bulk_change_table_spec.rb +++ b/spec/rubocop/cop/rails/bulk_change_table_spec.rb @@ -414,12 +414,12 @@ def change let(:yaml) { nil } before do - allow(File).to receive(:exist?) - .with('config/database.yml') - .and_return(true) - allow(YAML).to receive(:load_file) - .with('config/database.yml') - .and_return(yaml) + allow(File).to receive(:exist?).with('config/database.yml').and_return(true) + if YAML.respond_to?(:unsafe_load_file) + allow(YAML).to receive(:unsafe_load_file).with('config/database.yml').and_return(yaml) + else + allow(YAML).to receive(:load_file).with('config/database.yml').and_return(yaml) + end end context 'mysql2' do