Files
sdk/pkg/dtd/test/utils.dart
T
Danny Tuppeny 23de3198fa Add support for notifying DTD clients of registered services
Fixes https://github.com/dart-lang/sdk/issues/56057

Change-Id: I57144297b8cb68b1c8b35d7bdcc619ad293fb4ed
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/372940
Reviewed-by: Ben Konyi <bkonyi@google.com>
Commit-Queue: Ben Konyi <bkonyi@google.com>
Reviewed-by: Derek Xu <derekx@google.com>
2024-07-08 16:12:21 +00:00

79 lines
2.2 KiB
Dart

// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'dart:async';
import 'dart:convert';
import 'dart:io';
/// Helper class for starting a Dart Tooling Daemon instance, and extracting
/// it's [trustedSecret] and [uri] from stdout.
class ToolingDaemonTestProcess {
ToolingDaemonTestProcess({this.unrestricted = false});
late final String? trustedSecret;
late final Uri uri;
late final Process? process;
final bool unrestricted;
Future<Process> start() async {
final completer = Completer<void>();
process = await Process.start(
Platform.resolvedExecutable,
[
'tooling-daemon',
'--machine',
if (unrestricted) '--unrestricted',
'--fakeAnalytics',
],
);
process!.handle(
stdoutLines: (line) {
stdout.write('DTD stdout: $line');
try {
final json = jsonDecode(line) as Map<String, Object?>;
final toolingDaemonDetails =
json['tooling_daemon_details'] as Map<String, Object?>;
trustedSecret =
toolingDaemonDetails['trusted_client_secret'] as String?;
uri = Uri.parse(toolingDaemonDetails['uri'] as String);
completer.complete();
} catch (e) {
// If we failed to decode then this line doesn't have json.
print('Json parsing error: $e');
}
},
stderrLines: (line) {
stderr.write('DTD stderr: $line');
},
);
await completer.future;
return process!;
}
void kill() {
process?.kill();
}
}
extension OutputProcessExtension on Process {
void handle({
required void Function(String) stdoutLines,
void Function(String)? stderrLines,
}) {
this
.stdout
.transform(utf8.decoder)
.transform(const LineSplitter())
.listen((line) => stdoutLines(line));
if (stderrLines != null) {
this
.stderr
.transform(utf8.decoder)
.transform(const LineSplitter())
.listen((line) => stderrLines(line));
}
}
}