Module: Crosstest::Core::FileSystem

Includes:
Util::String
Defined in:
lib/crosstest/core/file_system.rb

Class Method Summary collapse

Methods included from Util::String

included

Methods included from Util::String::ClassMethods

#ansi2html, #escape_html, #highlight, #slugify

Class Method Details

.find_file(search_path, scenario_name, ignored_patterns = nil) ⇒ Object

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



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/crosstest/core/file_system.rb', line 8

def find_file(search_path, scenario_name, ignored_patterns = nil)
  ignored_patterns ||= read_gitignore(search_path)
  glob_string = "#{search_path}/**/*#{slugify(scenario_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)

  # Filter out ignored filesFind the first file, not including generated files
  files = potential_files.select do |f|
    !ignored? ignored_patterns, search_path, f
  end

  # Select the shortest path, likely the best match
  file = files.min_by(&:length)

  fail Errno::ENOENT, "No file was found for #{scenario_name} within #{search_path}" if file.nil?
  Pathname.new file
end

.relativize(file, base_path) ⇒ Object



27
28
29
30
31
# File 'lib/crosstest/core/file_system.rb', line 27

def relativize(file, base_path)
  absolute_file = File.absolute_path(file)
  absolute_base_path = File.absolute_path(base_path)
  Pathname.new(absolute_file).relative_path_from Pathname.new(absolute_base_path)
end