Skip to content

Commit aa77289

Browse files
committed
Package Init - Generate tests for .executable, .tool
Generate tests for the `.executable` and `.tool` template types when generating a new package with `swift package init` Issue: #8990
1 parent cacb4fb commit aa77289

File tree

2 files changed

+176
-111
lines changed

2 files changed

+176
-111
lines changed

Sources/Workspace/InitPackage.swift

Lines changed: 28 additions & 18 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 {
@@ -337,22 +361,8 @@ public final class InitPackage {
337361
"""
338362
} else if packageType == .macro {
339363
let testTarget: String
340-
if options.supportedTestingLibraries.contains(.swiftTesting) {
341-
testTarget = """
342-
343-
// A test target used to develop the macro implementation.
344-
.testTarget(
345-
name: "\(pkgname)Tests",
346-
dependencies: [
347-
"\(pkgname)Macros",
348-
.product(name: "SwiftSyntaxMacrosTestSupport", package: "swift-syntax"),
349-
.product(name: "Testing", package: "swift-testing"),
350-
]
351-
),
352-
"""
353-
} else if options.supportedTestingLibraries.contains(.xctest) {
364+
if !options.supportedTestingLibraries.isEmpty {
354365
testTarget = """
355-
356366
// A test target used to develop the macro implementation.
357367
.testTarget(
358368
name: "\(pkgname)Tests",
@@ -661,7 +671,7 @@ public final class InitPackage {
661671
}
662672

663673
switch packageType {
664-
case .empty, .executable, .tool, .buildToolPlugin, .commandPlugin: return
674+
case .empty, .buildToolPlugin, .commandPlugin: return
665675
default: break
666676
}
667677
let tests = destinationPath.appending("Tests")
@@ -874,8 +884,8 @@ public final class InitPackage {
874884

875885
let testClassFile = try AbsolutePath(validating: "\(moduleName)Tests.swift", relativeTo: testModule)
876886
switch packageType {
877-
case .empty, .buildToolPlugin, .commandPlugin, .executable, .tool: break
878-
case .library:
887+
case .empty, .buildToolPlugin, .commandPlugin: break
888+
case .library, .executable, .tool:
879889
try writeLibraryTestsFile(testClassFile)
880890
case .macro:
881891
try writeMacroTestsFile(testClassFile)

0 commit comments

Comments
 (0)