diff options
Diffstat (limited to 'spec/rubocop/cop/rspec')
-rw-r--r-- | spec/rubocop/cop/rspec/avoid_conditional_statements_spec.rb | 42 | ||||
-rw-r--r-- | spec/rubocop/cop/rspec/avoid_test_prof_spec.rb | 2 | ||||
-rw-r--r-- | spec/rubocop/cop/rspec/httparty_basic_auth_spec.rb (renamed from spec/rubocop/cop/rspec/htt_party_basic_auth_spec.rb) | 2 | ||||
-rw-r--r-- | spec/rubocop/cop/rspec/invalid_feature_category_spec.rb | 14 | ||||
-rw-r--r-- | spec/rubocop/cop/rspec/misspelled_aggregate_failures_spec.rb | 136 | ||||
-rw-r--r-- | spec/rubocop/cop/rspec/shared_groups_metadata_spec.rb | 70 |
6 files changed, 257 insertions, 9 deletions
diff --git a/spec/rubocop/cop/rspec/avoid_conditional_statements_spec.rb b/spec/rubocop/cop/rspec/avoid_conditional_statements_spec.rb new file mode 100644 index 00000000000..3f45f660aa5 --- /dev/null +++ b/spec/rubocop/cop/rspec/avoid_conditional_statements_spec.rb @@ -0,0 +1,42 @@ +# frozen_string_literal: true + +require 'rubocop_spec_helper' + +require_relative '../../../../rubocop/cop/rspec/avoid_conditional_statements' + +RSpec.describe RuboCop::Cop::RSpec::AvoidConditionalStatements, feature_category: :tooling do + context 'when using conditionals' do + it 'flags if conditional' do + expect_offense(<<~RUBY) + if page.has_css?('[data-testid="begin-commit-button"]') + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Don't use `if` conditional statement in specs, it might create flakiness. See https://gitlab.com/gitlab-org/gitlab/-/issues/385304#note_1345437109 + find('[data-testid="begin-commit-button"]').click + end + RUBY + end + + it 'flags unless conditional' do + expect_offense(<<~RUBY) + RSpec.describe 'Multi-file editor new directory', :js, feature_category: :web_ide do + it 'creates directory in current directory' do + unless page.has_css?('[data-testid="begin-commit-button"]') + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Don't use `unless` conditional statement in specs, it might create flakiness. See https://gitlab.com/gitlab-org/gitlab/-/issues/385304#note_1345437109 + find('[data-testid="begin-commit-button"]').click + end + end + end + RUBY + end + + it 'flags ternary operator' do + expect_offense(<<~RUBY) + RSpec.describe 'Multi-file editor new directory', :js, feature_category: :web_ide do + it 'creates directory in current directory' do + user.present ? user : nil + ^^^^^^^^^^^^^^^^^^^^^^^^^ Don't use `user.present ? user : nil` conditional statement in specs, it might create flakiness. See https://gitlab.com/gitlab-org/gitlab/-/issues/385304#note_1345437109 + end + end + RUBY + end + end +end diff --git a/spec/rubocop/cop/rspec/avoid_test_prof_spec.rb b/spec/rubocop/cop/rspec/avoid_test_prof_spec.rb index b180134b226..db8c7b1d783 100644 --- a/spec/rubocop/cop/rspec/avoid_test_prof_spec.rb +++ b/spec/rubocop/cop/rspec/avoid_test_prof_spec.rb @@ -5,7 +5,7 @@ require 'rspec-parameterized' require_relative '../../../../rubocop/cop/rspec/avoid_test_prof' -RSpec.describe RuboCop::Cop::RSpec::AvoidTestProf, feature_category: :not_owned do +RSpec.describe RuboCop::Cop::RSpec::AvoidTestProf, feature_category: :shared do using RSpec::Parameterized::TableSyntax context 'when there are offenses' do diff --git a/spec/rubocop/cop/rspec/htt_party_basic_auth_spec.rb b/spec/rubocop/cop/rspec/httparty_basic_auth_spec.rb index 537a7a9a7e9..c7f4c61b501 100644 --- a/spec/rubocop/cop/rspec/htt_party_basic_auth_spec.rb +++ b/spec/rubocop/cop/rspec/httparty_basic_auth_spec.rb @@ -4,7 +4,7 @@ require 'rubocop_spec_helper' require_relative '../../../../rubocop/cop/rspec/httparty_basic_auth' -RSpec.describe RuboCop::Cop::RSpec::HTTPartyBasicAuth do +RSpec.describe RuboCop::Cop::RSpec::HTTPartyBasicAuth, feature_category: :shared do context 'when passing `basic_auth: { user: ... }`' do it 'registers an offense and corrects', :aggregate_failures do expect_offense(<<~SOURCE, 'spec/foo.rb') diff --git a/spec/rubocop/cop/rspec/invalid_feature_category_spec.rb b/spec/rubocop/cop/rspec/invalid_feature_category_spec.rb index 0d2fd029a13..e5287f7105e 100644 --- a/spec/rubocop/cop/rspec/invalid_feature_category_spec.rb +++ b/spec/rubocop/cop/rspec/invalid_feature_category_spec.rb @@ -17,7 +17,7 @@ RSpec.describe RuboCop::Cop::RSpec::InvalidFeatureCategory, feature_category: :t it 'flags invalid feature category in nested context' do expect_offense(<<~RUBY, valid: valid_category, invalid: invalid_category) - RSpec.describe 'foo', feature_category: :%{valid} do + RSpec.describe 'foo', feature_category: :"%{valid}" do context 'bar', foo: :bar, feature_category: :%{invalid} do ^^{invalid} Please use a valid feature category. See https://docs.gitlab.com/ee/development/feature_categorization/#rspec-examples. end @@ -27,7 +27,7 @@ RSpec.describe RuboCop::Cop::RSpec::InvalidFeatureCategory, feature_category: :t it 'flags invalid feature category in examples' do expect_offense(<<~RUBY, valid: valid_category, invalid: invalid_category) - RSpec.describe 'foo', feature_category: :%{valid} do + RSpec.describe 'foo', feature_category: :"%{valid}" do it 'bar', feature_category: :%{invalid} do ^^{invalid} Please use a valid feature category. See https://docs.gitlab.com/ee/development/feature_categorization/#rspec-examples. end @@ -37,9 +37,9 @@ RSpec.describe RuboCop::Cop::RSpec::InvalidFeatureCategory, feature_category: :t it 'does not flag if feature category is valid' do expect_no_offenses(<<~RUBY) - RSpec.describe 'foo', feature_category: :#{valid_category} do - context 'bar', feature_category: :#{valid_category} do - it 'baz', feature_category: :#{valid_category} do + RSpec.describe 'foo', feature_category: :"#{valid_category}" do + context 'bar', feature_category: :"#{valid_category}" do + it 'baz', feature_category: :"#{valid_category}" do end end end @@ -50,8 +50,8 @@ RSpec.describe RuboCop::Cop::RSpec::InvalidFeatureCategory, feature_category: :t mistyped = make_typo(valid_category) expect_offense(<<~RUBY, invalid: mistyped, valid: valid_category) - RSpec.describe 'foo', feature_category: :%{invalid} do - ^^{invalid} Please use a valid feature category. Did you mean `:%{valid}`? See [...] + RSpec.describe 'foo', feature_category: :"%{invalid}" do + ^^^^{invalid} Please use a valid feature category. Did you mean `:%{valid}`? See [...] end RUBY end diff --git a/spec/rubocop/cop/rspec/misspelled_aggregate_failures_spec.rb b/spec/rubocop/cop/rspec/misspelled_aggregate_failures_spec.rb new file mode 100644 index 00000000000..c551c03b896 --- /dev/null +++ b/spec/rubocop/cop/rspec/misspelled_aggregate_failures_spec.rb @@ -0,0 +1,136 @@ +# frozen_string_literal: true + +require 'rubocop_spec_helper' +require 'rspec-parameterized' + +require_relative '../../../../rubocop/cop/rspec/misspelled_aggregate_failures' + +RSpec.describe RuboCop::Cop::RSpec::MisspelledAggregateFailures, feature_category: :shared do + shared_examples 'misspelled tag' do |misspelled| + it 'flags and auto-corrects misspelled tags in describe' do + expect_offense(<<~'RUBY', misspelled: misspelled) + RSpec.describe 'a feature', :%{misspelled} do + ^^{misspelled} Use `:aggregate_failures` to aggregate failures. + describe 'inner', :%{misspelled} do + ^^{misspelled} Use `:aggregate_failures` to aggregate failures. + end + end + RUBY + + expect_correction(<<~'RUBY') + RSpec.describe 'a feature', :aggregate_failures do + describe 'inner', :aggregate_failures do + end + end + RUBY + end + + it 'flags and auto-corrects misspelled tags in context' do + expect_offense(<<~'RUBY', misspelled: misspelled) + context 'a feature', :%{misspelled} do + ^^{misspelled} Use `:aggregate_failures` to aggregate failures. + end + RUBY + + expect_correction(<<~'RUBY') + context 'a feature', :aggregate_failures do + end + RUBY + end + + it 'flags and auto-corrects misspelled tags in examples' do + expect_offense(<<~'RUBY', misspelled: misspelled) + it 'aggregates', :%{misspelled} do + ^^{misspelled} Use `:aggregate_failures` to aggregate failures. + end + + specify :%{misspelled} do + ^^{misspelled} Use `:aggregate_failures` to aggregate failures. + end + + it :%{misspelled} do + ^^{misspelled} Use `:aggregate_failures` to aggregate failures. + end + RUBY + + expect_correction(<<~'RUBY') + it 'aggregates', :aggregate_failures do + end + + specify :aggregate_failures do + end + + it :aggregate_failures do + end + RUBY + end + + it 'flags and auto-corrects misspelled tags in any order' do + expect_offense(<<~'RUBY', misspelled: misspelled) + it 'aggregates', :foo, :%{misspelled} do + ^^{misspelled} Use `:aggregate_failures` to aggregate failures. + end + + it 'aggregates', :%{misspelled}, :bar do + ^^{misspelled} Use `:aggregate_failures` to aggregate failures. + end + RUBY + + expect_correction(<<~'RUBY') + it 'aggregates', :foo, :aggregate_failures do + end + + it 'aggregates', :aggregate_failures, :bar do + end + RUBY + end + end + + shared_examples 'legit tag' do |legit_tag| + it 'does not flag' do + expect_no_offenses(<<~RUBY) + RSpec.describe 'a feature', :#{legit_tag} do + end + + it 'is ok', :#{legit_tag} do + end + RUBY + end + end + + context 'with misspelled tags' do + where(:tag) do + # From https://gitlab.com/gitlab-org/gitlab/-/issues/396356#list + %w[ + aggregate_errors + aggregate_failure + aggregated_failures + aggregate_results + aggregated_errors + aggregates_failures + aggregate_failues + + aggregate_bar + aggregate_foo + ] + end + + with_them do + it_behaves_like 'misspelled tag', params[:tag] + end + end + + context 'with legit tags' do + where(:tag) do + %w[ + aggregate + aggregations + aggregate_two_underscores + ] + end + + with_them do + it_behaves_like 'legit tag', params[:tag] + end + end +end diff --git a/spec/rubocop/cop/rspec/shared_groups_metadata_spec.rb b/spec/rubocop/cop/rspec/shared_groups_metadata_spec.rb new file mode 100644 index 00000000000..3dd568e7dcd --- /dev/null +++ b/spec/rubocop/cop/rspec/shared_groups_metadata_spec.rb @@ -0,0 +1,70 @@ +# frozen_string_literal: true + +require 'rubocop_spec_helper' +require 'rspec-parameterized' + +require_relative '../../../../rubocop/cop/rspec/shared_groups_metadata' + +RSpec.describe RuboCop::Cop::RSpec::SharedGroupsMetadata, feature_category: :tooling do + context 'with hash metadata' do + it 'flags metadata in shared example' do + expect_offense(<<~RUBY) + RSpec.shared_examples 'foo', feature_category: :shared do + ^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid using metadata on shared examples and shared context. They might cause flaky tests. See https://gitlab.com/gitlab-org/gitlab/-/issues/404388 + end + + shared_examples 'foo', feature_category: :shared do + ^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid using metadata on shared examples and shared context. They might cause flaky tests. See https://gitlab.com/gitlab-org/gitlab/-/issues/404388 + end + RUBY + end + + it 'flags metadata in shared context' do + expect_offense(<<~RUBY) + RSpec.shared_context 'foo', feature_category: :shared do + ^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid using metadata on shared examples and shared context. They might cause flaky tests. See https://gitlab.com/gitlab-org/gitlab/-/issues/404388 + end + + shared_context 'foo', feature_category: :shared do + ^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid using metadata on shared examples and shared context. They might cause flaky tests. See https://gitlab.com/gitlab-org/gitlab/-/issues/404388 + end + RUBY + end + end + + context 'with symbol metadata' do + it 'flags metadata in shared example' do + expect_offense(<<~RUBY) + RSpec.shared_examples 'foo', :aggregate_failures do + ^^^^^^^^^^^^^^^^^^^ Avoid using metadata on shared examples and shared context. They might cause flaky tests. See https://gitlab.com/gitlab-org/gitlab/-/issues/404388 + end + + shared_examples 'foo', :aggregate_failures do + ^^^^^^^^^^^^^^^^^^^ Avoid using metadata on shared examples and shared context. They might cause flaky tests. See https://gitlab.com/gitlab-org/gitlab/-/issues/404388 + end + RUBY + end + + it 'flags metadata in shared context' do + expect_offense(<<~RUBY) + RSpec.shared_context 'foo', :aggregate_failures do + ^^^^^^^^^^^^^^^^^^^ Avoid using metadata on shared examples and shared context. They might cause flaky tests. See https://gitlab.com/gitlab-org/gitlab/-/issues/404388 + end + + shared_context 'foo', :aggregate_failures do + ^^^^^^^^^^^^^^^^^^^ Avoid using metadata on shared examples and shared context. They might cause flaky tests. See https://gitlab.com/gitlab-org/gitlab/-/issues/404388 + end + RUBY + end + end + + it 'does not flag if feature category is missing' do + expect_no_offenses(<<~RUBY) + RSpec.shared_examples 'foo' do + end + + shared_examples 'foo' do + end + RUBY + end +end |