Class: Crosstest::Psychic::FileFinder

Inherits:
Object
  • Object
show all
Defined in:
lib/crosstest/psychic/file_finder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(search_path, ignored_patterns) ⇒ FileFinder

Returns a new instance of FileFinder


6
7
8
9
# File 'lib/crosstest/psychic/file_finder.rb', line 6

def initialize(search_path, ignored_patterns)
  @search_path = search_path
  @ignored_patterns = ignored_patterns || read_gitignore(search_path)
end

Instance Attribute Details

#ignored_patternsObject (readonly)

Returns the value of attribute ignored_patterns


4
5
6
# File 'lib/crosstest/psychic/file_finder.rb', line 4

def ignored_patterns
  @ignored_patterns
end

#search_pathObject (readonly)

Returns the value of attribute search_path


4
5
6
# File 'lib/crosstest/psychic/file_finder.rb', line 4

def search_path
  @search_path
end

Instance Method Details

#find_file(name) ⇒ Object

Finds a file by loosely matching the file name to a scenario name


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/crosstest/psychic/file_finder.rb', line 12

def find_file(name)
  return name if File.exist? File.expand_path(name, search_path)

  # Filter out ignored files
  files = potential_files(name).select do |f|
    !ignored? f
  end

  if block_given?
    file = yield files
  else
    # Select the shortest path, likely the best match
    file = files.min_by(&:length)
  end

  fail Errno::ENOENT, "No file was found for #{name} within #{search_path}" if file.nil?
  Crosstest::Core::FileSystem.relativize(file, search_path)
end

#potential_files(name) ⇒ Object


31
32
33
34
35
36
37
# File 'lib/crosstest/psychic/file_finder.rb', line 31

def potential_files(name)
  slugified_name = Crosstest::Core::FileSystem.slugify(name)
  glob_string = "#{search_path}/**/*#{slugified_name}*.*"
  potential_files = Dir.glob(glob_string, File::FNM_CASEFOLD)
  potential_files.concat Dir.glob(glob_string.gsub('_', '-'), File::FNM_CASEFOLD)
  potential_files.concat Dir.glob(glob_string.gsub('_', ''), File::FNM_CASEFOLD)
end