Skip to content

Commit b234e08

Browse files
authored
Add missing testTarget for package init swift-testing tests (#9152)
### Motivation: When running `swift package init --type executable --enable-swift-testing` an example test is generated but the test target is not created in the manifest. ### Modifications: Add the missing test target to the manifest when these types are initialized with swift-testing tests. This applies to the `tool` and `executable` types. ### Result: Packages are initialized with a test target and running `swift test` runs the generated test successfully. Issue: #9149
1 parent 5f05670 commit b234e08

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

Sources/Workspace/InitPackage.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,20 +300,44 @@ public final class InitPackage {
300300
301301
"""
302302
if packageType == .executable {
303+
let testTarget: String
304+
if !options.supportedTestingLibraries.isEmpty {
305+
testTarget = """
306+
.testTarget(
307+
name: "\(pkgname)Tests",
308+
dependencies: ["\(pkgname)"]
309+
),
310+
"""
311+
} else {
312+
testTarget = ""
313+
}
303314
param += """
304315
.executableTarget(
305316
name: "\(pkgname)"
306317
),
318+
\(testTarget)
307319
]
308320
"""
309321
} else if packageType == .tool {
322+
let testTarget: String
323+
if !options.supportedTestingLibraries.isEmpty {
324+
testTarget = """
325+
.testTarget(
326+
name: "\(pkgname)Tests",
327+
dependencies: ["\(pkgname)"]
328+
),
329+
"""
330+
} else {
331+
testTarget = ""
332+
}
310333
param += """
311334
.executableTarget(
312335
name: "\(pkgname)",
313336
dependencies: [
314337
.product(name: "ArgumentParser", package: "swift-argument-parser"),
315338
]
316339
),
340+
\(testTarget)
317341
]
318342
"""
319343
} else if packageType == .buildToolPlugin {

Tests/WorkspaceTests/InitTests.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,8 @@ final class InitTests: XCTestCase {
316316
// Verify basic file system content that we expect in the package
317317
let manifest = path.appending("Package.swift")
318318
XCTAssertFileExists(manifest)
319+
let manifestContents: String = try localFileSystem.readFileContents(manifest)
320+
XCTAssertMatch(manifestContents, .contains(".testTarget("))
319321
let testFile = path.appending("Tests").appending("FooTests").appending("FooTests.swift")
320322
let testFileContents: String = try localFileSystem.readFileContents(testFile)
321323
XCTAssertMatch(testFileContents, .contains(#"import Testing"#))
@@ -344,6 +346,8 @@ final class InitTests: XCTestCase {
344346
// Verify basic file system content that we expect in the package
345347
let manifest = path.appending("Package.swift")
346348
XCTAssertFileExists(manifest)
349+
let manifestContents: String = try localFileSystem.readFileContents(manifest)
350+
XCTAssertMatch(manifestContents, .contains(".testTarget("))
347351
let testFile = path.appending("Tests").appending("FooTests").appending("FooTests.swift")
348352
let testFileContents: String = try localFileSystem.readFileContents(testFile)
349353
XCTAssertMatch(testFileContents, .contains(#"import Testing"#))

0 commit comments

Comments
 (0)