Skip to content

Commit

Permalink
Reconfigure RSpec/PredicateMatcher for better expectation failure m…
Browse files Browse the repository at this point in the history
…essages

Changed expectations would previously output messages like this:
```
     Failure/Error: expect(options_keys.include?(:aautocorrect)).to be(true)

       expected true
            got false
```

The new expectations print the actual vs expected value:
```
     Failure/Error: expect(options_keys).to include(:aautocorrect)

       expected [:autocorrect_all, :autocorrect] to include :aautocorrect
       Diff:
       @@ -1 +1 @@
       -[:aautocorrect]
       +[:autocorrect_all, :autocorrect]
```

The same improvement also applies to something like `expect($stderr.string.include?("whatever)`.to be(true)`
or `expect(foo.nil?).to be(true)` (and probably more)
  • Loading branch information
Earlopain committed Jun 17, 2024
1 parent d0707df commit fe8a333
Show file tree
Hide file tree
Showing 51 changed files with 476 additions and 498 deletions.
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ RSpec:
- expect_offense

RSpec/PredicateMatcher:
EnforcedStyle: explicit
Strict: false

RSpec/SpecFilePathFormat:
CustomTransform:
Expand Down
4 changes: 3 additions & 1 deletion lib/rubocop/cop/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,9 @@ def line(node_or_range)
def same_line?(node1, node2)
line1 = line(node1)
line2 = line(node2)
line1 && line2 && line1 == line2
return false unless line1 && line2

line1 == line2
end

def indent(node, offset: 0)
Expand Down
8 changes: 4 additions & 4 deletions spec/project_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def expected
description = config.dig(name, 'Description')
expect(description.nil?).to(be(false),
"`Description` configuration is required for `#{name}`.")
expect(description.include?("\n")).to be(false)
expect(description).not_to include("\n")

start_with_subject = description.match(/\AThis cop (?<verb>.+?) .*/)
suggestion = start_with_subject[:verb]&.capitalize if start_with_subject
Expand Down Expand Up @@ -194,7 +194,7 @@ def expected
let(:non_reference_lines) { lines.take_while { |line| !line.start_with?('[@') } }

it 'has newline at end of file' do
expect(changelog.end_with?("\n")).to be true
expect(changelog).to end_with("\n")
end

it 'has either entries, headers, empty lines, or comments' do
Expand Down Expand Up @@ -224,7 +224,7 @@ def expected
end

it 'has a reference' do
issues.each { |issue| expect(issue[:ref].blank?).to be(false) }
issues.each { |issue| expect(issue[:ref]).not_to be_blank }
end

it 'has a valid issue number prefixed with #' do
Expand Down Expand Up @@ -306,7 +306,7 @@ def expected
dir = File.expand_path('../changelog', __dir__)

it 'does not have a directory' do
expect(Dir["#{dir}/*"].none? { |path| File.directory?(path) }).to be(true)
expect(Dir["#{dir}/*"]).to be_none { |path| File.directory?(path) }
end

Dir["#{dir}/*.md"].each do |path|
Expand Down
32 changes: 16 additions & 16 deletions spec/rubocop/cli/auto_gen_config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def f
$stdout = StringIO.new
expect(RuboCop::CLI.new.run([])).to eq(0)
expect($stderr.string).to eq('')
expect($stdout.string.include?('no offenses detected')).to be(true)
expect($stdout.string).to include('no offenses detected')
end
end
end
Expand Down Expand Up @@ -148,7 +148,7 @@ def f

it "bases other cops' configuration on the overridden LineLength:Max" do
expect(cli.run(['--auto-gen-config'])).to eq(0)
expect($stdout.string.include?(<<~YAML)).to be(true)
expect($stdout.string).to include(<<~YAML)
Added inheritance from `.rubocop_todo.yml` in `.rubocop.yml`.
Phase 1 of 2: run Layout/LineLength cop (skipped because the default Layout/LineLength:Max is overridden)
Phase 2 of 2: run all cops
Expand Down Expand Up @@ -194,7 +194,7 @@ def f

