Skip to content

Commit

Permalink
Fix issues building when using Xcode Command Line Tools (no XCTest.) (#…
Browse files Browse the repository at this point in the history
…316)

This PR removes some stray dependencies on XCTest that prevent building
the testing library and its test target when XCTest is not available on
Darwin. This can occur if the Xcode Command Line Tools are installed
instead of the full IDE.

Automated testing for this change is difficult because it relies on a
build environment that is not supported in CI (namely the presence of
the CL tools but not Xcode nor XCTest.framework.) I have manually tested
the change against swift-testing's own test target.

For more information about this change, see
apple/swift-package-manager#7426. That PR
affects the Swift main-branch toolchain (Swift 6), not Swift 5.10 or
earlier.

Resolves rdar://125371899.

### Checklist:

- [x] Code and documentation should follow the style of the [Style
Guide](https://github.com/apple/swift-testing/blob/main/Documentation/StyleGuide.md).
- [x] If public symbols are renamed or modified, DocC references should
be updated.
  • Loading branch information
grynspan committed Mar 26, 2024
1 parent 0112a46 commit 705de54
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 18 deletions.
15 changes: 1 addition & 14 deletions Sources/Testing/Running/XCTestScaffold.swift
Expand Up @@ -8,7 +8,7 @@
// See https://swift.org/CONTRIBUTORS.txt for Swift project authors
//

#if !SWT_NO_XCTEST_SCAFFOLDING
#if !SWT_NO_XCTEST_SCAFFOLDING && canImport(XCTest)
private import TestingInternals
public import XCTest

Expand All @@ -25,19 +25,6 @@ extension XCTSourceCodeContext {
self.init(callStackAddresses: addresses, location: sourceLocation)
}
}

/// An error that is reported by ``XCTestScaffold`` when a test times out.
///
/// This type is not part of the public interface of the testing library.
struct TimeoutError: Error, CustomStringConvertible {
/// The time limit exceeded by the test that timed out.
var timeLimit: TimeValue

var description: String {
"Timed out after \(timeLimit) seconds."
}
}

extension XCTIssue {
init(_ issue: Issue, processLaunchedByXcode: Bool) {
var error = issue.error
Expand Down
12 changes: 12 additions & 0 deletions Sources/Testing/Traits/TimeLimitTrait.swift
Expand Up @@ -108,6 +108,18 @@ extension Test {

// MARK: -

/// An error that is reported by ``XCTestScaffold`` when a test times out.
///
/// This type is not part of the public interface of the testing library.
struct TimeoutError: Error, CustomStringConvertible {
/// The time limit exceeded by the test that timed out.
var timeLimit: TimeValue

var description: String {
"Timed out after \(timeLimit) seconds."
}
}

#if !SWT_NO_UNSTRUCTURED_TASKS
/// Invoke a function with a timeout.
///
Expand Down
2 changes: 1 addition & 1 deletion Tests/TestingMacrosTests/TestSupport/Scaffolding.swift
Expand Up @@ -8,7 +8,7 @@
// See https://swift.org/CONTRIBUTORS.txt for Swift project authors
//

#if !SWT_NO_XCTEST_SCAFFOLDING && !SWIFT_PM_SUPPORTS_SWIFT_TESTING
#if !SWT_NO_XCTEST_SCAFFOLDING && !SWIFT_PM_SUPPORTS_SWIFT_TESTING && canImport(XCTest)
import XCTest
import Testing

Expand Down
1 change: 1 addition & 0 deletions Tests/TestingTests/IssueTests.swift
Expand Up @@ -10,6 +10,7 @@

@testable @_spi(Experimental) @_spi(ForToolsIntegrationOnly) import Testing
private import TestingInternals
import Foundation

#if canImport(XCTest)
import XCTest
Expand Down
2 changes: 2 additions & 0 deletions Tests/TestingTests/ObjCInteropTests.swift
Expand Up @@ -8,6 +8,7 @@
See https://swift.org/CONTRIBUTORS.txt for Swift project authors
*/

#if canImport(XCTest)
import XCTest
@testable @_spi(ForToolsIntegrationOnly) import Testing

Expand Down Expand Up @@ -106,3 +107,4 @@ struct ObjCAndXCTestInteropTests {
}
}
}
#endif
2 changes: 2 additions & 0 deletions Tests/TestingTests/RunnerTests.swift
Expand Up @@ -10,6 +10,7 @@

#if canImport(XCTest)
import XCTest
#endif
@testable @_spi(Experimental) @_spi(ForToolsIntegrationOnly) import Testing

struct MyError: Error, Equatable {
Expand All @@ -35,6 +36,7 @@ private let randomNumber = Int.random(in: 0 ..< .max)
throw MyParameterizedError(index: i)
}

#if canImport(XCTest)
@Suite(.hidden, .disabled())
struct NeverRunTests {
private static var someCondition: Bool {
Expand Down
2 changes: 1 addition & 1 deletion Tests/TestingTests/TestSupport/Scaffolding.swift
Expand Up @@ -8,7 +8,7 @@
// See https://swift.org/CONTRIBUTORS.txt for Swift project authors
//

#if !SWT_NO_XCTEST_SCAFFOLDING && !SWIFT_PM_SUPPORTS_SWIFT_TESTING
#if !SWT_NO_XCTEST_SCAFFOLDING && !SWIFT_PM_SUPPORTS_SWIFT_TESTING && canImport(XCTest)
import XCTest
import Testing

Expand Down
2 changes: 0 additions & 2 deletions Tests/TestingTests/Traits/TimeLimitTraitTests.swift
Expand Up @@ -214,13 +214,11 @@ struct TimeLimitTraitTests {
}
}

#if !SWT_NO_XCTEST_SCAFFOLDING && SWT_TARGET_OS_APPLE
@Test("TimeoutError.description property")
func timeoutErrorDescription() async throws {
let timeLimit = TimeValue((0, 0))
#expect(String(describing: TimeoutError(timeLimit: timeLimit)).contains("0.000"))
}
#endif

@Test("Issue.Kind.timeLimitExceeded.description property",
arguments: [
Expand Down

0 comments on commit 705de54

Please sign in to comment.