class TestFileFinder::FileFinder def initialize(paths: []) @paths = [paths].flatten end def test_files search end private attr_reader :paths def search file_path_guesses.select { |path| File.exist?(path) } end def file_path_guesses paths.map do |path| guess_test_files_for(path) end.compact.uniq end def context { app: %r{^app/(.+)\.rb$}, lib: %r{^lib/(.+)\.rb$}, spec: %r{^spec/(.+)_spec.rb$}, spec_dir: 'spec', } end def guess_test_files_for(path) if (match = context[:app]&.match(path)) return "#{context[:spec_dir]}/#{match[1]}_spec.rb" end if (match = context[:lib]&.match(path)) return "#{context[:spec_dir]}/lib/#{match[1]}_spec.rb" end if context[:spec]&.match(path) return path end end end