Module: DeltaTest::Utils

Defined in:
lib/delta_test/utils.rb

Class Method Summary collapse

Class Method Details

.files_grep(files, patterns = [], exclude_patterns = []) ⇒ Array<T>

Wildcard pattern matching against a file list

Returns:

  • (Array<T>)


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/delta_test/utils.rb', line 52

def files_grep(files, patterns = [], exclude_patterns = [])
  patterns = patterns
    .map { |p| grep_pattern_to_regexp(p) }
  exclude_patterns = exclude_patterns
    .map { |p| grep_pattern_to_regexp(p) }

  any_patterns         = patterns.any?
  any_exclude_patterns = exclude_patterns.any?

  files.select do |file|
    matcher = ->(p) { p === file.to_s }

    (
      !any_patterns || patterns.any?(&matcher)
    ) && (
      !any_exclude_patterns || !exclude_patterns.any?(&matcher)
    )
  end
end

.find_file_upward(*file_names) ⇒ String

Find file upward from pwd

Returns:

  • (String)


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/delta_test/utils.rb', line 26

def find_file_upward(*file_names)
  pwd  = Dir.pwd
  base = Hash.new { |h, k| h[k] = pwd }
  file = {}

  while base.values.all? { |b| '.' != b && '/' != b }
    file_names.each do |name|
      file[name] = File.join(base[name], name)
      base[name] = File.dirname(base[name])

      return file[name] if File.exists?(file[name])
    end
  end

  nil
end

.regulate_filepath(file, base_path) ⇒ Pathname

Convert to relative and clean path

Returns:

  • (Pathname)


13
14
15
16
17
# File 'lib/delta_test/utils.rb', line 13

def regulate_filepath(file, base_path)
  file = Pathname.new(file)
  file = file.relative_path_from(base_path) rescue file
  file.cleanpath
end