rubocop-temporal 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +3 -0
- data/.rubocop.yml +12 -0
- data/.travis.yml +6 -0
- data/Gemfile +10 -0
- data/Gemfile.lock +57 -0
- data/LICENSE.txt +21 -0
- data/README.md +114 -0
- data/Rakefile +40 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/config/default.yml +2 -0
- data/lib/rubocop/cop/temporal_cops.rb +22 -0
- data/lib/rubocop/temporal/inject.rb +20 -0
- data/lib/rubocop/temporal/version.rb +7 -0
- data/lib/rubocop/temporal.rb +15 -0
- data/lib/rubocop-temporal.rb +11 -0
- data/rubocop-temporal.gemspec +31 -0
- metadata +78 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 4febd507038f166b850ed9f780ec490d20eb090fd40284f5aa17d37bb429988e
|
4
|
+
data.tar.gz: 51328f405b3d2c39258d9470919c64f77b0dc3e7c9314f02c4bcdf95e7bd7e17
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 998e8a5869f3d034bdc26f5dbb9dc7a0e218bbf630e4a1f7943df5fca4ed61cbc539136fdde24f328d9133dca25e3c136d8e8cd1403cae2c927cc05bd07d300b
|
7
|
+
data.tar.gz: 6deb7be8332b81765508d3938450e3fa3e515d1accda100ce9101253855ad9cb6985a6515ffa1aa02b8a56c56687493574445a47278a13e4640835aac0f4bc63
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
rubocop-temporal (0.1.0)
|
5
|
+
rubocop (~> 1.8)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
ast (2.4.2)
|
11
|
+
diff-lcs (1.4.4)
|
12
|
+
minitest (5.14.4)
|
13
|
+
parallel (1.21.0)
|
14
|
+
parser (3.0.3.1)
|
15
|
+
ast (~> 2.4.1)
|
16
|
+
rainbow (3.0.0)
|
17
|
+
rake (12.3.3)
|
18
|
+
regexp_parser (2.1.1)
|
19
|
+
rexml (3.2.5)
|
20
|
+
rspec (3.10.0)
|
21
|
+
rspec-core (~> 3.10.0)
|
22
|
+
rspec-expectations (~> 3.10.0)
|
23
|
+
rspec-mocks (~> 3.10.0)
|
24
|
+
rspec-core (3.10.1)
|
25
|
+
rspec-support (~> 3.10.0)
|
26
|
+
rspec-expectations (3.10.1)
|
27
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
28
|
+
rspec-support (~> 3.10.0)
|
29
|
+
rspec-mocks (3.10.2)
|
30
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
31
|
+
rspec-support (~> 3.10.0)
|
32
|
+
rspec-support (3.10.2)
|
33
|
+
rubocop (1.23.0)
|
34
|
+
parallel (~> 1.10)
|
35
|
+
parser (>= 3.0.0.0)
|
36
|
+
rainbow (>= 2.2.2, < 4.0)
|
37
|
+
regexp_parser (>= 1.8, < 3.0)
|
38
|
+
rexml
|
39
|
+
rubocop-ast (>= 1.12.0, < 2.0)
|
40
|
+
ruby-progressbar (~> 1.7)
|
41
|
+
unicode-display_width (>= 1.4.0, < 3.0)
|
42
|
+
rubocop-ast (1.13.0)
|
43
|
+
parser (>= 3.0.1.1)
|
44
|
+
ruby-progressbar (1.11.0)
|
45
|
+
unicode-display_width (2.1.0)
|
46
|
+
|
47
|
+
PLATFORMS
|
48
|
+
x86_64-darwin-19
|
49
|
+
|
50
|
+
DEPENDENCIES
|
51
|
+
minitest (~> 5.0)
|
52
|
+
rake (~> 12.0)
|
53
|
+
rspec
|
54
|
+
rubocop-temporal!
|
55
|
+
|
56
|
+
BUNDLED WITH
|
57
|
+
2.2.31
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2021 Achilleas Buisman
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,114 @@
|
|
1
|
+
# Rubocop::Temporal
|
2
|
+
|
3
|
+
Is your test suite suddenly failing randomly? Do you use `travel_to` without blocks? The Department of Temporal Investigations is here to ~~stop~~ help you.
|
4
|
+
|
5
|
+
TLDR: This cop checks for calls to `travel_to` without a block. This is dangerous because a test where this happen might break other tests subtely because now the code runs either in the future or in the past. Using a block will protect you, because after running the block, you will be returned to real time.
|
6
|
+
|
7
|
+
## The problem
|
8
|
+
|
9
|
+
So what is the problem? Let's explore this a bit.
|
10
|
+
|
11
|
+
Imagine you have the following spec:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
describe 'Enable discounts on New Years Eve' do
|
15
|
+
context 'when it is new years' do
|
16
|
+
it 'shows fireworks' do
|
17
|
+
travel_to '2021-12-31'
|
18
|
+
|
19
|
+
expect(product.price).to eq 100 * 0.5
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
```
|
24
|
+
|
25
|
+
Looks good, ship it!
|
26
|
+
|
27
|
+
Oh no, random failures:
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
|
31
|
+
# Mail is not enqueued suddenly, but only sometimes. When you run this spec individually, it is always sent
|
32
|
+
describe 'Special member invite' do
|
33
|
+
it 'invites people who spent at least €1000 to become special members' do
|
34
|
+
10.times { user.purchase(create(:product, price: 100)) }
|
35
|
+
|
36
|
+
expect(ActionMailer::MailDeliveryJob).to have_been_enqueued.with('UserMailer', 'special_member_invite', 'deliver_now', user)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
# This also passes individually, but sometimes we see:
|
41
|
+
# Luke: Noooooooooooooo
|
42
|
+
# Darth: No. I am your father
|
43
|
+
describe 'Message view' do
|
44
|
+
it 'displays messages from old to new' do
|
45
|
+
travel_to 2.days.ago
|
46
|
+
darth_message = send_message(to: 'Luke', 'No. I am your father')
|
47
|
+
|
48
|
+
travel_back
|
49
|
+
luke_message = send_message(to: 'Darth', 'Noooooooooooooo')
|
50
|
+
|
51
|
+
expect(page).to have_content(<~~MESSAGE)
|
52
|
+
Darth: No. I am your father
|
53
|
+
Luke: Noooooooooooooo
|
54
|
+
MESSAGE
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
```
|
59
|
+
|
60
|
+
Why?
|
61
|
+
|
62
|
+
Many 🕐🕑🕒🕓 hours, later 🧽.
|
63
|
+
|
64
|
+
It turns out, whenever the `Enable discounts on New Years Eve` spec runs, it sets the date to the end of this year, but we never return to the current date. The random fails then only happen when the NYE spec runs before them, hence 'random'.
|
65
|
+
|
66
|
+
The spec `Special member invite` fails because on New Years Eve all prices are cut in half, so the user never reaches the treshold of €1000.
|
67
|
+
|
68
|
+
The spec `Message view` fails because when we did `travel_to 2.days.ago` we went to the 29th of December, not `2.days.ago` from the current date. Then when we do `travel_back` we go back to the current date, lets say the 15th of May and send the second message, or so we think. In reality it is now the first message.
|
69
|
+
|
70
|
+
Side note: this does not randomly fail after the 31st of December 2021.
|
71
|
+
|
72
|
+
The solution? Passing a block to `travel_to`. After the block runs, you will be returned to real time.
|
73
|
+
|
74
|
+
## Installation
|
75
|
+
|
76
|
+
Add this line to your application's Gemfile:
|
77
|
+
|
78
|
+
```ruby
|
79
|
+
gem 'rubocop-temporal', require: false
|
80
|
+
```
|
81
|
+
|
82
|
+
And then execute:
|
83
|
+
|
84
|
+
$ bundle install
|
85
|
+
|
86
|
+
Or install it yourself as:
|
87
|
+
|
88
|
+
$ gem install rubocop-temporal
|
89
|
+
|
90
|
+
## Usage
|
91
|
+
|
92
|
+
Add the following to your `.rubocop.yml` config:
|
93
|
+
|
94
|
+
```
|
95
|
+
require:
|
96
|
+
- rubocop-temporal
|
97
|
+
```
|
98
|
+
|
99
|
+
If `require:` was already there, just add ` - rubocop-temporal` nested underneath it.
|
100
|
+
|
101
|
+
## Development
|
102
|
+
|
103
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
104
|
+
|
105
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
106
|
+
|
107
|
+
## Contributing
|
108
|
+
|
109
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/abuisman/rubocop-temporal.
|
110
|
+
|
111
|
+
|
112
|
+
## License
|
113
|
+
|
114
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "bundler/gem_tasks"
|
4
|
+
require "rake/testtask"
|
5
|
+
|
6
|
+
Rake::TestTask.new(:test) do |t|
|
7
|
+
t.libs << "test"
|
8
|
+
t.libs << "lib"
|
9
|
+
t.test_files = FileList["test/**/*_test.rb"]
|
10
|
+
end
|
11
|
+
|
12
|
+
task default: :test
|
13
|
+
|
14
|
+
require 'rspec/core/rake_task'
|
15
|
+
|
16
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
17
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
18
|
+
end
|
19
|
+
|
20
|
+
desc 'Generate a new cop with a template'
|
21
|
+
task :new_cop, [:cop] do |_task, args|
|
22
|
+
require 'rubocop'
|
23
|
+
|
24
|
+
cop_name = args.fetch(:cop) do
|
25
|
+
warn 'usage: bundle exec rake new_cop[Department/Name]'
|
26
|
+
exit!
|
27
|
+
end
|
28
|
+
|
29
|
+
github_user = `git config github.user`.chop
|
30
|
+
github_user = 'your_id' if github_user.empty?
|
31
|
+
|
32
|
+
generator = RuboCop::Cop::Generator.new(cop_name, github_user)
|
33
|
+
|
34
|
+
generator.write_source
|
35
|
+
generator.write_spec
|
36
|
+
generator.inject_require(root_file_path: 'lib/rubocop/cop/temporal_cops.rb')
|
37
|
+
generator.inject_config(config_file_path: 'config/default.yml')
|
38
|
+
|
39
|
+
puts generator.todo
|
40
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require "bundler/setup"
|
5
|
+
require "rubocop/temporal"
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require "pry"
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require "irb"
|
15
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/config/default.yml
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Temporal
|
4
|
+
class TravelToWithBlock < RuboCop::Cop::Cop
|
5
|
+
def_node_matcher(
|
6
|
+
:travel_to,
|
7
|
+
<<~PATTERN
|
8
|
+
(send nil? :travel_to ...)
|
9
|
+
PATTERN
|
10
|
+
)
|
11
|
+
|
12
|
+
MSG = "Always use travel_to with a block"
|
13
|
+
|
14
|
+
def on_send(node)
|
15
|
+
travel_to(node) do
|
16
|
+
next if node.block_node
|
17
|
+
|
18
|
+
add_offense(node)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# The original code is from https://github.com/rubocop-hq/rubocop-rspec/blob/master/lib/rubocop/rspec/inject.rb
|
4
|
+
# See https://github.com/rubocop-hq/rubocop-rspec/blob/master/MIT-LICENSE.md
|
5
|
+
module RuboCop
|
6
|
+
module Temporal
|
7
|
+
# Because RuboCop doesn't yet support plugins, we have to monkey patch in a
|
8
|
+
# bit of our configuration.
|
9
|
+
module Inject
|
10
|
+
def self.defaults!
|
11
|
+
path = CONFIG_DEFAULT.to_s
|
12
|
+
hash = ConfigLoader.send(:load_yaml_configuration, path)
|
13
|
+
config = Config.new(hash, path).tap(&:make_excludes_absolute)
|
14
|
+
puts "configuration from #{path}" if ConfigLoader.debug?
|
15
|
+
config = ConfigLoader.merge_with_default(config, path)
|
16
|
+
ConfigLoader.instance_variable_set(:@default_configuration, config)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "rubocop/temporal/version"
|
4
|
+
|
5
|
+
module RuboCop
|
6
|
+
module Temporal
|
7
|
+
class Error < StandardError; end
|
8
|
+
# Your code goes here...
|
9
|
+
PROJECT_ROOT = Pathname.new(__dir__).parent.parent.expand_path.freeze
|
10
|
+
CONFIG_DEFAULT = PROJECT_ROOT.join('config', 'default.yml').freeze
|
11
|
+
CONFIG = YAML.safe_load(CONFIG_DEFAULT.read).freeze
|
12
|
+
|
13
|
+
private_constant(:CONFIG_DEFAULT, :PROJECT_ROOT)
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rubocop'
|
4
|
+
|
5
|
+
require_relative 'rubocop/temporal'
|
6
|
+
require_relative 'rubocop/temporal/version'
|
7
|
+
require_relative 'rubocop/temporal/inject'
|
8
|
+
|
9
|
+
RuboCop::Temporal::Inject.defaults!
|
10
|
+
|
11
|
+
require_relative 'rubocop/cop/temporal_cops'
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "lib/rubocop/temporal/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "rubocop-temporal"
|
7
|
+
spec.version = RuboCop::Temporal::VERSION
|
8
|
+
spec.authors = ["Achilleas Buisman"]
|
9
|
+
spec.email = ["temporal@abuisman.nl"]
|
10
|
+
|
11
|
+
spec.summary = "Rubocop cop that makes sure travel_to is called with a block"
|
12
|
+
spec.description = "A rubocop cop that check that `travel_to` is only called with a block."
|
13
|
+
spec.homepage = "https://github.com/abuisman/rubocop-temporal"
|
14
|
+
spec.license = "MIT"
|
15
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.5.0")
|
16
|
+
|
17
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
18
|
+
spec.metadata["source_code_uri"] = "https://github.com/abuisman/rubocop-temporal"
|
19
|
+
spec.metadata["changelog_uri"] = "https://github.com/abuisman/rubocop-temporal"
|
20
|
+
|
21
|
+
# Specify which files should be added to the gem when it is released.
|
22
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
23
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
24
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
25
|
+
end
|
26
|
+
spec.bindir = "exe"
|
27
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
|
+
spec.require_paths = ["lib"]
|
29
|
+
|
30
|
+
spec.add_runtime_dependency "rubocop", "~> 1.8"
|
31
|
+
end
|
metadata
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rubocop-temporal
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Achilleas Buisman
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-12-03 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rubocop
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.8'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.8'
|
27
|
+
description: A rubocop cop that check that `travel_to` is only called with a block.
|
28
|
+
email:
|
29
|
+
- temporal@abuisman.nl
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- ".gitignore"
|
35
|
+
- ".rspec"
|
36
|
+
- ".rubocop.yml"
|
37
|
+
- ".travis.yml"
|
38
|
+
- Gemfile
|
39
|
+
- Gemfile.lock
|
40
|
+
- LICENSE.txt
|
41
|
+
- README.md
|
42
|
+
- Rakefile
|
43
|
+
- bin/console
|
44
|
+
- bin/setup
|
45
|
+
- config/default.yml
|
46
|
+
- lib/rubocop-temporal.rb
|
47
|
+
- lib/rubocop/cop/temporal_cops.rb
|
48
|
+
- lib/rubocop/temporal.rb
|
49
|
+
- lib/rubocop/temporal/inject.rb
|
50
|
+
- lib/rubocop/temporal/version.rb
|
51
|
+
- rubocop-temporal.gemspec
|
52
|
+
homepage: https://github.com/abuisman/rubocop-temporal
|
53
|
+
licenses:
|
54
|
+
- MIT
|
55
|
+
metadata:
|
56
|
+
homepage_uri: https://github.com/abuisman/rubocop-temporal
|
57
|
+
source_code_uri: https://github.com/abuisman/rubocop-temporal
|
58
|
+
changelog_uri: https://github.com/abuisman/rubocop-temporal
|
59
|
+
post_install_message:
|
60
|
+
rdoc_options: []
|
61
|
+
require_paths:
|
62
|
+
- lib
|
63
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: 2.5.0
|
68
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '0'
|
73
|
+
requirements: []
|
74
|
+
rubygems_version: 3.1.6
|
75
|
+
signing_key:
|
76
|
+
specification_version: 4
|
77
|
+
summary: Rubocop cop that makes sure travel_to is called with a block
|
78
|
+
test_files: []
|