Skip to content

Make computeXCTestMinimumDeploymentTarget() more robust #2729

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

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
11 changes: 8 additions & 3 deletions Sources/PackageLoading/MinimumDeploymentTarget.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ public struct MinimumDeploymentTarget {
return PlatformVersion(versionString)
}

static func computeXCTestMinimumDeploymentTarget(with runResult: ProcessResult) throws -> PlatformVersion? {
guard let output = try runResult.utf8Output().spm_chuzzle() else { return nil }
let sdkPath = try AbsolutePath(validating: output)
let xcTestPath = sdkPath.appending(RelativePath("Developer/Library/Frameworks/XCTest.framework/XCTest"))
return try computeMinimumDeploymentTarget(of: xcTestPath)
}

static func computeXCTestMinimumDeploymentTarget(for platform: PackageModel.Platform) -> PlatformVersion {
guard let sdkName = platform.sdkName else {
return platform.oldestSupportedVersion
Expand All @@ -39,10 +46,8 @@ public struct MinimumDeploymentTarget {
#if os(macOS)
do {
let runResult = try Process.popen(arguments: ["xcrun", "--sdk", sdkName, "--show-sdk-platform-path"])
let sdkPath = AbsolutePath(try runResult.utf8Output().spm_chuzzle() ?? "")
let xcTestPath = sdkPath.appending(RelativePath("Developer/Library/Frameworks/XCTest.framework/XCTest"))

if let version = try computeMinimumDeploymentTarget(of: xcTestPath) {
if let version = try computeXCTestMinimumDeploymentTarget(with: runResult) {
return version
}
} catch { } // we do not treat this a fatal and instead use the fallback minimum deployment target
Expand Down
57 changes: 57 additions & 0 deletions Tests/PackageLoadingTests/MinimumDeploymentTargetTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
This source file is part of the Swift.org open source project

Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
Licensed under Apache License v2.0 with Runtime Library Exception

See http://swift.org/LICENSE.txt for license information
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
*/

import TSCBasic
import XCTest

@testable import PackageLoading

class MinimumDeploymentTargetTests: XCTestCase {
#if os(macOS) // these tests eventually call `xcrun`.
func testDoesNotAssertWithNoOutput() throws {
let result = ProcessResult(arguments: [],
environment: [:],
exitStatus: .terminated(code: 0),
output: "".asResult,
stderrOutput: "xcodebuild: error: SDK \"macosx\" cannot be located.".asResult)

XCTAssertNil(try MinimumDeploymentTarget.computeXCTestMinimumDeploymentTarget(with: result))
}

func testThrowsWithNonPathOutput() throws {
let result = ProcessResult(arguments: [],
environment: [:],
exitStatus: .terminated(code: 0),
output: "some string".asResult,
stderrOutput: "".asResult)

XCTAssertThrowsError(try MinimumDeploymentTarget.computeXCTestMinimumDeploymentTarget(with: result))
}

func testThrowsWithErrorForOutput() throws {
let result = ProcessResult(arguments: [],
environment: [:],
exitStatus: .terminated(code: 0),
output: .failure(DummyError()),
stderrOutput: "".asResult)

XCTAssertThrowsError(try MinimumDeploymentTarget.computeXCTestMinimumDeploymentTarget(with: result))
}
#endif
}

private struct DummyError: Error {
}

private extension String {
var asResult: Result<[UInt8], Error> {
return .success(Array(utf8))
}
}