Skip to content

Commit

Permalink
Add LetSetup cop
Browse files Browse the repository at this point in the history
Closes rubocop#94
  • Loading branch information
backus committed Aug 7, 2016
1 parent 0e789f3 commit b31ab8f
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 0 deletions.
3 changes: 3 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ RSpec/InstanceVariable:
Description: 'Checks for the usage of instance variables.'
Enabled: true

RSpec/LetSetup:
Description: 'Checks for `let!` being used for test setup.'

RSpec/FilePath:
Description: 'Checks the file and folder naming of the spec file.'
Enabled: true
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop-rspec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
require 'rubocop/cop/rspec/file_path'
require 'rubocop/cop/rspec/focus'
require 'rubocop/cop/rspec/instance_variable'
require 'rubocop/cop/rspec/let_setup'
require 'rubocop/cop/rspec/multiple_describes'
require 'rubocop/cop/rspec/multiple_expectations'
require 'rubocop/cop/rspec/named_subject'
Expand Down
59 changes: 59 additions & 0 deletions lib/rubocop/cop/rspec/let_setup.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# frozen_string_literal: true

module RuboCop
module Cop
module RSpec
# Detect unreferenced `let!` calls being used for test setup
#
# @example
# # Bad
# let!(:my_widget) { create(:widget) }
#
# it 'counts widgets' do
# expect(Widget.count).to eq(1)
# end
#
# # Good
# it 'counts widgets' do
# create(:widget)
# expect(Widget.count).to eq(1)
# end
#
# # Good
# before { create(:widget) }
#
# it 'counts widegts' do
# expect(Widget.count).to eq(1)
# end
class LetSetup < Cop
include RuboCop::RSpec::TopLevelDescribe, RuboCop::RSpec::Language

MSG = 'Do not use `let!` for test setup.'.freeze

def_node_search :let_bang, '(block $(send nil :let! (sym $_)) args ...)'

def_node_search :method_called?, '(send nil %)'

def_node_matcher :example_group?, <<-PATTERN
(block (send _ {#{ExampleGroups::ALL.to_node_pattern}} ...) ...)
PATTERN

def on_block(node)
return unless example_group?(node)

unused_let_bang(node) do |let|
add_offense(let, :expression)
end
end

private

def unused_let_bang(node)
let_bang(node) do |method_send, method_name|
yield(method_send) unless method_called?(node, method_name)
end
end
end
end
end
end
66 changes: 66 additions & 0 deletions spec/rubocop/cop/rspec/let_setup_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# frozen_string_literal: true

describe RuboCop::Cop::RSpec::LetSetup do
subject(:cop) { described_class.new }

it 'complains when let! is used and not referenced' do
expect_violation(<<-RUBY)
describe Foo do
let!(:foo) { bar }
^^^^^^^^^^ Do not use `let!` for test setup.
it 'does not use foo' do
expect(baz).to eq(qux)
end
end
RUBY
end

it 'ignores let! when used in `before`' do
expect_no_violations(<<-RUBY)
describe Foo do
let!(:foo) { bar }
before do
foo
end
it 'does not use foo' do
expect(baz).to eq(qux)
end
end
RUBY
end

it 'ignores let! when used in example' do
expect_no_violations(<<-RUBY)
describe Foo do
let!(:foo) { bar }
it 'uses foo' do
foo
expect(baz).to eq(qux)
end
end
RUBY
end

it 'complains when let! is used and not referenced within nested group' do
expect_violation(<<-RUBY)
describe Foo do
context 'when something special happens' do
let!(:foo) { bar }
^^^^^^^^^^ Do not use `let!` for test setup.
it 'does not use foo' do
expect(baz).to eq(qux)
end
end
it 'references some other foo' do
foo
end
end
RUBY
end
end

0 comments on commit b31ab8f

Please sign in to comment.