From 090f71b6515b902773407929958d25248235932b Mon Sep 17 00:00:00 2001 From: Ryo Nakamura Date: Thu, 11 Aug 2022 07:29:47 +0900 Subject: [PATCH] [Fix #10893] Fix loading behavior on running without `bundle exec` --- ...fix_loading_behavior_on_running_without.md | 1 + lib/rubocop/feature_loader.rb | 4 ++- spec/rubocop/feature_loader_spec.rb | 31 ++++++++++++------- 3 files changed, 24 insertions(+), 12 deletions(-) create mode 100644 changelog/fix_fix_loading_behavior_on_running_without.md diff --git a/changelog/fix_fix_loading_behavior_on_running_without.md b/changelog/fix_fix_loading_behavior_on_running_without.md new file mode 100644 index 00000000000..ae465e5904a --- /dev/null +++ b/changelog/fix_fix_loading_behavior_on_running_without.md @@ -0,0 +1 @@ +* [#10909](https://github.com/rubocop/rubocop/pull/10909): Fix loading behavior on running without `bundle exec`. ([@r7kamura][]) diff --git a/lib/rubocop/feature_loader.rb b/lib/rubocop/feature_loader.rb index e80fe0fbcab..af22a2ab868 100644 --- a/lib/rubocop/feature_loader.rb +++ b/lib/rubocop/feature_loader.rb @@ -37,7 +37,9 @@ def load raise if e.path != target begin - ::Kernel.require(namespaced_target) + # Don't use `::Kernel.require(target)` to prevent the following error: + # https://github.com/rubocop/rubocop/issues/10893 + require(namespaced_target) rescue ::LoadError => error_for_namespaced_target raise e if error_for_namespaced_target.path == namespaced_target diff --git a/spec/rubocop/feature_loader_spec.rb b/spec/rubocop/feature_loader_spec.rb index 4ac7cd102b1..c247bd37a3c 100644 --- a/spec/rubocop/feature_loader_spec.rb +++ b/spec/rubocop/feature_loader_spec.rb @@ -14,20 +14,28 @@ 'feature' end + let(:allow_feature_loader) do + allow_any_instance_of(described_class) # rubocop:disable RSpec/AnyInstance + end + + let(:expect_feature_loader) do + expect_any_instance_of(described_class) # rubocop:disable RSpec/AnyInstance + end + context 'with normally lodable feature' do before do - allow(Kernel).to receive(:require) + allow_feature_loader.to receive(:require) end it 'loads it normally' do - expect(Kernel).to receive(:require).with('feature') + expect_feature_loader.to receive(:require).with('feature') load end end context 'with dot-prefixed lodable feature' do before do - allow(Kernel).to receive(:require) + allow_feature_loader.to receive(:require) end let(:feature) do @@ -35,15 +43,15 @@ end it 'loads it as relative path' do - expect(Kernel).to receive(:require).with('path-to-config/./path/to/feature') + expect_feature_loader.to receive(:require).with('path-to-config/./path/to/feature') load end end context 'with namespaced feature' do before do - allow(Kernel).to receive(:require).with('feature-foo').and_call_original - allow(Kernel).to receive(:require).with('feature/foo') + allow_feature_loader.to receive(:require).with('feature-foo').and_call_original + allow_feature_loader.to receive(:require).with('feature/foo') end let(:feature) do @@ -51,15 +59,16 @@ end it 'loads it as namespaced feature' do - expect(Kernel).to receive(:require).with('feature/foo') + expect_feature_loader.to receive(:require).with('feature/foo') load end end context 'with dot-prefixed namespaced feature' do before do - allow(Kernel).to receive(:require).with('path-to-config/./feature-foo').and_call_original - allow(Kernel).to receive(:require).with('path-to-config/./feature/foo') + allow_feature_loader.to receive(:require).with('path-to-config/./feature-foo') + .and_call_original + allow_feature_loader.to receive(:require).with('path-to-config/./feature/foo') end let(:feature) do @@ -67,14 +76,14 @@ end it 'loads it as namespaced feature' do - expect(Kernel).to receive(:require).with('path-to-config/./feature/foo') + expect_feature_loader.to receive(:require).with('path-to-config/./feature/foo') load end end context 'with unexpected LoadError from require' do before do - allow(Kernel).to receive(:require).and_raise(LoadError) + allow_feature_loader.to receive(:require).and_raise(LoadError) end it 'raises LoadError' do