rubocop-petal 1.2.0 → 1.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/config/base.yml +0 -2
- data/config/default.yml +14 -0
- data/lib/rubocop/cop/rails/enum_starting_value.rb +1 -1
- data/lib/rubocop/cop/rspec/aggregate_examples/language.rb +76 -0
- data/lib/rubocop/cop/rspec/aggregate_examples/line_range_helpers.rb +31 -0
- data/lib/rubocop/cop/rspec/aggregate_examples/matchers_with_side_effects.rb +83 -0
- data/lib/rubocop/cop/rspec/aggregate_examples/metadata_helpers.rb +65 -0
- data/lib/rubocop/cop/rspec/aggregate_examples/node_matchers.rb +79 -0
- data/lib/rubocop/cop/rspec/aggregate_examples.rb +199 -0
- data/lib/rubocop/cop/rspec/multiple_expectations_auto_correct.rb +41 -0
- data/lib/rubocop/petal/version.rb +1 -1
- metadata +37 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e00aaceb476db83ee7becc110d01e4fc67b0e7fbdea207aceee51465f6314de6
|
4
|
+
data.tar.gz: 0033f05f11d00e7b32798cb2aa77ad8c9158486676d69d0c489d21a6dac217d2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 66b77b9959c0fcdcfed06a5badcef6dd6dcc3c70383391ff63dea069725a964ba178484a60e747c131cf08cbbf167efce4b7691cf00f5f40e7f34a49ffbc1fab
|
7
|
+
data.tar.gz: 4348a41527e5c4374b8e610c72cab2e855f484952e3bc4a98e4a86e7ad2a650c935733919d2e3610c81936b0eea716d46eb88d8171dc3980e3bf045ec1a3b2f4
|
data/config/base.yml
CHANGED
data/config/default.yml
CHANGED
@@ -41,6 +41,20 @@ Migration/StandaloneAddReference:
|
|
41
41
|
Include:
|
42
42
|
- db/migrate/**
|
43
43
|
|
44
|
+
RSpec/AggregateExamples:
|
45
|
+
Description: Checks if example group contains two or more aggregatable examples.
|
46
|
+
Enabled: true
|
47
|
+
StyleGuide: https://rspec.rubystyle.guide/#expectation-per-example
|
48
|
+
AddAggregateFailuresMetadata: true
|
49
|
+
MatchersWithSideEffects:
|
50
|
+
- allow_value
|
51
|
+
- allow_values
|
52
|
+
- validate_presence_of
|
53
|
+
- validate_absence_of
|
54
|
+
- validate_length_of
|
55
|
+
- validate_inclusion_of
|
56
|
+
- validates_exclusion_of
|
57
|
+
|
44
58
|
RSpec/AuthenticatedAs:
|
45
59
|
Description: 'Suggest to use authenticated_as instead of legacy api_key.'
|
46
60
|
Enabled: true
|
@@ -0,0 +1,76 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# This is shamelessly borrowed from RuboCop RSpec
|
4
|
+
# https://github.com/rubocop-hq/rubocop-rspec/blob/master/lib/rubocop/rspec/language.rb
|
5
|
+
module RuboCop
|
6
|
+
module Cop
|
7
|
+
module RSpec
|
8
|
+
# RSpec public API methods that are commonly used in cops
|
9
|
+
class AggregateExamples < ::RuboCop::Cop::Cop
|
10
|
+
module Language
|
11
|
+
RSPEC = '{(const {nil? cbase} :RSpec) nil?}'
|
12
|
+
|
13
|
+
# Set of method selectors
|
14
|
+
class SelectorSet
|
15
|
+
def initialize(selectors)
|
16
|
+
@selectors = selectors
|
17
|
+
end
|
18
|
+
|
19
|
+
def ==(other)
|
20
|
+
selectors.eql?(other.selectors)
|
21
|
+
end
|
22
|
+
|
23
|
+
def +(other)
|
24
|
+
self.class.new(selectors + other.selectors)
|
25
|
+
end
|
26
|
+
|
27
|
+
def include?(selector)
|
28
|
+
selectors.include?(selector)
|
29
|
+
end
|
30
|
+
|
31
|
+
def block_pattern
|
32
|
+
"(block #{send_pattern} ...)"
|
33
|
+
end
|
34
|
+
|
35
|
+
def send_pattern
|
36
|
+
"(send #{RSPEC} #{node_pattern_union} ...)"
|
37
|
+
end
|
38
|
+
|
39
|
+
def node_pattern_union
|
40
|
+
"{#{node_pattern}}"
|
41
|
+
end
|
42
|
+
|
43
|
+
def node_pattern
|
44
|
+
selectors.map(&:inspect).join(' ')
|
45
|
+
end
|
46
|
+
|
47
|
+
protected
|
48
|
+
|
49
|
+
attr_reader :selectors
|
50
|
+
end
|
51
|
+
|
52
|
+
module ExampleGroups
|
53
|
+
GROUPS = SelectorSet.new(%i[describe context feature example_group])
|
54
|
+
SKIPPED = SelectorSet.new(%i[xdescribe xcontext xfeature])
|
55
|
+
FOCUSED = SelectorSet.new(%i[fdescribe fcontext ffeature])
|
56
|
+
|
57
|
+
ALL = GROUPS + SKIPPED + FOCUSED
|
58
|
+
end
|
59
|
+
|
60
|
+
module Examples
|
61
|
+
EXAMPLES = SelectorSet.new(%i[it specify example scenario its])
|
62
|
+
FOCUSED = SelectorSet.new(%i[fit fspecify fexample fscenario focus])
|
63
|
+
SKIPPED = SelectorSet.new(%i[xit xspecify xexample xscenario skip])
|
64
|
+
PENDING = SelectorSet.new(%i[pending])
|
65
|
+
|
66
|
+
ALL = EXAMPLES + FOCUSED + SKIPPED + PENDING
|
67
|
+
end
|
68
|
+
|
69
|
+
module Runners
|
70
|
+
ALL = SelectorSet.new(%i[to to_not not_to])
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module RSpec
|
6
|
+
class AggregateExamples < ::RuboCop::Cop::Cop
|
7
|
+
# @internal Support methods for keeping newlines around examples.
|
8
|
+
module LineRangeHelpers
|
9
|
+
include RangeHelp
|
10
|
+
|
11
|
+
private
|
12
|
+
|
13
|
+
def range_for_replace(examples)
|
14
|
+
range = range_by_whole_lines(examples.first.source_range,
|
15
|
+
include_final_newline: true)
|
16
|
+
next_range = range_by_whole_lines(examples[1].source_range)
|
17
|
+
if adjacent?(range, next_range)
|
18
|
+
range.resize(range.length + 1)
|
19
|
+
else
|
20
|
+
range
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def adjacent?(range, another_range)
|
25
|
+
range.end_pos + 1 == another_range.begin_pos
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'language'
|
4
|
+
|
5
|
+
module RuboCop
|
6
|
+
module Cop
|
7
|
+
module RSpec
|
8
|
+
class AggregateExamples < ::RuboCop::Cop::Cop
|
9
|
+
# When aggregated, the expectations will fail when not supposed to or
|
10
|
+
# have a risk of not failing when expected to. One example is
|
11
|
+
# `validate_presence_of :comment` as it leaves an empty comment after
|
12
|
+
# itself on the subject making it invalid and the subsequent expectation
|
13
|
+
# to fail.
|
14
|
+
# Examples with those matchers are not supposed to be aggregated.
|
15
|
+
#
|
16
|
+
# @example MatchersWithSideEffects
|
17
|
+
#
|
18
|
+
# # .rubocop.yml
|
19
|
+
# # RSpec/AggregateExamples:
|
20
|
+
# # MatchersWithSideEffects:
|
21
|
+
# # - allow_value
|
22
|
+
# # - allow_values
|
23
|
+
# # - validate_presence_of
|
24
|
+
#
|
25
|
+
# # bad, but isn't automatically correctable
|
26
|
+
# describe do
|
27
|
+
# it { is_expected.to validate_presence_of(:comment) }
|
28
|
+
# it { is_expected.to be_valid }
|
29
|
+
# end
|
30
|
+
#
|
31
|
+
# @internal
|
32
|
+
# Support for taking special care of the matchers that have side
|
33
|
+
# effects, i.e. leave the subject in a modified state.
|
34
|
+
module MatchersWithSideEffects
|
35
|
+
extend RuboCop::NodePattern::Macros
|
36
|
+
include Language
|
37
|
+
|
38
|
+
MSG_FOR_EXPECTATIONS_WITH_SIDE_EFFECTS =
|
39
|
+
'Aggregate with the example at line %d. IMPORTANT! Pay attention ' \
|
40
|
+
'to the expectation order, some of the matchers have side effects.'
|
41
|
+
|
42
|
+
private
|
43
|
+
|
44
|
+
def message_for(example, first_example)
|
45
|
+
return super unless example_with_side_effects?(example)
|
46
|
+
|
47
|
+
format(MSG_FOR_EXPECTATIONS_WITH_SIDE_EFFECTS, first_example.loc.line)
|
48
|
+
end
|
49
|
+
|
50
|
+
def matcher_with_side_effects_names
|
51
|
+
cop_config.fetch('MatchersWithSideEffects', [])
|
52
|
+
.map(&:to_sym)
|
53
|
+
end
|
54
|
+
|
55
|
+
def matcher_with_side_effects_name?(matcher_name)
|
56
|
+
matcher_with_side_effects_names.include?(matcher_name)
|
57
|
+
end
|
58
|
+
|
59
|
+
# In addition to base definition, matches examples with:
|
60
|
+
# - no matchers known to have side-effects
|
61
|
+
def_node_matcher :example_for_autocorrect?, <<-PATTERN
|
62
|
+
[ #super !#example_with_side_effects? ]
|
63
|
+
PATTERN
|
64
|
+
|
65
|
+
# Matches the example with matcher with side effects
|
66
|
+
def_node_matcher :example_with_side_effects?, <<-PATTERN
|
67
|
+
(block #{Examples::EXAMPLES.send_pattern} _ #expectation_with_side_effects?)
|
68
|
+
PATTERN
|
69
|
+
|
70
|
+
# Matches the expectation with matcher with side effects
|
71
|
+
def_node_matcher :expectation_with_side_effects?, <<-PATTERN
|
72
|
+
(send #expectation? #{Runners::ALL.node_pattern_union} #matcher_with_side_effects?)
|
73
|
+
PATTERN
|
74
|
+
|
75
|
+
# Matches the matcher with side effects
|
76
|
+
def_node_search :matcher_with_side_effects?, <<-PATTERN
|
77
|
+
(send nil? #matcher_with_side_effects_name? ...)
|
78
|
+
PATTERN
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module RSpec
|
6
|
+
class AggregateExamples < ::RuboCop::Cop::Cop
|
7
|
+
# @internal
|
8
|
+
# Support methods for example metadata.
|
9
|
+
# Examples with similar metadata are grouped.
|
10
|
+
#
|
11
|
+
# Depending on the configuration, `aggregate_failures` metadata
|
12
|
+
# is added to aggregated examples.
|
13
|
+
module MetadataHelpers
|
14
|
+
private
|
15
|
+
|
16
|
+
def metadata_for_aggregated_example(metadata)
|
17
|
+
metadata_to_add = metadata.compact.map(&:source)
|
18
|
+
metadata_to_add.unshift(':aggregate_failures') if add_aggregate_failures_metadata?
|
19
|
+
if metadata_to_add.any?
|
20
|
+
"(#{metadata_to_add.join(', ')})"
|
21
|
+
else
|
22
|
+
''
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
# Used to group examples for aggregation. `aggregate_failures`
|
27
|
+
# and `aggregate_failures: true` metadata are not taken in
|
28
|
+
# consideration, as it is dynamically set basing on cofiguration.
|
29
|
+
# If `aggregate_failures: false` is set on the example, it's
|
30
|
+
# preserved and is treated as regular metadata.
|
31
|
+
def metadata_without_aggregate_failures(example)
|
32
|
+
metadata = example_metadata(example) || []
|
33
|
+
|
34
|
+
symbols = metadata_symbols_without_aggregate_failures(metadata)
|
35
|
+
pairs = metadata_pairs_without_aggegate_failures(metadata)
|
36
|
+
|
37
|
+
[*symbols, pairs].flatten.compact
|
38
|
+
end
|
39
|
+
|
40
|
+
def example_metadata(example)
|
41
|
+
example.send_node.arguments
|
42
|
+
end
|
43
|
+
|
44
|
+
def metadata_symbols_without_aggregate_failures(metadata)
|
45
|
+
metadata
|
46
|
+
.select(&:sym_type?)
|
47
|
+
.reject { |item| item.value == :aggregate_failures }
|
48
|
+
end
|
49
|
+
|
50
|
+
def metadata_pairs_without_aggegate_failures(metadata)
|
51
|
+
map = metadata.find(&:hash_type?)
|
52
|
+
pairs = map&.pairs || []
|
53
|
+
pairs.reject do |pair|
|
54
|
+
pair.key.value == :aggregate_failures && pair.value.true_type?
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def add_aggregate_failures_metadata?
|
59
|
+
cop_config.fetch('AddAggregateFailuresMetadata', false)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'language'
|
4
|
+
|
5
|
+
module RuboCop
|
6
|
+
module Cop
|
7
|
+
module RSpec
|
8
|
+
class AggregateExamples < ::RuboCop::Cop::Cop
|
9
|
+
# @internal
|
10
|
+
# Node matchers and searchers.
|
11
|
+
module NodeMatchers
|
12
|
+
extend RuboCop::NodePattern::Macros
|
13
|
+
include Language
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def_node_matcher :example_group_with_several_examples, <<-PATTERN
|
18
|
+
(block
|
19
|
+
#{ExampleGroups::ALL.send_pattern}
|
20
|
+
_
|
21
|
+
(begin $...)
|
22
|
+
)
|
23
|
+
PATTERN
|
24
|
+
|
25
|
+
def example_method?(method_name)
|
26
|
+
%i[it specify example scenario].include?(method_name)
|
27
|
+
end
|
28
|
+
|
29
|
+
# Matches examples with:
|
30
|
+
# - expectation statements exclusively
|
31
|
+
# - no title (e.g. `it('jumps over the lazy dog')`)
|
32
|
+
# - no HEREDOC
|
33
|
+
def_node_matcher :example_for_autocorrect?, <<-PATTERN
|
34
|
+
[
|
35
|
+
#example_with_expectations_only?
|
36
|
+
!#example_has_title?
|
37
|
+
!#contains_heredoc?
|
38
|
+
]
|
39
|
+
PATTERN
|
40
|
+
|
41
|
+
def_node_matcher :example_with_expectations_only?, <<-PATTERN
|
42
|
+
(block #{Examples::EXAMPLES.send_pattern} _
|
43
|
+
{ #single_expectation? (begin #single_expectation?+) }
|
44
|
+
)
|
45
|
+
PATTERN
|
46
|
+
|
47
|
+
# Matches the example with a title (e.g. `it('is valid')`)
|
48
|
+
def_node_matcher :example_has_title?, <<-PATTERN
|
49
|
+
(block
|
50
|
+
(send nil? #example_method? str ...)
|
51
|
+
...
|
52
|
+
)
|
53
|
+
PATTERN
|
54
|
+
|
55
|
+
# Searches for HEREDOC in examples. It can be tricky to aggregate,
|
56
|
+
# especially when interleaved with parenthesis or curly braces.
|
57
|
+
def contains_heredoc?(node)
|
58
|
+
node.each_descendant(:str, :xstr, :dstr).any?(&:heredoc?)
|
59
|
+
end
|
60
|
+
|
61
|
+
def_node_matcher :subject_with_no_args?, <<-PATTERN
|
62
|
+
(send _ _)
|
63
|
+
PATTERN
|
64
|
+
|
65
|
+
def_node_matcher :expectation?, <<-PATTERN
|
66
|
+
{
|
67
|
+
(send nil? {:is_expected :are_expected})
|
68
|
+
(send nil? :expect #subject_with_no_args?)
|
69
|
+
}
|
70
|
+
PATTERN
|
71
|
+
|
72
|
+
def_node_matcher :single_expectation?, <<-PATTERN
|
73
|
+
(send #expectation? #{Runners::ALL.node_pattern_union} _)
|
74
|
+
PATTERN
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
@@ -0,0 +1,199 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'aggregate_examples/matchers_with_side_effects'
|
4
|
+
require_relative 'aggregate_examples/metadata_helpers'
|
5
|
+
require_relative 'aggregate_examples/line_range_helpers'
|
6
|
+
require_relative 'aggregate_examples/node_matchers'
|
7
|
+
|
8
|
+
# This is shamelessly borrowed from test-prof
|
9
|
+
# https://github.com/test-prof/test-prof/blob/02d8f355c158fb021e58ff1327d624a8299762b6/lib/test_prof/cops/rspec/aggregate_examples.rb
|
10
|
+
|
11
|
+
module RuboCop
|
12
|
+
module Cop
|
13
|
+
module RSpec
|
14
|
+
# Checks if example groups contain two or more aggregatable examples.
|
15
|
+
#
|
16
|
+
# @see https://github.com/rubocop-hq/rspec-style-guide#expectation-per-example
|
17
|
+
#
|
18
|
+
# This cop is primarily for reducing the cost of repeated expensive
|
19
|
+
# context initialization.
|
20
|
+
#
|
21
|
+
# @example
|
22
|
+
#
|
23
|
+
# # bad
|
24
|
+
# describe do
|
25
|
+
# it do
|
26
|
+
# expect(number).to be_positive
|
27
|
+
# expect(number).to be_odd
|
28
|
+
# end
|
29
|
+
#
|
30
|
+
# it { is_expected.to be_prime }
|
31
|
+
# end
|
32
|
+
#
|
33
|
+
# # good
|
34
|
+
# describe do
|
35
|
+
# it do
|
36
|
+
# expect(number).to be_positive
|
37
|
+
# expect(number).to be_odd
|
38
|
+
# is_expected.to be_prime
|
39
|
+
# end
|
40
|
+
# end
|
41
|
+
#
|
42
|
+
# # fair - subject has side effects
|
43
|
+
# describe do
|
44
|
+
# it do
|
45
|
+
# expect(multiply_by(2)).to be_multiple_of(2)
|
46
|
+
# end
|
47
|
+
#
|
48
|
+
# it do
|
49
|
+
# expect(multiply_by(3)).to be_multiple_of(3)
|
50
|
+
# end
|
51
|
+
# end
|
52
|
+
#
|
53
|
+
# Block expectation syntax is deliberately not supported due to:
|
54
|
+
#
|
55
|
+
# 1. `subject { -> { ... } }` syntax being hard to detect, e.g. the
|
56
|
+
# following looks like an example with non-block syntax, but it might
|
57
|
+
# be, depending on how the subject is defined:
|
58
|
+
#
|
59
|
+
# it { is_expected.to do_something }
|
60
|
+
#
|
61
|
+
# If the subject is defined in a `shared_context`, it's impossible to
|
62
|
+
# detect that at all.
|
63
|
+
#
|
64
|
+
# 2. Aggregation should use composition with an `.and`. Also, aggregation
|
65
|
+
# of the `not_to` expectations is barely possible when a matcher
|
66
|
+
# doesn't provide a negated variant.
|
67
|
+
#
|
68
|
+
# 3. Aggregation of block syntax with non-block syntax should be in a
|
69
|
+
# specific order.
|
70
|
+
#
|
71
|
+
# RSpec [comes with an `aggregate_failures` helper](https://relishapp.com/rspec/rspec-expectations/docs/aggregating-failures)
|
72
|
+
# not to fail the example on first unmet expectation that might come
|
73
|
+
# handy with aggregated examples.
|
74
|
+
# It can be [used in metadata form](https://relishapp.com/rspec/rspec-core/docs/expectation-framework-integration/aggregating-failures#use-%60:aggregate-failures%60-metadata),
|
75
|
+
# or [enabled globally](https://relishapp.com/rspec/rspec-core/docs/expectation-framework-integration/aggregating-failures#enable-failure-aggregation-globally-using-%60define-derived-metadata%60).
|
76
|
+
#
|
77
|
+
# @example Globally enable `aggregate_failures`
|
78
|
+
#
|
79
|
+
# # spec/spec_helper.rb
|
80
|
+
# config.define_derived_metadata do |metadata|
|
81
|
+
# unless metadata.key?(:aggregate_failures)
|
82
|
+
# metadata[:aggregate_failures] = true
|
83
|
+
# end
|
84
|
+
# end
|
85
|
+
#
|
86
|
+
# To match the style being used in the spec suite, AggregateExamples
|
87
|
+
# can be configured to add `:aggregate_failures` metadata to the
|
88
|
+
# example or not. The option not to add metadata can be also used
|
89
|
+
# when it's not desired to make expectations after previously failed
|
90
|
+
# ones, commonly known as fail-fast.
|
91
|
+
#
|
92
|
+
# The terms "aggregate examples" and "aggregate failures" not to be
|
93
|
+
# confused. The former stands for putting several expectations to
|
94
|
+
# a single example. The latter means to run all the expectations in
|
95
|
+
# the example instead of aborting on the first one.
|
96
|
+
#
|
97
|
+
# @example AddAggregateFailuresMetadata: true (default)
|
98
|
+
#
|
99
|
+
# # Metadata set using a symbol
|
100
|
+
# it(:aggregate_failures) do
|
101
|
+
# expect(number).to be_positive
|
102
|
+
# expect(number).to be_odd
|
103
|
+
# end
|
104
|
+
#
|
105
|
+
# @example AddAggregateFailuresMetadata: false
|
106
|
+
#
|
107
|
+
# it do
|
108
|
+
# expect(number).to be_positive
|
109
|
+
# expect(number).to be_odd
|
110
|
+
# end
|
111
|
+
#
|
112
|
+
class AggregateExamples < ::RuboCop::Cop::Cop
|
113
|
+
include LineRangeHelpers
|
114
|
+
include MetadataHelpers
|
115
|
+
include NodeMatchers
|
116
|
+
|
117
|
+
# Methods from the following modules override and extend methods of this
|
118
|
+
# class, extracting specific behavior.
|
119
|
+
prepend MatchersWithSideEffects
|
120
|
+
|
121
|
+
MSG = 'Aggregate with the example at line %d.'
|
122
|
+
|
123
|
+
def on_block(node)
|
124
|
+
example_group_with_several_examples(node) do |all_examples|
|
125
|
+
example_clusters(all_examples).each_value do |examples|
|
126
|
+
examples[1..].each do |example|
|
127
|
+
add_offense(example,
|
128
|
+
location: :expression,
|
129
|
+
message: message_for(example, examples[0]))
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
def autocorrect(example_node)
|
136
|
+
clusters = example_clusters_for_autocorrect(example_node)
|
137
|
+
return if clusters.empty?
|
138
|
+
|
139
|
+
lambda do |corrector|
|
140
|
+
clusters.each do |metadata, examples|
|
141
|
+
range = range_for_replace(examples)
|
142
|
+
replacement = aggregated_example(examples, metadata)
|
143
|
+
corrector.replace(range, replacement)
|
144
|
+
examples[1..].map { |example| drop_example(corrector, example) }
|
145
|
+
end
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
private
|
150
|
+
|
151
|
+
# Clusters of examples in the same example group, on the same nesting
|
152
|
+
# level that can be aggregated.
|
153
|
+
def example_clusters(all_examples)
|
154
|
+
all_examples
|
155
|
+
.select { |example| example_with_expectations_only?(example) }
|
156
|
+
.group_by { |example| metadata_without_aggregate_failures(example) }
|
157
|
+
.select { |_, examples| examples.count > 1 }
|
158
|
+
end
|
159
|
+
|
160
|
+
# Clusters of examples that can be aggregated without losing any
|
161
|
+
# information (e.g. metadata or docstrings)
|
162
|
+
def example_clusters_for_autocorrect(example_node)
|
163
|
+
examples_in_group = example_node.parent.each_child_node(:block)
|
164
|
+
.select { |example| example_for_autocorrect?(example) }
|
165
|
+
example_clusters(examples_in_group)
|
166
|
+
end
|
167
|
+
|
168
|
+
def message_for(_example, first_example)
|
169
|
+
format(MSG, first_example.loc.line)
|
170
|
+
end
|
171
|
+
|
172
|
+
def drop_example(corrector, example)
|
173
|
+
aggregated_range = range_by_whole_lines(example.source_range,
|
174
|
+
include_final_newline: true)
|
175
|
+
corrector.remove(aggregated_range)
|
176
|
+
end
|
177
|
+
|
178
|
+
def aggregated_example(examples, metadata)
|
179
|
+
base_indent = ' ' * examples.first.source_range.column
|
180
|
+
metadata = metadata_for_aggregated_example(metadata)
|
181
|
+
[
|
182
|
+
"#{base_indent}it#{metadata} do",
|
183
|
+
*examples.map { |example| transform_body(example, base_indent) },
|
184
|
+
"#{base_indent}end\n"
|
185
|
+
].join("\n")
|
186
|
+
end
|
187
|
+
|
188
|
+
# Extracts and transforms the body, keeping proper indentation.
|
189
|
+
def transform_body(node, base_indent)
|
190
|
+
"#{base_indent} #{new_body(node)}"
|
191
|
+
end
|
192
|
+
|
193
|
+
def new_body(node)
|
194
|
+
node.body.source
|
195
|
+
end
|
196
|
+
end
|
197
|
+
end
|
198
|
+
end
|
199
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rubocop-rspec'
|
4
|
+
|
5
|
+
module RuboCop
|
6
|
+
module Cop
|
7
|
+
module RSpec
|
8
|
+
module MultipleExpectationsAutoCorrect
|
9
|
+
def self.prepended(base)
|
10
|
+
base.extend(AutoCorrector)
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def flag_example(node, expectation_count:)
|
16
|
+
add_offense(
|
17
|
+
node.send_node,
|
18
|
+
message: format(
|
19
|
+
MultipleExpectations::MSG,
|
20
|
+
total: expectation_count,
|
21
|
+
max: max_expectations
|
22
|
+
)
|
23
|
+
) do |corrector|
|
24
|
+
autocorrect(node, corrector)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def autocorrect(node, corrector)
|
29
|
+
if (args = node.children.first.arguments.first)
|
30
|
+
corrector.insert_after(args, ', :aggregate_failures')
|
31
|
+
else
|
32
|
+
corrector.insert_after(node.children.first, ' :aggregate_failures')
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
RuboCop::Cop::RSpec::MultipleExpectations
|
41
|
+
.prepend(RuboCop::Cop::RSpec::MultipleExpectationsAutoCorrect)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rubocop-petal
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jean-Francis Bastien
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2024-02-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rubocop
|
@@ -16,57 +16,71 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '1.
|
19
|
+
version: '1.59'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '1.
|
26
|
+
version: '1.59'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rubocop-factory_bot
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '2.24'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '2.24'
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: rubocop-performance
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
30
44
|
requirements:
|
31
45
|
- - "~>"
|
32
46
|
- !ruby/object:Gem::Version
|
33
|
-
version: '1.
|
47
|
+
version: '1.20'
|
34
48
|
type: :runtime
|
35
49
|
prerelease: false
|
36
50
|
version_requirements: !ruby/object:Gem::Requirement
|
37
51
|
requirements:
|
38
52
|
- - "~>"
|
39
53
|
- !ruby/object:Gem::Version
|
40
|
-
version: '1.
|
54
|
+
version: '1.20'
|
41
55
|
- !ruby/object:Gem::Dependency
|
42
56
|
name: rubocop-rails
|
43
57
|
requirement: !ruby/object:Gem::Requirement
|
44
58
|
requirements:
|
45
59
|
- - "~>"
|
46
60
|
- !ruby/object:Gem::Version
|
47
|
-
version: '2.
|
61
|
+
version: '2.23'
|
48
62
|
type: :runtime
|
49
63
|
prerelease: false
|
50
64
|
version_requirements: !ruby/object:Gem::Requirement
|
51
65
|
requirements:
|
52
66
|
- - "~>"
|
53
67
|
- !ruby/object:Gem::Version
|
54
|
-
version: '2.
|
68
|
+
version: '2.23'
|
55
69
|
- !ruby/object:Gem::Dependency
|
56
70
|
name: rubocop-rspec
|
57
71
|
requirement: !ruby/object:Gem::Requirement
|
58
72
|
requirements:
|
59
73
|
- - "~>"
|
60
74
|
- !ruby/object:Gem::Version
|
61
|
-
version: '2.
|
75
|
+
version: '2.25'
|
62
76
|
type: :runtime
|
63
77
|
prerelease: false
|
64
78
|
version_requirements: !ruby/object:Gem::Requirement
|
65
79
|
requirements:
|
66
80
|
- - "~>"
|
67
81
|
- !ruby/object:Gem::Version
|
68
|
-
version: '2.
|
69
|
-
description:
|
82
|
+
version: '2.25'
|
83
|
+
description:
|
70
84
|
email:
|
71
85
|
- jfbastien@petalmd.com
|
72
86
|
executables: []
|
@@ -96,9 +110,16 @@ files:
|
|
96
110
|
- lib/rubocop/cop/rails/enum_prefix.rb
|
97
111
|
- lib/rubocop/cop/rails/enum_starting_value.rb
|
98
112
|
- lib/rubocop/cop/rails/risky_activerecord_invocation.rb
|
113
|
+
- lib/rubocop/cop/rspec/aggregate_examples.rb
|
114
|
+
- lib/rubocop/cop/rspec/aggregate_examples/language.rb
|
115
|
+
- lib/rubocop/cop/rspec/aggregate_examples/line_range_helpers.rb
|
116
|
+
- lib/rubocop/cop/rspec/aggregate_examples/matchers_with_side_effects.rb
|
117
|
+
- lib/rubocop/cop/rspec/aggregate_examples/metadata_helpers.rb
|
118
|
+
- lib/rubocop/cop/rspec/aggregate_examples/node_matchers.rb
|
99
119
|
- lib/rubocop/cop/rspec/authenticated_as.rb
|
100
120
|
- lib/rubocop/cop/rspec/create_list_max.rb
|
101
121
|
- lib/rubocop/cop/rspec/json_response.rb
|
122
|
+
- lib/rubocop/cop/rspec/multiple_expectations_auto_correct.rb
|
102
123
|
- lib/rubocop/cop/rspec/sidekiq_inline.rb
|
103
124
|
- lib/rubocop/cop/rspec/stub_products.rb
|
104
125
|
- lib/rubocop/cop/sidekiq/no_nil_return.rb
|
@@ -114,7 +135,7 @@ metadata:
|
|
114
135
|
source_code_uri: https://github.com/petalmd/rubocop-petal
|
115
136
|
bug_tracker_uri: https://github.com/petalmd/rubocop-petal/issues
|
116
137
|
rubygems_mfa_required: 'true'
|
117
|
-
post_install_message:
|
138
|
+
post_install_message:
|
118
139
|
rdoc_options: []
|
119
140
|
require_paths:
|
120
141
|
- lib
|
@@ -122,15 +143,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
122
143
|
requirements:
|
123
144
|
- - ">="
|
124
145
|
- !ruby/object:Gem::Version
|
125
|
-
version: '2.
|
146
|
+
version: '2.7'
|
126
147
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
127
148
|
requirements:
|
128
149
|
- - ">="
|
129
150
|
- !ruby/object:Gem::Version
|
130
151
|
version: '0'
|
131
152
|
requirements: []
|
132
|
-
rubygems_version: 3.
|
133
|
-
signing_key:
|
153
|
+
rubygems_version: 3.4.18
|
154
|
+
signing_key:
|
134
155
|
specification_version: 4
|
135
156
|
summary: Petal custom cops
|
136
157
|
test_files: []
|