Skip to content

Commit 9f13aba

Browse files
authored
Update package templates to use Swift Testing in the toolchain rather than as a package dependency. (#7872)
This PR updates the package templates used by `swift package init` to use Swift Testing by default where possible, and to not add a package dependency on the Swift Testing open-source repository since it is now building in the toolchain. Resolves rdar://128272585.
1 parent 5cfc3d2 commit 9f13aba

File tree

8 files changed

+78
-143
lines changed

8 files changed

+78
-143
lines changed
Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,9 @@
1-
// swift-tools-version: 5.10
1+
// swift-tools-version: 6.0
22
import PackageDescription
33

44
let package = Package(
55
name: "SwiftTesting",
6-
platforms: [
7-
.macOS(.v13), .iOS(.v16), .watchOS(.v9), .tvOS(.v16), .visionOS(.v1)
8-
],
9-
dependencies: [
10-
.package(url: "https://github.com/apple/swift-testing.git", branch: "main"),
11-
],
126
targets: [
13-
.testTarget(
14-
name: "SwiftTestingTests",
15-
dependencies: [.product(name: "Testing", package: "swift-testing"),]
16-
),
7+
.testTarget(name: "SwiftTestingTests"),
178
]
189
)

Sources/Commands/PackageCommands/Init.swift

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,16 @@ extension SwiftPackageCommand {
5656

5757
let packageName = self.packageName ?? cwd.basename
5858

59-
// Which testing libraries should be used? XCTest is on by default,
60-
// but Swift Testing must remain off by default until it is present
61-
// in the Swift toolchain.
59+
// Testing is on by default, with XCTest only enabled explicitly.
60+
// For macros this is reversed, since we don't support testing
61+
// macros with Swift Testing yet.
6262
var supportedTestingLibraries = Set<TestingLibrary>()
63-
if testLibraryOptions.isEnabled(.xctest, swiftCommandState: swiftCommandState) {
63+
if testLibraryOptions.isExplicitlyEnabled(.xctest, swiftCommandState: swiftCommandState) ||
64+
(initMode == .macro && testLibraryOptions.isEnabled(.xctest, swiftCommandState: swiftCommandState)) {
6465
supportedTestingLibraries.insert(.xctest)
6566
}
66-
if testLibraryOptions.isExplicitlyEnabled(.swiftTesting, swiftCommandState: swiftCommandState) {
67+
if testLibraryOptions.isExplicitlyEnabled(.swiftTesting, swiftCommandState: swiftCommandState) ||
68+
(initMode != .macro && testLibraryOptions.isEnabled(.swiftTesting, swiftCommandState: swiftCommandState)) {
6769
supportedTestingLibraries.insert(.swiftTesting)
6870
}
6971

Sources/PackageModelSyntax/AddTarget.swift

Lines changed: 1 addition & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,6 @@ public struct AddTarget {
8484
// SwiftSyntax.
8585
target.dependencies.append(contentsOf: macroTargetDependencies)
8686

87-
case .test where configuration.testHarness == .swiftTesting:
88-
// Testing targets using swift-testing need to depend on
89-
// SwiftTesting from the swift-testing package.
90-
target.dependencies.append(contentsOf: swiftTestingTestTargetDependencies)
91-
9287
default:
9388
break;
9489
}
@@ -163,17 +158,6 @@ public struct AddTarget {
163158
}
164159
}
165160

166-
case .test where configuration.testHarness == .swiftTesting:
167-
if !manifest.description.contains("swift-testing") {
168-
newPackageCall = try AddPackageDependency
169-
.addPackageDependencyLocal(
170-
.swiftTesting(
171-
configuration: installedSwiftPMConfiguration
172-
),
173-
to: newPackageCall
174-
)
175-
}
176-
177161
default: break;
178162
}
179163

@@ -212,8 +196,7 @@ public struct AddTarget {
212196
importModuleNames.append("XCTest")
213197

214198
case .swiftTesting:
215-
// Import is handled by the added dependency.
216-
break
199+
importModuleNames.append("Testing")
217200
}
218201
}
219202

@@ -381,36 +364,3 @@ fileprivate extension PackageDependency {
381364
)
382365
}
383366
}
384-
385-
/// The set of dependencies we need to introduce to a newly-created macro
386-
/// target.
387-
fileprivate let swiftTestingTestTargetDependencies: [TargetDescription.Dependency] = [
388-
.product(name: "Testing", package: "swift-testing"),
389-
]
390-
391-
392-
/// The package dependency for swift-testing, for use in test files.
393-
fileprivate extension PackageDependency {
394-
/// Source control URL for the swift-syntax package.
395-
static var swiftTestingURL: SourceControlURL {
396-
"https://github.com/apple/swift-testing.git"
397-
}
398-
399-
/// Package dependency on the swift-testing package.
400-
static func swiftTesting(
401-
configuration: InstalledSwiftPMConfiguration
402-
) -> PackageDependency {
403-
let swiftTestingVersionDefault =
404-
configuration.swiftTestingVersionForTestTemplate
405-
let swiftTestingVersion = Version(swiftTestingVersionDefault.description)!
406-
407-
return .sourceControl(
408-
identity: PackageIdentity(url: swiftTestingURL),
409-
nameForTargetDependencyResolutionOnly: nil,
410-
location: .remote(swiftTestingURL),
411-
requirement: .range(.upToNextMajor(from: swiftTestingVersion)),
412-
productFilter: .everything,
413-
traits: []
414-
)
415-
}
416-
}

