Skip to content

Commit

Permalink
[Fixes rubocop#113] Add basic compatibility check with main gem
Browse files Browse the repository at this point in the history
  • Loading branch information
marcandre committed Sep 24, 2020
1 parent 20420e7 commit fa01438
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/rubocop/ast.rb
Expand Up @@ -63,6 +63,7 @@
require_relative 'ast/node/yield_node'
require_relative 'ast/builder'
require_relative 'ast/processed_source'
require_relative 'ast/rubocop_compatibility'
require_relative 'ast/token'
require_relative 'ast/traversal'
require_relative 'ast/version'
30 changes: 30 additions & 0 deletions lib/rubocop/ast/rubocop_compatibility.rb
@@ -0,0 +1,30 @@
# frozen_string_literal: true

module RuboCop
# ...
module AST
# Responsible for compatibility with main gem
# @api private
module RuboCopCompatibility
INCOMPATIBLE_COPS = {
'0.89.0' => 'Layout/LineLength'
}.freeze
def rubocop_loaded
loaded = Gem::Version.new(RuboCop::Version::STRING)
incompatible = INCOMPATIBLE_COPS.select do |k, _v|
loaded < Gem::Version.new(k)
end.values
return if incompatible.empty?

warn <<~WARNING
*** WARNING – Incompatible version of `rubocop` and `rubocop-ast`
You may encounter issues with the following \
Cop#{'s' if incompatible.size > 1}: #{incompatible.join(', ')}
Please upgrade rubocop to at least v#{INCOMPATIBLE_COPS.keys.last}
WARNING
end
end

extend RuboCopCompatibility
end
end
24 changes: 24 additions & 0 deletions spec/rubocop/ast/rubocop_compatibility_spec.rb
@@ -0,0 +1,24 @@
# frozen_string_literal: true

RSpec.describe RuboCop::AST::RuboCopCompatibility do
subject(:callback) { RuboCop::AST.rubocop_loaded }
before do
stub_const '::RuboCop::Version::STRING', rubocop_version
end

context 'when ran from an incompatible version of Rubocop' do
let(:rubocop_version) { '0.42.0' }

it 'issues a warning' do
expect { callback }.to output(/LineLength/).to_stderr
end
end

context 'when ran from a compatible version of Rubocop' do
let(:rubocop_version) { '0.89.0' }

it 'issues a warning' do
expect { callback }.not_to output.to_stderr
end
end
end

0 comments on commit fa01438

Please sign in to comment.