module DeltaTest::Utils
Public Class Methods
files_grep(files, patterns = [], exclude_patterns = [])
click to toggle source
Wildcard pattern matching against a file list
@params {Array<T as String|Pathname>} files @params {Array<String>} patterns @params {Array<String>} exclude_patterns
@return {Array<T>}
# 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)
click to toggle source
Find file upward from pwd
@params {String} file_names
@return {String}
# 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)
click to toggle source
Convert to relative and clean path
@params {String|Pathname} file @params {Pathname} base_path
@return {Pathname}
# 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
Private Class Methods
grep_pattern_to_regexp(pattern)
click to toggle source
Convert file wildcard pattern to a regular expression
@params {String} pattern
@return {String}
# File lib/delta_test/utils.rb, line 82 def grep_pattern_to_regexp(pattern) pattern = Regexp.escape(pattern) .gsub('\*\*/', '.*/?') .gsub('\*\*', '.*') .gsub('\*', '[^/]*') Regexp.new('^%s$' % pattern) end