From ff28e1767274f2e6a1b179d429dae9f1e3a82420 Mon Sep 17 00:00:00 2001 From: Rolf Schmidt Date: Mon, 20 Sep 2021 09:14:27 +0200 Subject: [PATCH] Maintenance: Add rubocop to check if a db_strategy is required in your spec test. --- .rubocop/cop/zammad/exists_db_strategy.rb | 45 +++++++++++++++++++ .rubocop/default.yml | 6 +++ .rubocop/rubocop_zammad.rb | 1 + .../sequence/import/freshdesk/ticket_spec.rb | 2 +- 4 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 .rubocop/cop/zammad/exists_db_strategy.rb diff --git a/.rubocop/cop/zammad/exists_db_strategy.rb b/.rubocop/cop/zammad/exists_db_strategy.rb new file mode 100644 index 000000000..f825e2bfc --- /dev/null +++ b/.rubocop/cop/zammad/exists_db_strategy.rb @@ -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 diff --git a/.rubocop/default.yml b/.rubocop/default.yml index 7273db0c4..bf0656c06 100644 --- a/.rubocop/default.yml +++ b/.rubocop/default.yml @@ -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 diff --git a/.rubocop/rubocop_zammad.rb b/.rubocop/rubocop_zammad.rb index f90ca647b..f525ef923 100644 --- a/.rubocop/rubocop_zammad.rb +++ b/.rubocop/rubocop_zammad.rb @@ -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' diff --git a/spec/lib/sequencer/sequence/import/freshdesk/ticket_spec.rb b/spec/lib/sequencer/sequence/import/freshdesk/ticket_spec.rb index 097526d44..fbad46203 100644 --- a/spec/lib/sequencer/sequence/import/freshdesk/ticket_spec.rb +++ b/spec/lib/sequencer/sequence/import/freshdesk/ticket_spec.rb @@ -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