2020-03-23 11:09:36 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-09-21 14:10:44 -04:00
|
|
|
require 'rubocop/cop/rspec/base'
|
|
|
|
require 'rubocop/cop/rspec/mixin/top_level_group'
|
2020-03-23 11:09:36 -04:00
|
|
|
|
|
|
|
module RuboCop
|
|
|
|
module Cop
|
2022-10-24 14:10:18 -04:00
|
|
|
module RSpec
|
2020-03-23 11:09:36 -04:00
|
|
|
# Cop that detects duplicate EE spec files
|
|
|
|
#
|
|
|
|
# There should not be files in both ee/spec/*/ee/my_spec.rb and ee/spec/*/my_spec.rb
|
|
|
|
#
|
|
|
|
# # bad
|
|
|
|
# ee/spec/controllers/my_spec.rb # describe MyClass
|
|
|
|
# ee/spec/controllers/ee/my_spec.rb # describe MyClass
|
|
|
|
#
|
|
|
|
# # good, spec for EE extension code
|
|
|
|
# ee/spec/controllers/ee/my_spec.rb # describe MyClass
|
|
|
|
#
|
|
|
|
# # good, spec for EE only code
|
|
|
|
# ee/spec/controllers/my_spec.rb # describe MyClass
|
|
|
|
#
|
2022-09-21 14:10:44 -04:00
|
|
|
class DuplicateSpecLocation < RuboCop::Cop::RSpec::Base
|
|
|
|
include RuboCop::Cop::RSpec::TopLevelGroup
|
2020-03-23 11:09:36 -04:00
|
|
|
|
|
|
|
MSG = 'Duplicate spec location in `%<path>s`.'
|
|
|
|
|
2022-09-21 14:10:44 -04:00
|
|
|
def on_top_level_group(node)
|
|
|
|
path = file_path_for_node(node.send_node).sub(%r{\A#{rails_root}/}, '')
|
2020-03-23 11:09:36 -04:00
|
|
|
duplicate_path = find_duplicate_path(path)
|
|
|
|
|
2022-10-24 14:10:18 -04:00
|
|
|
return unless duplicate_path && File.exist?(File.join(rails_root, duplicate_path))
|
|
|
|
|
|
|
|
add_offense(node.send_node, message: format(MSG, path: duplicate_path))
|
2020-03-23 11:09:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def ee_spec?(path)
|
|
|
|
File.fnmatch?('ee/spec/**/*.rb', path, File::FNM_PATHNAME)
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_duplicate_path(path)
|
|
|
|
return unless ee_spec?(path)
|
|
|
|
|
|
|
|
if File.fnmatch?('ee/spec/**/ee/**', path)
|
|
|
|
path.match('\A(ee/spec/[^/]+)/ee/(.+)') do |match|
|
|
|
|
File.join(match[1], match[2])
|
|
|
|
end
|
|
|
|
else
|
|
|
|
path.match('\A(ee/spec/[^/]+)/(.+)') do |match|
|
|
|
|
File.join(match[1], 'ee', match[2])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def file_path_for_node(node)
|
|
|
|
node.location.expression.source_buffer.name
|
|
|
|
end
|
|
|
|
|
|
|
|
def rails_root
|
|
|
|
File.expand_path('../../..', __dir__)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|