Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generalize findSwiftExecutable into findExecutable #436

Merged
merged 2 commits into from
May 17, 2024
Merged
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
37 changes: 30 additions & 7 deletions Tests/CartonCommandTests/CommandTestHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,42 @@

import ArgumentParser
import XCTest
import CartonHelpers

struct CommandTestError: Swift.Error & CustomStringConvertible {
init(_ description: String) {
self.description = description
}

var description: String
}

extension XCTest {
func findSwiftExecutable() throws -> String {
func findExecutable(name: String) throws -> AbsolutePath {
let whichBin = "/usr/bin/which"
let process = Process()
process.executableURL = URL(fileURLWithPath: "/usr/bin/which")
process.arguments = ["swift"]
process.executableURL = URL(fileURLWithPath: whichBin)
process.arguments = [name]
let output = Pipe()
process.standardOutput = output
try process.run()
process.waitUntilExit()
guard process.terminationStatus == EXIT_SUCCESS else {
throw CommandTestError("Executable \(name) was not found: status=\(process.terminationStatus)")
}
let outputData = output.fileHandleForReading.readDataToEndOfFile()
return String(data: outputData, encoding: .utf8)!.trimmingCharacters(
in: .whitespacesAndNewlines)
guard let string = String(data: outputData, encoding: .utf8) else {
throw CommandTestError("Output from \(whichBin) is not UTF-8 string")
}
let path = string.trimmingCharacters(in: .whitespacesAndNewlines)
guard !path.isEmpty else {
throw CommandTestError("Output from \(whichBin) is empty")
}
return try AbsolutePath(validating: path)
}

func findSwiftExecutable() throws -> AbsolutePath {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

String ではなく AbsolutePath を返すように変更しています。
その方がさまざまな CartonHelper の他のコードと一緒に使いやすいからです。

try findExecutable(name: "swift")
}

struct SwiftRunResult {
Expand All @@ -42,9 +65,9 @@ extension XCTest {
func swiftRunProcess(
_ arguments: [CustomStringConvertible],
packageDirectory: URL
) throws -> (Process, stdout: Pipe, stderr: Pipe) {
) throws -> (Foundation.Process, stdout: Pipe, stderr: Pipe) {
let process = Process()
process.executableURL = URL(fileURLWithPath: try findSwiftExecutable())
process.executableURL = try findSwiftExecutable().asURL
process.arguments = ["run"] + arguments.map(\.description)
process.currentDirectoryURL = packageDirectory
let stdoutPipe = Pipe()
Expand Down
Loading