it 'skips the cop from both phases of the run' do
expect(cli.run(['--auto-gen-config'])).to eq(0)
expect($stdout.string.include?(<<~YAML)).to be(true)
expect($stdout.string).to include(<<~YAML)
Added inheritance from `.rubocop_todo.yml` in `.rubocop.yml`.
Phase 1 of 2: run Layout/LineLength cop (skipped because Layout/LineLength is disabled)
Phase 2 of 2: run all cops
Expand Down Expand Up @@ -244,7 +244,7 @@ def f

it "bases other cops' configuration on the overridden LineLength:Max" do
expect(cli.run(['--auto-gen-config'])).to eq(0)
expect($stdout.string.include?(<<~YAML)).to be(true)
expect($stdout.string).to include(<<~YAML)
Added inheritance from `.rubocop_todo.yml` in `.rubocop.yml`.
Phase 1 of 2: run Layout/LineLength cop (skipped because the default Layout/LineLength:Max is overridden)
Phase 2 of 2: run all cops
Expand Down Expand Up @@ -511,7 +511,7 @@ def f
Max: 95
YAML
expect(cli.run(%w[--auto-gen-config --config dir/cop_config.yml])).to eq(0)
expect(Dir['.*'].include?('.rubocop_todo.yml')).to be(true)
expect(Dir['.*']).to include('.rubocop_todo.yml')
todo_contents = File.read('.rubocop_todo.yml').lines[8..].join
expect(todo_contents).to eq(<<~YAML)
# Offense count: 1
Expand Down Expand Up @@ -552,7 +552,7 @@ def f
create_empty_file('cop_config.yml')

expect(cli.run(%w[--auto-gen-config --config cop_config.yml])).to eq(0)
expect(Dir['.*'].include?('.rubocop_todo.yml')).to be(true)
expect(Dir['.*']).to include('.rubocop_todo.yml')
todo_contents = File.read('.rubocop_todo.yml').lines[8..].join
expect(todo_contents).to eq(<<~YAML)
# Offense count: 1
Expand Down Expand Up @@ -597,7 +597,7 @@ def fooBar; end
expect(cli.run(%w[--auto-gen-config])).to eq(0)
expect($stderr.string).to eq('')
# expect($stdout.string).to include('Created .rubocop_todo.yml.')
expect(Dir['.*'].include?('.rubocop_todo.yml')).to be(true)
expect(Dir['.*']).to include('.rubocop_todo.yml')
todo_contents = File.read('.rubocop_todo.yml').lines[8..].join
expect(todo_contents).to eq(<<~YAML)
# Offense count: 1
Expand Down Expand Up @@ -639,7 +639,7 @@ def fooBar; end
YAML
expect(cli.run(%w[--auto-gen-config])).to eq(0)
expect($stderr.string).to eq('')
expect(Dir['.*'].include?('.rubocop_todo.yml')).to be(true)
expect(Dir['.*']).to include('.rubocop_todo.yml')
todo_contents = File.read('.rubocop_todo.yml').lines[8..].join
expect(todo_contents).to eq(<<~YAML)
# Offense count: 1
Expand Down Expand Up @@ -673,7 +673,7 @@ def foo; end
YAML
expect(cli.run(%w[--auto-gen-config])).to eq(0)
expect($stderr.string).to eq('')
expect(Dir['.*'].include?('.rubocop_todo.yml')).to be(true)
expect(Dir['.*']).to include('.rubocop_todo.yml')
todo_contents = File.read('.rubocop_todo.yml').lines[8..].join
expect(todo_contents).to eq(<<~YAML)
# Offense count: 1
Expand Down Expand Up @@ -711,7 +711,7 @@ def foo; end
Dir.chdir('dir') { expect(cli.run(%w[--auto-gen-config])).to eq(0) }
expect($stderr.string).to eq('')
# expect($stdout.string).to include('Created .rubocop_todo.yml.')
expect(Dir['dir/.*'].include?('dir/.rubocop_todo.yml')).to be(true)
expect(Dir['dir/.*']).to include('dir/.rubocop_todo.yml')
todo_contents = File.read('dir/.rubocop_todo.yml').lines[8..].join
expect(todo_contents).to eq(<<~YAML)
# Offense count: 1
Expand Down Expand Up @@ -752,7 +752,7 @@ def foo; end
YAML
expect(cli.run(%w[--auto-gen-config])).to eq(0)
expect($stderr.string).to eq('')
expect($stdout.string.include?(<<~YAML)).to be(true)
expect($stdout.string).to include(<<~YAML)
Added inheritance from `.rubocop_todo.yml` in `.rubocop.yml`.
YAML
expect(File.read('.rubocop.yml')).to eq(<<~YAML)
Expand All @@ -772,7 +772,7 @@ def foo; end
YAML
expect(cli.run(%w[--auto-gen-config])).to eq(0)
expect($stderr.string).to eq('')
expect($stdout.string.include?(<<~YAML)).to be(true)
expect($stdout.string).to include(<<~YAML)
Added inheritance from `.rubocop_todo.yml` in `.rubocop.yml`.
YAML
expect(File.read('.rubocop.yml')).to eq(<<~YAML)
Expand Down Expand Up @@ -808,7 +808,7 @@ def a; end

