Commit c11f8b46 authored by Andreas Brandl's avatar Andreas Brandl

Merge branch '343055-fix-cop-for-removed-methods' into 'master'

Update BG migration cop to match removed methods

See merge request gitlab-org/gitlab!77273
parents 10f0c14f 5f93cd96
......@@ -11,9 +11,8 @@ module RuboCop
ENFORCED_SINCE = 2020_02_12_00_00_00
MSG = <<~MSG
Don't call the background migration worker directly, use the `#migrate_async`,
`#migrate_in`, `#bulk_migrate_async` or `#bulk_migrate_in` migration helpers
instead.
Don't call the background migration worker directly, use the `#migrate_in` or
`#queue_background_migration_jobs_by_range_at_intervals` migration helpers instead.
MSG
def_node_matcher :calls_background_migration_worker?, <<~PATTERN
......@@ -26,28 +25,6 @@ module RuboCop
add_offense(node, location: :expression) if calls_background_migration_worker?(node)
end
def autocorrect(node)
# This gets rid of the receiver `BackgroundMigrationWorker` and
# replaces `perform` with `schedule`
schedule_method = method_name(node).to_s.sub('perform', 'migrate')
arguments = arguments(node).map(&:source).join(', ')
replacement = "#{schedule_method}(#{arguments})"
lambda do |corrector|
corrector.replace(node.source_range, replacement)
end
end
private
def method_name(node)
node.children.second
end
def arguments(node)
node.children[2..]
end
end
end
end
......
......@@ -43,24 +43,18 @@ RSpec.describe RuboCop::Cop::Migration::ScheduleAsync do
end
context 'BackgroundMigrationWorker.perform_async' do
it 'adds an offense when calling `BackgroundMigrationWorker.peform_async` and corrects', :aggregate_failures do
it 'adds an offense when calling `BackgroundMigrationWorker.peform_async`' do
expect_offense(<<~RUBY)
def up
BackgroundMigrationWorker.perform_async(ClazzName, "Bar", "Baz")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Don't call [...]
end
RUBY
expect_correction(<<~RUBY)
def up
migrate_async(ClazzName, "Bar", "Baz")
end
RUBY
end
end
context 'BackgroundMigrationWorker.perform_in' do
it 'adds an offense and corrects', :aggregate_failures do
it 'adds an offense' do
expect_offense(<<~RUBY)
def up
BackgroundMigrationWorker
......@@ -68,17 +62,11 @@ RSpec.describe RuboCop::Cop::Migration::ScheduleAsync do
.perform_in(delay, ClazzName, "Bar", "Baz")
end
RUBY
expect_correction(<<~RUBY)
def up
migrate_in(delay, ClazzName, "Bar", "Baz")
end
RUBY
end
end
context 'BackgroundMigrationWorker.bulk_perform_async' do
it 'adds an offense and corrects', :aggregate_failures do
it 'adds an offense' do
expect_offense(<<~RUBY)
def up
BackgroundMigrationWorker
......@@ -86,17 +74,11 @@ RSpec.describe RuboCop::Cop::Migration::ScheduleAsync do
.bulk_perform_async(jobs)
end
RUBY
expect_correction(<<~RUBY)
def up
bulk_migrate_async(jobs)
end
RUBY
end
end
context 'BackgroundMigrationWorker.bulk_perform_in' do
it 'adds an offense and corrects', :aggregate_failures do
it 'adds an offense' do
expect_offense(<<~RUBY)
def up
BackgroundMigrationWorker
......@@ -104,12 +86,6 @@ RSpec.describe RuboCop::Cop::Migration::ScheduleAsync do
.bulk_perform_in(5.minutes, jobs)
end
RUBY
expect_correction(<<~RUBY)
def up
bulk_migrate_in(5.minutes, jobs)
end
RUBY
end
end
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment