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.

41 changes: 17 additions & 24 deletions Sources/Command/Command.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,18 +100,15 @@ public enum CommandEvent: Sendable {
public enum CommandError: Error, CustomStringConvertible, Sendable {
case terminated(Int32, stderr: String)
case signalled(Int32)
case errorObtainingExecutable(executable: String, error: String)
case executableNotFound(String)
case missingExecutableName

public var description: String {
switch self {
case let .signalled(code): return "The command terminated after receiving a signal with code \(code)"
case let .terminated(code, _): return "The command terminated with the code \(code)"
case let .errorObtainingExecutable(
name,
error
): return "There was an error trying to obtain the path to the executable '\(name)': \(error)"
case let .executableNotFound(name): return "Couldn't locate the executable '\(name)' in the environment."
case .missingExecutableName: return "The executable name is missing."
}
}
}
Expand Down Expand Up @@ -179,9 +176,7 @@ public struct CommandRunner: CommandRunning, Sendable {
let executable = try lookupExecutable(firstArgument: arguments.first)
process.executableURL = executable

if let executable {
logger?.debug("Running command: \(executable.absoluteString) \(processArguments.joined(separator: " "))")
}
logger?.debug("Running command: \(executable.absoluteString) \(processArguments.joined(separator: " "))")

let threadSafeProcess = ThreadSafe(process)

Expand Down Expand Up @@ -238,8 +233,10 @@ public struct CommandRunner: CommandRunning, Sendable {
}
}

func lookupExecutable(firstArgument: String?) throws -> URL? {
guard let firstArgument else { return nil }
func lookupExecutable(firstArgument: String?) throws -> URL {
guard let firstArgument else {
throw CommandError.missingExecutableName
}

// If the first argument is an absolute URL to an executable, return it.
if let executablePath = try? Path.AbsolutePath(validating: firstArgument) {
Expand All @@ -250,7 +247,7 @@ public struct CommandRunner: CommandRunning, Sendable {
let arguments: [String]

#if os(Windows)
command = "where"
command = "C:\\Windows\\System32\\where.exe"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch!

arguments = [firstArgument]
#else
command = "/usr/bin/which"
Expand All @@ -265,27 +262,23 @@ public struct CommandRunner: CommandRunning, Sendable {
let process = Process()
process.executableURL = URL(fileURLWithPath: command)
process.arguments = arguments
process.environment = ProcessInfo.processInfo.environment
process.currentDirectoryURL = URL(fileURLWithPath: FileManager.default.currentDirectoryPath)
Comment on lines +265 to +266
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wrongly assumed those values would be inherited 😅. Good catch @AndrewBarba


let pipe = Pipe()
process.standardOutput = pipe
process.standardError = pipe

do {
try process.run()
} catch {
let data = pipe.fileHandleForReading.readDataToEndOfFile()
let output = String(data: data, encoding: .utf8) ?? ""
throw CommandError.errorObtainingExecutable(executable: firstArgument, error: output)
}

let data = pipe.fileHandleForReading.readDataToEndOfFile()
try process.run()
process.waitUntilExit()

if let output = String(data: data, encoding: .utf8) {
let trimmedOutput = output.trimmingCharacters(in: .whitespacesAndNewlines)
return trimmedOutput.isEmpty ? nil : URL(fileURLWithPath: trimmedOutput)
let data = try pipe.fileHandleForReading.readToEnd()
let output = String(data: data ?? .init(), encoding: .utf8)?.trimmingCharacters(in: .whitespacesAndNewlines)

guard let output, !output.isEmpty else {
throw CommandError.executableNotFound(firstArgument)
}

return nil
return URL(fileURLWithPath: output)
}
}
18 changes: 11 additions & 7 deletions Tests/CommandTests/CommandTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ final class CommandTests: XCTestCase {
let executableURL = try commandRunner.lookupExecutable(firstArgument: absolutePath)

// Then
XCTAssertEqual(executableURL?.path, absolutePath)
XCTAssertEqual(executableURL.path, absolutePath)
}

func test_lookupExecutable_withRegularCommand() throws {
Expand All @@ -34,19 +34,23 @@ final class CommandTests: XCTestCase {
let executableURL = try commandRunner.lookupExecutable(firstArgument: command)

// Then
XCTAssertNotNil(executableURL)
XCTAssertTrue(executableURL!.path.hasSuffix("/\(command)"))
XCTAssertTrue(executableURL.path.hasSuffix("/\(command)"))
}

func test_lookupExecutable_withInvalidCommand() throws {
// Given
let commandRunner = CommandRunner()
let command = "nonexistentcommand"

// When
let executableURL = try commandRunner.lookupExecutable(firstArgument: command)
// When & Then
XCTAssertThrowsError(try commandRunner.lookupExecutable(firstArgument: command))
}

// Then
XCTAssertNil(executableURL)
func test_lookupExecutable_withMissingExecutableCommand() throws {
// Given
let commandRunner = CommandRunner()

// When & Then
XCTAssertThrowsError(try commandRunner.lookupExecutable(firstArgument: nil))
}
}