From cbf5814c49c8a7bc2a8407c6bc1567982747b27e Mon Sep 17 00:00:00 2001 From: Benjamin Quorning <22333+bquorning@users.noreply.github.com> Date: Mon, 24 Oct 2022 15:49:12 +0200 Subject: [PATCH 1/6] Merge pull request #1434 from ydah/fix_false_positive_for_no_expectation Fix a false positive for `RSpec/NoExpectationExample` when allowed pattern methods with arguments --- CHANGELOG.md | 2 ++ lib/rubocop/cop/rspec/no_expectation_example.rb | 2 +- spec/rubocop/cop/rspec/no_expectation_example_spec.rb | 9 +++++++++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a10f69bc1..b84edcfee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Master (Unreleased) +- Fix a false positive for `RSpec/NoExpectationExample` when allowed pattern methods with arguments. ([@ydah]) + ## 2.14.1 (2022-10-24) - Fix an error for `RSpec/Rails/InferredSpecType` with redundant type before other Hash metadata. ([@ydah]) diff --git a/lib/rubocop/cop/rspec/no_expectation_example.rb b/lib/rubocop/cop/rspec/no_expectation_example.rb index f09adecb7..6d37e888a 100644 --- a/lib/rubocop/cop/rspec/no_expectation_example.rb +++ b/lib/rubocop/cop/rspec/no_expectation_example.rb @@ -77,7 +77,7 @@ class NoExpectationExample < Base def_node_search :includes_expectation?, <<~PATTERN { #{send_pattern('#Expectations.all')} - (send nil? `#matches_allowed_pattern?) + (send nil? `#matches_allowed_pattern? ...) } PATTERN diff --git a/spec/rubocop/cop/rspec/no_expectation_example_spec.rb b/spec/rubocop/cop/rspec/no_expectation_example_spec.rb index 74a7a923e..b0caff9a0 100644 --- a/spec/rubocop/cop/rspec/no_expectation_example_spec.rb +++ b/spec/rubocop/cop/rspec/no_expectation_example_spec.rb @@ -165,6 +165,15 @@ end end + context 'when only allowed pattern methods with arguments are used' do + it 'registers no offenses' do + expect_no_offenses(<<~RUBY) + it { expect_something(foo, bar) } + it { expect_something(foo, bar, baz) } + RUBY + end + end + context 'when allowed pattern methods and other method are used' do it 'registers no offenses' do expect_no_offenses(<<~RUBY) From e124e7e0d42bf579f70cbf7a3f587a4df23ddae5 Mon Sep 17 00:00:00 2001 From: Phil Pirozhkov Date: Tue, 25 Oct 2022 00:18:22 +0300 Subject: [PATCH 2/6] Merge pull request #1438 from rubocop/fix-infinite-correction-loop Fix infinite correction loop in RSpec/ExampleWording --- CHANGELOG.md | 1 + lib/rubocop/cop/rspec/example_wording.rb | 15 +++++++-------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b84edcfee..ef4f0947f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Master (Unreleased) +- Fix autocorrection loop in `RSpec/ExampleWording` for insufficient example wording. ([@pirj]) - Fix a false positive for `RSpec/NoExpectationExample` when allowed pattern methods with arguments. ([@ydah]) ## 2.14.1 (2022-10-24) diff --git a/lib/rubocop/cop/rspec/example_wording.rb b/lib/rubocop/cop/rspec/example_wording.rb index 88703299f..157525929 100644 --- a/lib/rubocop/cop/rspec/example_wording.rb +++ b/lib/rubocop/cop/rspec/example_wording.rb @@ -68,20 +68,15 @@ def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler add_wording_offense(description_node, MSG_SHOULD) elsif message.match?(IT_PREFIX) add_wording_offense(description_node, MSG_IT) - else - check_and_handle_insufficient_examples(description_node) + elsif insufficient_docstring?(description_node) + add_offense(docstring(description_node), + message: MSG_INSUFFICIENT_DESCRIPTION) end end end private - def check_and_handle_insufficient_examples(description) - if insufficient_examples.include?(preprocess(text(description))) - add_wording_offense(description, MSG_INSUFFICIENT_DESCRIPTION) - end - end - def add_wording_offense(node, message) docstring = docstring(node) @@ -137,6 +132,10 @@ def ignored_words cop_config.fetch('IgnoredWords', []) end + def insufficient_docstring?(description_node) + insufficient_examples.include?(preprocess(text(description_node))) + end + def insufficient_examples examples = cop_config.fetch('DisallowedExamples', []) examples.map! { |example| preprocess(example) } From 58da998dc17b564f2417a18585b16e843c133a78 Mon Sep 17 00:00:00 2001 From: Benjamin Quorning <22333+bquorning@users.noreply.github.com> Date: Tue, 25 Oct 2022 08:14:51 +0200 Subject: [PATCH 3/6] Merge pull request #1437 from rubocop/fix-mistaken-positional-arguments-reordering Skip reordering positional arguments for include_* in RSpec/SortMetadata --- CHANGELOG.md | 1 + lib/rubocop/cop/rspec/sort_metadata.rb | 3 +- spec/rubocop/cop/rspec/sort_metadata_spec.rb | 33 ++++++++------------ 3 files changed, 15 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ef4f0947f..9dd25680b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## Master (Unreleased) - Fix autocorrection loop in `RSpec/ExampleWording` for insufficient example wording. ([@pirj]) +- Fix `RSpec/SortMetadata` not to reorder arguments of `include_`/`it_behaves_like`. ([@pirj]) - Fix a false positive for `RSpec/NoExpectationExample` when allowed pattern methods with arguments. ([@ydah]) ## 2.14.1 (2022-10-24) diff --git a/lib/rubocop/cop/rspec/sort_metadata.rb b/lib/rubocop/cop/rspec/sort_metadata.rb index 59d482ea3..74c458bd1 100644 --- a/lib/rubocop/cop/rspec/sort_metadata.rb +++ b/lib/rubocop/cop/rspec/sort_metadata.rb @@ -26,8 +26,7 @@ class SortMetadata < Base def_node_matcher :rspec_metadata, <<~PATTERN (block (send - #rspec? {#Examples.all #ExampleGroups.all #SharedGroups.all #Hooks.all #Includes.all} - _ ${send str sym}* (hash $...)?) + #rspec? {#Examples.all #ExampleGroups.all #SharedGroups.all #Hooks.all} _ ${send str sym}* (hash $...)?) ...) PATTERN diff --git a/spec/rubocop/cop/rspec/sort_metadata_spec.rb b/spec/rubocop/cop/rspec/sort_metadata_spec.rb index f367a0f6f..c6129a366 100644 --- a/spec/rubocop/cop/rspec/sort_metadata_spec.rb +++ b/spec/rubocop/cop/rspec/sort_metadata_spec.rb @@ -160,6 +160,17 @@ RUBY end + it "ignores includes' positional arguments" do + expect_no_offenses(<<~RUBY) + include_examples 'z', 'a' do + end + it_behaves_like 'z', 'a' do + end + include_context 'z', 'a' do + end + RUBY + end + context 'when using custom RSpec language ' \ 'without adjusting the RuboCop RSpec language configuration' do it 'does not register an offense' do @@ -179,31 +190,13 @@ 'and adjusting the RuboCop RSpec language configuration' do before do other_cops.tap do |config| - config.dig('RSpec', 'Language', 'Includes', 'Context').push( + config.dig('RSpec', 'Language', 'ExampleGroups', 'Regular').push( 'describan', 'contexto_compartido' ) - config.dig('RSpec', 'Language', 'Includes', 'Examples').push('ejemplo') + config.dig('RSpec', 'Language', 'Examples', 'Regular').push('ejemplo') end end - let(:language_config) do - <<~YAML - RSpec: - Language: - ExampleGroups: - Regular: - - describan - Examples: - Regular: - - ejemplo - Hooks: - - antes - SharedGroups: - Context: - - contexto_compartido - YAML - end - it 'registers an offense' do expect_offense(<<~RUBY) RSpec.describan "Algo", :b, :a do From b0a638a05bc19cf236829d16f2059e09e78ba3b7 Mon Sep 17 00:00:00 2001 From: Phil Pirozhkov Date: Tue, 25 Oct 2022 19:15:35 +0300 Subject: [PATCH 4/6] Merge pull request #1443 from ydah/fix/1442 Fix an incorrect autocorrect for `FactoryBot/ConsistentParenthesesStyle` with `omit_parentheses` option when method name and first argument are not on same line --- CHANGELOG.md | 1 + .../ROOT/pages/cops_rspec_factorybot.adoc | 10 +++++ .../consistent_parentheses_style.rb | 11 ++++++ .../consistent_parentheses_style_spec.rb | 38 +++++++++++++++++++ 4 files changed, 60 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9dd25680b..02e940186 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Master (Unreleased) +- Fix an incorrect autocorrect for `FactoryBot/ConsistentParenthesesStyle` with `omit_parentheses` option when method name and first argument are not on same line. ([@ydah]) - Fix autocorrection loop in `RSpec/ExampleWording` for insufficient example wording. ([@pirj]) - Fix `RSpec/SortMetadata` not to reorder arguments of `include_`/`it_behaves_like`. ([@pirj]) - Fix a false positive for `RSpec/NoExpectationExample` when allowed pattern methods with arguments. ([@ydah]) diff --git a/docs/modules/ROOT/pages/cops_rspec_factorybot.adoc b/docs/modules/ROOT/pages/cops_rspec_factorybot.adoc index d8c11de74..9cd77afc9 100644 --- a/docs/modules/ROOT/pages/cops_rspec_factorybot.adoc +++ b/docs/modules/ROOT/pages/cops_rspec_factorybot.adoc @@ -96,6 +96,16 @@ create :user build :user create :login create :login + +# also good +# when method name and first argument are not on same line +create( + :user +) +build( + :user, + name: 'foo' +) ---- === Configurable attributes diff --git a/lib/rubocop/cop/rspec/factory_bot/consistent_parentheses_style.rb b/lib/rubocop/cop/rspec/factory_bot/consistent_parentheses_style.rb index 76918656e..5d02cbe43 100644 --- a/lib/rubocop/cop/rspec/factory_bot/consistent_parentheses_style.rb +++ b/lib/rubocop/cop/rspec/factory_bot/consistent_parentheses_style.rb @@ -30,6 +30,16 @@ module FactoryBot # create :login # create :login # + # # also good + # # when method name and first argument are not on same line + # create( + # :user + # ) + # build( + # :user, + # name: 'foo' + # ) + # class ConsistentParenthesesStyle < Base extend AutoCorrector include ConfigurableEnforcedStyle @@ -66,6 +76,7 @@ def on_send(node) def process_with_parentheses(node) return unless style == :omit_parentheses + return unless same_line?(node, node.first_argument) add_offense(node.loc.selector, message: MSG_OMIT_PARENS) do |corrector| diff --git a/spec/rubocop/cop/rspec/factory_bot/consistent_parentheses_style_spec.rb b/spec/rubocop/cop/rspec/factory_bot/consistent_parentheses_style_spec.rb index 15c0ac065..ac7b834db 100644 --- a/spec/rubocop/cop/rspec/factory_bot/consistent_parentheses_style_spec.rb +++ b/spec/rubocop/cop/rspec/factory_bot/consistent_parentheses_style_spec.rb @@ -275,5 +275,43 @@ end RUBY end + + context 'when create and first argument are on same line' do + it 'register an offense' do + expect_offense(<<~RUBY) + create(:user, + ^^^^^^ Prefer method call without parentheses + name: 'foo' + ) + RUBY + + expect_correction(<<~RUBY) + create :user, + name: 'foo' + + RUBY + end + end + + context 'when create and first argument are not on same line' do + it 'does not register an offense' do + expect_no_offenses(<<~RUBY) + create( + :user + ) + RUBY + end + end + + context 'when create and some argument are not on same line' do + it 'does not register an offense' do + expect_no_offenses(<<~RUBY) + create( + :user, + name: 'foo' + ) + RUBY + end + end end end From 3c78b928c9be7728e269ac52c0d900c73224ac2b Mon Sep 17 00:00:00 2001 From: Phil Pirozhkov Date: Tue, 25 Oct 2022 20:23:47 +0300 Subject: [PATCH 5/6] Merge pull request #1441 from r7kamura/feature/routing-spec Change `RSpec/FilePath` so that it only checks suffix when path is under spec/routing or type is defined as routing --- CHANGELOG.md | 1 + lib/rubocop/cop/rspec/file_path.rb | 23 ++++++++++------ spec/rubocop/cop/rspec/file_path_spec.rb | 34 ++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 02e940186..65aa2e5fc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ - Fix autocorrection loop in `RSpec/ExampleWording` for insufficient example wording. ([@pirj]) - Fix `RSpec/SortMetadata` not to reorder arguments of `include_`/`it_behaves_like`. ([@pirj]) - Fix a false positive for `RSpec/NoExpectationExample` when allowed pattern methods with arguments. ([@ydah]) +- Change `RSpec/FilePath` so that it only checks suffix when path is under spec/routing or type is defined as routing. ([@r7kamura]) ## 2.14.1 (2022-10-24) diff --git a/lib/rubocop/cop/rspec/file_path.rb b/lib/rubocop/cop/rspec/file_path.rb index 2ef8a7159..ed4fbf444 100644 --- a/lib/rubocop/cop/rspec/file_path.rb +++ b/lib/rubocop/cop/rspec/file_path.rb @@ -76,8 +76,6 @@ def on_top_level_example_group(node) return unless top_level_groups.one? example_group(node) do |send_node, example_group, arguments| - next if routing_spec?(arguments) - ensure_correct_file_path(send_node, example_group, arguments) end end @@ -85,7 +83,7 @@ def on_top_level_example_group(node) private def ensure_correct_file_path(send_node, example_group, arguments) - pattern = pattern_for(example_group, arguments.first) + pattern = pattern_for(example_group, arguments) return if filename_ends_with?(pattern) # For the suffix shown in the offense message, modify the regular @@ -97,11 +95,13 @@ def ensure_correct_file_path(send_node, example_group, arguments) end def routing_spec?(args) - args.any?(&method(:routing_metadata?)) + args.any?(&method(:routing_metadata?)) || routing_spec_path? end - def pattern_for(example_group, method_name) - if spec_suffix_only? || !example_group.const_type? + def pattern_for(example_group, arguments) + method_name = arguments.first + if spec_suffix_only? || !example_group.const_type? || + routing_spec?(arguments) return pattern_for_spec_suffix_only end @@ -149,8 +149,7 @@ def ignore_methods? end def filename_ends_with?(pattern) - filename = File.expand_path(processed_source.buffer.name) - filename.match?("#{pattern}$") + expanded_file_path.match?("#{pattern}$") end def relevant_rubocop_rspec_file?(_file) @@ -160,6 +159,14 @@ def relevant_rubocop_rspec_file?(_file) def spec_suffix_only? cop_config['SpecSuffixOnly'] end + + def routing_spec_path? + expanded_file_path.include?('spec/routing/') + end + + def expanded_file_path + File.expand_path(processed_source.buffer.name) + end end end end diff --git a/spec/rubocop/cop/rspec/file_path_spec.rb b/spec/rubocop/cop/rspec/file_path_spec.rb index 392e76189..6330e6185 100644 --- a/spec/rubocop/cop/rspec/file_path_spec.rb +++ b/spec/rubocop/cop/rspec/file_path_spec.rb @@ -248,6 +248,40 @@ class Foo RUBY end + context 'when path is under spec/routing and it ends with _spec.rb' do + it 'does not register an offense' do + expect_no_offenses(<<~RUBY, 'spec/routing/foo_spec.rb') + describe 'routes to the foo controller' do; end + RUBY + end + end + + context 'when path is under spec/routing and it does not end with _spec.rb' do + it 'registers an offense' do + expect_offense(<<~RUBY, 'spec/routing/foo.rb') + describe 'routes to the foo controller' do; end + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Spec path should end with `*_spec.rb`. + RUBY + end + end + + context 'when `type: :routing` is used and it ends with _spec.rb' do + it 'does not register an offense' do + expect_no_offenses(<<~RUBY, 'spec/foo_spec.rb') + describe 'routes to the foo controller', type: :routing do; end + RUBY + end + end + + context 'when `type: :routing` is used and it does not end with _spec.rb' do + it 'registers an offense' do + expect_offense(<<~RUBY, 'spec/foo.rb') + describe 'routes to the foo controller', type: :routing do; end + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Spec path should end with `*_spec.rb`. + RUBY + end + end + context 'when configured with CustomTransform' do let(:cop_config) { { 'CustomTransform' => { 'FooFoo' => 'foofoo' } } } From e5780842e998dd3d028a95d3840cd7a57e5590ee Mon Sep 17 00:00:00 2001 From: Benjamin Quorning Date: Tue, 25 Oct 2022 19:42:37 +0200 Subject: [PATCH 6/6] Bump version to 2.14.2 --- CHANGELOG.md | 2 ++ lib/rubocop/rspec/version.rb | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 65aa2e5fc..437dd2b9e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Master (Unreleased) +## 2.14.2 (2022-10-25) + - Fix an incorrect autocorrect for `FactoryBot/ConsistentParenthesesStyle` with `omit_parentheses` option when method name and first argument are not on same line. ([@ydah]) - Fix autocorrection loop in `RSpec/ExampleWording` for insufficient example wording. ([@pirj]) - Fix `RSpec/SortMetadata` not to reorder arguments of `include_`/`it_behaves_like`. ([@pirj]) diff --git a/lib/rubocop/rspec/version.rb b/lib/rubocop/rspec/version.rb index 8a48aeb02..9ffe55a71 100644 --- a/lib/rubocop/rspec/version.rb +++ b/lib/rubocop/rspec/version.rb @@ -4,7 +4,7 @@ module RuboCop module RSpec # Version information for the RSpec RuboCop plugin. module Version - STRING = '2.14.1' + STRING = '2.14.2' end end end