Skip to content

Commit

Permalink
Use expect_correction in Lint/NonDeterministicRequireOrder spec
Browse files Browse the repository at this point in the history
  • Loading branch information
biinari committed Jul 8, 2020
1 parent 80813b5 commit 739afb6
Showing 1 changed file with 22 additions and 32 deletions.
54 changes: 22 additions & 32 deletions spec/rubocop/cop/lint/non_deterministic_require_order_spec.rb
Expand Up @@ -7,37 +7,38 @@

context 'when requiring files' do
context 'with unsorted index' do
it 'registers an offsense' do
it 'registers an offsense and autocorrects to add .sort' do
expect_offense(<<~RUBY)
Dir["./lib/**/*.rb"].each do |file|
^^^^^^^^^^^^^^^^^^^^^^^^^ Sort files before requiring them.
require file
end
RUBY
expect_correction(<<~RUBY)
Dir["./lib/**/*.rb"].sort.each do |file|
require file
end
RUBY
end

it 'registers an offsense with extra logic' do
expect_offense(<<~RUBY)
Dir["./lib/**/*.rb"].each do |file|
^^^^^^^^^^^^^^^^^^^^^^^^^ Sort files before requiring them.
if file.starts_with('_')
if file.start_with?('_')
puts "Not required."
else
require file
end
end
RUBY
end

it 'auto-corrects to add .sort' do
new_source = autocorrect_source(<<~RUBY)
Dir["./lib/**/*.rb"].each do |file|
require file
end
RUBY
expect(new_source).to eq(<<~RUBY)
expect_correction(<<~RUBY)
Dir["./lib/**/*.rb"].sort.each do |file|
require file
if file.start_with?('_')
puts "Not required."
else
require file
end
end
RUBY
end
Expand Down Expand Up @@ -72,22 +73,14 @@
end

context 'with unsorted glob' do
it 'registers an offsense' do
it 'registers an offsense and autocorrects to add .sort' do
expect_offense(<<~RUBY)
Dir.glob(Rails.root.join(__dir__, 'test', '*.rb'), File::FNM_DOTMATCH).each do |file|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Sort files before requiring them.
require file
end
RUBY
end

it 'auto-corrects to add .sort' do
new_source = autocorrect_source(<<~RUBY)
Dir.glob(Rails.root.join(__dir__, 'test', '*.rb'), File::FNM_DOTMATCH).each do |file|
require file
end
RUBY
expect(new_source).to eq(<<~RUBY)
expect_correction(<<~RUBY)
Dir.glob(Rails.root.join(__dir__, 'test', '*.rb'), File::FNM_DOTMATCH).sort.each do |file|
require file
end
Expand Down Expand Up @@ -124,22 +117,14 @@
end

context 'with direct block glob' do
it 'registers an offsense' do
it 'registers an offsense and autocorrects to add .sort.each' do
expect_offense(<<~RUBY)
Dir.glob("./lib/**/*.rb") do |file|
^^^^^^^^^^^^^^^^^^^^^^^^^ Sort files before requiring them.
require file
end
RUBY
end

it 'auto-corrects to add .sort.each' do
new_source = autocorrect_source(<<~RUBY)
Dir.glob("./lib/**/*.rb") do |file|
require file
end
RUBY
expect(new_source).to eq(<<~RUBY)
expect_correction(<<~RUBY)
Dir.glob("./lib/**/*.rb").sort.each do |file|
require file
end
Expand Down Expand Up @@ -173,6 +158,11 @@
require file
end
RUBY
expect_correction(<<~RUBY)
::Dir.glob("./lib/**/*.rb").sort.each do |file|
require file
end
RUBY
end
end
end
Expand Down

0 comments on commit 739afb6

Please sign in to comment.