Commit 6b350a14 authored by Thong Kuah's avatar Thong Kuah

Convert more rubocop specs to fast_spec_helper

Replace Rails.root with manual directory as we don't require rails in
fast_spec_helper.
parent 01682e0e
# frozen_string_literal: true
require 'spec_helper'
require 'fast_spec_helper'
require 'rubocop'
require 'parser/current'
require_relative '../../rubocop/code_reuse_helpers'
......@@ -69,13 +69,13 @@ RSpec.describe RuboCop::CodeReuseHelpers do
describe '#in_finder?' do
it 'returns true for a node in the finders directory' do
node = parse_source('10', Rails.root.join('app', 'finders', 'foo.rb'))
node = parse_source('10', rails_root_join('app', 'finders', 'foo.rb'))
expect(cop.in_finder?(node)).to eq(true)
end
it 'returns false for a node outside the finders directory' do
node = parse_source('10', Rails.root.join('app', 'foo', 'foo.rb'))
node = parse_source('10', rails_root_join('app', 'foo', 'foo.rb'))
expect(cop.in_finder?(node)).to eq(false)
end
......@@ -83,13 +83,13 @@ RSpec.describe RuboCop::CodeReuseHelpers do
describe '#in_model?' do
it 'returns true for a node in the models directory' do
node = parse_source('10', Rails.root.join('app', 'models', 'foo.rb'))
node = parse_source('10', rails_root_join('app', 'models', 'foo.rb'))
expect(cop.in_model?(node)).to eq(true)
end
it 'returns false for a node outside the models directory' do
node = parse_source('10', Rails.root.join('app', 'foo', 'foo.rb'))
node = parse_source('10', rails_root_join('app', 'foo', 'foo.rb'))
expect(cop.in_model?(node)).to eq(false)
end
......@@ -97,13 +97,13 @@ RSpec.describe RuboCop::CodeReuseHelpers do
describe '#in_service_class?' do
it 'returns true for a node in the services directory' do
node = parse_source('10', Rails.root.join('app', 'services', 'foo.rb'))
node = parse_source('10', rails_root_join('app', 'services', 'foo.rb'))
expect(cop.in_service_class?(node)).to eq(true)
end
it 'returns false for a node outside the services directory' do
node = parse_source('10', Rails.root.join('app', 'foo', 'foo.rb'))
node = parse_source('10', rails_root_join('app', 'foo', 'foo.rb'))
expect(cop.in_service_class?(node)).to eq(false)
end
......@@ -111,13 +111,13 @@ RSpec.describe RuboCop::CodeReuseHelpers do
describe '#in_presenter?' do
it 'returns true for a node in the presenters directory' do
node = parse_source('10', Rails.root.join('app', 'presenters', 'foo.rb'))
node = parse_source('10', rails_root_join('app', 'presenters', 'foo.rb'))
expect(cop.in_presenter?(node)).to eq(true)
end
it 'returns false for a node outside the presenters directory' do
node = parse_source('10', Rails.root.join('app', 'foo', 'foo.rb'))
node = parse_source('10', rails_root_join('app', 'foo', 'foo.rb'))
expect(cop.in_presenter?(node)).to eq(false)
end
......@@ -125,13 +125,13 @@ RSpec.describe RuboCop::CodeReuseHelpers do
describe '#in_serializer?' do
it 'returns true for a node in the serializers directory' do
node = parse_source('10', Rails.root.join('app', 'serializers', 'foo.rb'))
node = parse_source('10', rails_root_join('app', 'serializers', 'foo.rb'))
expect(cop.in_serializer?(node)).to eq(true)
end
it 'returns false for a node outside the serializers directory' do
node = parse_source('10', Rails.root.join('app', 'foo', 'foo.rb'))
node = parse_source('10', rails_root_join('app', 'foo', 'foo.rb'))
expect(cop.in_serializer?(node)).to eq(false)
end
......@@ -139,13 +139,13 @@ RSpec.describe RuboCop::CodeReuseHelpers do
describe '#in_worker?' do
it 'returns true for a node in the workers directory' do
node = parse_source('10', Rails.root.join('app', 'workers', 'foo.rb'))
node = parse_source('10', rails_root_join('app', 'workers', 'foo.rb'))
expect(cop.in_worker?(node)).to eq(true)
end
it 'returns false for a node outside the workers directory' do
node = parse_source('10', Rails.root.join('app', 'foo', 'foo.rb'))
node = parse_source('10', rails_root_join('app', 'foo', 'foo.rb'))
expect(cop.in_worker?(node)).to eq(false)
end
......@@ -153,13 +153,13 @@ RSpec.describe RuboCop::CodeReuseHelpers do
describe '#in_api?' do
it 'returns true for a node in the API directory' do
node = parse_source('10', Rails.root.join('lib', 'api', 'foo.rb'))
node = parse_source('10', rails_root_join('lib', 'api', 'foo.rb'))
expect(cop.in_api?(node)).to eq(true)
end
it 'returns false for a node outside the API directory' do
node = parse_source('10', Rails.root.join('lib', 'foo', 'foo.rb'))
node = parse_source('10', rails_root_join('lib', 'foo', 'foo.rb'))
expect(cop.in_api?(node)).to eq(false)
end
......@@ -167,21 +167,21 @@ RSpec.describe RuboCop::CodeReuseHelpers do
describe '#in_directory?' do
it 'returns true for a directory in the CE app/ directory' do
node = parse_source('10', Rails.root.join('app', 'models', 'foo.rb'))
node = parse_source('10', rails_root_join('app', 'models', 'foo.rb'))
expect(cop.in_directory?(node, 'models')).to eq(true)
end
it 'returns true for a directory in the EE app/ directory' do
node =
parse_source('10', Rails.root.join('ee', 'app', 'models', 'foo.rb'))
parse_source('10', rails_root_join('ee', 'app', 'models', 'foo.rb'))
expect(cop.in_directory?(node, 'models')).to eq(true)
end
it 'returns false for a directory in the lib/ directory' do
node =
parse_source('10', Rails.root.join('lib', 'models', 'foo.rb'))
parse_source('10', rails_root_join('lib', 'models', 'foo.rb'))
expect(cop.in_directory?(node, 'models')).to eq(false)
end
......
# frozen_string_literal: true
require 'spec_helper'
require 'fast_spec_helper'
require 'rubocop'
require 'rubocop/rspec/support'
require_relative '../../../../rubocop/cop/code_reuse/active_record'
RSpec.describe RuboCop::Cop::CodeReuse::ActiveRecord do
RSpec.describe RuboCop::Cop::CodeReuse::ActiveRecord, type: :rubocop do
include CopHelper
subject(:cop) { described_class.new }
......@@ -46,7 +45,7 @@ RSpec.describe RuboCop::Cop::CodeReuse::ActiveRecord do
end
it 'does not flag the use of ActiveRecord models in a model' do
path = Rails.root.join('app', 'models', 'foo.rb').to_s
path = rails_root_join('app', 'models', 'foo.rb').to_s
expect_no_offenses(<<~SOURCE, path)
def foo
......@@ -56,7 +55,7 @@ RSpec.describe RuboCop::Cop::CodeReuse::ActiveRecord do
end
it 'does not flag the use of ActiveRecord models in a spec' do
path = Rails.root.join('spec', 'foo_spec.rb').to_s
path = rails_root_join('spec', 'foo_spec.rb').to_s
expect_no_offenses(<<~SOURCE, path)
def foo
......@@ -66,10 +65,7 @@ RSpec.describe RuboCop::Cop::CodeReuse::ActiveRecord do
end
it 'does not flag the use of ActiveRecord models in a background migration' do
path = Rails
.root
.join('lib', 'gitlab', 'background_migration', 'foo.rb')
.to_s
path = rails_root_join('lib', 'gitlab', 'background_migration', 'foo.rb').to_s
expect_no_offenses(<<~SOURCE, path)
def foo
......@@ -79,7 +75,7 @@ RSpec.describe RuboCop::Cop::CodeReuse::ActiveRecord do
end
it 'does not flag the use of ActiveRecord models in lib/gitlab/database' do
path = Rails.root.join('lib', 'gitlab', 'database', 'foo.rb').to_s
path = rails_root_join('lib', 'gitlab', 'database', 'foo.rb').to_s
expect_no_offenses(<<~SOURCE, path)
def foo
......
# frozen_string_literal: true
require 'spec_helper'
require 'fast_spec_helper'
require 'rubocop'
require 'parser/current'
require_relative '../../rubocop/qa_helpers'
RSpec.describe RuboCop::QAHelpers do
RSpec.describe RuboCop::QAHelpers, type: :rubocop do
def parse_source(source, path = 'foo.rb')
buffer = Parser::Source::Buffer.new(path)
buffer.source = source
......@@ -23,13 +24,13 @@ RSpec.describe RuboCop::QAHelpers do
describe '#in_qa_file?' do
it 'returns true for a node in the qa/ directory' do
node = parse_source('10', Rails.root.join('qa', 'qa', 'page', 'dashboard', 'groups.rb'))
node = parse_source('10', rails_root_join('qa', 'qa', 'page', 'dashboard', 'groups.rb'))
expect(cop.in_qa_file?(node)).to eq(true)
end
it 'returns false for a node outside the qa/ directory' do
node = parse_source('10', Rails.root.join('app', 'foo', 'foo.rb'))
node = parse_source('10', rails_root_join('app', 'foo', 'foo.rb'))
expect(cop.in_qa_file?(node)).to eq(false)
end
......
# frozen_string_literal: true
# For specs which don't load Rails, provide a path to Rails root
module FastRailsRoot
RAILS_ROOT = File.absolute_path("#{__dir__}/../../..")
def rails_root_join(*args)
File.join(RAILS_ROOT, *args)
end
end
......@@ -4,6 +4,7 @@ require_relative "helpers/stub_configuration"
require_relative "helpers/stub_metrics"
require_relative "helpers/stub_object_storage"
require_relative "helpers/stub_env"
require_relative "helpers/fast_rails_root"
require 'rubocop/rspec/support'
RSpec.configure do |config|
......@@ -14,6 +15,7 @@ RSpec.configure do |config|
config.include StubMetrics
config.include StubObjectStorage
config.include StubENV
config.include FastRailsRoot
config.include RuboCop::RSpec::ExpectOffense, type: :rubocop
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