diff options
Diffstat (limited to 'spec/rubocop/cop')
7 files changed, 359 insertions, 3 deletions
diff --git a/spec/rubocop/cop/gitlab/doc_url_spec.rb b/spec/rubocop/cop/gitlab/doc_url_spec.rb new file mode 100644 index 00000000000..4a7ef14ccbc --- /dev/null +++ b/spec/rubocop/cop/gitlab/doc_url_spec.rb @@ -0,0 +1,69 @@ +# frozen_string_literal: true + +require 'rubocop_spec_helper' +require_relative '../../../../rubocop/cop/gitlab/doc_url' + +RSpec.describe RuboCop::Cop::Gitlab::DocUrl, feature_category: :not_owned do + context 'when string literal is added with docs url prefix' do + context 'when inlined' do + it 'registers an offense' do + expect_offense(<<~RUBY) + 'See [the docs](https://docs.gitlab.com/ee/user/permissions#roles).' + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `#help_page_url` instead of directly including link. See https://docs.gitlab.com/ee/development/documentation/#linking-to-help-in-ruby. + RUBY + end + end + + context 'when multilined' do + it 'registers an offense' do + expect_offense(<<~'RUBY') + 'See the docs: ' \ + 'https://docs.gitlab.com/ee/user/permissions#roles' + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `#help_page_url` instead of directly including link. See https://docs.gitlab.com/ee/development/documentation/#linking-to-help-in-ruby. + RUBY + end + end + + context 'with heredoc' do + it 'registers an offense' do + expect_offense(<<~RUBY) + <<-HEREDOC + See the docs: + https://docs.gitlab.com/ee/user/permissions#roles + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `#help_page_url` instead of directly including link. See https://docs.gitlab.com/ee/development/documentation/#linking-to-help-in-ruby. + HEREDOC + RUBY + end + end + end + + context 'when string literal is added without docs url prefix' do + context 'when inlined' do + it 'does not register an offense' do + expect_no_offenses(<<~RUBY) + '[The DevSecOps Platform](https://about.gitlab.com/)' + RUBY + end + end + + context 'when multilined' do + it 'does not register an offense' do + expect_no_offenses(<<~RUBY) + 'The DevSecOps Platform: ' \ + 'https://about.gitlab.com/' + RUBY + end + end + + context 'with heredoc' do + it 'does not register an offense' do + expect_no_offenses(<<~RUBY) + <<-HEREDOC + The DevSecOps Platform: + https://about.gitlab.com/ + HEREDOC + RUBY + end + end + end +end diff --git a/spec/rubocop/cop/migration/prevent_single_statement_with_disable_ddl_transaction_spec.rb b/spec/rubocop/cop/migration/prevent_single_statement_with_disable_ddl_transaction_spec.rb new file mode 100644 index 00000000000..968de7d2160 --- /dev/null +++ b/spec/rubocop/cop/migration/prevent_single_statement_with_disable_ddl_transaction_spec.rb @@ -0,0 +1,67 @@ +# frozen_string_literal: true + +require 'rubocop_spec_helper' +require_relative '../../../../rubocop/cop/migration/prevent_single_statement_with_disable_ddl_transaction' + +RSpec.describe RuboCop::Cop::Migration::PreventSingleStatementWithDisableDdlTransaction, feature_category: :database do + context 'when in migration' do + before do + allow(cop).to receive(:in_migration?).and_return(true) + end + + it 'registers an offense when `disable_ddl_transaction!` is only for the :validate_foreign_key statement' do + code = <<~RUBY + class SomeMigration < Gitlab::Database::Migration[2.1] + disable_ddl_transaction! + def up + validate_foreign_key :emails, :user_id + end + def down + # no-op + end + end + RUBY + + expect_offense(<<~RUBY, node: code, msg: described_class::MSG) + class SomeMigration < Gitlab::Database::Migration[2.1] + disable_ddl_transaction! + ^^^^^^^^^^^^^^^^^^^^^^^^ %{msg} + def up + validate_foreign_key :emails, :user_id + end + def down + # no-op + end + end + RUBY + end + + it 'registers no offense when `disable_ddl_transaction!` is used with more than one statement' do + expect_no_offenses(<<~RUBY) + class SomeMigration < Gitlab::Database::Migration[2.1] + disable_ddl_transaction! + def up + add_concurrent_foreign_key :emails, :users, column: :user_id, on_delete: :cascade, validate: false + validate_foreign_key :emails, :user_id + end + def down + remove_foreign_key_if_exists :emails, column: :user_id + end + end + RUBY + end + end + + context 'when outside of migration' do + it 'registers no offense' do + expect_no_offenses(<<~RUBY) + class SomeMigration + disable_ddl_transaction! + def up + validate_foreign_key :deployments, :environment_id + end + end + RUBY + end + end +end diff --git a/spec/rubocop/cop/migration/versioned_migration_class_spec.rb b/spec/rubocop/cop/migration/versioned_migration_class_spec.rb index 506e3146afa..332b02078f4 100644 --- a/spec/rubocop/cop/migration/versioned_migration_class_spec.rb +++ b/spec/rubocop/cop/migration/versioned_migration_class_spec.rb @@ -3,7 +3,7 @@ require 'rubocop_spec_helper' require_relative '../../../../rubocop/cop/migration/versioned_migration_class' -RSpec.describe RuboCop::Cop::Migration::VersionedMigrationClass do +RSpec.describe RuboCop::Cop::Migration::VersionedMigrationClass, feature_category: :database do let(:migration) do <<~SOURCE class TestMigration < Gitlab::Database::Migration[2.1] @@ -49,7 +49,15 @@ RSpec.describe RuboCop::Cop::Migration::VersionedMigrationClass do it 'adds an offence if inheriting from ActiveRecord::Migration' do expect_offense(<<~RUBY) class MyMigration < ActiveRecord::Migration[6.1] - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Don't inherit from ActiveRecord::Migration but use Gitlab::Database::Migration[2.1] instead. See https://docs.gitlab.com/ee/development/migration_style_guide.html#migration-helpers-and-versioning. + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Don't inherit from ActiveRecord::Migration or old versions of Gitlab::Database::Migration. Use Gitlab::Database::Migration[2.1] instead. See https://docs.gitlab.com/ee/development/migration_style_guide.html#migration-helpers-and-versioning. + end + RUBY + end + + it 'adds an offence if inheriting from old version of Gitlab::Database::Migration' do + expect_offense(<<~RUBY) + class MyMigration < Gitlab::Database::Migration[2.0] + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Don't inherit from ActiveRecord::Migration or old versions of Gitlab::Database::Migration. Use Gitlab::Database::Migration[2.1] instead. See https://docs.gitlab.com/ee/development/migration_style_guide.html#migration-helpers-and-versioning. end RUBY end diff --git a/spec/rubocop/cop/rspec/env_mocking_spec.rb b/spec/rubocop/cop/rspec/env_mocking_spec.rb new file mode 100644 index 00000000000..189fccf483a --- /dev/null +++ b/spec/rubocop/cop/rspec/env_mocking_spec.rb @@ -0,0 +1,61 @@ +# frozen_string_literal: true + +require 'rubocop_spec_helper' + +require_relative '../../../../rubocop/cop/rspec/env_mocking' + +RSpec.describe RuboCop::Cop::RSpec::EnvMocking, feature_category: :tooling do + offense_call_brackets_string_quotes = %(allow(ENV).to receive(:[]).with('FOO').and_return('bar')) + offense_call_brackets_variables = %(allow(ENV).to receive(:[]).with(key).and_return(value)) + + offense_call_fetch_string_quotes = %(allow(ENV).to receive(:fetch).with('FOO').and_return('bar')) + offense_call_fetch_variables = %(allow(ENV).to receive(:fetch).with(key).and_return(value)) + + offense_call_root_env_variables = %(allow(::ENV).to receive(:[]).with(key).and_return(value)) + offense_call_key_value_method_calls = + %(allow(ENV).to receive(:[]).with(fetch_key(object)).and_return(fetch_value(object))) + + acceptable_mocking_other_methods = %(allow(ENV).to receive(:foo).with("key").and_return("value")) + + let(:source_file) { 'spec/foo_spec.rb' } + + shared_examples 'cop offense mocking the ENV constant correctable with stub_env' do |content, autocorrected_content| + it "registers an offense for `#{content}` and corrects", :aggregate_failures do + expect_offense(<<~CODE, content: content) + %{content} + ^{content} Don't mock the ENV, use `stub_env` instead. + CODE + + expect_correction(<<~CODE) + #{autocorrected_content} + CODE + end + end + + context 'with mocking bracket calls ' do + it_behaves_like 'cop offense mocking the ENV constant correctable with stub_env', + offense_call_brackets_string_quotes, %(stub_env('FOO', 'bar')) + it_behaves_like 'cop offense mocking the ENV constant correctable with stub_env', + offense_call_brackets_variables, %(stub_env(key, value)) + end + + context 'with mocking fetch calls' do + it_behaves_like 'cop offense mocking the ENV constant correctable with stub_env', + offense_call_fetch_string_quotes, %(stub_env('FOO', 'bar')) + it_behaves_like 'cop offense mocking the ENV constant correctable with stub_env', + offense_call_fetch_variables, %(stub_env(key, value)) + end + + context 'with other special cases and variations' do + it_behaves_like 'cop offense mocking the ENV constant correctable with stub_env', + offense_call_root_env_variables, %(stub_env(key, value)) + it_behaves_like 'cop offense mocking the ENV constant correctable with stub_env', + offense_call_key_value_method_calls, %(stub_env(fetch_key(object), fetch_value(object))) + end + + context 'with acceptable cases' do + it 'does not register an offense for mocking other methods' do + expect_no_offenses(acceptable_mocking_other_methods) + end + end +end diff --git a/spec/rubocop/cop/rspec/invalid_feature_category_spec.rb b/spec/rubocop/cop/rspec/invalid_feature_category_spec.rb new file mode 100644 index 00000000000..0d2fd029a13 --- /dev/null +++ b/spec/rubocop/cop/rspec/invalid_feature_category_spec.rb @@ -0,0 +1,120 @@ +# frozen_string_literal: true + +require 'rubocop_spec_helper' +require 'rspec-parameterized' + +require_relative '../../../../rubocop/cop/rspec/invalid_feature_category' + +RSpec.describe RuboCop::Cop::RSpec::InvalidFeatureCategory, feature_category: :tooling do + shared_examples 'feature category validation' do |valid_category| + it 'flags invalid feature category in top level example group' do + expect_offense(<<~RUBY, invalid: invalid_category) + RSpec.describe 'foo', feature_category: :%{invalid}, foo: :bar do + ^^{invalid} Please use a valid feature category. See https://docs.gitlab.com/ee/development/feature_categorization/#rspec-examples. + end + RUBY + end + + 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 + 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 + end + RUBY + end + + it 'flags invalid feature category in examples' do + expect_offense(<<~RUBY, valid: valid_category, invalid: invalid_category) + 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 + end + RUBY + end + + 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 + end + end + end + RUBY + end + + it 'suggests an alternative' do + 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 [...] + end + RUBY + end + + def make_typo(string) + "#{string}#{string[-1]}" + end + end + + let(:invalid_category) { :invalid_category } + + context 'with categories defined in config/feature_categories.yml' do + where(:valid_category) do + YAML.load_file(rails_root_join('config/feature_categories.yml')) + end + + with_them do + it_behaves_like 'feature category validation', params[:valid_category] + end + end + + context 'with custom categories' do + it_behaves_like 'feature category validation', 'tooling' + it_behaves_like 'feature category validation', 'shared' + end + + it 'flags invalid feature category for non-symbols' do + expect_offense(<<~RUBY, invalid: invalid_category) + RSpec.describe 'foo', feature_category: "%{invalid}" do + ^^^{invalid} Please use a symbol as value. + end + + RSpec.describe 'foo', feature_category: 42 do + ^^ Please use a symbol as value. + end + RUBY + end + + it 'does not flag use of invalid categories in non-example code' do + # See https://gitlab.com/gitlab-org/gitlab/-/issues/381882#note_1265865125 + expect_no_offenses(<<~RUBY) + RSpec.describe 'A spec' do + let(:api_handler) do + Class.new(described_class) do + namespace '/test' do + get 'hello', feature_category: :foo, urgency: :#{invalid_category} do + end + end + end + end + + it 'tests something' do + Gitlab::ApplicationContext.with_context(feature_category: :#{invalid_category}) do + payload = generator.generate(exception, extra) + end + end + end + RUBY + end + + describe '#external_dependency_checksum' do + it 'returns a SHA256 digest used by RuboCop to invalid cache' do + expect(cop.external_dependency_checksum).to match(/^\h{64}$/) + end + end +end diff --git a/spec/rubocop/cop/rspec/missing_feature_category_spec.rb b/spec/rubocop/cop/rspec/missing_feature_category_spec.rb new file mode 100644 index 00000000000..41b1d2b8580 --- /dev/null +++ b/spec/rubocop/cop/rspec/missing_feature_category_spec.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +require 'rubocop_spec_helper' +require_relative '../../../../rubocop/cop/rspec/missing_feature_category' + +RSpec.describe RuboCop::Cop::RSpec::MissingFeatureCategory, feature_category: :tooling do + it 'flags missing feature category in top level example group' do + expect_offense(<<~RUBY) + RSpec.describe 'foo' do + ^^^^^^^^^^^^^^^^^^^^ Please add missing feature category. See https://docs.gitlab.com/ee/development/feature_categorization/#rspec-examples. + end + + RSpec.describe 'foo', some: :tag do + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Please add missing feature category. See https://docs.gitlab.com/ee/development/feature_categorization/#rspec-examples. + end + RUBY + end + + it 'does not flag if feature category is defined' do + expect_no_offenses(<<~RUBY) + RSpec.describe 'foo', feature_category: :foo do + end + + RSpec.describe 'foo', some: :tag, feature_category: :foo do + end + + RSpec.describe 'foo', feature_category: :foo, some: :tag do + end + RUBY + end +end diff --git a/spec/rubocop/cop/scalability/file_uploads_spec.rb b/spec/rubocop/cop/scalability/file_uploads_spec.rb index 1395615479f..43ac9457ed6 100644 --- a/spec/rubocop/cop/scalability/file_uploads_spec.rb +++ b/spec/rubocop/cop/scalability/file_uploads_spec.rb @@ -3,7 +3,7 @@ require 'rubocop_spec_helper' require_relative '../../../../rubocop/cop/scalability/file_uploads' -RSpec.describe RuboCop::Cop::Scalability::FileUploads do +RSpec.describe RuboCop::Cop::Scalability::FileUploads, feature_category: :scalability do let(:message) { 'Do not upload files without workhorse acceleration. Please refer to https://docs.gitlab.com/ee/development/uploads.html' } context 'with required params' do |