Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add RSpec/StubProducts cop #18

Merged
merged 7 commits into from
Apr 7, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

# v0.3.1

Correct cop name `Migration/SchemaStatementsMethods` in config ([#17](https://github.com/petalmd/rubocop-petal/pull/17))
* Correct cop name `Migration/SchemaStatementsMethods` in config ([#17](https://github.com/petalmd/rubocop-petal/pull/17))
* Added cop `RSpec/StubProducts` ([#18](https://github.com/petalmd/rubocop-petal/pull/18))
makesime marked this conversation as resolved.
Show resolved Hide resolved

# v0.3.0

Expand Down
6 changes: 6 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ RSpec/CreateListMax:
Include:
- spec/**/*

RSpec/StubProducts:
Description: 'Suggest to use stub_products instead of veil/unveil_product.'
Enabled: true
makesime marked this conversation as resolved.
Show resolved Hide resolved
Include:
- spec/**/*

Grape/HelpersIncludeModule:
Description: 'Prevent using helpers with block to include module'
Enabled: true
Expand Down
39 changes: 39 additions & 0 deletions lib/rubocop/cop/rspec/stub_products.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# frozen_string_literal: true

module RuboCop
module Cop
module RSpec
# Suggest to use stub_products instead of veil/unveil_product
# which queries the database and can cause flaky tests.
#
# # bad
# unveil_product('MY_PRODUCT')
# veil_product('MY_PRODUCT')
#
# # good
# stub_products('MY_PRODUCT' => true)
# stub_products('MY_PRODUCT' => false)
# stub_products('MY_PRODUCT' => group)
# stub_products('MY_PRODUCT' => [group1, group2])
# stub_products(MY_PRODUCT: true)
#
class StubProducts < Base
makesime marked this conversation as resolved.
Show resolved Hide resolved
MSG = 'Use `stub_products` instead of veil/unveil_product.'

def_node_search :veil_product?, <<~PATTERN
(send nil? :veil_product _)
PATTERN

def_node_search :unveil_product?, <<~PATTERN
(send nil? :unveil_product _)
PATTERN

def on_send(node)
return unless veil_product?(node) || unveil_product?(node)

add_offense(node)
end
end
end
end
end
29 changes: 29 additions & 0 deletions spec/rubocop/cop/rspec/stub_products_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::RSpec::StubProducts, :config do
context 'when using veil_product' do
it 'registers an offense' do
expect_offense(<<~RUBY)
veil_product('MY_PRODUCT')
^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `stub_products` instead of veil/unveil_product.
RUBY
end
end

context 'when using unveil_product' do
it 'registers an offense' do
expect_offense(<<~RUBY)
unveil_product('MY_PRODUCT')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `stub_products` instead of veil/unveil_product.
RUBY
end
end

context 'when not using veil nor unveil_product' do
it 'doesnt register an offense' do
expect_no_offenses(<<~RUBY)
stub_products('MY_PRODUCT' => group)
RUBY
end
end
end