Skip to content

Commit

Permalink
[Fix rubocop#10893] Fix loading behavior on running without `bundle e…
Browse files Browse the repository at this point in the history
…xec`
  • Loading branch information
r7kamura committed Aug 11, 2022
1 parent 8ca7aae commit 090f71b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
1 change: 1 addition & 0 deletions 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][])
4 changes: 3 additions & 1 deletion lib/rubocop/feature_loader.rb
Expand Up @@ -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

Expand Down
31 changes: 20 additions & 11 deletions spec/rubocop/feature_loader_spec.rb
Expand Up @@ -14,67 +14,76 @@
'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
'./path/to/feature'
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
'feature-foo'
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
'./feature-foo'
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
Expand Down

0 comments on commit 090f71b

Please sign in to comment.