Class: SpecsFor::Finder

Inherits:
Object
  • Object
show all
Defined in:
lib/specs_for/finder.rb

Overview

Finder maps source files to their corresponding spec files.

Constant Summary collapse

SPEC_SUFFIX =
"_spec.rb"
SPEC_DIRS =

Default search paths for spec files, in priority order.

%w[spec test].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(spec_dirs: SPEC_DIRS) ⇒ Finder

Returns a new instance of Finder.



13
14
15
# File 'lib/specs_for/finder.rb', line 13

def initialize(spec_dirs: SPEC_DIRS)
  @spec_dirs = Array(spec_dirs)
end

Instance Attribute Details

#spec_dirsObject (readonly)

Returns the value of attribute spec_dirs.



11
12
13
# File 'lib/specs_for/finder.rb', line 11

def spec_dirs
  @spec_dirs
end

Instance Method Details

#candidates_for(file) ⇒ Array<String>

Compute candidate spec paths for a single file.

Parameters:

  • file (String)

Returns:

  • (Array<String>)


33
34
35
36
37
# File 'lib/specs_for/finder.rb', line 33

def candidates_for(file)
  return [file] if spec_file?(file)

  spec_dirs.flat_map { |dir| spec_paths_in(file, dir) }
end

#find(files) ⇒ Array<String>

Given a list of file paths, return the unique set of existing spec files that correspond to those paths.

Parameters:

  • files (Array<String>)

    source (or spec) file paths

Returns:

  • (Array<String>)

    sorted list of discovered spec file paths



22
23
24
25
26
27
# File 'lib/specs_for/finder.rb', line 22

def find(files)
  files.flat_map { |file| candidates_for(file) }
       .uniq
       .select { |f| File.exist?(f) }
       .sort
end

#spec_file?(file) ⇒ Boolean

Return true when file already looks like a spec file.

Returns:

  • (Boolean)


40
41
42
# File 'lib/specs_for/finder.rb', line 40

def spec_file?(file)
  file.end_with?(SPEC_SUFFIX)
end