From f79e8c72cd5c4c99eae5ea059e6d6e577d44929c Mon Sep 17 00:00:00 2001 From: Thorsten Eckel Date: Mon, 31 Jan 2022 15:10:31 +0100 Subject: [PATCH] Refactoring: Add possibility to write tests for custom Zammad rubocop cops. --- .../cop/zammad/have_no_over_not_to_spec.rb | 24 +++++++++++++++ spec/support/rubocop.rb | 29 +++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 spec/rubocop/cop/zammad/have_no_over_not_to_spec.rb create mode 100644 spec/support/rubocop.rb diff --git a/spec/rubocop/cop/zammad/have_no_over_not_to_spec.rb b/spec/rubocop/cop/zammad/have_no_over_not_to_spec.rb new file mode 100644 index 000000000..35ea9593d --- /dev/null +++ b/spec/rubocop/cop/zammad/have_no_over_not_to_spec.rb @@ -0,0 +1,24 @@ +# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/ + +require 'rails_helper' +require_relative '../../../../.rubocop/cop/zammad/have_no_over_not_to' + +RSpec.describe RuboCop::Cop::Zammad::HaveNoOverNotTo, type: :rubocop do + + it 'accepts .to have_no' do + expect_no_offenses("expect(page).to have_no_css('#elem')") + end + + context 'with .not_to have' do + it 'registers an offense' do + expect_offense(<<~RUBY) + expect(page).not_to have_css('#elem') + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `.to have_no_css` over `.not_to have_css`. + RUBY + + expect_correction(<<~RUBY) + expect(page).to have_no_css('#elem') + RUBY + end + end +end diff --git a/spec/support/rubocop.rb b/spec/support/rubocop.rb new file mode 100644 index 000000000..ad885f048 --- /dev/null +++ b/spec/support/rubocop.rb @@ -0,0 +1,29 @@ +# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/ + +require 'rubocop/rspec/support' + +# https://github.com/rubocop/rubocop/tree/91e72f8bb4a5a646845e7915052f912d60a3d280/lib/rubocop/rspec/shared_contexts.rb:52 +RSpec.shared_context 'when checking custom RuboCop cops' do + + include RuboCop::RSpec::ExpectOffense + + let(:cop_options) { {} } + let(:cop_config) { {} } + + let(:cur_cop_config) do + RuboCop::ConfigLoader + .default_configuration.for_cop(described_class) + .merge({ + 'Enabled' => true, # in case it is 'pending' + 'AutoCorrect' => true # in case defaults set it to false + }) + .merge(cop_config) + end + + let(:config) { ::RuboCop::Config.new({ described_class.cop_name => cur_cop_config }, "#{Rails.configuration.root}/.rubocop.yml") } + let(:cop) { described_class.new(config, cop_options) } +end + +RSpec.configure do |config| + config.include_context 'when checking custom RuboCop cops', type: :rubocop +end