expect(cli.run(['--auto-gen-config'])).to eq(0)
expect($stderr.string).to eq('')
expect($stdout.string.include?('Created .rubocop_todo.yml.')).to be(true)
expect($stdout.string).to include('Created .rubocop_todo.yml.')
expected =
['# This configuration was generated by',
'# `rubocop --auto-gen-config`',
Expand Down Expand Up @@ -1215,7 +1215,7 @@ def a; end

expect(cli.run(['--auto-gen-config', '--no-offense-counts'])).to eq(0)
expect($stderr.string).to eq('')
expect($stdout.string.include?('Created .rubocop_todo.yml.')).to be(true)
expect($stdout.string).to include('Created .rubocop_todo.yml.')
expected =
['# This configuration was generated by',
'# `rubocop --auto-gen-config --no-offense-counts`',
Expand Down Expand Up @@ -1310,7 +1310,7 @@ def function(arg1, arg2, arg3, arg4, arg5, arg6, arg7)
$stdout = StringIO.new
expect(cli.run(['--auto-gen-config', '--auto-gen-only-exclude',
'--exclude-limit', '1'])).to eq(0)
expect($stdout.string.include?(<<~STRING)).to be(true)
expect($stdout.string).to include(<<~STRING)
Phase 1 of 2: run Layout/LineLength cop (skipped because only excludes will be generated due to `--auto-gen-only-exclude` flag)
Phase 2 of 2: run all cops
STRING
Expand Down Expand Up @@ -1481,7 +1481,7 @@ def function(arg1, arg2, arg3, arg4, arg5, arg6, arg7)
it 'displays report summary but no offenses' do
expect(cli.run(['--auto-gen-config'])).to eq(0)

expect($stdout.string.include?(<<~OUTPUT)).to be(true)
expect($stdout.string).to include(<<~OUTPUT)
Inspecting 1 file
C
Expand Down
8 changes: 4 additions & 4 deletions spec/rubocop/cli/autocorrect_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -875,7 +875,7 @@ def method
expect(
cli.run(['-a', '--only', 'Layout/SpaceAroundKeyword,Layout/SpaceInsideRangeLiteral'])
).to eq(0)
expect($stdout.string.include?('no offenses detected')).to be(true)
expect($stdout.string).to include('no offenses detected')
expect(File.read('example.rb')).to eq(source)
end

Expand Down Expand Up @@ -1363,7 +1363,7 @@ def func2
uncorrected = $stdout.string.split($RS).select do |line|
line.include?('example.rb:') && !line.include?('[Corrected]')
end
expect(uncorrected.empty?).to be(true) # Hence exit code 0.
expect(uncorrected).to be_empty # Hence exit code 0.
end

it 'corrects only IndentationWidth without crashing' do
Expand Down Expand Up @@ -1401,7 +1401,7 @@ def func2
Enabled: false
YAML
expect(cli.run(['--autocorrect-all'])).to eq(0)
expect($stdout.string.include?('no offenses detected')).to be(true)
expect($stdout.string).to include('no offenses detected')
expect(File.read('example.rb')).to eq("#{source}\n")
end

Expand Down Expand Up @@ -1590,7 +1590,7 @@ module B
uncorrected = $stdout.string.split($RS).select do |line|
line.include?('example.rb:') && !line.include?('[Corrected]')
end
expect(uncorrected.empty?).to be(false) # Hence exit code 1.
expect(uncorrected).not_to be_empty # Hence exit code 1.
end

it 'can correct single line methods' do
Expand Down
Loading

0 comments on commit fe8a333

Please sign in to comment.