Class: Mutante::SpecFinder

Inherits:
Object
  • Object
show all
Defined in:
lib/mutante/spec_finder.rb

Overview

Resolves which spec file(s) to run for a given source file.

Priority:

1. Explicit mapping from Configuration#test_mappings.
2. Mirrored path under spec/ (app/foo/bar.rb -> spec/foo/bar_spec.rb).
3. Nil, meaning "fall back to the whole suite".

Constant Summary collapse

PLACEHOLDER_RE =
/\{(basename|relative|relative_dir)\}/

Instance Method Summary collapse

Constructor Details

#initialize(configuration) ⇒ SpecFinder

Returns a new instance of SpecFinder.



11
12
13
14
# File 'lib/mutante/spec_finder.rb', line 11

def initialize(configuration)
  @configuration = configuration
  @root          = configuration.root
end

Instance Method Details

#call(source_file) ⇒ Object

Returns an Array of spec file paths (relative to the project root), or nil to indicate the whole test suite should be used.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/mutante/spec_finder.rb', line 18

def call(source_file)
  rel = relative(source_file)

  mapping = matching_mapping(rel)
  if mapping
    paths = expand(mapping, rel)
    existing = paths.select { |p| File.exist?(File.join(@root, p)) }
    return existing unless existing.empty?
  end

  mirror = mirrored_spec(rel)
  return [mirror] if mirror && File.exist?(File.join(@root, mirror))

  nil
end