Commit 8c71cbde authored by Albert Salim's avatar Albert Salim

Move TestFileFinder into tooling

parent cdaa892d
......@@ -339,7 +339,7 @@ rspec foss-impact:
- run_timed_command "scripts/gitaly-test-build"
- run_timed_command "scripts/gitaly-test-spawn"
- source scripts/rspec_helpers.sh
- scripts/find_foss_tests tmp/matching_foss_tests.txt
- tooling/bin/find_foss_tests tmp/matching_foss_tests.txt
- rspec_simple_job "--tag ~quarantine --tag ~geo --tag ~level:migration $(cat tmp/matching_foss_tests.txt)"
artifacts:
expire_in: 7d
......
......@@ -13,4 +13,6 @@ require 'active_support/all'
ActiveSupport::Dependencies.autoload_paths << 'lib'
ActiveSupport::Dependencies.autoload_paths << 'ee/lib'
ActiveSupport::Dependencies.autoload_paths << 'tooling/lib'
ActiveSupport::XmlMini.backend = 'Nokogiri'
# frozen_string_literal: true
require 'fast_spec_helper'
describe Quality::TestFileFinder do
let(:file) { 'app/finders/admin/projects_finder.rb' }
let(:test_files) { ['spec/finders/admin/projects_finder_spec.rb'] }
subject { Quality::TestFileFinder.new(file) }
shared_examples 'finding matching test files' do
it 'returns matching test files' do
expect(subject.test_files).to match_array(test_files)
end
end
shared_examples 'not finding a matching test file' do
it 'returns empty array' do
expect(subject.test_files).to be_empty
end
end
describe '#test_files' do
it_behaves_like 'finding matching test files'
context 'when given non .rb files' do
let(:file) { 'app/assets/images/emoji.png' }
it_behaves_like 'not finding a matching test file'
end
context 'when given file in app/' do
let(:file) { 'app/finders/admin/projects_finder.rb' }
let(:test_files) { ['spec/finders/admin/projects_finder_spec.rb'] }
it_behaves_like 'finding matching test files'
end
context 'when given file in lib/' do
let(:file) { 'lib/banzai/color_parser.rb' }
let(:test_files) { ['spec/lib/banzai/color_parser_spec.rb'] }
it_behaves_like 'finding matching test files'
end
context 'when given a test file' do
let(:file) { 'spec/lib/banzai/color_parser_spec.rb' }
let(:test_files) { ['spec/lib/banzai/color_parser_spec.rb'] }
it_behaves_like 'finding matching test files'
end
context 'when given an ee app file' do
let(:file) { 'ee/app/models/analytics/cycle_analytics/group_level.rb' }
let(:test_files) { ['ee/spec/models/analytics/cycle_analytics/group_level_spec.rb'] }
it_behaves_like 'finding matching test files'
end
context 'when given an ee module file' do
let(:file) { 'ee/app/models/ee/user.rb' }
let(:test_files) { ['spec/app/models/user_spec.rb', 'ee/spec/models/ee/user_spec.rb'] }
it_behaves_like 'finding matching test files'
end
context 'when given an ee lib file' do
let(:file) { 'ee/lib/flipper_session.rb' }
let(:test_files) { ['ee/spec/lib/flipper_session_spec.rb'] }
it_behaves_like 'finding matching test files'
end
context 'when given an ee test file' do
let(:file) { 'ee/spec/models/container_registry/event_spec.rb' }
let(:test_files) { ['ee/spec/models/container_registry/event_spec.rb'] }
it_behaves_like 'finding matching test files'
end
context 'when given an ee module test file' do
let(:file) { 'ee/spec/models/ee/appearance_spec.rb' }
let(:test_files) { ['ee/spec/models/ee/appearance_spec.rb', 'spec/models/appearance_spec.rb'] }
it_behaves_like 'finding matching test files'
end
context 'with foss_test_only: true' do
let(:file) { 'ee/app/models/ee/user.rb' }
let(:test_files) { ['spec/app/models/user_spec.rb'] }
subject { Quality::TestFileFinder.new(file, foss_test_only: true) }
it 'excludes matching ee test files' do
expect(subject.test_files).to match_array(test_files)
end
end
end
end
# frozen_string_literal: true
require 'fast_spec_helper'
describe Tooling::TestFileFinder do
subject { Tooling::TestFileFinder.new(file) }
describe '#test_files' do
context 'when given non .rb files' do
let(:file) { 'app/assets/images/emoji.png' }
it 'does not return a test file' do
expect(subject.test_files).to be_empty
end
end
context 'when given file in app/' do
let(:file) { 'app/finders/admin/projects_finder.rb' }
it 'returns the matching app spec file' do
expect(subject.test_files).to contain_exactly('spec/finders/admin/projects_finder_spec.rb')
end
end
context 'when given file in lib/' do
let(:file) { 'lib/banzai/color_parser.rb' }
it 'returns the matching app spec file' do
expect(subject.test_files).to contain_exactly('spec/lib/banzai/color_parser_spec.rb')
end
end
context 'when given a file in tooling/' do
let(:file) { 'tooling/lib/quality/test_file_finder.rb' }
it 'returns the matching tooling test' do
expect(subject.test_files).to contain_exactly('spec/tooling/lib/quality/test_file_finder_spec.rb')
end
end
context 'when given a test file' do
let(:file) { 'spec/lib/banzai/color_parser_spec.rb' }
it 'returns the matching test file itself' do
expect(subject.test_files).to contain_exactly('spec/lib/banzai/color_parser_spec.rb')
end
end
context 'when given an app file in ee/' do
let(:file) { 'ee/app/models/analytics/cycle_analytics/group_level.rb' }
it 'returns the matching ee/ test file' do
expect(subject.test_files).to contain_exactly('ee/spec/models/analytics/cycle_analytics/group_level_spec.rb')
end
end
context 'when given a module file in ee/' do
let(:file) { 'ee/app/models/ee/user.rb' }
it 'returns the matching ee/ module test file and the ee/ model test file' do
test_files = ['ee/spec/models/ee/user_spec.rb', 'spec/app/models/user_spec.rb']
expect(subject.test_files).to contain_exactly(*test_files)
end
end
context 'when given a lib file in ee/' do
let(:file) { 'ee/lib/flipper_session.rb' }
it 'returns the matching ee/ lib test file' do
expect(subject.test_files).to contain_exactly('ee/spec/lib/flipper_session_spec.rb')
end
end
context 'when given a test file in ee/' do
let(:file) { 'ee/spec/models/container_registry/event_spec.rb' }
it 'returns the test file itself' do
expect(subject.test_files).to contain_exactly('ee/spec/models/container_registry/event_spec.rb')
end
end
context 'when given a module test file in ee/' do
let(:file) { 'ee/spec/models/ee/appearance_spec.rb' }
it 'returns the matching module test file itself and the corresponding spec model test file' do
test_files = ['ee/spec/models/ee/appearance_spec.rb', 'spec/models/appearance_spec.rb']
expect(subject.test_files).to contain_exactly(*test_files)
end
end
context 'with foss_test_only: true' do
subject { Tooling::TestFileFinder.new(file, foss_test_only: true) }
context 'when given a module file in ee/' do
let(:file) { 'ee/app/models/ee/user.rb' }
it 'returns only the corresponding spec model test file in foss' do
expect(subject.test_files).to contain_exactly('spec/app/models/user_spec.rb')
end
end
context 'when given an app file in ee/' do
let(:file) { 'ee/app/models/approval.rb' }
it 'returns no test file in foss' do
expect(subject.test_files).to be_empty
end
end
end
end
end
#!/usr/bin/env ruby
# frozen_string_literal: true
require_relative '../lib/gitlab/popen'
require_relative '../lib/quality/test_file_finder'
require_relative '../../lib/gitlab/popen'
require_relative '../lib/tooling/test_file_finder'
output_file = ARGV.shift
......@@ -19,7 +20,7 @@ end
changed_files = git_diff.stdout.split("\n")
tests_to_run = changed_files.flat_map do |file|
test_files = Quality::TestFileFinder.new(file, foss_test_only: true).test_files
test_files = Tooling::TestFileFinder.new(file, foss_test_only: true).test_files
test_files.select { |f| File.exist?(f) }
end
......
......@@ -3,7 +3,7 @@
require 'ostruct'
require 'set'
module Quality
module Tooling
class TestFileFinder
RUBY_EXTENSION = '.rb'
EE_PREFIX = 'ee/'
......@@ -43,6 +43,7 @@ module Quality
OpenStruct.new.tap do |foss|
foss.app = %r{^app/(.+)\.rb$}
foss.lib = %r{^lib/(.+)\.rb$}
foss.tooling = %r{^(tooling/lib/.+)\.rb$}
foss.spec = %r{^spec/(.+)_spec.rb$}
foss.spec_dir = 'spec'
end
......@@ -57,6 +58,10 @@ module Quality
result << "#{context.spec_dir}/lib/#{match[1]}_spec.rb"
end
if (match = context.tooling&.match(file))
result << "#{context.spec_dir}/#{match[1]}_spec.rb"
end
if context.spec&.match(file)
result << file
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