Skip to content

Commit 8970e01

Browse files
authored
Conditionalize the keyword in the generated test main (#5696)
# Motivation Right now the code for the test main file contains this generated code ```swift var tests = [XCTestCaseEntry]() ``` For the rare cases where a package has no tests the generated code produces a warning since the `var tests` was never mutated. If these users are also running `warnings-as-errors` their compile will fail. # Modification This PR conditionalizes the keyword of the variable depending on if there is a test or not. # Result The code is not generating any warnings any more.
1 parent 630bdea commit 8970e01

File tree

5 files changed

+32
-1
lines changed

5 files changed

+32
-1
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// swift-tools-version:4.2
2+
import PackageDescription
3+
4+
let package = Package(
5+
name: "Simple",
6+
targets: [
7+
.target(name: "Simple"),
8+
.testTarget(name: "SimpleTests", dependencies: ["Simple"]),
9+
]
10+
)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
struct Simple {
2+
3+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Sources/Build/BuildOperationBuildSystemDelegateHandler.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ final class TestDiscoveryCommand: CustomLLBuildCommand {
148148
throw InternalError("main output (\(LLBuildManifest.TestDiscoveryTool.mainFileName)) not found")
149149
}
150150

151+
let testsKeyword = tests.isEmpty ? "let" : "var"
152+
151153
// Write the main file.
152154
let stream = try LocalFileOutputByteStream(mainFile)
153155

@@ -157,7 +159,7 @@ final class TestDiscoveryCommand: CustomLLBuildCommand {
157159
stream <<< "@available(*, deprecated, message: \"Not actually deprecated. Marked as deprecated to allow inclusion of deprecated tests (which test deprecated functionality) without warnings\")" <<< "\n"
158160
stream <<< "struct Runner" <<< " {" <<< "\n"
159161
stream <<< indent(4) <<< "static func main()" <<< " {" <<< "\n"
160-
stream <<< indent(8) <<< "var tests = [XCTestCaseEntry]()" <<< "\n"
162+
stream <<< indent(8) <<< "\(testsKeyword) tests = [XCTestCaseEntry]()" <<< "\n"
161163
for module in testsByModule.keys {
162164
stream <<< indent(8) <<< "tests += __\(module)__allTests()" <<< "\n"
163165
}

Tests/FunctionalTests/TestDiscoveryTests.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,21 @@ class TestDiscoveryTests: XCTestCase {
5454
}
5555
}
5656

57+
func testDiscovery_whenNoTests() throws {
58+
#if os(macOS)
59+
try XCTSkipIf(true)
60+
#endif
61+
try fixture(name: "Miscellaneous/TestDiscovery/NoTests") { fixturePath in
62+
let (stdout, stderr) = try executeSwiftTest(fixturePath)
63+
// in "swift test" build output goes to stderr
64+
XCTAssertMatch(stderr, .contains("Build complete!"))
65+
// we are expecting that no warning is produced
66+
XCTAssertNoMatch(stderr, .contains("warning:"))
67+
// in "swift test" test output goes to stdout
68+
XCTAssertMatch(stdout, .contains("Executed 0 tests"))
69+
}
70+
}
71+
5772
func testManifestOverride() throws {
5873
#if os(macOS)
5974
try XCTSkipIf(true)

0 commit comments

Comments
 (0)