Skip to content

Commit d598054

Browse files
committed
[SR-1606] [Commands] Fix warnings in template package.
- This also adds more explicit variables to distinguish between where the example package, module, and type names are used. - Fixes: https://bugs.swift.org/browse/SR-1606
1 parent a086f6c commit d598054

File tree

1 file changed

+40
-26
lines changed

1 file changed

+40
-26
lines changed

Sources/Commands/init.swift

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,25 @@ extension InitError: CustomStringConvertible {
3030
}
3131
}
3232

33+
/// Create an initial template package.
3334
final class InitPackage {
35+
let rootd = POSIX.getcwd()
36+
37+
/// The mode in use.
3438
let mode: InitMode
39+
40+
/// The name of the example package to create.
3541
let pkgname: String
36-
let rootd = POSIX.getcwd()
42+
43+
/// The name of the example module to create.
44+
var moduleName: String {
45+
return pkgname
46+
}
47+
48+
/// The name of the example type to create (within the package).
49+
var typeName: String {
50+
return pkgname
51+
}
3752

3853
init(mode: InitMode) throws {
3954
// Validate that the name is valid.
@@ -93,14 +108,15 @@ final class InitPackage {
93108
print("Creating Sources/")
94109
try Utility.makeDirectories(sources)
95110

96-
let sourceFileName = (mode == .executable) ? "main.swift" : "\(pkgname).swift"
111+
let sourceFileName = (mode == .executable) ? "main.swift" : "\(typeName).swift"
97112
let sourceFile = Path.join(sources, sourceFileName)
98113
let sourceFileFP = try Utility.fopen(sourceFile, mode: .write)
99114
defer { sourceFileFP.closeFile() }
100115
print("Creating Sources/\(sourceFileName)")
101116
switch mode {
102117
case .library:
103-
try fputs("struct \(pkgname) {\n\n", sourceFileFP)
118+
try fputs("struct \(typeName) {\n\n", sourceFileFP)
119+
try fputs(" var text = \"Hello, World!\"\n", sourceFileFP)
104120
try fputs("}\n", sourceFileFP)
105121
case .executable:
106122
try fputs("print(\"Hello, world!\")\n", sourceFileFP)
@@ -127,39 +143,37 @@ final class InitPackage {
127143
defer { linuxMainFP.closeFile() }
128144
print("Creating Tests/LinuxMain.swift")
129145
try fputs("import XCTest\n", linuxMainFP)
130-
try fputs("@testable import \(pkgname)TestSuite\n\n", linuxMainFP)
146+
try fputs("@testable import \(moduleName)TestSuite\n\n", linuxMainFP)
131147
try fputs("XCTMain([\n", linuxMainFP)
132-
try fputs("\t testCase(\(pkgname)Tests.allTests),\n", linuxMainFP)
148+
try fputs(" testCase(\(typeName)Tests.allTests),\n", linuxMainFP)
133149
try fputs("])\n", linuxMainFP)
134150
}
135151

136152
private func writeTestFileStubs(testsPath: String) throws {
137-
let testModule = Path.join(testsPath, pkgname)
138-
print("Creating Tests/\(pkgname)/")
153+
let testModule = Path.join(testsPath, moduleName)
154+
print("Creating Tests/\(moduleName)/")
139155
try Utility.makeDirectories(testModule)
140156

141-
let testsFile = Path.join(testModule, "\(pkgname)Tests.swift")
142-
print("Creating Tests/\(pkgname)/\(pkgname)Tests.swift")
157+
let testsFile = Path.join(testModule, "\(moduleName)Tests.swift")
158+
print("Creating Tests/\(moduleName)/\(moduleName)Tests.swift")
143159
let testsFileFP = try Utility.fopen(testsFile, mode: .write)
144160
defer { testsFileFP.closeFile() }
145161
try fputs("import XCTest\n", testsFileFP)
146-
try fputs("@testable import \(pkgname)\n\n", testsFileFP)
147-
148-
try fputs("class \(pkgname)Tests: XCTestCase {\n\n", testsFileFP)
149-
150-
try fputs("\tfunc testExample() {\n", testsFileFP)
151-
try fputs("\t\t// This is an example of a functional test case.\n", testsFileFP)
152-
try fputs("\t\t// Use XCTAssert and related functions to verify your tests produce the correct results.\n", testsFileFP)
153-
try fputs("\t}\n\n", testsFileFP)
154-
155-
try fputs("}\n", testsFileFP)
156-
157-
try fputs("extension \(pkgname)Tests {\n", testsFileFP)
158-
try fputs("\tstatic var allTests : [(String, \(pkgname)Tests -> () throws -> Void)] {\n", testsFileFP)
159-
try fputs("\t\treturn [\n", testsFileFP)
160-
try fputs("\t\t\t(\"testExample\", testExample),\n", testsFileFP)
161-
try fputs("\t\t]\n", testsFileFP)
162-
try fputs("\t}\n", testsFileFP)
162+
try fputs("@testable import \(moduleName)\n", testsFileFP)
163+
try fputs("\n", testsFileFP)
164+
try fputs("class \(moduleName)Tests: XCTestCase {\n", testsFileFP)
165+
try fputs(" func testExample() {\n", testsFileFP)
166+
try fputs(" // This is an example of a functional test case.\n", testsFileFP)
167+
try fputs(" // Use XCTAssert and related functions to verify your tests produce the correct results.\n", testsFileFP)
168+
try fputs(" XCTAssertEqual(\(typeName)().text, \"Hello, World!\")\n", testsFileFP)
169+
try fputs(" }\n", testsFileFP)
170+
try fputs("\n", testsFileFP)
171+
try fputs("\n", testsFileFP)
172+
try fputs(" static var allTests : [(String, (\(moduleName)Tests) -> () throws -> Void)] {\n", testsFileFP)
173+
try fputs(" return [\n", testsFileFP)
174+
try fputs(" (\"testExample\", testExample),\n", testsFileFP)
175+
try fputs(" ]\n", testsFileFP)
176+
try fputs(" }\n", testsFileFP)
163177
try fputs("}\n", testsFileFP)
164178
}
165179
}

0 commit comments

Comments
 (0)