Skip to content

Commit

Permalink
Add the ability to specify testing libraries when initing a package.
Browse files Browse the repository at this point in the history
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 apple/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.
  • Loading branch information
grynspan committed Dec 11, 2023
1 parent d2eb0ca commit 50ba727
Show file tree
Hide file tree
Showing 4 changed files with 335 additions and 67 deletions.
21 changes: 21 additions & 0 deletions Sources/Commands/PackageTools/Init.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import ArgumentParser
import Basics
import CoreCommands
import Workspace
import SPMBuildCore

extension SwiftPackageTool {
struct Init: SwiftCommand {
Expand All @@ -38,6 +39,18 @@ extension SwiftPackageTool {
"""))
var initMode: InitPackage.PackageType = .library

/// Whether to enable support for XCTest.
@Flag(name: .customLong("xctest"),
inversion: .prefixedEnableDisable,
help: "Enable support for XCTest")
var enableXCTestSupport: Bool = true

/// Whether to enable support for swift-testing.
@Flag(name: .customLong("experimental-swift-testing"),
inversion: .prefixedEnableDisable,
help: "Enable experimental support for swift-testing")
var enableSwiftTestingLibrarySupport: Bool = false

@Option(name: .customLong("name"), help: "Provide custom package name")
var packageName: String?

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

var testingLibraries: Set<BuildParameters.Testing.Library> = []
if enableXCTestSupport {
testingLibraries.insert(.xctest)
}
if enableSwiftTestingLibrarySupport {
testingLibraries.insert(.swiftTesting)
}
let packageName = self.packageName ?? cwd.basename
let initPackage = try InitPackage(
name: packageName,
packageType: initMode,
supportedTestingLibraries: testingLibraries,
destinationPath: cwd,
installedSwiftPMConfiguration: swiftTool.getHostToolchain().installedSwiftPMConfiguration,
fileSystem: swiftTool.fileSystem
Expand Down
3 changes: 2 additions & 1 deletion Sources/SPMTestSupport/misc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -444,12 +444,13 @@ extension InitPackage {
public convenience init(
name: String,
packageType: PackageType,
supportedTestingLibraries: Set<BuildParameters.Testing.Library> = [.xctest],
destinationPath: AbsolutePath,
fileSystem: FileSystem
) throws {
try self.init(
name: name,
options: InitPackageOptions(packageType: packageType),
options: InitPackageOptions(packageType: packageType, supportedTestingLibraries: supportedTestingLibraries),
destinationPath: destinationPath,
installedSwiftPMConfiguration: .default,
fileSystem: fileSystem
Expand Down

0 comments on commit 50ba727

Please sign in to comment.