Skip to content

Commit 30be346

Browse files
JhonnyBillMaciidgh
authored andcommitted
Improving test tool diagnostics (#1721)
* Improve error diagnostics in TestRunner * Formatted TestRunner calls and init to improve readability * Updated comments with punctuation
1 parent 6f95e63 commit 30be346

File tree

1 file changed

+45
-12
lines changed

1 file changed

+45
-12
lines changed

Sources/Commands/SwiftTestTool.swift

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,13 @@ public class SwiftTestTool: SwiftTool<TestToolOptions> {
213213
switch options.testCaseSpecifier {
214214
case .none:
215215
let runner = TestRunner(
216-
path: testPath, xctestArg: nil, processSet: processSet, sanitizers: options.sanitizers, toolchain: toolchain)
216+
path: testPath,
217+
xctestArg: nil,
218+
processSet: processSet,
219+
sanitizers: options.sanitizers,
220+
toolchain: toolchain,
221+
diagnostics: diagnostics
222+
)
217223
ranSuccessfully = runner.test()
218224

219225
case .regex, .specific:
@@ -234,8 +240,13 @@ public class SwiftTestTool: SwiftTool<TestToolOptions> {
234240
// Finally, run the tests.
235241
for test in tests {
236242
let runner = TestRunner(
237-
path: testPath, xctestArg: test.specifier,
238-
processSet: processSet, sanitizers: options.sanitizers, toolchain: toolchain)
243+
path: testPath,
244+
xctestArg: test.specifier,
245+
processSet: processSet,
246+
sanitizers: options.sanitizers,
247+
toolchain: toolchain,
248+
diagnostics: diagnostics
249+
)
239250
ranSuccessfully = runner.test() && ranSuccessfully
240251
}
241252
}
@@ -264,7 +275,8 @@ public class SwiftTestTool: SwiftTool<TestToolOptions> {
264275
sanitizers: options.sanitizers,
265276
toolchain: toolchain,
266277
xUnitOutput: options.xUnitOutput,
267-
numJobs: options.numberOfWorkers ?? ProcessInfo.processInfo.activeProcessorCount
278+
numJobs: options.numberOfWorkers ?? ProcessInfo.processInfo.activeProcessorCount,
279+
diagnostics: diagnostics
268280
)
269281
try runner.run(tests)
270282

@@ -446,18 +458,28 @@ final class TestRunner {
446458

447459
// The toolchain to use.
448460
private let toolchain: UserToolchain
461+
462+
/// Diagnostics Engine to emit diagnostics.
463+
let diagnostics: DiagnosticsEngine
449464

450465
/// Creates an instance of TestRunner.
451466
///
452467
/// - Parameters:
453468
/// - path: Path to valid XCTest binary.
454469
/// - xctestArg: Arguments to pass to XCTest.
455-
init(path: AbsolutePath, xctestArg: String? = nil, processSet: ProcessSet, sanitizers: EnabledSanitizers, toolchain: UserToolchain) {
470+
init(path: AbsolutePath,
471+
xctestArg: String? = nil,
472+
processSet: ProcessSet,
473+
sanitizers: EnabledSanitizers,
474+
toolchain: UserToolchain,
475+
diagnostics: DiagnosticsEngine
476+
) {
456477
self.path = path
457478
self.xctestArg = xctestArg
458479
self.processSet = processSet
459480
self.sanitizers = sanitizers
460481
self.toolchain = toolchain
482+
self.diagnostics = diagnostics
461483
}
462484

463485
/// Constructs arguments to execute XCTest.
@@ -503,7 +525,9 @@ final class TestRunner {
503525
output += "\n" + exitSignalText(code: signal)
504526
default: break
505527
}
506-
} catch {}
528+
} catch {
529+
diagnostics.emit(error)
530+
}
507531
return (success, output)
508532
}
509533

@@ -523,9 +547,7 @@ final class TestRunner {
523547
default: break
524548
}
525549
} catch {
526-
// FIXME: We may need a better way to print errors from here.
527-
stderrStream <<< "error: \(error)" <<< "\n"
528-
stderrStream.flush()
550+
diagnostics.emit(error)
529551
}
530552
return false
531553
}
@@ -572,23 +594,28 @@ final class ParallelTestRunner {
572594
let toolchain: UserToolchain
573595
let xUnitOutput: AbsolutePath?
574596

575-
/// Number of tests to execute in parallel
597+
/// Number of tests to execute in parallel.
576598
let numJobs: Int
577599

600+
/// Diagnostics Engine to emit diagnostics.
601+
let diagnostics: DiagnosticsEngine
602+
578603
init(
579604
testPath: AbsolutePath,
580605
processSet: ProcessSet,
581606
sanitizers: EnabledSanitizers,
582607
toolchain: UserToolchain,
583608
xUnitOutput: AbsolutePath? = nil,
584-
numJobs: Int
609+
numJobs: Int,
610+
diagnostics: DiagnosticsEngine
585611
) {
586612
self.testPath = testPath
587613
self.processSet = processSet
588614
self.sanitizers = sanitizers
589615
self.toolchain = toolchain
590616
self.xUnitOutput = xUnitOutput
591617
self.numJobs = numJobs
618+
self.diagnostics = diagnostics
592619
progressBar = createProgressBar(forStream: stdoutStream, header: "Testing:")
593620

594621
assert(numJobs > 0, "num jobs should be > 0")
@@ -635,7 +662,13 @@ final class ParallelTestRunner {
635662
// Dequeue a specifier and run it till we encounter nil.
636663
while let test = self.pendingTests.dequeue() {
637664
let testRunner = TestRunner(
638-
path: self.testPath, xctestArg: test.specifier, processSet: self.processSet, sanitizers: self.sanitizers, toolchain: self.toolchain)
665+
path: self.testPath,
666+
xctestArg: test.specifier,
667+
processSet: self.processSet,
668+
sanitizers: self.sanitizers,
669+
toolchain: self.toolchain,
670+
diagnostics: self.diagnostics
671+
)
639672
let (success, output) = testRunner.test()
640673
if !success {
641674
self.ranSuccessfully = false

0 commit comments

Comments
 (0)