debian-mirror-gitlab/spec/rubocop/code_reuse_helpers_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

326 lines
9.2 KiB
Ruby
Raw Normal View History

2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
2020-07-28 23:09:34 +05:30
require 'fast_spec_helper'
2018-12-05 23:21:45 +05:30
require 'parser/current'
require_relative '../../rubocop/code_reuse_helpers'
2020-07-28 23:09:34 +05:30
RSpec.describe RuboCop::CodeReuseHelpers do
2021-03-11 19:13:27 +05:30
def build_and_parse_source(source, path = 'foo.rb')
2018-12-05 23:21:45 +05:30
buffer = Parser::Source::Buffer.new(path)
buffer.source = source
builder = RuboCop::AST::Builder.new
parser = Parser::CurrentRuby.new(builder)
parser.parse(buffer)
end
let(:cop) do
Class.new do
include RuboCop::CodeReuseHelpers
end.new
end
2022-01-26 12:08:38 +05:30
let(:ee_file_path) { File.expand_path('../../ee/app/models/license.rb', __dir__) }
2018-12-05 23:21:45 +05:30
describe '#send_to_constant?' do
it 'returns true when sending to a constant' do
2021-03-11 19:13:27 +05:30
node = build_and_parse_source('Foo.bar')
2018-12-05 23:21:45 +05:30
expect(cop.send_to_constant?(node)).to eq(true)
end
it 'returns false when sending to something other than a constant' do
2021-03-11 19:13:27 +05:30
node = build_and_parse_source('10')
2018-12-05 23:21:45 +05:30
expect(cop.send_to_constant?(node)).to eq(false)
end
end
describe '#send_receiver_name_ends_with?' do
it 'returns true when the receiver ends with a suffix' do
2021-03-11 19:13:27 +05:30
node = build_and_parse_source('FooFinder.new')
2018-12-05 23:21:45 +05:30
expect(cop.send_receiver_name_ends_with?(node, 'Finder')).to eq(true)
end
it 'returns false when the receiver is the same as a suffix' do
2021-03-11 19:13:27 +05:30
node = build_and_parse_source('Finder.new')
2018-12-05 23:21:45 +05:30
expect(cop.send_receiver_name_ends_with?(node, 'Finder')).to eq(false)
end
end
describe '#file_path_for_node' do
it 'returns the file path of a node' do
2021-03-11 19:13:27 +05:30
node = build_and_parse_source('10')
2018-12-05 23:21:45 +05:30
path = cop.file_path_for_node(node)
expect(path).to eq('foo.rb')
end
end
describe '#name_of_constant' do
it 'returns the name of a constant' do
2021-03-11 19:13:27 +05:30
node = build_and_parse_source('Foo')
2018-12-05 23:21:45 +05:30
expect(cop.name_of_constant(node)).to eq(:Foo)
end
end
describe '#in_finder?' do
it 'returns true for a node in the finders directory' do
2021-03-11 19:13:27 +05:30
node = build_and_parse_source('10', rails_root_join('app', 'finders', 'foo.rb'))
2018-12-05 23:21:45 +05:30
expect(cop.in_finder?(node)).to eq(true)
end
it 'returns false for a node outside the finders directory' do
2021-03-11 19:13:27 +05:30
node = build_and_parse_source('10', rails_root_join('app', 'foo', 'foo.rb'))
2018-12-05 23:21:45 +05:30
expect(cop.in_finder?(node)).to eq(false)
end
end
describe '#in_model?' do
it 'returns true for a node in the models directory' do
2021-03-11 19:13:27 +05:30
node = build_and_parse_source('10', rails_root_join('app', 'models', 'foo.rb'))
2018-12-05 23:21:45 +05:30
expect(cop.in_model?(node)).to eq(true)
end
it 'returns false for a node outside the models directory' do
2021-03-11 19:13:27 +05:30
node = build_and_parse_source('10', rails_root_join('app', 'foo', 'foo.rb'))
2018-12-05 23:21:45 +05:30
expect(cop.in_model?(node)).to eq(false)
end
end
describe '#in_service_class?' do
it 'returns true for a node in the services directory' do
2021-03-11 19:13:27 +05:30
node = build_and_parse_source('10', rails_root_join('app', 'services', 'foo.rb'))
2018-12-05 23:21:45 +05:30
expect(cop.in_service_class?(node)).to eq(true)
end
it 'returns false for a node outside the services directory' do
2021-03-11 19:13:27 +05:30
node = build_and_parse_source('10', rails_root_join('app', 'foo', 'foo.rb'))
2018-12-05 23:21:45 +05:30
expect(cop.in_service_class?(node)).to eq(false)
end
end
describe '#in_presenter?' do
it 'returns true for a node in the presenters directory' do
2021-03-11 19:13:27 +05:30
node = build_and_parse_source('10', rails_root_join('app', 'presenters', 'foo.rb'))
2018-12-05 23:21:45 +05:30
expect(cop.in_presenter?(node)).to eq(true)
end
it 'returns false for a node outside the presenters directory' do
2021-03-11 19:13:27 +05:30
node = build_and_parse_source('10', rails_root_join('app', 'foo', 'foo.rb'))
2018-12-05 23:21:45 +05:30
expect(cop.in_presenter?(node)).to eq(false)
end
end
describe '#in_serializer?' do
it 'returns true for a node in the serializers directory' do
2021-03-11 19:13:27 +05:30
node = build_and_parse_source('10', rails_root_join('app', 'serializers', 'foo.rb'))
2018-12-05 23:21:45 +05:30
expect(cop.in_serializer?(node)).to eq(true)
end
it 'returns false for a node outside the serializers directory' do
2021-03-11 19:13:27 +05:30
node = build_and_parse_source('10', rails_root_join('app', 'foo', 'foo.rb'))
2018-12-05 23:21:45 +05:30
expect(cop.in_serializer?(node)).to eq(false)
end
end
describe '#in_worker?' do
it 'returns true for a node in the workers directory' do
2021-03-11 19:13:27 +05:30
node = build_and_parse_source('10', rails_root_join('app', 'workers', 'foo.rb'))
2018-12-05 23:21:45 +05:30
expect(cop.in_worker?(node)).to eq(true)
end
it 'returns false for a node outside the workers directory' do
2021-03-11 19:13:27 +05:30
node = build_and_parse_source('10', rails_root_join('app', 'foo', 'foo.rb'))
2018-12-05 23:21:45 +05:30
expect(cop.in_worker?(node)).to eq(false)
end
end
2021-09-30 23:02:18 +05:30
describe '#in_graphql_types?' do
%w[
app/graphql/types
ee/app/graphql/ee/types
ee/app/graphql/types
].each do |path|
it "returns true for a node in #{path}" do
node = build_and_parse_source('10', rails_root_join(path, 'foo.rb'))
expect(cop.in_graphql_types?(node)).to eq(true)
end
end
%w[
app/graphql/resolvers
app/foo
].each do |path|
it "returns true for a node in #{path}" do
node = build_and_parse_source('10', rails_root_join(path, 'foo.rb'))
expect(cop.in_graphql_types?(node)).to eq(false)
end
end
end
2018-12-05 23:21:45 +05:30
describe '#in_api?' do
it 'returns true for a node in the API directory' do
2021-03-11 19:13:27 +05:30
node = build_and_parse_source('10', rails_root_join('lib', 'api', 'foo.rb'))
2018-12-05 23:21:45 +05:30
expect(cop.in_api?(node)).to eq(true)
end
it 'returns false for a node outside the API directory' do
2021-03-11 19:13:27 +05:30
node = build_and_parse_source('10', rails_root_join('lib', 'foo', 'foo.rb'))
2018-12-05 23:21:45 +05:30
expect(cop.in_api?(node)).to eq(false)
end
end
2021-09-30 23:02:18 +05:30
describe '#in_spec?' do
it 'returns true for a node in the spec directory' do
node = build_and_parse_source('10', rails_root_join('spec', 'foo.rb'))
expect(cop.in_spec?(node)).to eq(true)
end
it 'returns true for a node in the ee/spec directory' do
node = build_and_parse_source('10', rails_root_join('ee', 'spec', 'foo.rb'))
expect(cop.in_spec?(node)).to eq(true)
end
it 'returns false for a node outside the spec directory' do
node = build_and_parse_source('10', rails_root_join('lib', 'foo.rb'))
expect(cop.in_spec?(node)).to eq(false)
end
end
describe '#in_app_directory?' do
2018-12-05 23:21:45 +05:30
it 'returns true for a directory in the CE app/ directory' do
2021-03-11 19:13:27 +05:30
node = build_and_parse_source('10', rails_root_join('app', 'models', 'foo.rb'))
2018-12-05 23:21:45 +05:30
2021-09-30 23:02:18 +05:30
expect(cop.in_app_directory?(node, 'models')).to eq(true)
2018-12-05 23:21:45 +05:30
end
it 'returns true for a directory in the EE app/ directory' do
node =
2021-03-11 19:13:27 +05:30
build_and_parse_source('10', rails_root_join('ee', 'app', 'models', 'foo.rb'))
2018-12-05 23:21:45 +05:30
2021-09-30 23:02:18 +05:30
expect(cop.in_app_directory?(node, 'models')).to eq(true)
2018-12-05 23:21:45 +05:30
end
it 'returns false for a directory in the lib/ directory' do
node =
2021-03-11 19:13:27 +05:30
build_and_parse_source('10', rails_root_join('lib', 'models', 'foo.rb'))
2018-12-05 23:21:45 +05:30
2021-09-30 23:02:18 +05:30
expect(cop.in_app_directory?(node, 'models')).to eq(false)
end
end
describe '#in_lib_directory?' do
it 'returns true for a directory in the CE lib/ directory' do
node = build_and_parse_source('10', rails_root_join('lib', 'models', 'foo.rb'))
expect(cop.in_lib_directory?(node, 'models')).to eq(true)
end
it 'returns true for a directory in the EE lib/ directory' do
node =
build_and_parse_source('10', rails_root_join('ee', 'lib', 'models', 'foo.rb'))
expect(cop.in_lib_directory?(node, 'models')).to eq(true)
end
it 'returns false for a directory in the app/ directory' do
node =
build_and_parse_source('10', rails_root_join('app', 'models', 'foo.rb'))
expect(cop.in_lib_directory?(node, 'models')).to eq(false)
2018-12-05 23:21:45 +05:30
end
end
describe '#name_of_receiver' do
it 'returns the name of a send receiver' do
2021-03-11 19:13:27 +05:30
node = build_and_parse_source('Foo.bar')
2018-12-05 23:21:45 +05:30
expect(cop.name_of_receiver(node)).to eq('Foo')
end
end
describe '#each_class_method' do
it 'yields every class method to the supplied block' do
2021-03-11 19:13:27 +05:30
node = build_and_parse_source(<<~RUBY)
2018-12-05 23:21:45 +05:30
class Foo
class << self
def first
end
end
def self.second
end
end
RUBY
nodes = cop.each_class_method(node).to_a
expect(nodes.length).to eq(2)
expect(nodes[0].children[0]).to eq(:first)
expect(nodes[1].children[1]).to eq(:second)
end
end
describe '#each_send_node' do
it 'yields every send node to the supplied block' do
2021-03-11 19:13:27 +05:30
node = build_and_parse_source("foo\nbar")
2018-12-05 23:21:45 +05:30
nodes = cop.each_send_node(node).to_a
expect(nodes.length).to eq(2)
expect(nodes[0].children[1]).to eq(:foo)
expect(nodes[1].children[1]).to eq(:bar)
end
end
describe '#disallow_send_to' do
it 'disallows sending a message to a constant' do
2021-03-11 19:13:27 +05:30
def_node = build_and_parse_source(<<~RUBY)
2018-12-05 23:21:45 +05:30
def foo
FooFinder.new
end
RUBY
send_node = def_node.each_child_node(:send).first
expect(cop)
.to receive(:add_offense)
.with(send_node, location: :expression, message: 'oops')
cop.disallow_send_to(def_node, 'Finder', 'oops')
end
end
2022-01-26 12:08:38 +05:30
2022-03-02 08:16:31 +05:30
%w[ee? jh?].each do |method_name|
it "delegates #{method_name} to GitlabEdition" do
expect(GitlabEdition).to receive(method_name)
2022-01-26 12:08:38 +05:30
2022-03-02 08:16:31 +05:30
cop.public_send(method_name)
2022-01-26 12:08:38 +05:30
end
end
2018-12-05 23:21:45 +05:30
end