Skip to content

Commit 50ba727

Browse files
committed
Add the ability to specify testing libraries when initing a package.
This PR adds `-enable-experimental-swift-testing` (and `-disable-xctest` and their inverses) to `swift package init`. These options behave, broadly, the same as they do for `swift test`. They determine which testing library a new package will use and adjust the generated template to match. It is important to note that any combination of the two libraries is supported: a developer may wish to use only one or the other, or both, or may wish to opt out of a test target entirely. All four combinations are supported, however for simplicity's sake if both libraries are enabled, we only generate example code for swift-testing. Note that right now, correct macro target support is impeded by swiftlang/swift-syntax#2400. I don't think that issue blocks a change here (since it's in an experimental feature anyway!) but it does mean that `swift package init --type macro --enable-experimental-swift-testing` produces some dead tests. Once that issue is resolved, we can revise the template to produce meaningful tests instead. Resolves rdar://99279056.
1 parent d2eb0ca commit 50ba727

File tree

4 files changed

+335
-67
lines changed

4 files changed

+335
-67
lines changed

Sources/Commands/PackageTools/Init.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import ArgumentParser
1414
import Basics
1515
import CoreCommands
1616
import Workspace
17+
import SPMBuildCore
1718

1819
extension SwiftPackageTool {
1920
struct Init: SwiftCommand {
@@ -38,6 +39,18 @@ extension SwiftPackageTool {
3839
"""))
3940
var initMode: InitPackage.PackageType = .library
4041

42+
/// Whether to enable support for XCTest.
43+
@Flag(name: .customLong("xctest"),
44+
inversion: .prefixedEnableDisable,
45+
help: "Enable support for XCTest")
46+
var enableXCTestSupport: Bool = true
47+
48+
/// Whether to enable support for swift-testing.
49+
@Flag(name: .customLong("experimental-swift-testing"),
50+
inversion: .prefixedEnableDisable,
51+
help: "Enable experimental support for swift-testing")
52+
var enableSwiftTestingLibrarySupport: Bool = false
53+
4154
@Option(name: .customLong("name"), help: "Provide custom package name")
4255
var packageName: String?
4356

@@ -46,10 +59,18 @@ extension SwiftPackageTool {
4659
throw InternalError("Could not find the current working directory")
4760
}
4861

62+
var testingLibraries: Set<BuildParameters.Testing.Library> = []
63+
if enableXCTestSupport {
64+
testingLibraries.insert(.xctest)
65+
}
66+
if enableSwiftTestingLibrarySupport {
67+
testingLibraries.insert(.swiftTesting)
68+
}
4969
let packageName = self.packageName ?? cwd.basename
5070
let initPackage = try InitPackage(
5171
name: packageName,
5272
packageType: initMode,
73+
supportedTestingLibraries: testingLibraries,
5374
destinationPath: cwd,
5475
installedSwiftPMConfiguration: swiftTool.getHostToolchain().installedSwiftPMConfiguration,
5576
fileSystem: swiftTool.fileSystem

Sources/SPMTestSupport/misc.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,12 +444,13 @@ extension InitPackage {
444444
public convenience init(
445445
name: String,
446446
packageType: PackageType,
447+
supportedTestingLibraries: Set<BuildParameters.Testing.Library> = [.xctest],
447448
destinationPath: AbsolutePath,
448449
fileSystem: FileSystem
449450
) throws {
450451
try self.init(
451452
name: name,
452-
options: InitPackageOptions(packageType: packageType),
453+
options: InitPackageOptions(packageType: packageType, supportedTestingLibraries: supportedTestingLibraries),
453454
destinationPath: destinationPath,
454455
installedSwiftPMConfiguration: .default,
455456
fileSystem: fileSystem

0 commit comments

Comments
 (0)