diff options
Diffstat (limited to 'spec/rubocop')
-rw-r--r-- | spec/rubocop/code_reuse_helpers_spec.rb | 60 | ||||
-rw-r--r-- | spec/rubocop/cop/code_reuse/worker_spec.rb | 19 | ||||
-rw-r--r-- | spec/rubocop/cop/gemspec/avoid_executing_git_spec.rb | 30 | ||||
-rw-r--r-- | spec/rubocop/cop/gitlab/deprecate_track_redis_hll_event_spec.rb | 19 | ||||
-rw-r--r-- | spec/rubocop/cop/gitlab/mark_used_feature_flags_spec.rb | 4 | ||||
-rw-r--r-- | spec/rubocop/cop/inject_enterprise_edition_module_spec.rb | 1 | ||||
-rw-r--r-- | spec/rubocop/cop_todo_spec.rb | 124 | ||||
-rw-r--r-- | spec/rubocop/formatter/todo_formatter_spec.rb | 12 |
8 files changed, 257 insertions, 12 deletions
diff --git a/spec/rubocop/code_reuse_helpers_spec.rb b/spec/rubocop/code_reuse_helpers_spec.rb index d437ada85ee..0d06d37d67a 100644 --- a/spec/rubocop/code_reuse_helpers_spec.rb +++ b/spec/rubocop/code_reuse_helpers_spec.rb @@ -152,6 +152,26 @@ RSpec.describe RuboCop::CodeReuseHelpers do end end + describe '#in_graphql?' do + it 'returns true for a node in the FOSS GraphQL directory' do + node = build_and_parse_source('10', rails_root_join('app', 'graphql', 'foo.rb')) + + expect(cop.in_graphql?(node)).to eq(true) + end + + it 'returns true for a node in the EE GraphQL directory' do + node = build_and_parse_source('10', rails_root_join('ee', 'app', 'graphql', 'foo.rb')) + + expect(cop.in_graphql?(node)).to eq(true) + end + + it 'returns false for a node outside the GraphQL directory' do + node = build_and_parse_source('10', rails_root_join('app', 'foo', 'foo.rb')) + + expect(cop.in_graphql?(node)).to eq(false) + end + end + describe '#in_graphql_types?' do %w[ app/graphql/types @@ -169,7 +189,7 @@ RSpec.describe RuboCop::CodeReuseHelpers do app/graphql/resolvers app/foo ].each do |path| - it "returns true for a node in #{path}" do + it "returns false for a node in #{path}" do node = build_and_parse_source('10', rails_root_join(path, 'foo.rb')) expect(cop.in_graphql_types?(node)).to eq(false) @@ -255,6 +275,44 @@ RSpec.describe RuboCop::CodeReuseHelpers do end end + describe '#in_graphql_directory?' do + it 'returns true for a directory in the FOSS app/graphql directory' do + node = build_and_parse_source('10', rails_root_join('app', 'graphql', 'subdir', 'foo.rb')) + + expect(cop.in_graphql_directory?(node, 'subdir')).to eq(true) + end + + it 'returns true for a directory in the EE app/graphql directory' do + node = build_and_parse_source('10', rails_root_join('ee', 'app', 'graphql', 'subdir', 'foo.rb')) + + expect(cop.in_graphql_directory?(node, 'subdir')).to eq(true) + end + + it 'returns true for a directory in the EE app/graphql/ee directory' do + node = build_and_parse_source('10', rails_root_join('ee', 'app', 'graphql', 'ee', 'subdir', 'foo.rb')) + + expect(cop.in_graphql_directory?(node, 'subdir')).to eq(true) + end + + it 'returns false for a directory in the FOSS app/graphql directory' do + node = build_and_parse_source('10', rails_root_join('app', 'graphql', 'anotherdir', 'foo.rb')) + + expect(cop.in_graphql_directory?(node, 'subdir')).to eq(false) + end + + it 'returns false for a directory in the EE app/graphql directory' do + node = build_and_parse_source('10', rails_root_join('ee', 'app', 'graphql', 'anotherdir', 'foo.rb')) + + expect(cop.in_graphql_directory?(node, 'subdir')).to eq(false) + end + + it 'returns false for a directory in the EE app/graphql/ee directory' do + node = build_and_parse_source('10', rails_root_join('ee', 'app', 'graphql', 'ee', 'anotherdir', 'foo.rb')) + + expect(cop.in_graphql_directory?(node, 'subdir')).to eq(false) + end + end + describe '#name_of_receiver' do it 'returns the name of a send receiver' do node = build_and_parse_source('Foo.bar') diff --git a/spec/rubocop/cop/code_reuse/worker_spec.rb b/spec/rubocop/cop/code_reuse/worker_spec.rb index 8155791a3e3..a548e90d8e1 100644 --- a/spec/rubocop/cop/code_reuse/worker_spec.rb +++ b/spec/rubocop/cop/code_reuse/worker_spec.rb @@ -31,7 +31,24 @@ RSpec.describe RuboCop::Cop::CodeReuse::Worker do resource :projects do get '/' do FooWorker.perform_async - ^^^^^^^^^^^^^^^^^^^^^^^ Workers can not be used in a Grape API. + ^^^^^^^^^^^^^^^^^^^^^^^ Workers can not be used in an API endpoint. + end + end + end + SOURCE + end + + it 'flags the use of a worker in GraphQL' do + allow(cop) + .to receive(:in_graphql?) + .and_return(true) + + expect_offense(<<~SOURCE) + module Mutations + class Foo < BaseMutation + def resolve + FooWorker.perform_async + ^^^^^^^^^^^^^^^^^^^^^^^ Workers can not be used in an API endpoint. end end end diff --git a/spec/rubocop/cop/gemspec/avoid_executing_git_spec.rb b/spec/rubocop/cop/gemspec/avoid_executing_git_spec.rb new file mode 100644 index 00000000000..f94a990a2f7 --- /dev/null +++ b/spec/rubocop/cop/gemspec/avoid_executing_git_spec.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true + +require 'fast_spec_helper' +require_relative '../../../../rubocop/cop/gemspec/avoid_executing_git' + +RSpec.describe RuboCop::Cop::Gemspec::AvoidExecutingGit do + subject(:cop) { described_class.new } + + it 'flags violation for executing git' do + expect_offense(<<~RUBY) + Gem::Specification.new do |gem| + gem.executable = `git ls-files -- bin/*`.split("\\n").map{ |f| File.basename(f) } + ^^^^^^^^^^^^^^^^^^^^^^^ Do not execute `git` in gemspec. + gem.files = `git ls-files`.split("\\n") + ^^^^^^^^^^^^^^ Do not execute `git` in gemspec. + gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\\n") + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not execute `git` in gemspec. + end + RUBY + end + + it 'does not flag violation for using a glob' do + expect_no_offenses(<<~RUBY) + Gem::Specification.new do |gem| + gem.files = Dir.glob("lib/**/*.*") + gem.test_files = Dir.glob("spec/**/**/*.*") + end + RUBY + end +end diff --git a/spec/rubocop/cop/gitlab/deprecate_track_redis_hll_event_spec.rb b/spec/rubocop/cop/gitlab/deprecate_track_redis_hll_event_spec.rb new file mode 100644 index 00000000000..453f0c36c14 --- /dev/null +++ b/spec/rubocop/cop/gitlab/deprecate_track_redis_hll_event_spec.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +require 'fast_spec_helper' +require_relative '../../../../rubocop/cop/gitlab/deprecate_track_redis_hll_event' + +RSpec.describe RuboCop::Cop::Gitlab::DeprecateTrackRedisHLLEvent do + subject(:cop) { described_class.new } + + it 'does not flag the use of track_event' do + expect_no_offenses('track_event :show, name: "p_analytics_insights"') + end + + it 'flags the use of track_redis_hll_event' do + expect_offense(<<~SOURCE) + track_redis_hll_event :show, name: 'p_analytics_valuestream' + ^^^^^^^^^^^^^^^^^^^^^ `track_redis_hll_event` is deprecated[...] + SOURCE + end +end diff --git a/spec/rubocop/cop/gitlab/mark_used_feature_flags_spec.rb b/spec/rubocop/cop/gitlab/mark_used_feature_flags_spec.rb index 2ec3ae7aada..9ab5cdc24a4 100644 --- a/spec/rubocop/cop/gitlab/mark_used_feature_flags_spec.rb +++ b/spec/rubocop/cop/gitlab/mark_used_feature_flags_spec.rb @@ -217,8 +217,8 @@ RSpec.describe RuboCop::Cop::Gitlab::MarkUsedFeatureFlags do allow(cop).to receive(:in_graphql_types?).and_return(true) end - include_examples 'sets flag as used', 'field :runners, Types::Ci::RunnerType.connection_type, null: true, feature_flag: :foo', 'foo' - include_examples 'sets flag as used', 'field :runners, null: true, feature_flag: :foo', 'foo' + include_examples 'sets flag as used', 'field :runners, Types::Ci::RunnerType.connection_type, null: true, _deprecated_feature_flag: :foo', 'foo' + include_examples 'sets flag as used', 'field :runners, null: true, _deprecated_feature_flag: :foo', 'foo' include_examples 'does not set any flags as used', 'field :solution' include_examples 'does not set any flags as used', 'field :runners, Types::Ci::RunnerType.connection_type' include_examples 'does not set any flags as used', 'field :runners, Types::Ci::RunnerType.connection_type, null: true, description: "hello world"' diff --git a/spec/rubocop/cop/inject_enterprise_edition_module_spec.rb b/spec/rubocop/cop/inject_enterprise_edition_module_spec.rb index 962efc23453..3596badc599 100644 --- a/spec/rubocop/cop/inject_enterprise_edition_module_spec.rb +++ b/spec/rubocop/cop/inject_enterprise_edition_module_spec.rb @@ -23,6 +23,7 @@ RSpec.describe RuboCop::Cop::InjectEnterpriseEditionModule do end SOURCE end + it 'flags the use of `extend_mod_with` in the middle of a file' do expect_offense(<<~SOURCE) class Foo diff --git a/spec/rubocop/cop_todo_spec.rb b/spec/rubocop/cop_todo_spec.rb new file mode 100644 index 00000000000..978df2c01ee --- /dev/null +++ b/spec/rubocop/cop_todo_spec.rb @@ -0,0 +1,124 @@ +# frozen_string_literal: true + +require 'fast_spec_helper' +require_relative '../../rubocop/cop_todo' + +RSpec.describe RuboCop::CopTodo do + let(:cop_name) { 'Cop/Rule' } + + subject(:cop_todo) { described_class.new(cop_name) } + + describe '#initialize' do + it 'initializes a cop todo' do + expect(cop_todo).to have_attributes( + cop_name: cop_name, + files: be_empty, + offense_count: 0, + previously_disabled: false + ) + end + end + + describe '#record' do + it 'records offenses' do + cop_todo.record('a.rb', 1) + cop_todo.record('b.rb', 2) + + expect(cop_todo).to have_attributes( + files: contain_exactly('a.rb', 'b.rb'), + offense_count: 3 + ) + end + end + + describe '#autocorrectable?' do + subject { cop_todo.autocorrectable? } + + context 'when found in rubocop registry' do + before do + fake_cop = double(:cop, support_autocorrect?: autocorrectable) # rubocop:disable RSpec/VerifiedDoubles + + allow(described_class).to receive(:find_cop_by_name) + .with(cop_name).and_return(fake_cop) + end + + context 'when autocorrectable' do + let(:autocorrectable) { true } + + it { is_expected.to be_truthy } + end + + context 'when not autocorrectable' do + let(:autocorrectable) { false } + + it { is_expected.to be_falsey } + end + end + + context 'when not found in rubocop registry' do + before do + allow(described_class).to receive(:find_cop_by_name) + .with(cop_name).and_return(nil).and_call_original + end + + it { is_expected.to be_falsey } + end + end + + describe '#to_yaml' do + subject(:yaml) { cop_todo.to_yaml } + + context 'when autocorrectable' do + before do + allow(cop_todo).to receive(:autocorrectable?).and_return(true) + end + + specify do + expect(yaml).to eq(<<~YAML) + --- + # Cop supports --auto-correct. + #{cop_name}: + Exclude: + YAML + end + end + + context 'when previously disabled' do + specify do + cop_todo.record('a.rb', 1) + cop_todo.record('b.rb', 2) + cop_todo.previously_disabled = true + + expect(yaml).to eq(<<~YAML) + --- + #{cop_name}: + # Offense count: 3 + # Temporarily disabled due to too many offenses + Enabled: false + Exclude: + - 'a.rb' + - 'b.rb' + YAML + end + end + + context 'with multiple files' do + before do + cop_todo.record('a.rb', 0) + cop_todo.record('c.rb', 0) + cop_todo.record('b.rb', 0) + end + + it 'sorts excludes alphabetically' do + expect(yaml).to eq(<<~YAML) + --- + #{cop_name}: + Exclude: + - 'a.rb' + - 'b.rb' + - 'c.rb' + YAML + end + end + end +end diff --git a/spec/rubocop/formatter/todo_formatter_spec.rb b/spec/rubocop/formatter/todo_formatter_spec.rb index fcff028f07d..df56ee45931 100644 --- a/spec/rubocop/formatter/todo_formatter_spec.rb +++ b/spec/rubocop/formatter/todo_formatter_spec.rb @@ -261,16 +261,12 @@ RSpec.describe RuboCop::Formatter::TodoFormatter do double(:offense, cop_name: cop_name) end - def stub_rubocop_registry(**cops) - rubocop_registry = double(:rubocop_registry) - - allow(RuboCop::Cop::Registry).to receive(:global).and_return(rubocop_registry) - - allow(rubocop_registry).to receive(:find_by_cop_name) - .with(String).and_return(nil) + def stub_rubocop_registry(cops) + allow(RuboCop::CopTodo).to receive(:find_cop_by_name) + .with(String).and_return(nil).and_call_original cops.each do |cop_name, attributes| - allow(rubocop_registry).to receive(:find_by_cop_name) + allow(RuboCop::CopTodo).to receive(:find_cop_by_name) .with(cop_name).and_return(fake_cop(**attributes)) end end |