Module: Polytrix::Util::FileSystem

Instance Method Summary collapse

Methods included from String

included

Methods included from String::ClassMethods

#ansi2html, #escape_html, #highlight, #slugify

Instance 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



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/polytrix/util.rb', line 199

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



218
219
220
221
222
# File 'lib/polytrix/util.rb', line 218

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