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:
Diffstat (limited to 'spec/rubocop/cop')
-rw-r--r--spec/rubocop/cop/file_decompression_spec.rb48
-rw-r--r--spec/rubocop/cop/gitlab/event_store_subscriber_spec.rb82
-rw-r--r--spec/rubocop/cop/migration/schedule_async_spec.rb51
-rw-r--r--spec/rubocop/cop/scalability/bulk_perform_with_context_spec.rb8
-rw-r--r--spec/rubocop/cop/sidekiq_load_balancing/worker_data_consistency_with_deduplication_spec.rb166
5 files changed, 186 insertions, 169 deletions
diff --git a/spec/rubocop/cop/file_decompression_spec.rb b/spec/rubocop/cop/file_decompression_spec.rb
new file mode 100644
index 00000000000..7be1a784001
--- /dev/null
+++ b/spec/rubocop/cop/file_decompression_spec.rb
@@ -0,0 +1,48 @@
+# frozen_string_literal: true
+
+require 'fast_spec_helper'
+require_relative '../../../rubocop/cop/file_decompression'
+
+RSpec.describe RuboCop::Cop::FileDecompression do
+ subject(:cop) { described_class.new }
+
+ it 'does not flag when using a system command not related to file decompression' do
+ expect_no_offenses('system("ls")')
+ end
+
+ described_class::FORBIDDEN_COMMANDS.map { [_1, '^' * _1.length] }.each do |cmd, len|
+ it "flags the when using '#{cmd}' system command" do
+ expect_offense(<<~SOURCE)
+ system('#{cmd}')
+ ^^^^^^^^#{len}^^ While extracting files check for symlink to avoid arbitrary file reading[...]
+ SOURCE
+
+ expect_offense(<<~SOURCE)
+ exec('#{cmd}')
+ ^^^^^^#{len}^^ While extracting files check for symlink to avoid arbitrary file reading[...]
+ SOURCE
+
+ expect_offense(<<~SOURCE)
+ Kernel.spawn('#{cmd}')
+ ^^^^^^^^^^^^^^#{len}^^ While extracting files check for symlink to avoid arbitrary file reading[...]
+ SOURCE
+
+ expect_offense(<<~SOURCE)
+ IO.popen('#{cmd}')
+ ^^^^^^^^^^#{len}^^ While extracting files check for symlink to avoid arbitrary file reading[...]
+ SOURCE
+ end
+
+ it "flags the when using '#{cmd}' subshell command" do
+ expect_offense(<<~SOURCE)
+ `#{cmd}`
+ ^#{len}^ While extracting files check for symlink to avoid arbitrary file reading[...]
+ SOURCE
+
+ expect_offense(<<~SOURCE)
+ %x(#{cmd})
+ ^^^#{len}^ While extracting files check for symlink to avoid arbitrary file reading[...]
+ SOURCE
+ end
+ end
+end
diff --git a/spec/rubocop/cop/gitlab/event_store_subscriber_spec.rb b/spec/rubocop/cop/gitlab/event_store_subscriber_spec.rb
new file mode 100644
index 00000000000..e17fb71f9bc
--- /dev/null
+++ b/spec/rubocop/cop/gitlab/event_store_subscriber_spec.rb
@@ -0,0 +1,82 @@
+# frozen_string_literal: true
+
+require 'fast_spec_helper'
+
+require_relative '../../../../rubocop/cop/gitlab/event_store_subscriber'
+
+RSpec.describe RuboCop::Cop::Gitlab::EventStoreSubscriber do
+ subject(:cop) { described_class.new }
+
+ context 'when an event store subscriber overrides #perform' do
+ it 'registers an offense' do
+ expect_offense(<<~WORKER)
+ class SomeWorker
+ include Gitlab::EventStore::Subscriber
+
+ def perform(*args)
+ ^^^^^^^^^^^^^^^^^^ Do not override `perform` in a `Gitlab::EventStore::Subscriber`.
+ end
+
+ def handle_event(event); end
+ end
+ WORKER
+ end
+ end
+
+ context 'when an event store subscriber does not override #perform' do
+ it 'does not register an offense' do
+ expect_no_offenses(<<~WORKER)
+ class SomeWorker
+ include Gitlab::EventStore::Subscriber
+
+ def handle_event(event); end
+ end
+ WORKER
+ end
+ end
+
+ context 'when an event store subscriber does not implement #handle_event' do
+ it 'registers an offense' do
+ expect_offense(<<~WORKER)
+ class SomeWorker
+ include Gitlab::EventStore::Subscriber
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ A `Gitlab::EventStore::Subscriber` must implement `#handle_event(event)`.
+ end
+ WORKER
+ end
+ end
+
+ context 'when a Sidekiq worker overrides #perform' do
+ it 'does not register an offense' do
+ expect_no_offenses(<<~WORKER)
+ class SomeWorker
+ include ApplicationWorker
+
+ def perform(*args); end
+ end
+ WORKER
+ end
+ end
+
+ context 'when a Sidekiq worker implements #handle_event' do
+ it 'does not register an offense' do
+ expect_no_offenses(<<~WORKER)
+ class SomeWorker
+ include ApplicationWorker
+
+ def handle_event(event); end
+ end
+ WORKER
+ end
+ end
+
+ context 'a non worker class' do
+ it 'does not register an offense' do
+ expect_no_offenses(<<~MODEL)
+ class Model < ApplicationRecord
+ include ActiveSupport::Concern
+ end
+ MODEL
+ end
+ end
+end
diff --git a/spec/rubocop/cop/migration/schedule_async_spec.rb b/spec/rubocop/cop/migration/schedule_async_spec.rb
index 5f848dd9b66..09d2c77369c 100644
--- a/spec/rubocop/cop/migration/schedule_async_spec.rb
+++ b/spec/rubocop/cop/migration/schedule_async_spec.rb
@@ -53,6 +53,17 @@ RSpec.describe RuboCop::Cop::Migration::ScheduleAsync do
end
end
+ context 'CiDatabaseWorker.perform_async' do
+ it 'adds an offense when calling `CiDatabaseWorker.peform_async`' do
+ expect_offense(<<~RUBY)
+ def up
+ CiDatabaseWorker.perform_async(ClazzName, "Bar", "Baz")
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Don't call [...]
+ end
+ RUBY
+ end
+ end
+
context 'BackgroundMigrationWorker.perform_in' do
it 'adds an offense' do
expect_offense(<<~RUBY)
@@ -65,6 +76,18 @@ RSpec.describe RuboCop::Cop::Migration::ScheduleAsync do
end
end
+ context 'CiDatabaseWorker.perform_in' do
+ it 'adds an offense' do
+ expect_offense(<<~RUBY)
+ def up
+ CiDatabaseWorker
+ ^^^^^^^^^^^^^^^^ Don't call [...]
+ .perform_in(delay, ClazzName, "Bar", "Baz")
+ end
+ RUBY
+ end
+ end
+
context 'BackgroundMigrationWorker.bulk_perform_async' do
it 'adds an offense' do
expect_offense(<<~RUBY)
@@ -77,12 +100,36 @@ RSpec.describe RuboCop::Cop::Migration::ScheduleAsync do
end
end
+ context 'CiDatabaseWorker.bulk_perform_async' do
+ it 'adds an offense' do
+ expect_offense(<<~RUBY)
+ def up
+ BackgroundMigration::CiDatabaseWorker
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Don't call [...]
+ .bulk_perform_async(jobs)
+ end
+ RUBY
+ end
+ end
+
context 'BackgroundMigrationWorker.bulk_perform_in' do
it 'adds an offense' do
expect_offense(<<~RUBY)
def up
- BackgroundMigrationWorker
- ^^^^^^^^^^^^^^^^^^^^^^^^^ Don't call [...]
+ ::BackgroundMigrationWorker
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^ Don't call [...]
+ .bulk_perform_in(5.minutes, jobs)
+ end
+ RUBY
+ end
+ end
+
+ context 'CiDatabaseWorker.bulk_perform_in' do
+ it 'adds an offense' do
+ expect_offense(<<~RUBY)
+ def up
+ ::BackgroundMigration::CiDatabaseWorker
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Don't call [...]
.bulk_perform_in(5.minutes, jobs)
end
RUBY
diff --git a/spec/rubocop/cop/scalability/bulk_perform_with_context_spec.rb b/spec/rubocop/cop/scalability/bulk_perform_with_context_spec.rb
index 01afaf3acb6..74912b53d37 100644
--- a/spec/rubocop/cop/scalability/bulk_perform_with_context_spec.rb
+++ b/spec/rubocop/cop/scalability/bulk_perform_with_context_spec.rb
@@ -39,9 +39,15 @@ RSpec.describe RuboCop::Cop::Scalability::BulkPerformWithContext do
CODE
end
- it "does not add an offense for scheduling BackgroundMigrations" do
+ it "does not add an offense for scheduling on the BackgroundMigrationWorker" do
expect_no_offenses(<<~CODE)
BackgroundMigrationWorker.bulk_perform_in(args)
CODE
end
+
+ it "does not add an offense for scheduling on the CiDatabaseWorker" do
+ expect_no_offenses(<<~CODE)
+ BackgroundMigration::CiDatabaseWorker.bulk_perform_in(args)
+ CODE
+ end
end
diff --git a/spec/rubocop/cop/sidekiq_load_balancing/worker_data_consistency_with_deduplication_spec.rb b/spec/rubocop/cop/sidekiq_load_balancing/worker_data_consistency_with_deduplication_spec.rb
deleted file mode 100644
index 6e7212b1002..00000000000
--- a/spec/rubocop/cop/sidekiq_load_balancing/worker_data_consistency_with_deduplication_spec.rb
+++ /dev/null
@@ -1,166 +0,0 @@
-# frozen_string_literal: true
-
-require 'fast_spec_helper'
-require 'rspec-parameterized'
-require_relative '../../../../rubocop/cop/sidekiq_load_balancing/worker_data_consistency_with_deduplication'
-
-RSpec.describe RuboCop::Cop::SidekiqLoadBalancing::WorkerDataConsistencyWithDeduplication do
- using RSpec::Parameterized::TableSyntax
-
- subject(:cop) { described_class.new }
-
- before do
- allow(cop)
- .to receive(:in_worker?)
- .and_return(true)
- end
-
- where(:data_consistency) { %i[delayed sticky] }
-
- with_them do
- let(:strategy) { described_class::DEFAULT_STRATEGY }
- let(:corrected) do
- <<~CORRECTED
- class SomeWorker
- include ApplicationWorker
-
- data_consistency :#{data_consistency}
-
- deduplicate #{strategy}, including_scheduled: true
- idempotent!
- end
- CORRECTED
- end
-
- context 'when deduplication strategy is not explicitly set' do
- it 'registers an offense and corrects using default strategy' do
- expect_offense(<<~CODE)
- class SomeWorker
- include ApplicationWorker
-
- data_consistency :#{data_consistency}
-
- idempotent!
- ^^^^^^^^^^^ Workers that declare either `:sticky` or `:delayed` data consistency [...]
- end
- CODE
-
- expect_correction(corrected)
- end
-
- context 'when identation is different' do
- let(:corrected) do
- <<~CORRECTED
- class SomeWorker
- include ApplicationWorker
-
- data_consistency :#{data_consistency}
-
- deduplicate #{strategy}, including_scheduled: true
- idempotent!
- end
- CORRECTED
- end
-
- it 'registers an offense and corrects with correct identation' do
- expect_offense(<<~CODE)
- class SomeWorker
- include ApplicationWorker
-
- data_consistency :#{data_consistency}
-
- idempotent!
- ^^^^^^^^^^^ Workers that declare either `:sticky` or `:delayed` data consistency [...]
- end
- CODE
-
- expect_correction(corrected)
- end
- end
- end
-
- context 'when deduplication strategy does not include including_scheduling option' do
- let(:strategy) { ':until_executed' }
-
- it 'registers an offense and corrects' do
- expect_offense(<<~CODE)
- class SomeWorker
- include ApplicationWorker
-
- data_consistency :#{data_consistency}
-
- deduplicate :until_executed
- ^^^^^^^^^^^^^^^^^^^^^^^^^^^ Workers that declare either `:sticky` or `:delayed` data consistency [...]
- idempotent!
- end
- CODE
-
- expect_correction(corrected)
- end
- end
-
- context 'when deduplication strategy has including_scheduling option disabled' do
- let(:strategy) { ':until_executed' }
-
- it 'registers an offense and corrects' do
- expect_offense(<<~CODE)
- class SomeWorker
- include ApplicationWorker
-
- data_consistency :#{data_consistency}
-
- deduplicate :until_executed, including_scheduled: false
- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Workers that declare either `:sticky` or `:delayed` data consistency [...]
- idempotent!
- end
- CODE
-
- expect_correction(corrected)
- end
- end
-
- context "when deduplication strategy is :none" do
- it 'does not register an offense' do
- expect_no_offenses(<<~CODE)
- class SomeWorker
- include ApplicationWorker
-
- data_consistency :always
-
- deduplicate :none
- idempotent!
- end
- CODE
- end
- end
-
- context "when deduplication strategy has including_scheduling option enabled" do
- it 'does not register an offense' do
- expect_no_offenses(<<~CODE)
- class SomeWorker
- include ApplicationWorker
-
- data_consistency :always
-
- deduplicate :until_executing, including_scheduled: true
- idempotent!
- end
- CODE
- end
- end
- end
-
- context "data_consistency: :always" do
- it 'does not register an offense' do
- expect_no_offenses(<<~CODE)
- class SomeWorker
- include ApplicationWorker
-
- data_consistency :always
-
- idempotent!
- end
- CODE
- end
- end
-end