Commit 09ad296e authored by Albert Salim's avatar Albert Salim

Add test file finder for ruby files

parent 119a1900
# frozen_string_literal: true
require 'ostruct'
module Quality
class TestFileFinder
RUBY_EXTENSION = '.rb'
EE_PREFIX = 'ee/'
def initialize(file, foss_test_only: false)
@file = file
@foss_test_only = foss_test_only
end
def test_files
contexts = [ee_context, foss_context]
contexts.flat_map do |context|
match_test_files_for(context)
end
end
private
attr_reader :file, :foss_test_only
def ee_context
OpenStruct.new.tap do |ee|
ee.app = %r{^#{EE_PREFIX}app/(.+)\.rb$} unless foss_test_only
ee.lib = %r{^#{EE_PREFIX}lib/(.+)\.rb$} unless foss_test_only
ee.spec_dir = "#{EE_PREFIX}spec" unless foss_test_only
ee.ee_modules = %r{^#{EE_PREFIX}(.*\/)ee/(.+)\.rb$}
ee.foss_spec_dir = 'spec'
end
end
def foss_context
OpenStruct.new.tap do |foss|
foss.app = %r{^app/(.+)\.rb$}
foss.lib = %r{^lib/(.+)\.rb$}
foss.spec_dir = 'spec'
end
end
def match_test_files_for(context)
test_files = []
if (match = context.app&.match(file))
test_files << "#{context.spec_dir}/#{match[1]}_spec.rb"
end
if (match = context.lib&.match(file))
test_files << "#{context.spec_dir}/lib/#{match[1]}_spec.rb"
end
if (match = context.ee_modules&.match(file))
test_files << "#{context.foss_spec_dir}/#{match[1]}#{match[2]}_spec.rb"
end
test_files
end
end
end
# 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' }
it_behaves_like 'not finding a matching test file'
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/lib/banzai/color_parser_spec.rb' }
it_behaves_like 'not finding a matching test file'
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
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