diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2023-09-28 18:12:02 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2023-09-28 18:12:02 +0300 |
commit | 33d999642a05b1c83c8230cac46086493e7fb368 (patch) | |
tree | 3bb3f2f358a36da67cb134cb1b7359a9724cfc23 /spec/rubocop | |
parent | 5573dc288d880386a96afc9458f7d1abd6cd745f (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/rubocop')
4 files changed, 145 insertions, 3 deletions
diff --git a/spec/rubocop/cop/gemfile/missing_feature_category_spec.rb b/spec/rubocop/cop/gemfile/missing_feature_category_spec.rb new file mode 100644 index 00000000000..5f8e32f0c03 --- /dev/null +++ b/spec/rubocop/cop/gemfile/missing_feature_category_spec.rb @@ -0,0 +1,63 @@ +# frozen_string_literal: true + +require 'rubocop_spec_helper' + +require_relative '../../../../rubocop/cop/gemfile/missing_feature_category' + +RSpec.describe RuboCop::Cop::Gemfile::MissingFeatureCategory, feature_category: :tooling do + let(:valid_category) { RuboCop::FeatureCategories.available.first } + let(:invalid_category) { :invalid_category } + + it 'flags missing feature category in gem method without keyword argument' do + expect_offense(<<~RUBY) + gem 'foo', '~> 1.0' + ^^^^^^^^^^^^^^^^^^^ Please use a valid feature category. See https://docs.gitlab.com/ee/development/feature_categorization/#gemfile + RUBY + end + + it 'flags missing feature category in gem method with keyword argument' do + expect_offense(<<~RUBY) + gem 'foo', '~> 1.0', require: false + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Please use a valid feature category. See https://docs.gitlab.com/ee/development/feature_categorization/#gemfile + RUBY + end + + it 'flags invalid feature category in gem method as the only keyword argument' do + expect_offense(<<~RUBY, invalid: invalid_category) + gem 'foo', '~> 1.0', feature_category: :%{invalid} + ^^{invalid} Please use a valid feature category. See https://docs.gitlab.com/ee/development/feature_categorization/#gemfile + RUBY + end + + it 'flags invalid feature category in gem method as the last keyword argument' do + expect_offense(<<~RUBY, invalid: invalid_category) + gem 'foo', '~> 1.0', require: false, feature_category: :%{invalid} + ^^{invalid} Please use a valid feature category. See https://docs.gitlab.com/ee/development/feature_categorization/#gemfile + RUBY + end + + it 'flags invalid feature category in gem method as the first keyword argument' do + expect_offense(<<~RUBY, invalid: invalid_category) + gem 'foo', '~> 1.0', feature_category: :%{invalid}, require: false + ^^{invalid} Please use a valid feature category. See https://docs.gitlab.com/ee/development/feature_categorization/#gemfile + RUBY + end + + it 'does not flag in gem method if feature category is valid as the only keyword argument' do + expect_no_offenses(<<~RUBY) + gem 'foo', '~> 1.0', feature_category: :#{valid_category} + RUBY + end + + it 'does not flag in gem method if feature category is valid as the last keyword argument' do + expect_no_offenses(<<~RUBY) + gem 'foo', '~> 1.0', require: false, feature_category: :#{valid_category} + 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/gitlab/feature_available_usage_spec.rb b/spec/rubocop/cop/gitlab/feature_available_usage_spec.rb index b15c298099d..184f2c3ee92 100644 --- a/spec/rubocop/cop/gitlab/feature_available_usage_spec.rb +++ b/spec/rubocop/cop/gitlab/feature_available_usage_spec.rb @@ -16,6 +16,10 @@ RSpec.describe RuboCop::Cop::Gitlab::FeatureAvailableUsage do expect_no_offenses('License.feature_available?(:push_rules)') end + it 'does not flag the use of Gitlab::Saas.feature_available?' do + expect_no_offenses('Gitlab::Saas.feature_available?("some/feature")') + end + it 'flags the use with a dynamic feature as nil' do expect_offense(<<~SOURCE) feature_available?(nil) diff --git a/spec/rubocop/cop/rspec/invalid_feature_category_spec.rb b/spec/rubocop/cop/rspec/invalid_feature_category_spec.rb index e5287f7105e..1055275752d 100644 --- a/spec/rubocop/cop/rspec/invalid_feature_category_spec.rb +++ b/spec/rubocop/cop/rspec/invalid_feature_category_spec.rb @@ -10,7 +10,7 @@ RSpec.describe RuboCop::Cop::RSpec::InvalidFeatureCategory, feature_category: :t 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. + ^^{invalid} Please use a valid feature category. See https://docs.gitlab.com/ee/development/feature_categorization/#rspec-examples end RUBY end @@ -19,7 +19,7 @@ RSpec.describe RuboCop::Cop::RSpec::InvalidFeatureCategory, feature_category: :t 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. + ^^{invalid} Please use a valid feature category. See https://docs.gitlab.com/ee/development/feature_categorization/#rspec-examples end end RUBY @@ -29,7 +29,7 @@ RSpec.describe RuboCop::Cop::RSpec::InvalidFeatureCategory, feature_category: :t 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. + ^^{invalid} Please use a valid feature category. See https://docs.gitlab.com/ee/development/feature_categorization/#rspec-examples end end RUBY diff --git a/spec/rubocop/feature_categories_spec.rb b/spec/rubocop/feature_categories_spec.rb index d2f35fbe575..85d1f4f8aff 100644 --- a/spec/rubocop/feature_categories_spec.rb +++ b/spec/rubocop/feature_categories_spec.rb @@ -5,6 +5,10 @@ require 'rubocop_spec_helper' require_relative '../../rubocop/feature_categories' RSpec.describe RuboCop::FeatureCategories, feature_category: :tooling do + subject(:feature_categories) { described_class.new(categories) } + + let(:categories) { ['valid_category'] } + describe '.available' do it 'returns a list of available feature categories in a set of strings' do expect(described_class.available).to be_a(Set) @@ -27,4 +31,75 @@ RSpec.describe RuboCop::FeatureCategories, feature_category: :tooling do expect(described_class.config_checksum).to match(/^\h{64}$/) end end + + describe '#check' do + let(:value_node) { instance_double(RuboCop::AST::SymbolNode, sym_type?: true) } + let(:document_link) { 'https://example.com' } + + def check + expect do |block| + feature_categories.check( + value_node: value_node, + document_link: document_link, + &block) + end + end + + context 'when value_node is not a symbol node' do + before do + allow(value_node).to receive(:sym_type?).and_return(false) + end + + it 'yields a message asking for a symbol value' do + check.to yield_with_args(described_class::MSG_SYMBOL) + end + end + + context 'when categories contain the value the value_node has' do + before do + allow(value_node).to receive(:value).and_return(categories.first) + end + + it 'returns nil without yielding anything' do + check.not_to yield_with_args + end + end + + context 'when categories do not contain the value the value_node has' do + before do + allow(value_node).to receive(:value).and_return('invalid_category') + end + + it 'yields a message asking for a feature category with document link' do + check.to yield_with_args(<<~MARKDOWN.chomp) + Please use a valid feature category. Did you mean `:valid_category`? See https://example.com + MARKDOWN + end + end + end + + describe '#suggestion_message' do + let(:value_node) { instance_double(RuboCop::AST::SymbolNode) } + + context 'when categories do not contain the value the value_node has' do + before do + allow(value_node).to receive(:value).and_return('invalid_category') + end + + it 'returns a message suggesting a similar category name' do + expect(feature_categories.suggestion_message(value_node)) + .to eq('Did you mean `:valid_category`? ') + end + + context 'when the value the value_node has is too different' do + before do + allow(value_node).to receive(:value).and_return('GitLab') + end + + it 'returns nil' do + expect(feature_categories.suggestion_message(value_node)).to be_nil + end + end + end + end end |