diff --git a/Sources/Workspace/InitPackage.swift b/Sources/Workspace/InitPackage.swift index f66d72c5985..79f8b8167fc 100644 --- a/Sources/Workspace/InitPackage.swift +++ b/Sources/Workspace/InitPackage.swift @@ -661,9 +661,12 @@ public final class InitPackage { } switch packageType { - case .empty, .executable, .tool, .buildToolPlugin, .commandPlugin: return - default: break + case .empty, .buildToolPlugin, .commandPlugin: + return + case .library, .executable, .tool, .macro: + break } + let tests = destinationPath.appending("Tests") guard self.fileSystem.exists(tests) == false else { return @@ -873,9 +876,11 @@ public final class InitPackage { try makeDirectories(testModule) let testClassFile = try AbsolutePath(validating: "\(moduleName)Tests.swift", relativeTo: testModule) + switch packageType { - case .empty, .buildToolPlugin, .commandPlugin, .executable, .tool: break - case .library: + case .empty, .buildToolPlugin, .commandPlugin: + break + case .library, .executable, .tool: try writeLibraryTestsFile(testClassFile) case .macro: try writeMacroTestsFile(testClassFile) diff --git a/Tests/WorkspaceTests/InitTests.swift b/Tests/WorkspaceTests/InitTests.swift index d09e117d1a5..287077570b6 100644 --- a/Tests/WorkspaceTests/InitTests.swift +++ b/Tests/WorkspaceTests/InitTests.swift @@ -297,6 +297,62 @@ final class InitTests: XCTestCase { } } + func testInitPackageExecutableWithSwiftTesting() async throws { + try testWithTemporaryDirectory { tmpPath in + let fs = localFileSystem + let path = tmpPath.appending("Foo") + let name = path.basename + try fs.createDirectory(path) + // Create the package + let initPackage = try InitPackage( + name: name, + packageType: .executable, + supportedTestingLibraries: [.swiftTesting], + destinationPath: path, + fileSystem: localFileSystem + ) + + try initPackage.writePackageStructure() + // Verify basic file system content that we expect in the package + let manifest = path.appending("Package.swift") + XCTAssertFileExists(manifest) + let testFile = path.appending("Tests").appending("FooTests").appending("FooTests.swift") + let testFileContents: String = try localFileSystem.readFileContents(testFile) + XCTAssertMatch(testFileContents, .contains(#"import Testing"#)) + XCTAssertNoMatch(testFileContents, .contains(#"import XCTest"#)) + XCTAssertMatch(testFileContents, .contains(#"@Test func example() async throws"#)) + XCTAssertNoMatch(testFileContents, .contains("func testExample() throws")) + } + } + + func testInitPackageToolWithSwiftTesting() async throws { + try testWithTemporaryDirectory { tmpPath in + let fs = localFileSystem + let path = tmpPath.appending("Foo") + let name = path.basename + try fs.createDirectory(path) + // Create the package + let initPackage = try InitPackage( + name: name, + packageType: .tool, + supportedTestingLibraries: [.swiftTesting], + destinationPath: path, + fileSystem: localFileSystem + ) + + try initPackage.writePackageStructure() + // Verify basic file system content that we expect in the package + let manifest = path.appending("Package.swift") + XCTAssertFileExists(manifest) + let testFile = path.appending("Tests").appending("FooTests").appending("FooTests.swift") + let testFileContents: String = try localFileSystem.readFileContents(testFile) + XCTAssertMatch(testFileContents, .contains(#"import Testing"#)) + XCTAssertNoMatch(testFileContents, .contains(#"import XCTest"#)) + XCTAssertMatch(testFileContents, .contains(#"@Test func example() async throws"#)) + XCTAssertNoMatch(testFileContents, .contains("func testExample() throws")) + } + } + func testInitPackageCommandPlugin() throws { try testWithTemporaryDirectory { tmpPath in let fs = localFileSystem