Skip to content

Commit 7ea9bb5

Browse files
author
Template Bot
committed
Apply template update: Rubocop: add linter to disallow before blocks
Source: mockdeep/Rails-Template#1399
1 parent f118560 commit 7ea9bb5

3 files changed

Lines changed: 115 additions & 0 deletions

File tree

.rubocop.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
require:
2+
- ./linters/rubocop/cop/rspec/no_before_hook
3+
14
inherit_from: .rubocop_todo.yml
25
inherit_mode: { merge: [Exclude] }
36

@@ -21,6 +24,7 @@ Layout/LineLength: { Max: 80, Exclude: [db/migrate/*.rb] }
2124
Layout/RedundantLineBreak: { InspectBlocks: true }
2225
Metrics/AbcSize: { Exclude: [db/migrate/*.rb], CountRepeatedAttributes: false }
2326
Metrics/BlockLength:
27+
<<<<<<< HEAD
2428
Exclude: [config/**/*.rb, db/migrate/*.rb, spec/**/*.rb, db/seeds/**/*.rb]
2529
Metrics/MethodLength: { Exclude: [db/migrate/*.rb] }
2630
Rails/SkipsModelValidations: { AllowedMethods: [update_all] }
@@ -31,6 +35,26 @@ RSpec/MessageExpectation:
3135
RSpec/MessageSpies: { EnforcedStyle: receive }
3236
RSpec/MultipleMemoizedHelpers: { Max: 0, AllowSubject: false }
3337
Style/ClassAndModuleChildren: { EnforcedStyle: compact }
38+
=======
39+
AllowedMethods:
40+
- describe
41+
Exclude:
42+
- Guardfile
43+
- db/schema.rb
44+
- config/environments/development.rb
45+
Rails/FilePath: { EnforcedStyle: slashes }
46+
Metrics/MethodLength: { Exclude: [db/migrate/**/*.rb] }
47+
RSpec/MultipleExpectations:
48+
Exclude:
49+
- spec/system/**/*.rb
50+
RSpec/MessageExpectation: { EnforcedStyle: expect }
51+
RSpec/MultipleMemoizedHelpers:
52+
Max: 0
53+
AllowSubject: false
54+
Exclude:
55+
- spec/linters/rubocop/**/*.rb
56+
Style/ArrayFirstLast: { Exclude: [Guardfile] }
57+
>>>>>>> 0b27ab6 (Rubocop: add linter to disallow before blocks (#1399))
3458
Style/MethodCallWithArgsParentheses:
3559
AllowedMethods:
3660
- and
@@ -45,9 +69,18 @@ Style/StringLiterals: { EnforcedStyle: double_quotes }
4569
Style/SymbolArray: { EnforcedStyle: brackets }
4670
Style/WordArray: { EnforcedStyle: brackets }
4771

72+
<<<<<<< HEAD
4873
# want to enable these, but they don't work right when using `.rubocop_todo.yml`
4974
Style/DocumentationMethod: { Enabled: false }
5075
Style/Documentation: { Enabled: false }
76+
=======
77+
I18n/GetText/DecorateString:
78+
Exclude:
79+
- linters/**/*.rb
80+
I18n/RailsI18n/DecorateString:
81+
Exclude:
82+
- linters/**/*.rb
83+
>>>>>>> 0b27ab6 (Rubocop: add linter to disallow before blocks (#1399))
5184

5285
################################################################################
5386
#
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# frozen_string_literal: true
2+
3+
module RuboCop
4+
module Cop
5+
module RSpec
6+
# Disallows the use of `before` hooks in specs.
7+
#
8+
# @example
9+
# # bad
10+
# before { do_something }
11+
# before(:each) { do_something }
12+
#
13+
# # good
14+
# # Inline setup directly in the example.
15+
class NoBeforeHook < Base
16+
MSG = "Do not use `before` hooks."
17+
18+
# @!method before_hook?(node)
19+
def_node_matcher(:before_hook?, <<~PATTERN)
20+
(block (send nil? :before ...) ...)
21+
PATTERN
22+
23+
def on_block(node)
24+
return unless before_hook?(node)
25+
26+
add_offense(node)
27+
end
28+
29+
alias on_numblock on_block
30+
end
31+
end
32+
end
33+
end
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# frozen_string_literal: true
2+
3+
require "rubocop"
4+
require "rubocop-rspec"
5+
require "rubocop/rspec/cop_helper"
6+
require "rubocop/rspec/expect_offense"
7+
require_relative("../../../../../linters/rubocop/cop/rspec/no_before_hook")
8+
9+
RSpec.describe RuboCop::Cop::RSpec::NoBeforeHook do
10+
include CopHelper
11+
include RuboCop::RSpec::ExpectOffense
12+
13+
let(:cop) { described_class.new }
14+
15+
it "registers an offense for a before block" do
16+
expect_offense(<<~RUBY)
17+
before { do_something }
18+
^^^^^^^^^^^^^^^^^^^^^^^ RSpec/NoBeforeHook: Do not use `before` hooks.
19+
RUBY
20+
end
21+
22+
it "registers an offense for before(:each)" do
23+
expect_offense(<<~RUBY)
24+
before(:each) { do_something }
25+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ RSpec/NoBeforeHook: Do not use `before` hooks.
26+
RUBY
27+
end
28+
29+
it "registers an offense for before(:all)" do
30+
expect_offense(<<~RUBY)
31+
before(:all) { do_something }
32+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ RSpec/NoBeforeHook: Do not use `before` hooks.
33+
RUBY
34+
end
35+
36+
it "does not register an offense for an after block" do
37+
expect_no_offenses(<<~RUBY)
38+
after { do_something }
39+
RUBY
40+
end
41+
42+
it "does not register an offense for a regular method call" do
43+
expect_no_offenses(<<~RUBY)
44+
def setup
45+
do_something
46+
end
47+
RUBY
48+
end
49+
end

0 commit comments

Comments
 (0)