Maintenance: Add rubocop to check if a db_strategy is required in your spec test.

This commit is contained in:
Rolf Schmidt 2021-09-20 09:14:27 +02:00 committed by Thorsten Eckel
parent 8d10872c5d
commit ff28e17672
4 changed files with 53 additions and 1 deletions

View file

@ -0,0 +1,45 @@
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
module RuboCop
module Cop
module Zammad
class ExistsDbStrategy < Base
def_node_matcher :migration_execute?, <<-PATTERN
$(send (const (const _ :ObjectManager ) :Attribute) :migration_execute)
PATTERN
def_node_matcher :create_attribute?, <<-PATTERN
$(send _ :create_attribute ...)
PATTERN
def_node_matcher :is_block?, <<-PATTERN
$(block ...)
PATTERN
def_node_matcher :has_reset?, <<-PATTERN
$(send _ {:describe :context :it} (_ ...) (hash ... (pair (sym :db_strategy) (sym {:reset :reset_all}))))
PATTERN
MSG = 'Add a `db_strategy: :reset` to your context/decribe when you are creating object manager attributes!'.freeze
def on_send(node)
return if !migration_execute?(node) && !create_attribute?(node)
reset = false
node_parent = node.parent
until node_parent.nil?
if is_block?(node_parent) && has_reset?(node_parent.children[0])
reset = true
break
end
node_parent = node_parent.parent
end
return if reset
add_offense(node)
end
end
end
end
end

View file

@ -314,3 +314,9 @@ Zammad/ExistsResetColumnInformation:
Exclude:
- 'db/migrate/201*_*.rb'
- 'db/migrate/2020*_*.rb'
Zammad/ExistsDbStrategy:
Include:
- "spec/**/*.rb"
Exclude:
- spec/support/capybara/common_actions.rb

View file

@ -2,6 +2,7 @@
require_relative 'cop/zammad/exists_condition'
require_relative 'cop/zammad/exists_date_time_precision'
require_relative 'cop/zammad/exists_db_strategy'
require_relative 'cop/zammad/exists_reset_column_information'
require_relative 'cop/zammad/correct_migration_timestamp'
require_relative 'cop/zammad/have_no_over_not_to'

View file

@ -2,7 +2,7 @@
require 'rails_helper'
RSpec.describe ::Sequencer::Sequence::Import::Freshdesk::Ticket, sequencer: :sequence, db_strategy: 'reset' do
RSpec.describe ::Sequencer::Sequence::Import::Freshdesk::Ticket, sequencer: :sequence, db_strategy: :reset do
context 'when importing tickets from Freshdesk' do