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