Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
e016d8d2
Commit
e016d8d2
authored
Oct 07, 2020
by
Alishan Ladhani
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Create cop to detect usages of Timecop.travel
parent
4985088f
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
101 additions
and
0 deletions
+101
-0
.rubocop.yml
.rubocop.yml
+8
-0
rubocop/cop/rspec/timecop_travel.rb
rubocop/cop/rspec/timecop_travel.rb
+41
-0
spec/rubocop/cop/rspec/timecop_travel_spec.rb
spec/rubocop/cop/rspec/timecop_travel_spec.rb
+52
-0
No files found.
.rubocop.yml
View file @
e016d8d2
...
...
@@ -300,6 +300,14 @@ RSpec/TimecopFreeze:
-
'
ee/spec/**/*.rb'
-
'
qa/spec/**/*.rb'
RSpec/TimecopTravel
:
Enabled
:
false
AutoCorrect
:
true
Include
:
-
'
spec/**/*.rb'
-
'
ee/spec/**/*.rb'
-
'
qa/spec/**/*.rb'
Naming/PredicateName
:
Enabled
:
true
Exclude
:
...
...
rubocop/cop/rspec/timecop_travel.rb
0 → 100644
View file @
e016d8d2
# frozen_string_literal: true
module
RuboCop
module
Cop
module
RSpec
# This cop checks for `Timecop.travel` usage in specs.
#
# @example
#
# # bad
# Timecop.travel(1.day.ago) { create(:issue) }
#
# # good
# travel_to(1.day.ago) { create(:issue) }
#
class
TimecopTravel
<
RuboCop
::
Cop
::
Cop
include
MatchRange
MESSAGE
=
'Do not use `Timecop.travel`, use `travel_to` instead. '
\
'See https://gitlab.com/gitlab-org/gitlab/-/issues/214432 for more info.'
def_node_matcher
:timecop_travel?
,
<<~
PATTERN
(send (const nil? :Timecop) :travel _)
PATTERN
def
on_send
(
node
)
return
unless
timecop_travel?
(
node
)
add_offense
(
node
,
location: :expression
,
message:
MESSAGE
)
end
def
autocorrect
(
node
)
->
(
corrector
)
do
each_match_range
(
node
.
source_range
,
/^(Timecop\.travel)/
)
do
|
match_range
|
corrector
.
replace
(
match_range
,
'travel_to'
)
end
end
end
end
end
end
end
spec/rubocop/cop/rspec/timecop_travel_spec.rb
0 → 100644
View file @
e016d8d2
# frozen_string_literal: true
require
'fast_spec_helper'
require
'rubocop'
require
'rubocop/rspec/support'
require_relative
'../../../../rubocop/cop/rspec/timecop_travel'
RSpec
.
describe
RuboCop
::
Cop
::
RSpec
::
TimecopTravel
,
type: :rubocop
do
include
CopHelper
subject
(
:cop
)
{
described_class
.
new
}
context
'when calling Timecop.travel'
do
let
(
:source
)
do
<<~
SRC
Timecop.travel(1.day.ago) { create(:issue) }
SRC
end
let
(
:corrected_source
)
do
<<~
SRC
travel_to(1.day.ago) { create(:issue) }
SRC
end
it
'registers an offence'
do
inspect_source
(
source
)
expect
(
cop
.
offenses
.
size
).
to
eq
(
1
)
end
it
'can autocorrect the source'
do
expect
(
autocorrect_source
(
source
)).
to
eq
(
corrected_source
)
end
end
context
'when calling a different method on Timecop'
do
let
(
:source
)
do
<<~
SRC
Timecop.freeze { create(:issue) }
SRC
end
it
'does not register an offence'
do
inspect_source
(
source
)
expect
(
cop
.
offenses
).
to
be_empty
end
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment