Skip to content

Commit 90002ed

Browse files
committed
Add test stubs for init mode
1 parent ce6ceb8 commit 90002ed

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

Sources/swift-build/initPackage.swift

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ func initPackage() throws {
2020
let gitignore = Path.join(rootd, ".gitignore")
2121
let sources = Path.join(rootd, "Sources")
2222
let tests = Path.join(rootd, "Tests")
23+
let testModule = Path.join(tests, pkgname)
24+
let testsFile = Path.join(testModule, "\(pkgname).swift")
2325
let main = Path.join(sources, "main.swift")
2426

2527
guard !manifest.exists else {
@@ -67,5 +69,54 @@ func initPackage() throws {
6769
if !tests.exists {
6870
print("Creating Tests/")
6971
try mkdir(tests)
72+
73+
print("Creating Tests/\(pkgname)/")
74+
try mkdir(testModule)
75+
try writeTestFileStubs(testsFile: testsFile, pkgname: pkgname)
76+
try writeLinuxMain(path: Path.join(tests, "LinuxMain.swift"), pkgname: pkgname)
77+
}
78+
}
79+
80+
private func writeLinuxMain(path filePath: String, pkgname: String) throws {
81+
let linuxMainFP = try fopen(filePath, mode: .Write)
82+
defer {
83+
fclose(linuxMainFP)
7084
}
85+
86+
print("Creating Tests/LinuxMain.swift")
87+
try fputs("import XCTest\n", linuxMainFP)
88+
try fputs("@testable import \(pkgname)test\n\n", linuxMainFP)
89+
try fputs("XCTMain([\n", linuxMainFP)
90+
try fputs("\t\(pkgname)(),\n", linuxMainFP)
91+
try fputs("])\n", linuxMainFP)
92+
}
93+
94+
private func writeTestFileStubs(testsFile testsFile: String, pkgname: String) throws {
95+
print("Creating Tests/\(pkgname)/\(pkgname).swift")
96+
let testsFileFP = try fopen(testsFile, mode: .Write)
97+
defer {
98+
fclose(testsFileFP)
99+
}
100+
101+
try fputs("import XCTest\n", testsFileFP)
102+
try fputs("@testable import \(pkgname)\n\n", testsFileFP)
103+
104+
try fputs("class \(pkgname): XCTestCase {\n\n", testsFileFP)
105+
106+
try fputs("\tfunc testExample() {\n", testsFileFP)
107+
try fputs("\t\t// This is an example of a functional test case.\n", testsFileFP)
108+
try fputs("\t\t// Use XCTAssert and related functions to verify your tests produce the correct results.\n", testsFileFP)
109+
try fputs("\t}\n\n", testsFileFP)
110+
111+
try fputs("}\n", testsFileFP)
112+
113+
try fputs("\n#if os(Linux)\n", testsFileFP)
114+
try fputs("extension \(pkgname): XCTestCaseProvider {\n", testsFileFP)
115+
try fputs("\tvar allTests : [(String, () throws -> Void)] {\n", testsFileFP)
116+
try fputs("\t\treturn [\n", testsFileFP)
117+
try fputs("\t\t\t(\"testExample\", testExample),\n", testsFileFP)
118+
try fputs("\t\t]\n", testsFileFP)
119+
try fputs("\t}\n", testsFileFP)
120+
try fputs("}\n", testsFileFP)
121+
try fputs("#endif\n", testsFileFP)
71122
}

0 commit comments

Comments
 (0)