From 43631b20bb7972449b72ac69ff99faf60cfcd0ba Mon Sep 17 00:00:00 2001 From: Danny Tuppeny Date: Tue, 8 Apr 2025 08:05:19 -0700 Subject: [PATCH] [dtd] Update tests to work via 'dart test' Now that the SDK uses Pub Workspaces, using the test runner is enabled in Dart-Code. However there are some differences when using 'dart test' that caused some of these tests to fail - this change addresses them: - Don't use Platform.script because it won't be the source Dart filename - Use `print` instead of `stdout.write` because the test runner captures that (see https://github.com/dart-lang/test/issues/1749) Change-Id: Ib0e4e1d83449767dfa9d96a6543bda09705f8d96 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/421160 Commit-Queue: Ben Konyi Reviewed-by: Ben Konyi Reviewed-by: Jake Macdonald --- pkg/dtd/test/example_test.dart | 29 +++++++++++++++++------------ pkg/dtd/test/utils.dart | 2 +- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/pkg/dtd/test/example_test.dart b/pkg/dtd/test/example_test.dart index 431b1708654..4416654f47a 100644 --- a/pkg/dtd/test/example_test.dart +++ b/pkg/dtd/test/example_test.dart @@ -5,6 +5,7 @@ import 'dart:async'; import 'dart:convert'; import 'dart:io'; +import 'dart:isolate'; import 'package:dtd/dtd.dart'; import 'package:test/test.dart'; @@ -14,6 +15,16 @@ import 'utils.dart'; void main() { late ToolingDaemonTestProcess toolingDaemonProcess; + /// Gets the URI for [filename] in the example folder. + Uri getExampleFileUri(String filename) { + // Use resolvePackageUriSync and not Platform.script so that this works + // when run through 'dart test'. + return Isolate.resolvePackageUriSync( + Uri.parse('package:dtd/'), + )! + .resolve('../example/$filename'); + } + setUp(() async { toolingDaemonProcess = ToolingDaemonTestProcess(); await toolingDaemonProcess.start(); @@ -31,16 +42,14 @@ void main() { Platform.resolvedExecutable, [ 'run', - Platform.script - .resolve('../example/dtd_stream_example.dart') - .toString(), + getExampleFileUri('dtd_stream_example.dart').toString(), toolingDaemonProcess.uri.toString(), ], ); final lines = []; streamProcess.handle( stdoutLines: (line) { - stdout.write('streamProcess stdout: $line'); + print('streamProcess stdout: $line'); lines.add(line); final json = jsonDecode(line) as Map; if (json['step'] == 'Event A received') { @@ -67,9 +76,7 @@ void main() { Platform.resolvedExecutable, [ 'run', - Platform.script - .resolve('../example/dtd_service_example.dart') - .toString(), + getExampleFileUri('dtd_service_example.dart').toString(), toolingDaemonProcess.uri.toString(), ], ); @@ -77,7 +84,7 @@ void main() { final stdoutMessages = >[]; serviceExampleProcess.handle( stdoutLines: (line) { - stdout.write('serviceExample stdout: $line'); + print('serviceExample stdout: $line'); stdoutMessages.add(jsonDecode(line) as Map); }, stderrLines: (line) => stderr.write('serviceExample stderr: $line'), @@ -132,9 +139,7 @@ void main() { Platform.resolvedExecutable, [ 'run', - Platform.script - .resolve('../example/dtd_file_system_service_example.dart') - .toString(), + getExampleFileUri('dtd_file_system_service_example.dart').toString(), toolingDaemonProcess.uri.toString(), tmpDirectory.uri.toString(), ], @@ -142,7 +147,7 @@ void main() { final lines = []; fileSystemServiceExampleProcess.handle( stdoutLines: (line) { - stdout.write('fileSystemServiceProcess stdout: $line'); + print('fileSystemServiceProcess stdout: $line'); lines.add(line); final json = jsonDecode(line) as Map; if (json['step'] == 'read') { diff --git a/pkg/dtd/test/utils.dart b/pkg/dtd/test/utils.dart index 9cdaa71495f..67d844cf7d6 100644 --- a/pkg/dtd/test/utils.dart +++ b/pkg/dtd/test/utils.dart @@ -28,7 +28,7 @@ class ToolingDaemonTestProcess { ); process!.handle( stdoutLines: (line) { - stdout.write('DTD stdout: $line'); + print('DTD stdout: $line'); try { final json = jsonDecode(line) as Map; final toolingDaemonDetails =