From 09eeadfd75dcc36efdff792a4ebe59c8c60d69f1 Mon Sep 17 00:00:00 2001 From: Jonathan Grynspan Date: Mon, 25 Mar 2024 19:41:39 -0400 Subject: [PATCH 1/2] Fix issues building when using Xcode Command Line Tools (no XCTest.) 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 https://github.com/apple/swift-package-manager/pull/7426 Resolves rdar://125371899. --- Sources/Testing/Running/XCTestScaffold.swift | 15 +-------------- Sources/Testing/Traits/TimeLimitTrait.swift | 12 ++++++++++++ .../TestSupport/Scaffolding.swift | 2 +- Tests/TestingTests/IssueTests.swift | 1 + Tests/TestingTests/ObjCInteropTests.swift | 2 ++ Tests/TestingTests/RunnerTests.swift | 2 ++ Tests/TestingTests/TestSupport/Scaffolding.swift | 2 +- 7 files changed, 20 insertions(+), 16 deletions(-) diff --git a/Sources/Testing/Running/XCTestScaffold.swift b/Sources/Testing/Running/XCTestScaffold.swift index 972581ec3..dd6362a86 100644 --- a/Sources/Testing/Running/XCTestScaffold.swift +++ b/Sources/Testing/Running/XCTestScaffold.swift @@ -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 @@ -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 diff --git a/Sources/Testing/Traits/TimeLimitTrait.swift b/Sources/Testing/Traits/TimeLimitTrait.swift index a70c14a3f..153a19bdd 100644 --- a/Sources/Testing/Traits/TimeLimitTrait.swift +++ b/Sources/Testing/Traits/TimeLimitTrait.swift @@ -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. /// diff --git a/Tests/TestingMacrosTests/TestSupport/Scaffolding.swift b/Tests/TestingMacrosTests/TestSupport/Scaffolding.swift index 813b442aa..766a7d614 100644 --- a/Tests/TestingMacrosTests/TestSupport/Scaffolding.swift +++ b/Tests/TestingMacrosTests/TestSupport/Scaffolding.swift @@ -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 diff --git a/Tests/TestingTests/IssueTests.swift b/Tests/TestingTests/IssueTests.swift index a17f5c1f8..b0135d85b 100644 --- a/Tests/TestingTests/IssueTests.swift +++ b/Tests/TestingTests/IssueTests.swift @@ -10,6 +10,7 @@ @testable @_spi(Experimental) @_spi(ForToolsIntegrationOnly) import Testing private import TestingInternals +import Foundation #if canImport(XCTest) import XCTest diff --git a/Tests/TestingTests/ObjCInteropTests.swift b/Tests/TestingTests/ObjCInteropTests.swift index eacc601c3..179460af4 100644 --- a/Tests/TestingTests/ObjCInteropTests.swift +++ b/Tests/TestingTests/ObjCInteropTests.swift @@ -8,6 +8,7 @@ See https://swift.org/CONTRIBUTORS.txt for Swift project authors */ +#if canImport(XCTest) import XCTest @testable @_spi(ForToolsIntegrationOnly) import Testing @@ -106,3 +107,4 @@ struct ObjCAndXCTestInteropTests { } } } +#endif diff --git a/Tests/TestingTests/RunnerTests.swift b/Tests/TestingTests/RunnerTests.swift index 28c811228..9b860b675 100644 --- a/Tests/TestingTests/RunnerTests.swift +++ b/Tests/TestingTests/RunnerTests.swift @@ -10,6 +10,7 @@ #if canImport(XCTest) import XCTest +#endif @testable @_spi(Experimental) @_spi(ForToolsIntegrationOnly) import Testing struct MyError: Error, Equatable { @@ -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 { diff --git a/Tests/TestingTests/TestSupport/Scaffolding.swift b/Tests/TestingTests/TestSupport/Scaffolding.swift index 813b442aa..766a7d614 100644 --- a/Tests/TestingTests/TestSupport/Scaffolding.swift +++ b/Tests/TestingTests/TestSupport/Scaffolding.swift @@ -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 From 67a9f26de84035a3396ed04a60e4ef4c8ee2b5d9 Mon Sep 17 00:00:00 2001 From: Jonathan Grynspan Date: Mon, 25 Mar 2024 19:50:53 -0400 Subject: [PATCH 2/2] Enable a test that doesn't really rely on XCTest --- Tests/TestingTests/Traits/TimeLimitTraitTests.swift | 2 -- 1 file changed, 2 deletions(-) diff --git a/Tests/TestingTests/Traits/TimeLimitTraitTests.swift b/Tests/TestingTests/Traits/TimeLimitTraitTests.swift index d76942f9d..bce00f064 100644 --- a/Tests/TestingTests/Traits/TimeLimitTraitTests.swift +++ b/Tests/TestingTests/Traits/TimeLimitTraitTests.swift @@ -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: [