Skip to content

Commit 339afc8

Browse files
authored
Fix: Generate tests for executable/tool with --enable-swift-testing (#9032)
Updated `InitPackage` to handle `.executable` and `.tool` types when generating test files with SwiftTesting. Added corresponding tests to verify correct test file content for these package types. ### Motivation: The `swift package init` command did not generate a `Tests` directory for executable or tool packages when the `--enable-swift-testing` flag was used. This was caused by logic in `Sources/Workspace/InitPackage.swift` that incorrectly skipped test generation for these package types. This change fixes that behavior, ensuring that tests are created as expected. ### Modifications: - In `Sources/Workspace/InitPackage.swift`, the `writeTests()` function was modified to proceed with test generation for `.executable` and `.tool` package types. - In the same file, the `writeTestFileStubs()` function was updated to include `.executable` and `.tool` types, allowing the creation of standard library-style test files for them. ### Result: After this change, running `swift package init --type executable --enable-swift-testing` or `swift package init --type tool --enable-swift-testing` correctly generates a `Tests` directory with sample test files, aligning the tool's behavior with its intended functionality. ### Reproduction and Verification: #### Reproducing the Issue (on `main` branch): 1. Create a temporary directory: `mkdir -p /tmp/test-repro && cd /tmp/test-repro` 2. Run `swift package init --type executable --enable-swift-testing`. 3. Verify that no `Tests` directory is created by running `ls -F`. #### Verifying the Fix (on this branch): 1. Build the package manager: `swift build` 2. Find the built executable: `find .build -name swift-package` 3. Create a temporary directory: `mkdir -p /tmp/test-fix && cd /tmp/test-fix` 4. Run the newly built package manager to init a project: `../.build/arm64-apple-macosx/debug/swift-package init --type executable --enable-swift-testing` (adjust path as needed). 5. Verify that a `Tests` directory is now created by running `ls -F`.
1 parent abd3157 commit 339afc8

File tree

2 files changed

+65
-4
lines changed

2 files changed

+65
-4
lines changed

Sources/Workspace/InitPackage.swift

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -661,9 +661,12 @@ public final class InitPackage {
661661
}
662662

663663
switch packageType {
664-
case .empty, .executable, .tool, .buildToolPlugin, .commandPlugin: return
665-
default: break
664+
case .empty, .buildToolPlugin, .commandPlugin:
665+
return
666+
case .library, .executable, .tool, .macro:
667+
break
666668
}
669+
667670
let tests = destinationPath.appending("Tests")
668671
guard self.fileSystem.exists(tests) == false else {
669672
return
@@ -873,9 +876,11 @@ public final class InitPackage {
873876
try makeDirectories(testModule)
874877

875878
let testClassFile = try AbsolutePath(validating: "\(moduleName)Tests.swift", relativeTo: testModule)
879+
876880
switch packageType {
877-
case .empty, .buildToolPlugin, .commandPlugin, .executable, .tool: break
878-
case .library:
881+
case .empty, .buildToolPlugin, .commandPlugin:
882+
break
883+
case .library, .executable, .tool:
879884
try writeLibraryTestsFile(testClassFile)
880885
case .macro:
881886
try writeMacroTestsFile(testClassFile)

Tests/WorkspaceTests/InitTests.swift

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,62 @@ final class InitTests: XCTestCase {
297297
}
298298
}
299299

300+
func testInitPackageExecutableWithSwiftTesting() async throws {
301+
try testWithTemporaryDirectory { tmpPath in
302+
let fs = localFileSystem
303+
let path = tmpPath.appending("Foo")
304+
let name = path.basename
305+
try fs.createDirectory(path)
306+
// Create the package
307+
let initPackage = try InitPackage(
308+
name: name,
309+
packageType: .executable,
310+
supportedTestingLibraries: [.swiftTesting],
311+
destinationPath: path,
312+
fileSystem: localFileSystem
313+
)
314+
315+
try initPackage.writePackageStructure()
316+
// Verify basic file system content that we expect in the package
317+
let manifest = path.appending("Package.swift")
318+
XCTAssertFileExists(manifest)
319+
let testFile = path.appending("Tests").appending("FooTests").appending("FooTests.swift")
320+
let testFileContents: String = try localFileSystem.readFileContents(testFile)
321+
XCTAssertMatch(testFileContents, .contains(#"import Testing"#))
322+
XCTAssertNoMatch(testFileContents, .contains(#"import XCTest"#))
323+
XCTAssertMatch(testFileContents, .contains(#"@Test func example() async throws"#))
324+
XCTAssertNoMatch(testFileContents, .contains("func testExample() throws"))
325+
}
326+
}
327+
328+
func testInitPackageToolWithSwiftTesting() async throws {
329+
try testWithTemporaryDirectory { tmpPath in
330+
let fs = localFileSystem
331+
let path = tmpPath.appending("Foo")
332+
let name = path.basename
333+
try fs.createDirectory(path)
334+
// Create the package
335+
let initPackage = try InitPackage(
336+
name: name,
337+
packageType: .tool,
338+
supportedTestingLibraries: [.swiftTesting],
339+
destinationPath: path,
340+
fileSystem: localFileSystem
341+
)
342+
343+
try initPackage.writePackageStructure()
344+
// Verify basic file system content that we expect in the package
345+
let manifest = path.appending("Package.swift")
346+
XCTAssertFileExists(manifest)
347+
let testFile = path.appending("Tests").appending("FooTests").appending("FooTests.swift")
348+
let testFileContents: String = try localFileSystem.readFileContents(testFile)
349+
XCTAssertMatch(testFileContents, .contains(#"import Testing"#))
350+
XCTAssertNoMatch(testFileContents, .contains(#"import XCTest"#))
351+
XCTAssertMatch(testFileContents, .contains(#"@Test func example() async throws"#))
352+
XCTAssertNoMatch(testFileContents, .contains("func testExample() throws"))
353+
}
354+
}
355+
300356
func testInitPackageCommandPlugin() throws {
301357
try testWithTemporaryDirectory { tmpPath in
302358
let fs = localFileSystem

0 commit comments

Comments
 (0)