Skip to content

Commit 5bd155f

Browse files
authored
[6.0.0] Update package templates to use Swift Testing in the toolchain rather than as a package dependency. (#7911)
1 parent aeaab78 commit 5bd155f

File tree

8 files changed

+78
-142
lines changed

8 files changed

+78
-142
lines changed
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

+7-5
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<BuildParameters.Testing.Library>()
63-
if testLibraryOptions.isEnabled(.xctest) {
63+
if testLibraryOptions.isExplicitlyEnabled(.xctest) ||
64+
(initMode == .macro && testLibraryOptions.isEnabled(.xctest)) {
6465
supportedTestingLibraries.insert(.xctest)
6566
}
66-
if testLibraryOptions.isExplicitlyEnabled(.swiftTesting) {
67+
if testLibraryOptions.isExplicitlyEnabled(.swiftTesting) ||
68+
(initMode != .macro && testLibraryOptions.isEnabled(.swiftTesting)) {
6769
supportedTestingLibraries.insert(.swiftTesting)
6870
}
6971

Sources/PackageModelSyntax/AddTarget.swift

+1-50
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

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

Sources/SPMTestSupport/misc.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ extension InitPackage {
474474
public convenience init(
475475
name: String,
476476
packageType: PackageType,
477-
supportedTestingLibraries: Set<BuildParameters.Testing.Library> = [.xctest],
477+
supportedTestingLibraries: Set<BuildParameters.Testing.Library> = [.swiftTesting],
478478
destinationPath: AbsolutePath,
479479
fileSystem: FileSystem
480480
) throws {

Sources/Workspace/InitPackage.swift

+21-30
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<BuildParameters.Testing.Library> = [.xctest],
39+
supportedTestingLibraries: Set<BuildParameters.Testing.Library>,
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",
@@ -687,6 +674,10 @@ public final class InitPackage {
687674
private func writeLibraryTestsFile(_ path: AbsolutePath) throws {
688675
var content = ""
689676

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

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

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

Tests/CommandsTests/TestCommandTests.swift

+18
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(TestingDisabled)
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(TestingDisabled)
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

+5-11
Original file line numberDiff line numberDiff line change
@@ -575,14 +575,8 @@ class ManifestEditTests: XCTestCase {
575575
// swift-tools-version: 5.5
576576
let package = Package(
577577
name: "packages",
578-
dependencies: [
579-
.package(url: "https://github.com/apple/swift-testing.git", from: "0.8.0"),
580-
],
581578
targets: [
582-
.testTarget(
583-
name: "MyTest",
584-
dependencies: [ .product(name: "Testing", package: "swift-testing") ]
585-
),
579+
.testTarget(name: "MyTest"),
586580
]
587581
)
588582
""",
@@ -618,7 +612,7 @@ class ManifestEditTests: XCTestCase {
618612
let package = Package(
619613
name: "packages",
620614
dependencies: [
621-
.package(url: "https://github.com/apple/swift-testing.git", from: "0.8.0"),
615+
.package(url: "https://github.com/swiftlang/swift-example.git", from: "1.2.3"),
622616
],
623617
targets: [
624618
.testTarget(
@@ -632,20 +626,20 @@ class ManifestEditTests: XCTestCase {
632626
let package = Package(
633627
name: "packages",
634628
dependencies: [
635-
.package(url: "https://github.com/apple/swift-testing.git", from: "0.8.0"),
629+
.package(url: "https://github.com/swiftlang/swift-example.git", from: "1.2.3"),
636630
],
637631
targets: [
638632
.testTarget(
639633
name: "MyTest",
640634
dependencies: [
641-
.product(name: "Testing", package: "swift-testing"),
635+
.product(name: "SomethingOrOther", package: "swift-example"),
642636
]
643637
),
644638
]
645639
)
646640
""") { manifest in
647641
try AddTargetDependency.addTargetDependency(
648-
.product(name: "Testing", package: "swift-testing"),
642+
.product(name: "SomethingOrOther", package: "swift-example"),
649643
targetName: "MyTest",
650644
to: manifest
651645
)

0 commit comments

Comments
 (0)