Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2023-01-26 21:07:51 +0300
committerGitLab Bot <gitlab-bot@gitlab.com>2023-01-26 21:07:51 +0300
commitcea01cb81787f0d2c119b287a5833f2e267324bc (patch)
treeca37d57c1ec57ef2fa475e6489c4c107cce89dbc /spec/rubocop
parentee24c7d68f57a67754a5d1e2ea99f688029d14bd (diff)
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/rubocop')
-rw-r--r--spec/rubocop/cop/rspec/env_mocking_spec.rb61
-rw-r--r--spec/rubocop/cop/rspec/invalid_feature_category_spec.rb83
2 files changed, 144 insertions, 0 deletions
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..0514197d6c4
--- /dev/null
+++ b/spec/rubocop/cop/rspec/invalid_feature_category_spec.rb
@@ -0,0 +1,83 @@
+# 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
+ 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'
+ 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
+
+ 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