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
cc471766
Commit
cc471766
authored
Aug 17, 2020
by
Alishan Ladhani
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add a cop to detect usages of Timecop.freeze
parent
1a1268bd
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_freeze.rb
rubocop/cop/rspec/timecop_freeze.rb
+41
-0
spec/rubocop/cop/rspec/timecop_freeze_spec.rb
spec/rubocop/cop/rspec/timecop_freeze_spec.rb
+52
-0
No files found.
.rubocop.yml
View file @
cc471766
...
...
@@ -282,6 +282,14 @@ Cop/ActiveRecordAssociationReload:
Gitlab/AvoidFeatureGet
:
Enabled
:
true
RSpec/TimecopFreeze
:
Enabled
:
false
AutoCorrect
:
true
Include
:
-
'
spec/**/*.rb'
-
'
ee/spec/**/*.rb'
-
'
qa/spec/**/*.rb'
Naming/PredicateName
:
Enabled
:
true
Exclude
:
...
...
rubocop/cop/rspec/timecop_freeze.rb
0 → 100644
View file @
cc471766
# frozen_string_literal: true
module
RuboCop
module
Cop
module
RSpec
# This cop checks for `Timecop.freeze` usage in specs.
#
# @example
#
# # bad
# Timecop.freeze(Time.current) { example.run }
#
# # good
# freeze_time(Time.current) { example.run }
#
class
TimecopFreeze
<
RuboCop
::
Cop
::
Cop
include
MatchRange
MESSAGE
=
'Do not use `Timecop.freeze`, use `freeze_time` instead. '
\
'See https://gitlab.com/gitlab-org/gitlab/-/issues/214432 for more info.'
def_node_matcher
:timecop_freeze?
,
<<~
PATTERN
(send (const nil? :Timecop) :freeze ?_)
PATTERN
def
on_send
(
node
)
return
unless
timecop_freeze?
(
node
)
add_offense
(
node
,
location: :expression
,
message:
MESSAGE
)
end
def
autocorrect
(
node
)
->
(
corrector
)
do
each_match_range
(
node
.
source_range
,
/^(Timecop\.freeze)/
)
do
|
match_range
|
corrector
.
replace
(
match_range
,
'freeze_time'
)
end
end
end
end
end
end
end
spec/rubocop/cop/rspec/timecop_freeze_spec.rb
0 → 100644
View file @
cc471766
# frozen_string_literal: true
require
'fast_spec_helper'
require
'rubocop'
require
'rubocop/rspec/support'
require_relative
'../../../../rubocop/cop/rspec/timecop_freeze'
RSpec
.
describe
RuboCop
::
Cop
::
RSpec
::
TimecopFreeze
,
type: :rubocop
do
include
CopHelper
subject
(
:cop
)
{
described_class
.
new
}
context
'when calling Timecop.freeze'
do
let
(
:source
)
do
<<~
SRC
Timecop.freeze(Time.current) { example.run }
SRC
end
let
(
:corrected_source
)
do
<<~
SRC
freeze_time(Time.current) { example.run }
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.travel(Time.current)
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