Module: Mutineer::ProjectPath
- Defined in:
- lib/mutineer/project_path.rb
Overview
Realpath-based path normalization against a project root. Shared by the
coverage cache (map keys) and mutant ids (the path hashed into each id), so
lib/x.rb, ./lib/x.rb, an absolute path and a path through a symlinked
root all resolve to the same key. Realpaths on both sides also absorb the
macOS /var vs /private/var alias.
Class Method Summary collapse
-
.absolute(path, root) ⇒ String
Expands
pathagainstroot, resolved to its real path when it exists. -
.relative(path, root) ⇒ String
Path of
pathrelative to the real project root. -
.root_real(root) ⇒ String
Canonical project root (
/varvs/private/var).
Instance Method Summary collapse
-
#absolute(path, root) ⇒ String
private
Expands
pathagainstroot, resolved to its real path when it exists. -
#relative(path, root) ⇒ String
private
Path of
pathrelative to the real project root. -
#root_real(root) ⇒ String
private
Canonical project root (
/varvs/private/var).
Class Method Details
.absolute(path, root) ⇒ String
Expands path against root, resolved to its real path when it exists.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/mutineer/project_path.rb', line 37 def absolute(path, root) path = FsPath.read(path) root = FsPath.read(root) # Join, don't expand: File.expand_path collapses `..` textually, before any # symlink is followed, so `link/../x.rb` would name the wrong file. The file # system resolves `..` physically in File.realpath. # A leading `~` names the home directory (as File.expand_path reads it), so # expand it rather than joining it under the root. raw = if File.absolute_path?(path) then path elsif path.b.start_with?("~".b) then File.(path) else File.(root).b + File::SEPARATOR.b + path.to_s.b end FsPath.read(File.exist?(raw) ? File.realpath(raw) : File.(raw)) end |
.relative(path, root) ⇒ String
Path of path relative to the real project root. A path outside the root
comes back as its absolute real path.
20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/mutineer/project_path.rb', line 20 def relative(path, root) abs = absolute(path, root) real_root = root_real(root) # Byte prefix: a non-UTF-8 file name is binary, and a non-ASCII root # is UTF-8. start_with? raises on that mix (#346). prefix = real_root.b prefix += "/".b unless prefix.end_with?("/".b) return abs unless FsPath.bytes_start_with?(abs, prefix) FsPath.bytes_delete_prefix(abs, prefix) end |
.root_real(root) ⇒ String
Canonical project root (/var vs /private/var). Any file system error
(missing, unreadable, a symlink loop) falls back to the expanded path, so
a caller such as the CLI's run-root warning never crashes on it.
58 59 60 61 62 |
# File 'lib/mutineer/project_path.rb', line 58 def root_real(root) FsPath.read(File.realpath(File.(root))) rescue SystemCallError FsPath.read(File.(root)) end |
Instance Method Details
#absolute(path, root) ⇒ String (private)
Expands path against root, resolved to its real path when it exists.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/mutineer/project_path.rb', line 37 def absolute(path, root) path = FsPath.read(path) root = FsPath.read(root) # Join, don't expand: File.expand_path collapses `..` textually, before any # symlink is followed, so `link/../x.rb` would name the wrong file. The file # system resolves `..` physically in File.realpath. # A leading `~` names the home directory (as File.expand_path reads it), so # expand it rather than joining it under the root. raw = if File.absolute_path?(path) then path elsif path.b.start_with?("~".b) then File.(path) else File.(root).b + File::SEPARATOR.b + path.to_s.b end FsPath.read(File.exist?(raw) ? File.realpath(raw) : File.(raw)) end |
#relative(path, root) ⇒ String (private)
Path of path relative to the real project root. A path outside the root
comes back as its absolute real path.
20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/mutineer/project_path.rb', line 20 def relative(path, root) abs = absolute(path, root) real_root = root_real(root) # Byte prefix: a non-UTF-8 file name is binary, and a non-ASCII root # is UTF-8. start_with? raises on that mix (#346). prefix = real_root.b prefix += "/".b unless prefix.end_with?("/".b) return abs unless FsPath.bytes_start_with?(abs, prefix) FsPath.bytes_delete_prefix(abs, prefix) end |
#root_real(root) ⇒ String (private)
Canonical project root (/var vs /private/var). Any file system error
(missing, unreadable, a symlink loop) falls back to the expanded path, so
a caller such as the CLI's run-root warning never crashes on it.
58 59 60 61 62 |
# File 'lib/mutineer/project_path.rb', line 58 def root_real(root) FsPath.read(File.realpath(File.(root))) rescue SystemCallError FsPath.read(File.(root)) end |