Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 25 additions & 8 deletions Sources/ShellKit/Shell+Path.swift
Original file line number Diff line number Diff line change
Expand Up @@ -180,14 +180,13 @@ extension Shell {
url = URL(fileURLWithPath: path)
} else {
#if os(Windows)
if path.count >= 2,
let second = path.dropFirst().first, second == ":" {
// Drive-letter absolutes live outside the unix-style
// virtual namespace; under a mapping they fall through
// to the unmapped sentinel below.
let driveURL = URL(fileURLWithPath: path)
guard sandbox?.pathMapping != nil else { return driveURL }
return Self.voidedUnmappedPath(path)
if path.hasPrefix("\\") || path.dropFirst().first == ":" {
// Drive-letter (`C:\x`) and backslash-rooted (`\x`,
// `\\server\share`) absolutes live outside the
// unix-style virtual namespace; under a mapping they
// fall through to the unmapped sentinel below.
guard sandbox?.pathMapping == nil else { return Self.voidedUnmappedPath(path) }
return URL(fileURLWithPath: onShellDrive(path) ?? path)
}
#endif
let cwd = environment.workingDirectory
Expand Down Expand Up @@ -227,6 +226,24 @@ extension Shell {
return URL(fileURLWithPath: translated.host)
}

#if os(Windows)
/// `path` on the shell's drive when a backslash roots it at the
/// current drive's root (`\x`, not a `\\server\share` UNC path):
/// Windows takes the current drive for it, and the shell's is its
/// working directory's, which needn't be the process's. `nil` for
/// any other path, or when the working directory names no drive.
private func onShellDrive(_ path: String) -> String? {
guard path.hasPrefix("\\") else { return nil }
// A second separator makes it a UNC path.
if let second = path.dropFirst().first, second == "\\" || second == "/" { return nil }
let cwd = environment.workingDirectory
guard let letter = cwd.first, letter.isASCII, letter.isLetter,
cwd.dropFirst().first == ":"
else { return nil }
return String(cwd.prefix(2)) + path
}
#endif

/// Resolve `path` against ``current``'s working directory.
/// Equivalent to `Shell.current.resolve(path)`; provided for
/// call-site convenience.
Expand Down
37 changes: 37 additions & 0 deletions Tests/ShellKitTests/ShellTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,43 @@ import ShellCommandKit // Shell.register(_:) bridge
#expect(url.path == "/tmp/some-where/file.txt")
}

#if os(Windows)
@Test func resolveBackslashRootedPathsAsAbsolute() async throws {
var env = Environment()
// A drive the process is unlikely to be on.
env.workingDirectory = #"Q:\work"#
let shell = Shell(environment: env)
// Rooted by a backslash, as by a drive letter: not joined to the
// working directory. A UNC path or a drive keeps its own root…
for path in [#"\\server\share\repo.git"#, "//server/share/repo.git", #"C:\repo.git"#] {
#expect(shell.resolve(path) == URL(fileURLWithPath: path))
}
// …and the current drive's root is the shell's, not the process's.
#expect(shell.resolve(#"\repo.git"#) == URL(fileURLWithPath: #"Q:\repo.git"#))
#expect(shell.resolve(#"\"#) == URL(fileURLWithPath: #"Q:\"#))
// Without a drive to take (a unix-style working directory), the
// process's. `Environment()` itself starts in the process's
// directory, drive included.
var driveless = Environment()
driveless.workingDirectory = "/virtual/cwd"
#expect(Shell(environment: driveless).resolve(#"\repo.git"#)
== URL(fileURLWithPath: #"\repo.git"#))
// Outside the virtual namespace, so voided under a mapping.
shell.sandbox = .confined(
to: PathMapping(mounts: [.init(virtual: "/work", host: #"C:\work"#)]),
home: "/work")
#expect(shell.resolve(#"\\server\share\repo.git"#).path
.contains(Shell.unmappedPathSentinel))
}
#else
@Test func resolveTakesABackslashAsAnOrdinaryCharacter() async throws {
var env = Environment()
env.workingDirectory = "/tmp/some-where"
let shell = Shell(environment: env)
#expect(shell.resolve(#"\file.txt"#).path == #"/tmp/some-where/\file.txt"#)
}
#endif

@Test func resolveStaticHonoursCurrent() async throws {
var env = Environment()
env.workingDirectory = "/var/scope"
Expand Down
Loading