summaryrefslogtreecommitdiff
path: root/spec/rubocop
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2020-02-19 18:09:10 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2020-02-19 18:09:10 +0000
commit33795139ea8e72756bee3675b4e16387425e6ab1 (patch)
tree3ca568fca61482e57810ee30ad5ce4b964a82c4e /spec/rubocop
parentc7e385e282bcb8505589bce526e692b7bb819ffa (diff)
downloadgitlab-ce-33795139ea8e72756bee3675b4e16387425e6ab1.tar.gz
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/rubocop')
-rw-r--r--spec/rubocop/cop/migration/schedule_async_spec.rb152
-rw-r--r--spec/rubocop/cop/scalability/idempotent_worker_spec.rb38
-rw-r--r--spec/rubocop/migration_helpers_spec.rb56
3 files changed, 246 insertions, 0 deletions
diff --git a/spec/rubocop/cop/migration/schedule_async_spec.rb b/spec/rubocop/cop/migration/schedule_async_spec.rb
new file mode 100644
index 00000000000..3453f1c51cc
--- /dev/null
+++ b/spec/rubocop/cop/migration/schedule_async_spec.rb
@@ -0,0 +1,152 @@
+# frozen_string_literal: true
+
+require 'fast_spec_helper'
+
+require 'rubocop'
+require 'rubocop/rspec/support'
+
+require_relative '../../../../rubocop/cop/migration/schedule_async'
+
+describe RuboCop::Cop::Migration::ScheduleAsync do
+ include CopHelper
+
+ let(:cop) { described_class.new }
+ let(:source) do
+ <<~SOURCE
+ def up
+ BackgroundMigrationWorker.perform_async(ClazzName, "Bar", "Baz")
+ end
+ SOURCE
+ end
+
+ shared_examples 'a disabled cop' do
+ it 'does not register any offenses' do
+ inspect_source(source)
+
+ expect(cop.offenses).to be_empty
+ end
+ end
+
+ context 'outside of a migration' do
+ it_behaves_like 'a disabled cop'
+ end
+
+ context 'in a migration' do
+ before do
+ allow(cop).to receive(:in_migration?).and_return(true)
+ end
+
+ context 'in an old migration' do
+ before do
+ allow(cop).to receive(:version).and_return(described_class::ENFORCED_SINCE - 5)
+ end
+
+ it_behaves_like 'a disabled cop'
+ end
+
+ context 'that is recent' do
+ before do
+ allow(cop).to receive(:version).and_return(described_class::ENFORCED_SINCE + 5)
+ end
+
+ context 'BackgroundMigrationWorker.perform_async' do
+ it 'adds an offence when calling `BackgroundMigrationWorker.peform_async`' do
+ inspect_source(source)
+
+ expect(cop.offenses.size).to eq(1)
+ end
+
+ it 'autocorrects to the right version' do
+ correct_source = <<~CORRECT
+ def up
+ migrate_async(ClazzName, "Bar", "Baz")
+ end
+ CORRECT
+
+ expect(autocorrect_source(source)).to eq(correct_source)
+ end
+ end
+
+ context 'BackgroundMigrationWorker.perform_in' do
+ let(:source) do
+ <<~SOURCE
+ def up
+ BackgroundMigrationWorker
+ .perform_in(delay, ClazzName, "Bar", "Baz")
+ end
+ SOURCE
+ end
+
+ it 'adds an offence' do
+ inspect_source(source)
+
+ expect(cop.offenses.size).to eq(1)
+ end
+
+ it 'autocorrects to the right version' do
+ correct_source = <<~CORRECT
+ def up
+ migrate_in(delay, ClazzName, "Bar", "Baz")
+ end
+ CORRECT
+
+ expect(autocorrect_source(source)).to eq(correct_source)
+ end
+ end
+
+ context 'BackgroundMigrationWorker.bulk_perform_async' do
+ let(:source) do
+ <<~SOURCE
+ def up
+ BackgroundMigrationWorker
+ .bulk_perform_async(jobs)
+ end
+ SOURCE
+ end
+
+ it 'adds an offence' do
+ inspect_source(source)
+
+ expect(cop.offenses.size).to eq(1)
+ end
+
+ it 'autocorrects to the right version' do
+ correct_source = <<~CORRECT
+ def up
+ bulk_migrate_async(jobs)
+ end
+ CORRECT
+
+ expect(autocorrect_source(source)).to eq(correct_source)
+ end
+ end
+
+ context 'BackgroundMigrationWorker.bulk_perform_in' do
+ let(:source) do
+ <<~SOURCE
+ def up
+ BackgroundMigrationWorker
+ .bulk_perform_in(5.minutes, jobs)
+ end
+ SOURCE
+ end
+
+ it 'adds an offence' do
+ inspect_source(source)
+
+ expect(cop.offenses.size).to eq(1)
+ end
+
+ it 'autocorrects to the right version' do
+ correct_source = <<~CORRECT
+ def up
+ bulk_migrate_in(5.minutes, jobs)
+ end
+ CORRECT
+
+ expect(autocorrect_source(source)).to eq(correct_source)
+ end
+ end
+ end
+ end
+end
diff --git a/spec/rubocop/cop/scalability/idempotent_worker_spec.rb b/spec/rubocop/cop/scalability/idempotent_worker_spec.rb
new file mode 100644
index 00000000000..7abd602f8bc
--- /dev/null
+++ b/spec/rubocop/cop/scalability/idempotent_worker_spec.rb
@@ -0,0 +1,38 @@
+# frozen_string_literal: true
+
+require 'fast_spec_helper'
+require 'rubocop'
+require_relative '../../../support/helpers/expect_offense'
+require_relative '../../../../rubocop/cop/scalability/idempotent_worker'
+
+describe RuboCop::Cop::Scalability::IdempotentWorker do
+ include CopHelper
+ include ExpectOffense
+
+ subject(:cop) { described_class.new }
+
+ before do
+ allow(cop)
+ .to receive(:in_worker?)
+ .and_return(true)
+ end
+
+ it 'adds an offense when not defining idempotent method' do
+ inspect_source(<<~CODE.strip_indent)
+ class SomeWorker
+ end
+ CODE
+
+ expect(cop.offenses.size).to eq(1)
+ end
+
+ it 'adds an offense when not defining idempotent method' do
+ inspect_source(<<~CODE.strip_indent)
+ class SomeWorker
+ idempotent!
+ end
+ CODE
+
+ expect(cop.offenses.size).to be_zero
+ end
+end
diff --git a/spec/rubocop/migration_helpers_spec.rb b/spec/rubocop/migration_helpers_spec.rb
new file mode 100644
index 00000000000..73ced8c58da
--- /dev/null
+++ b/spec/rubocop/migration_helpers_spec.rb
@@ -0,0 +1,56 @@
+# frozen_string_literal: true
+
+require 'fast_spec_helper'
+require 'rubocop'
+require 'rspec-parameterized'
+
+require_relative '../../rubocop/migration_helpers'
+
+describe RuboCop::MigrationHelpers do
+ using RSpec::Parameterized::TableSyntax
+
+ subject(:fake_cop) { Class.new { include RuboCop::MigrationHelpers }.new }
+
+ let(:node) { double(:node) }
+
+ before do
+ allow(node).to receive_message_chain('location.expression.source_buffer.name')
+ .and_return(name)
+ end
+
+ describe '#in_migration?' do
+ where(:name, :expected) do
+ '/gitlab/db/migrate/20200210184420_create_operations_scopes_table.rb' | true
+ '/gitlab/db/post_migrate/20200210184420_create_operations_scopes_table.rb' | true
+ '/gitlab/db/geo/migrate/20200210184420_create_operations_scopes_table.rb' | true
+ '/gitlab/db/geo/post_migrate/20200210184420_create_operations_scopes_table.rb' | true
+ '/gitlab/db/elsewhere/20200210184420_create_operations_scopes_table.rb' | false
+ end
+
+ with_them do
+ it { expect(fake_cop.in_migration?(node)).to eq(expected) }
+ end
+ end
+
+ describe '#in_post_deployment_migration?' do
+ where(:name, :expected) do
+ '/gitlab/db/migrate/20200210184420_create_operations_scopes_table.rb' | false
+ '/gitlab/db/post_migrate/20200210184420_create_operations_scopes_table.rb' | true
+ '/gitlab/db/geo/migrate/20200210184420_create_operations_scopes_table.rb' | false
+ '/gitlab/db/geo/post_migrate/20200210184420_create_operations_scopes_table.rb' | true
+ '/gitlab/db/elsewhere/20200210184420_create_operations_scopes_table.rb' | false
+ end
+
+ with_them do
+ it { expect(fake_cop.in_post_deployment_migration?(node)).to eq(expected) }
+ end
+ end
+
+ describe "#version" do
+ let(:name) do
+ '/path/to/gitlab/db/migrate/20200210184420_create_operations_scopes_table.rb'
+ end
+
+ it { expect(fake_cop.version(node)).to eq(20200210184420) }
+ end
+end