我正在用 Swift 开发一个命令行应用程序,它将调用swift
以初始化包、编译、测试等。我正在使用 swift 包管理器和 Xcode。在我的工具中,我swift
从Process
. 如果我从命令行运行我的工具,这工作正常。但是,如果我从 Xcode 运行我的工具,则会将一些奇怪的东西打印到标准错误中。特别是,我得到
Failed to open macho file at /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift for reading: Too many levels of symbolic links
这是什么原因造成的?有什么方法可以避免它,或者我可以不从 Xcode 运行/测试这个实用程序吗?
以下是复制步骤:
mkdir XcodeRedirectionTest
cd XcodeRedirectionTest
swift package init
open Package.swift
然后在Tests/XcodeRedirectionTestTests/XcodeRedirectionTestTests.swift
,粘贴
import XCTest
final class XcodeRedirectionTestTests: XCTestCase {
func testExample() {
let process = Process()
process.launchPath = "/usr/bin/swift"
process.arguments = ["--version"]
let standardOutput = Pipe()
let standardError = Pipe()
process.standardOutput = standardOutput
process.standardError = standardError
process.launch()
process.waitUntilExit()
guard let output = String(data: standardOutput.fileHandleForReading.readDataToEndOfFile(), encoding: .utf8) else { XCTFail(); return }
guard let error = String(data: standardError.fileHandleForReading.readDataToEndOfFile(), encoding: .utf8) else { XCTFail(); return }
XCTAssertEqual(process.terminationReason, .exit)
XCTAssertEqual(process.terminationStatus, 0)
XCTAssertEqual(error, "")
XCTAssert(output.starts(with: "Apple Swift version 5.4"))
}
}
Xcode 中的测试失败
file://.../XcodeRedirectionTest/Tests/XcodeRedirectionTestTests/XcodeRedirectionTestTests.swift: test failure: XcodeRedirectionTestTests.testExample() failed: XCTAssertEqual failed: ("2021-05-18 19:43:18.084463-0400 swift[2836:4100702] Failed to open macho file at /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift for reading: Too many levels of symbolic links
") is not equal to ("")
也就是说(所以你不必滚动(尽可能多)),标准错误最终是
Failed to open macho file at /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift for reading: Too many levels of symbolic links
但是,swift test
从命令行运行成功。没有标准错误!
如果重要的话,我会在 macOS Big Sur 11.2.3 上运行 Xcode 12.5。