Skip to content

Commit 11aaba1

Browse files
committed
Add --fail-hard flag to APITest test command so that the CLI utility can be told to exit with code 1 given any failing tests.
1 parent 6632e09 commit 11aaba1

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

Sources/APITesting/APITestCommand.swift

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ public final class APITestCommand: Command {
2121
)
2222
var dumpFiles: Bool
2323

24+
@Flag(
25+
name: "fail-hard",
26+
short: "f",
27+
help: "Produce a non-zero exit code if any tests fail."
28+
)
29+
var shouldFailHard: Bool
30+
2431
public init() {}
2532
}
2633

@@ -65,17 +72,26 @@ public final class APITestCommand: Command {
6572
let zipToArg = signature.dumpFiles ? cwd + "/out/api_test_files.zip" : nil
6673
let testLogPath = cwd + "/out/api_test.log"
6774

68-
try Self.kickTestsOff(
75+
let future = Self.kickTestsOff(
6976
testProperties: testProperties,
7077
outPath: path,
7178
zipPath: zipToArg,
7279
testLogPath: testLogPath,
7380
eventLoop: eventLoop,
7481
testLogger: logger
75-
)
82+
).recover { _ in
83+
if signature.shouldFailHard {
84+
exit(1)
85+
}
86+
}
87+
88+
try future
7689
.wait()
7790
}
7891

92+
/// Kick off API Tests.
93+
///
94+
/// - returns: An `EventLoopFuture` that will have failed if any tests have failed.
7995
public static func kickTestsOff(
8096
testProperties: APITestProperties,
8197
outPath: String,
@@ -112,6 +128,8 @@ public final class APITestCommand: Command {
112128
/// notable exception of test summaries on failure (although if this logger is `nil`,
113129
/// the test summary will be logged to the testLogger).
114130
/// - testLogger: A logger to which test-related log messages will be recorded.
131+
///
132+
/// - returns: An `EventLoopFuture` that will have failed if any tests have failed.
115133
public static func kickTestsOff<Persister, Tracker: TestProgressTracker>(
116134
testProgressTracking: (Tracker, Persister)?,
117135
testProperties: APITestProperties,
@@ -172,10 +190,10 @@ public final class APITestCommand: Command {
172190
try? cleanupOutFolder(outPath, logger: testLogger)
173191
requestLogger?.info("Cleaning up tests in \(outPath)")
174192
}
175-
.recover { error in
193+
.flatMapError { error in
176194
if let requestLogger = requestLogger {
177195
requestLogger.error("Testing Failed",
178-
metadata: ["error": .stringConvertible(String(describing: error))])
196+
metadata: ["error": .stringConvertible(String(describing: error))])
179197
// following is tmp to workaround above metadata not being dumped to console with previous call:
180198
requestLogger.error("\(String(describing: error))")
181199
} else {
@@ -184,7 +202,9 @@ public final class APITestCommand: Command {
184202
message: String(describing: error))
185203
}
186204

187-
let _ = trackProgress(testProgressTracker?.markFailed())
205+
// once finished tracking progress, just recreate a new failed future to return.
206+
return trackProgress(testProgressTracker?.markFailed())
207+
.flatMap { eventLoop.makeFailedFuture(error) }
188208
}
189209
}
190210
}

0 commit comments

Comments
 (0)