Skip to content

Commit ecc9b05

Browse files
authored
migrate SwiftTestTool to Observability APIs (#3779)
motication: adopt new Observability APIs changes: replace DiagnosticsEngine with Observability APIs in SwiftTestTool
1 parent fe2a091 commit ecc9b05

File tree

2 files changed

+22
-20
lines changed

2 files changed

+22
-20
lines changed

Sources/Commands/SwiftTestTool.swift

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ public struct SwiftTestTool: SwiftCommand {
204204

205205
public func run(_ swiftTool: SwiftTool) throws {
206206
// Validate commands arguments
207-
try validateArguments(diagnostics: swiftTool.observabilityScope.makeDiagnosticsEngine())
207+
try self.validateArguments(observabilityScope: swiftTool.observabilityScope)
208208

209209
switch options.mode {
210210
case .listTests:
@@ -301,7 +301,7 @@ public struct SwiftTestTool: SwiftCommand {
301301
toolchain: toolchain,
302302
testEnv: testEnv,
303303
outputStream: swiftTool.outputStream,
304-
diagnostics: swiftTool.observabilityScope.makeDiagnosticsEngine()
304+
observabilityScope: swiftTool.observabilityScope
305305
)
306306

307307
// Finally, run the tests.
@@ -342,10 +342,10 @@ public struct SwiftTestTool: SwiftCommand {
342342
toolchain: toolchain,
343343
xUnitOutput: options.xUnitOutput,
344344
numJobs: options.numberOfWorkers ?? ProcessInfo.processInfo.activeProcessorCount,
345-
diagnostics: swiftTool.observabilityScope.makeDiagnosticsEngine(),
346345
options: swiftOptions,
347346
buildParameters: buildParameters,
348-
outputStream: swiftTool.outputStream
347+
outputStream: swiftTool.outputStream,
348+
observabilityScope: swiftTool.observabilityScope
349349
)
350350
try runner.run(tests, outputStream: swiftTool.outputStream)
351351

@@ -527,24 +527,24 @@ public struct SwiftTestTool: SwiftCommand {
527527
/// Private function that validates the commands arguments
528528
///
529529
/// - Throws: if a command argument is invalid
530-
private func validateArguments(diagnostics: DiagnosticsEngine) throws {
530+
private func validateArguments(observabilityScope: ObservabilityScope) throws {
531531
// Validation for --num-workers.
532532
if let workers = options.numberOfWorkers {
533533

534534
// The --num-worker option should be called with --parallel.
535535
guard options.mode == .runParallel else {
536-
diagnostics.emit(error: "--num-workers must be used with --parallel")
536+
observabilityScope.emit(error: "--num-workers must be used with --parallel")
537537
throw ExitCode.failure
538538
}
539539

540540
guard workers > 0 else {
541-
diagnostics.emit(error: "'--num-workers' must be greater than zero")
541+
observabilityScope.emit(error: "'--num-workers' must be greater than zero")
542542
throw ExitCode.failure
543543
}
544544
}
545545

546546
if options.shouldGenerateLinuxMain {
547-
diagnostics.emit(warning: "'--generate-linuxmain' option is deprecated; tests are automatically discovered on all platforms")
547+
observabilityScope.emit(warning: "'--generate-linuxmain' option is deprecated; tests are automatically discovered on all platforms")
548548
}
549549
}
550550

@@ -589,8 +589,8 @@ final class TestRunner {
589589
/// Output stream for test results
590590
private let outputStream: OutputByteStream
591591

592-
/// Diagnostics Engine to emit diagnostics.
593-
private let diagnostics: DiagnosticsEngine
592+
/// ObservabilityScope to emit diagnostics.
593+
private let observabilityScope: ObservabilityScope
594594

595595
/// Creates an instance of TestRunner.
596596
///
@@ -604,15 +604,15 @@ final class TestRunner {
604604
toolchain: UserToolchain,
605605
testEnv: [String: String],
606606
outputStream: OutputByteStream,
607-
diagnostics: DiagnosticsEngine
607+
observabilityScope: ObservabilityScope
608608
) {
609609
self.bundlePaths = bundlePaths
610610
self.xctestArg = xctestArg
611611
self.processSet = processSet
612612
self.toolchain = toolchain
613613
self.testEnv = testEnv
614614
self.outputStream = outputStream
615-
self.diagnostics = diagnostics
615+
self.observabilityScope = observabilityScope.makeChildScope(description: "Test Runner")
616616
}
617617

618618
/// Executes and returns execution status. Prints test output on standard streams if requested
@@ -657,6 +657,8 @@ final class TestRunner {
657657
return String(bytes: stdout + stderr, encoding: .utf8)?.spm_chuzzle() ?? ""
658658
}
659659

660+
let testObservabilityScope = self.observabilityScope.makeChildScope(description: "running test at \(path)")
661+
660662
do {
661663
let outputRedirection = Process.OutputRedirection.stream(
662664
stdout: {
@@ -688,7 +690,7 @@ final class TestRunner {
688690
default: break
689691
}
690692
} catch {
691-
self.diagnostics.emit(error)
693+
testObservabilityScope.emit(error)
692694
}
693695
return (false, makeOutput())
694696
}
@@ -738,27 +740,27 @@ final class ParallelTestRunner {
738740
/// Output stream for test results
739741
private let outputStream: OutputByteStream
740742

741-
/// Diagnostics Engine to emit diagnostics.
742-
private let diagnostics: DiagnosticsEngine
743+
/// ObservabilityScope to emit diagnostics.
744+
private let observabilityScope: ObservabilityScope
743745

744746
init(
745747
bundlePaths: [AbsolutePath],
746748
processSet: ProcessSet,
747749
toolchain: UserToolchain,
748750
xUnitOutput: AbsolutePath? = nil,
749751
numJobs: Int,
750-
diagnostics: DiagnosticsEngine,
751752
options: SwiftToolOptions,
752753
buildParameters: BuildParameters,
753-
outputStream: OutputByteStream
754+
outputStream: OutputByteStream,
755+
observabilityScope: ObservabilityScope
754756
) {
755757
self.bundlePaths = bundlePaths
756758
self.processSet = processSet
757759
self.toolchain = toolchain
758760
self.xUnitOutput = xUnitOutput
759761
self.numJobs = numJobs
760762
self.outputStream = outputStream
761-
self.diagnostics = diagnostics
763+
self.observabilityScope = observabilityScope.makeChildScope(description: "Parallel Test Runner")
762764

763765
if ProcessEnv.vars["SWIFTPM_TEST_RUNNER_PROGRESS_BAR"] == "lit" {
764766
progressAnimation = PercentProgressAnimation(stream: outputStream, header: "Testing:")
@@ -819,7 +821,7 @@ final class ParallelTestRunner {
819821
toolchain: self.toolchain,
820822
testEnv: testEnv,
821823
outputStream: self.outputStream,
822-
diagnostics: self.diagnostics
824+
observabilityScope: self.observabilityScope
823825
)
824826
let (success, output) = testRunner.test(writeToOutputStream: false)
825827
if !success {

Tests/CommandsTests/TestToolTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ final class TestToolTests: XCTestCase {
3333
XCTAssert(stdout.contains("Swift Package Manager"), "got stdout:\n" + stdout)
3434
}
3535

36-
func testNumWorkersParallelRequeriment() throws {
36+
func testNumWorkersParallelRequirement() throws {
3737
// Running swift-test fixtures on linux is not yet possible.
3838
#if os(macOS)
3939
fixture(name: "Miscellaneous/EchoExecutable") { path in

0 commit comments

Comments
 (0)