From df1fd9270719f11924bf99f308f24475521d8861 Mon Sep 17 00:00:00 2001 From: fatkodima Date: Fri, 10 Mar 2023 22:25:10 +0200 Subject: [PATCH] Fix loading `database.yml` file with aliases --- .../fix_loading_database_yml_with_aliases.md | 1 + lib/rubocop/cop/rails/bulk_change_table.rb | 17 +++++++--- .../cop/rails/bulk_change_table_spec.rb | 31 +++++++++---------- 3 files changed, 28 insertions(+), 21 deletions(-) create mode 100644 changelog/fix_loading_database_yml_with_aliases.md diff --git a/changelog/fix_loading_database_yml_with_aliases.md b/changelog/fix_loading_database_yml_with_aliases.md new file mode 100644 index 0000000000..d491d5a04c --- /dev/null +++ b/changelog/fix_loading_database_yml_with_aliases.md @@ -0,0 +1 @@ +* [#947](https://github.com/rubocop/rubocop-rails/issues/947): Fix loading `database.yml` file with aliases. ([@fatkodima][]) diff --git a/lib/rubocop/cop/rails/bulk_change_table.rb b/lib/rubocop/cop/rails/bulk_change_table.rb index a076958ddb..44813b6525 100644 --- a/lib/rubocop/cop/rails/bulk_change_table.rb +++ b/lib/rubocop/cop/rails/bulk_change_table.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +require 'yaml' + module RuboCop module Cop module Rails @@ -192,11 +194,8 @@ def database_from_yaml def database_yaml return nil unless File.exist?('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 + src = File.read('config/database.yml') + yaml = load_yaml(src) return nil unless yaml.is_a? Hash config = yaml['development'] @@ -207,6 +206,14 @@ def database_yaml nil end + def load_yaml(src) + if Psych::VERSION > '4.0' + YAML.safe_load(src, aliases: true) + else + YAML.load(src) # rubocop:disable Security/YAMLLoad + end + end + def support_bulk_alter? case database when MYSQL diff --git a/spec/rubocop/cop/rails/bulk_change_table_spec.rb b/spec/rubocop/cop/rails/bulk_change_table_spec.rb index 20987f19c6..a43cd6994a 100644 --- a/spec/rubocop/cop/rails/bulk_change_table_spec.rb +++ b/spec/rubocop/cop/rails/bulk_change_table_spec.rb @@ -440,24 +440,22 @@ def change end context 'when `database.yml` is exists' do - let(:yaml) { nil } + let(:yaml) { '' } before do 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 + allow(File).to receive(:read).and_call_original + allow(File).to receive(:read).with('config/database.yml').and_return(yaml) end context 'mysql2' do let(:yaml) do - { - 'development' => { - 'adapter' => 'mysql2' - } - } + <<~YAML + default: &default + adapter: mysql2 + development: + <<: *default + YAML end it_behaves_like 'offense for mysql' @@ -465,11 +463,12 @@ def change context 'postgresql' do let(:yaml) do - { - 'development' => { - 'adapter' => 'postgresql' - } - } + <<~YAML + default: &default + adapter: postgresql + development: + <<: *default + YAML end context 'with Rails 5.2', :rails52 do