From 950a5b444f29f7a8ed1617e9d576b1f6abd038c6 Mon Sep 17 00:00:00 2001 From: Franz Liedke Date: Tue, 17 Oct 2023 20:58:08 +0200 Subject: [PATCH] RSpec/MetadataStyle: Support multiple string arguments before metadata --- CHANGELOG.md | 2 ++ lib/rubocop/cop/rspec/metadata_style.rb | 5 ++++ spec/rubocop/cop/rspec/metadata_style_spec.rb | 24 +++++++++++++++++++ 3 files changed, 31 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 93659584b..0c248c3c5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - Add support single quoted string and percent string and heredoc for `RSpec/Rails/HttpStatus`. ([@ydah]) - Change to be inline disable for `RSpec/SpecFilePathFormat` like `RSpec/FilePath`. ([@ydah]) +- Fix a false positive for `RSpec/MetadataStyle` with example groups having multiple string arguments. ([@franzliedke]) ## 2.24.1 (2023-09-23) @@ -842,6 +843,7 @@ Compatibility release so users can upgrade RuboCop to 0.51.0. No new features. [@faucct]: https://github.com/faucct [@foton]: https://github.com/foton [@francois-ferrandis]: https://github.com/francois-ferrandis +[@franzliedke]: https://github.com/franzliedke [@g-rath]: https://github.com/G-Rath [@geniou]: https://github.com/geniou [@gsamokovarov]: https://github.com/gsamokovarov diff --git a/lib/rubocop/cop/rspec/metadata_style.rb b/lib/rubocop/cop/rspec/metadata_style.rb index 9dc6df78c..bb9d4c628 100644 --- a/lib/rubocop/cop/rspec/metadata_style.rb +++ b/lib/rubocop/cop/rspec/metadata_style.rb @@ -45,6 +45,11 @@ class MetadataStyle < Base # rubocop:disable Metrics/ClassLength PATTERN def on_metadata(symbols, hash) + # RSpec example groups accept two string arguments. In such a case, + # the rspec_metadata matcher will interpret the second string + # argument as a metadata symbol. + symbols.shift if symbols.first&.str_type? + symbols.each do |symbol| on_metadata_symbol(symbol) end diff --git a/spec/rubocop/cop/rspec/metadata_style_spec.rb b/spec/rubocop/cop/rspec/metadata_style_spec.rb index 30a69de6b..58e4a852a 100644 --- a/spec/rubocop/cop/rspec/metadata_style_spec.rb +++ b/spec/rubocop/cop/rspec/metadata_style_spec.rb @@ -203,6 +203,15 @@ end end + context 'with boolean hash metadata after 2 string arguments' do + it 'registers no offense' do + expect_no_offenses(<<~RUBY) + describe 'Something', 'Something else', { a: true } do + end + RUBY + end + end + context 'with 1 symbol metadata' do it 'registers offense' do expect_offense(<<~RUBY) @@ -234,6 +243,21 @@ end end + context 'with symbol metadata after 2 string arguments' do + it 'registers offense' do + expect_offense(<<~RUBY) + describe 'Something', 'Something else', :a do + ^^ Use hash style for metadata. + end + RUBY + + expect_correction(<<~RUBY) + describe 'Something', 'Something else', a: true do + end + RUBY + end + end + context 'with symbol metadata with parentheses' do it 'registers offense' do expect_offense(<<~RUBY)