Commit 0b58729f authored by Doug Stull's avatar Doug Stull Committed by Rémy Coutable

Fix rubocop loading order issues

- due to our loading order during test, we are loading
  the cop_helper first instead of letting the rubocop
  be required first and the class to be defined.
parent acce00c3
# frozen_string_literal: true
require 'tempfile'
# This module provides methods that make it easier to test Cops.
module CopHelper
extend RSpec::SharedContext
let(:ruby_version) { 2.4 }
let(:rails_version) { false }
def inspect_source_file(source)
Tempfile.open('tmp') { |f| inspect_source(source, f) }
end
def inspect_source(source, file = nil)
RuboCop::Formatter::DisabledConfigFormatter.config_to_allow_offenses = {}
RuboCop::Formatter::DisabledConfigFormatter.detected_styles = {}
processed_source = parse_source(source, file)
raise 'Error parsing example code' unless processed_source.valid_syntax?
_investigate(cop, processed_source)
end
def parse_source(source, file = nil)
if file&.respond_to?(:write)
file.write(source)
file.rewind
file = file.path
end
RuboCop::ProcessedSource.new(source, ruby_version, file)
end
def autocorrect_source_file(source)
Tempfile.open('tmp') { |f| autocorrect_source(source, f) }
end
def autocorrect_source(source, file = nil)
RuboCop::Formatter::DisabledConfigFormatter.config_to_allow_offenses = {}
RuboCop::Formatter::DisabledConfigFormatter.detected_styles = {}
cop.instance_variable_get(:@options)[:auto_correct] = true
processed_source = parse_source(source, file)
_investigate(cop, processed_source)
@last_corrector.rewrite
end
def _investigate(cop, processed_source)
team = RuboCop::Cop::Team.new([cop], nil, raise_error: true)
report = team.investigate(processed_source)
@last_corrector = report.correctors.first || RuboCop::Cop::Corrector.new(processed_source)
report.offenses
end
end
module RuboCop
module Cop
# Monkey-patch Cop for tests to provide easy access to messages and
# highlights.
# this file is an exact copy of source except for this line
# where we change to the new Base class defined in rubocop and skirt around our superclass mismatch for class Cop
# when running a rubocop spec.
class Base
def messages
offenses.sort.map(&:message)
end
def highlights
offenses.sort.map { |o| o.location.source }
end
end
end
end
...@@ -6,7 +6,13 @@ require_relative "helpers/stub_object_storage" ...@@ -6,7 +6,13 @@ require_relative "helpers/stub_object_storage"
require_relative "helpers/stub_env" require_relative "helpers/stub_env"
require_relative "helpers/fast_rails_root" require_relative "helpers/fast_rails_root"
require_relative 'rubocop_patch' # so we need to load rubocop here due to the rubocop support file loading cop_helper
# which monkey patches class Cop
# if cop helper is loaded before rubocop (where class Cop is defined as class Cop < Base)
# we get a `superclass mismatch for class Cop` error when running a rspec for a locally defined
# rubocop cop - therefore we need rubocop required first since it had an inheritance added to the Cop class
require 'rubocop'
require 'rubocop/rspec/support'
RSpec.configure do |config| RSpec.configure do |config|
config.mock_with :rspec config.mock_with :rspec
......
# frozen_string_literal: true
# There is an issue between rubocop versions 0.86 and 0.87 (verified by testing locally)
# where the monkey patching in cop_helper is referencing class Cop and should really be referencing class Base instead
# the gem's version of the cop_helper causes this issue when testing a rubocop cop locally and in CI
# The only difference in this file as compared to gem source file is that we include our own cop_helper instead
# which is a direct copy with a fix for the monkey patching part.
# Doing this, resolves the issue.
# Ideally we should move away from using the cop_helper at all as is the direction of rubocop as seen
# here - https://github.com/rubocop-hq/rubocop/issues/8003
# more info https://gitlab.com/gitlab-org/gitlab/-/merge_requests/46477
require_relative 'helpers/cop_helper'
require 'rubocop/rspec/host_environment_simulation_helper'
require 'rubocop/rspec/shared_contexts'
require 'rubocop/rspec/expect_offense'
RSpec.configure do |config|
config.include CopHelper
config.include HostEnvironmentSimulatorHelper
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