Skip to content

Commit f52b6f5

Browse files
authored
Fix new package XCTest file formatting and include throws keyword (#3448) (#3449)
Motivation: Fix formatting of the XCTest source file in newly `init`'ed packages, and include the `throws` keyword on the example XCTest method as a convenience to users and to promote use of throwing test methods in XCTests. Changes: - Fix a bug in the formatting of XCTest source files in `InitPackage`, where the closing `"""` string terminator was outdented one level too far, which resulted in the file contents being indented unnecessarily. - Include the `throws` keyword on `func testExample()` in the new Library type package template. - Modify an existing test to validate these improvements. rdar://77209458 rdar://77209464
1 parent 0bc0a71 commit f52b6f5

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

Sources/Workspace/InitPackage.swift

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -356,19 +356,15 @@ public final class InitPackage {
356356
@testable import \(moduleName)
357357
358358
final class \(moduleName)Tests: XCTestCase {
359-
func testExample() {
359+
func testExample() throws {
360360
// This is an example of a functional test case.
361361
// Use XCTAssert and related functions to verify your tests produce the correct
362362
// results.
363363
XCTAssertEqual(\(typeName)().text, "Hello, World!")
364364
}
365-
366-
"""
367-
368-
stream <<< """
369365
}
370366
371-
"""
367+
"""
372368
}
373369
}
374370

@@ -421,10 +417,6 @@ public final class InitPackage {
421417
return Bundle.main.bundleURL
422418
#endif
423419
}
424-
425-
"""
426-
427-
stream <<< """
428420
}
429421
430422
"""

Tests/WorkspaceTests/InitTests.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,20 @@ class InitTests: XCTestCase {
127127
XCTAssertTrue(readmeContents.hasPrefix("# Foo\n"))
128128

129129
XCTAssertEqual(try fs.getDirectoryContents(path.appending(component: "Sources").appending(component: "Foo")), ["Foo.swift"])
130+
131+
let tests = path.appending(component: "Tests")
130132
XCTAssertEqual(
131-
try fs.getDirectoryContents(path.appending(component: "Tests")).sorted(),
133+
try fs.getDirectoryContents(tests).sorted(),
132134
["FooTests"])
133135

136+
let testFile = tests.appending(component: "FooTests").appending(component: "FooTests.swift")
137+
let testFileContents = try localFileSystem.readFileContents(testFile).description
138+
XCTAssertTrue(testFileContents.hasPrefix("import XCTest"), """
139+
Validates formatting of XCTest source file, in particular that it does not contain leading whitespace:
140+
\(testFileContents)
141+
""")
142+
XCTAssertTrue(testFileContents.contains("func testExample() throws"), "Contents:\n\(testFileContents)")
143+
134144
// Try building it
135145
XCTAssertBuilds(path)
136146
let triple = Resources.default.toolchain.triple

0 commit comments

Comments
 (0)