-
-
Notifications
You must be signed in to change notification settings - Fork 276
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Instead of calling `output` with an empty string, you should use the inverse runner and call `output` without an argument. E.g. instead of expect { foo }.to output('').to_stdout you should call expect { foo }.not_to output.to_stdout
- Loading branch information
Showing
8 changed files
with
156 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module RSpec | ||
# Check that the `output` matcher is not called with an empty string. | ||
# | ||
# @example | ||
# # bad | ||
# expect { foo }.to output('').to_stdout | ||
# expect { bar }.not_to output('').to_stderr | ||
# | ||
# # good | ||
# expect { foo }.not_to output.to_stdout | ||
# expect { bar }.to output.to_stderr | ||
# | ||
class EmptyOutput < Base | ||
extend AutoCorrector | ||
|
||
MSG = 'Use `%<runner>s` instead of matching on an empty output.' | ||
RESTRICT_ON_SEND = Runners.all | ||
|
||
# @!method matching_empty_output(node) | ||
def_node_matcher :matching_empty_output, <<~PATTERN | ||
(send | ||
(block | ||
(send nil? :expect) ... | ||
) | ||
#Runners.all | ||
(send $(send nil? :output (str empty?)) ...) | ||
) | ||
PATTERN | ||
|
||
def on_send(send_node) | ||
matching_empty_output(send_node) do |node| | ||
runner = send_node.method?(:to) ? 'not_to' : 'to' | ||
message = format(MSG, runner: runner) | ||
add_offense(node, message: message) do |corrector| | ||
corrector.replace(send_node.loc.selector, runner) | ||
corrector.replace(node, 'output') | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::RSpec::EmptyOutput, :config do | ||
it 'registers an offense when using `#output` with an empty string' do | ||
expect_offense(<<~RUBY) | ||
expect { foo }.to output('').to_stderr | ||
^^^^^^^^^^ Use `not_to` instead of matching on an empty output. | ||
expect { foo }.to output('').to_stdout | ||
^^^^^^^^^^ Use `not_to` instead of matching on an empty output. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
expect { foo }.not_to output.to_stderr | ||
expect { foo }.not_to output.to_stdout | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when negatively matching `#output` with ' \ | ||
'an empty string' do | ||
expect_offense(<<~RUBY) | ||
expect { foo }.not_to output('').to_stderr | ||
^^^^^^^^^^ Use `to` instead of matching on an empty output. | ||
expect { foo }.to_not output('').to_stdout | ||
^^^^^^^^^^ Use `to` instead of matching on an empty output. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
expect { foo }.to output.to_stderr | ||
expect { foo }.to output.to_stdout | ||
RUBY | ||
end | ||
|
||
describe 'compound expectations' do | ||
it 'does not register an offense when matching empty strings' do | ||
expect_no_offenses(<<~RUBY) | ||
expect { | ||
:noop | ||
}.to output('').to_stdout.and output('').to_stderr | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when matching non-empty strings' do | ||
expect_no_offenses(<<~RUBY) | ||
expect { | ||
warn "foo" | ||
puts "bar" | ||
}.to output("bar\n").to_stdout.and output(/foo/).to_stderr | ||
RUBY | ||
end | ||
end | ||
|
||
it 'does not register an offense when using `#output` with ' \ | ||
'a non-empty string' do | ||
expect_no_offenses(<<~RUBY) | ||
expect { foo }.to output('foo').to_stderr | ||
expect { foo }.not_to output('foo').to_stderr | ||
expect { foo }.to_not output('foo').to_stderr | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `not_to output`' do | ||
expect_no_offenses(<<~RUBY) | ||
expect { foo }.not_to output.to_stderr | ||
expect { foo }.to_not output.to_stderr | ||
RUBY | ||
end | ||
end |