Sources/Workspace/InitPackage.swift

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public final class InitPackage {
3636

3737
public init(
3838
packageType: PackageType,
39-
supportedTestingLibraries: Set<TestingLibrary> = [.xctest],
39+
supportedTestingLibraries: Set<TestingLibrary>,
4040
platforms: [SupportedPlatform] = []
4141
) {
4242
self.packageType = packageType
@@ -186,8 +186,8 @@ public final class InitPackage {
186186

187187
var platforms = options.platforms
188188

189-
// Macros and swift-testing require macOS 10.15, iOS 13, etc.
190-
if packageType == .macro || options.supportedTestingLibraries.contains(.swiftTesting) {
189+
// Macros require macOS 10.15, iOS 13, etc.
190+
if packageType == .macro {
191191
func addIfMissing(_ newPlatform: SupportedPlatform) {
192192
if platforms.contains(where: { platform in
193193
platform.platform == newPlatform.platform
@@ -275,9 +275,6 @@ public final class InitPackage {
275275
} else if packageType == .macro {
276276
dependencies.append(#".package(url: "https://github.com/swiftlang/swift-syntax.git", from: "\#(self.installedSwiftPMConfiguration.swiftSyntaxVersionForMacroTemplate.description)")"#)
277277
}
278-
if options.supportedTestingLibraries.contains(.swiftTesting) {
279-
dependencies.append(#".package(url: "https://github.com/apple/swift-testing.git", from: "0.11.0")"#)
280-
}
281278
if !dependencies.isEmpty {
282279
let dependencies = dependencies.map { dependency in
283280
" \(dependency),"
@@ -384,17 +381,7 @@ public final class InitPackage {
384381
"""
385382
} else {
386383
let testTarget: String
387-
if options.supportedTestingLibraries.contains(.swiftTesting) {
388-
testTarget = """
389-
.testTarget(
390-
name: "\(pkgname)Tests",
391-
dependencies: [
392-
"\(pkgname)",
393-
.product(name: "Testing", package: "swift-testing"),
394-
]
395-
),
396-
"""
397-
} else if options.supportedTestingLibraries.contains(.xctest) {
384+
if !options.supportedTestingLibraries.isEmpty {
398385
testTarget = """
399386
.testTarget(
400387
name: "\(pkgname)Tests",
@@ -688,6 +675,10 @@ public final class InitPackage {
688675
private func writeLibraryTestsFile(_ path: AbsolutePath) throws {
689676
var content = ""
690677

678+
// XCTest is only added if it was explicitly asked for, so add tests
679+
// for it *and* Testing if it is enabled (or just XCTest if Testing
680+
// is explicitly disabled).
681+
691682
if options.supportedTestingLibraries.contains(.swiftTesting) {
692683
content += "import Testing\n"
693684
}
@@ -696,20 +687,18 @@ public final class InitPackage {
696687
}
697688
content += "@testable import \(moduleName)\n"
698689

699-
// Prefer swift-testing if specified, otherwise XCTest. If both are
700-
// specified, the developer is free to write tests using both
701-
// libraries, but we still only want to present a single library's
702-
// example tests.
690+
703691
if options.supportedTestingLibraries.contains(.swiftTesting) {
704692
content += """
705693
706-
@Test func example() throws {
707-
// swift-testing Documentation
708-
// https://swiftpackageindex.com/apple/swift-testing/main/documentation/testing
694+
@Test func example() async throws {
695+
// Write your test here and use APIs like `#expect(...)` to check expected conditions.
709696
}
710697
711698
"""
712-
} else if options.supportedTestingLibraries.contains(.xctest) {
699+
}
700+
701+
if options.supportedTestingLibraries.contains(.xctest) {
713702
content += """
714703
715704
final class \(moduleName)Tests: XCTestCase {
@@ -762,13 +751,15 @@ public final class InitPackage {
762751
763752
"""##
764753

765-
// Prefer swift-testing if specified, otherwise XCTest. If both are
766-
// specified, the developer is free to write tests using both
767-
// libraries, but we still only want to present a single library's
768-
// example tests.
754+
755+
// XCTest is only added if it was explicitly asked for, so add tests
756+
// for it *and* Testing if it is enabled.
757+
769758
if options.supportedTestingLibraries.contains(.swiftTesting) {
770759
// FIXME: https://github.com/swiftlang/swift-syntax/issues/2400
771-
} else if options.supportedTestingLibraries.contains(.xctest) {
760+
}
761+
762+
if options.supportedTestingLibraries.contains(.xctest) {
772763
content += ##"""
773764
final class \##(moduleName)Tests: XCTestCase {
774765
func testMacro() throws {

Sources/_InternalTestSupport/misc.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ extension InitPackage {
432432
public convenience init(
433433
name: String,
434434
packageType: PackageType,
435-
supportedTestingLibraries: Set<TestingLibrary> = [.xctest],
435+
supportedTestingLibraries: Set<TestingLibrary> = [.swiftTesting],
436436
destinationPath: AbsolutePath,
437437
fileSystem: FileSystem
438438
) throws {

Tests/CommandsTests/TestCommandTests.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,10 +276,28 @@ final class TestCommandTests: CommandsTestCase {
276276
}
277277

278278
func testBasicSwiftTestingIntegration() async throws {
279+
#if !canImport(Testing)
279280
try XCTSkipUnless(
280281
nil != Environment.current["SWIFT_PM_SWIFT_TESTING_TESTS_ENABLED"],
281282
"Skipping \(#function) because swift-testing tests are not explicitly enabled"
282283
)
284+
#endif
285+
286+
try await fixture(name: "Miscellaneous/TestDiscovery/SwiftTesting") { fixturePath in
287+
do {
288+
let (stdout, _) = try await SwiftPM.Test.execute(["--enable-swift-testing", "--disable-xctest"], packagePath: fixturePath)
289+
XCTAssertMatch(stdout, .contains(#"Test "SOME TEST FUNCTION" started"#))
290+
}
291+
}
292+
}
293+
294+
func testBasicSwiftTestingIntegration_ExperimentalFlag() async throws {
295+
#if !canImport(Testing)
296+
try XCTSkipUnless(
297+
nil != Environment.current["SWIFT_PM_SWIFT_TESTING_TESTS_ENABLED"],
298+
"Skipping \(#function) because swift-testing tests are not explicitly enabled"
299+
)
300+
#endif
283301

284302
try await fixture(name: "Miscellaneous/TestDiscovery/SwiftTesting") { fixturePath in
285303
do {

Tests/PackageModelSyntaxTests/ManifestEditTests.swift

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -581,14 +581,8 @@ class ManifestEditTests: XCTestCase {
581581
// swift-tools-version: 5.5
582582
let package = Package(
583583
name: "packages",
584-
dependencies: [
585-
.package(url: "https://github.com/apple/swift-testing.git", from: "0.8.0"),
586-
],
587584
targets: [
588-
.testTarget(
589-
name: "MyTest",
590-
dependencies: [ .product(name: "Testing", package: "swift-testing") ]
591-
),
585+
.testTarget(name: "MyTest"),
592586
]
593587
)
594588
""",
@@ -624,7 +618,7 @@ class ManifestEditTests: XCTestCase {
624618
let package = Package(
625619
name: "packages",
626620
dependencies: [
627-
.package(url: "https://github.com/apple/swift-testing.git", from: "0.8.0"),
621+
.package(url: "https://github.com/swiftlang/swift-example.git", from: "1.2.3"),
628622
],
629623
targets: [
630624
.testTarget(
@@ -638,20 +632,20 @@ class ManifestEditTests: XCTestCase {
638632
let package = Package(
639633
name: "packages",
640634
dependencies: [
641-
.package(url: "https://github.com/apple/swift-testing.git", from: "0.8.0"),
635+
.package(url: "https://github.com/swiftlang/swift-example.git", from: "1.2.3"),
642636
],
643637
targets: [
644638
.testTarget(
645639
name: "MyTest",
646640
dependencies: [
647-
.product(name: "Testing", package: "swift-testing"),
641+
.product(name: "SomethingOrOther", package: "swift-example"),
648642
]
649643
),
650644
]
651645
)
652646
""") { manifest in
653647
try AddTargetDependency.addTargetDependency(
654-
.product(name: "Testing", package: "swift-testing"),
648+
.product(name: "SomethingOrOther", package: "swift-example"),
655649
targetName: "MyTest",
656650
to: manifest
657651
)

0 commit comments

Comments
 (0)