-
-
Notifications
You must be signed in to change notification settings - Fork 278
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
301 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module RSpec | ||
module Rails | ||
# Prefer to travel in `before` rather than `around`. | ||
# | ||
# @safety | ||
# This cop is unsafe because the automatic `travel_back` is only run | ||
# on test cases that are considered as Rails related. | ||
# | ||
# And also, this cop's autocorrection is unsafe because the order of | ||
# execution will change if other steps exist before traveling in | ||
# `around`. | ||
# | ||
# @example | ||
# # bad | ||
# around do |example| | ||
# freeze_time do | ||
# example.run | ||
# end | ||
# end | ||
# | ||
# # good | ||
# before { freeze_time } | ||
class TravelAround < Base | ||
extend AutoCorrector | ||
|
||
MSG = 'Prefer to travel in `before` rather than `around`.' | ||
|
||
TRAVEL_METHOD_NAMES = %i[ | ||
freeze_time | ||
travel | ||
travel_to | ||
].to_set.freeze | ||
|
||
# @!method extract_run_in_travel(node) | ||
def_node_matcher :extract_run_in_travel, <<~PATTERN | ||
(block | ||
$(send nil? TRAVEL_METHOD_NAMES ...) | ||
(args ...) | ||
(send _ :run) | ||
) | ||
PATTERN | ||
|
||
# @!method match_around_each?(node) | ||
def_node_matcher :match_around_each?, <<~PATTERN | ||
(block | ||
(send _ :around (sym :each)?) | ||
... | ||
) | ||
PATTERN | ||
|
||
def on_block(node) | ||
run_node = extract_run_in_travel(node) | ||
return unless run_node | ||
|
||
around_node = extract_surrounding_around_block(run_node) | ||
return unless around_node | ||
|
||
add_offense(node) do |corrector| | ||
autocorrect(corrector, node, run_node, around_node) | ||
end | ||
end | ||
alias on_numblock on_block | ||
|
||
private | ||
|
||
def autocorrect(corrector, node, run_node, around_node) | ||
corrector.replace( | ||
node, | ||
node.body.source | ||
) | ||
corrector.insert_before( | ||
around_node, | ||
"before { #{run_node.source} }\n\n" | ||
) | ||
end | ||
|
||
# @param node [RuboCop::AST::BlockNode] | ||
# @return [RuboCop::AST::BlockNode, nil] | ||
def extract_surrounding_around_block(node) | ||
node.each_ancestor(:block).find do |ancestor| | ||
match_around_each?(ancestor) | ||
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,157 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::RSpec::Rails::TravelAround do | ||
context 'with `freeze_time` in `before`' do | ||
it 'registers no offense' do | ||
expect_no_offenses(<<~RUBY) | ||
before { freeze_time } | ||
RUBY | ||
end | ||
end | ||
|
||
context 'with `freeze_time` in `around(:all)`' do | ||
it 'registers no offense' do | ||
expect_no_offenses(<<~RUBY) | ||
around(:all) do |example| | ||
freeze_time do | ||
example.run | ||
end | ||
end | ||
RUBY | ||
end | ||
end | ||
|
||
context 'with `freeze_time` in `around(:suite)`' do | ||
it 'registers no offense' do | ||
expect_no_offenses(<<~RUBY) | ||
around(:suite) do |example| | ||
freeze_time do | ||
example.run | ||
end | ||
end | ||
RUBY | ||
end | ||
end | ||
|
||
context 'with another step in `freeze_time`' do | ||
it 'registers no offense' do | ||
expect_no_offenses(<<~RUBY) | ||
around do |example| | ||
freeze_time do | ||
do_some_preparation | ||
example.run | ||
end | ||
end | ||
RUBY | ||
end | ||
end | ||
|
||
context 'with `freeze_time` in `around`' do | ||
it 'registers offense' do | ||
expect_offense(<<~RUBY) | ||
around do |example| | ||
freeze_time do | ||
^^^^^^^^^^^^^^ Prefer to travel in `before` rather than `around`. | ||
example.run | ||
end | ||
end | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
before { freeze_time } | ||
around do |example| | ||
example.run | ||
end | ||
RUBY | ||
end | ||
end | ||
|
||
context 'with `freeze_time` in `around(:each)`' do | ||
it 'registers offense' do | ||
expect_offense(<<~RUBY) | ||
around(:each) do |example| | ||
freeze_time do | ||
^^^^^^^^^^^^^^ Prefer to travel in `before` rather than `around`. | ||
example.run | ||
end | ||
end | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
before { freeze_time } | ||
around(:each) do |example| | ||
example.run | ||
end | ||
RUBY | ||
end | ||
end | ||
|
||
context 'with `freeze_time` and another node in `around`' do | ||
it 'registers offense' do | ||
expect_offense(<<~RUBY) | ||
around do |example| | ||
foo | ||
freeze_time do | ||
^^^^^^^^^^^^^^ Prefer to travel in `before` rather than `around`. | ||
example.run | ||
end | ||
end | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
before { freeze_time } | ||
around do |example| | ||
foo | ||
example.run | ||
end | ||
RUBY | ||
end | ||
end | ||
|
||
context 'with `travel` in `around`' do | ||
it 'registers offense' do | ||
expect_offense(<<~RUBY) | ||
around do |example| | ||
travel(duration) do | ||
^^^^^^^^^^^^^^^^^^^ Prefer to travel in `before` rather than `around`. | ||
example.run | ||
end | ||
end | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
before { travel(duration) } | ||
around do |example| | ||
example.run | ||
end | ||
RUBY | ||
end | ||
end | ||
|
||
context 'with `travel_to` in `around`' do | ||
it 'registers offense' do | ||
expect_offense(<<~RUBY) | ||
around do |example| | ||
travel_to(time) do | ||
^^^^^^^^^^^^^^^^^^ Prefer to travel in `before` rather than `around`. | ||
example.run | ||
end | ||
end | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
before { travel_to(time) } | ||
around do |example| | ||
example.run | ||
end | ||
RUBY | ||
end | ||
end | ||
end |