Sha256: 9b985f907acc6fd451d6f3cbce99f8996f03bac0a32b84e614ad52fecebb651f
Contents?: true
Size: 1.78 KB
Versions: 3
Compression:
Stored size: 1.78 KB
Contents
module TestFileFinder module MappingStrategies class PatternMatching attr_reader :pattern_matchers def self.load(mapping_file) content = File.read(mapping_file) maps = YAML.load(content)['mapping'] validate(maps) new do |mapping| maps.each do |map| source = map['source'] test = map['test'] mapping.relate(source, test) end end end def self.validate(maps) raise InvalidMappingFileError, 'missing `mapping` in test mapping file' if maps.nil? raise InvalidMappingFileError, 'missing `source` or `test` in test mapping file' if maps.any? { |map| incomplete?(map) } end def self.incomplete?(map) map['source'].nil? || map['test'].nil? end def self.default_rails_mapping new do |mapping| mapping.relate(%r{^app/(.+)\.rb$}, 'spec/%s_spec.rb') mapping.relate(%r{^lib/(.+)\.rb$}, 'spec/lib/%s_spec.rb') mapping.relate(%r{^spec/(.+)_spec.rb$}, 'spec/%s_spec.rb') end end def initialize @pattern_matchers = [] yield self if block_given? end def relate(source, test) @pattern_matchers << pattern_matcher_for(source, test) end def match(files) @pattern_matchers.inject(Set.new) do |result, pattern_matcher| test_files = pattern_matcher.call(files) result.merge(test_files) end.to_a end private def pattern_matcher_for(source, test) regexp = %r{^#{source}$} proc do |files| Array(files).flat_map do |file| if (match = regexp.match(file)) test % match.captures end end.compact end end end end end
Version data entries
3 entries across 3 versions & 1 rubygems