Module: FastCov::Utils

Defined in:
lib/fast_cov/utils.rb

Class Method Summary collapse

Class Method Details

.path_within?(path, directory) ⇒ Boolean

Check if path is within directory, correctly handling:

  • Trailing slashes on directory

  • Sibling directories with longer names (e.g., /a/b/c vs /a/b/cd)

Returns:

  • (Boolean)


8
9
10
11
12
13
# File 'lib/fast_cov/utils.rb', line 8

def self.path_within?(path, directory)
  dir = directory.end_with?("/") ? directory.chop : directory
  return true if path == dir

  path.start_with?("#{dir}/")
end

.relativize_paths(set, root) ⇒ Object

Mutates set in place: converts absolute paths to relative paths from root. Paths not within root are left unchanged.



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/fast_cov/utils.rb', line 17

def self.relativize_paths(set, root)
  prefix = root.end_with?("/") ? root : "#{root}/"

  set.to_a.each do |abs_path|
    next unless abs_path.is_a?(String)
    next unless abs_path.start_with?(prefix) || abs_path == root.chomp("/")

    set.delete(abs_path)
    set.add(abs_path.delete_prefix(prefix))
  end

  set
end

.resolve_caller(locations, root) ⇒ Object

Walk caller locations to find the first frame whose file is within root. Handles indirect calls (e.g., YAML.load_file -> File.open) where the immediate caller is a stdlib/gem file outside the project.



34
35
36
37
38
39
40
41
42
# File 'lib/fast_cov/utils.rb', line 34

def self.resolve_caller(locations, root)
  locations.each do |loc|
    path = loc.absolute_path
    next unless path

    return path if path_within?(path, root)
  end
  nil
end