[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 <bkonyi@google.com>
Reviewed-by: Ben Konyi <bkonyi@google.com>
Reviewed-by: Jake Macdonald <jakemac@google.com>
diff --git a/pkg/dtd/test/example_test.dart b/pkg/dtd/test/example_test.dart
index 431b170..4416654 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 @@
 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 @@
       Platform.resolvedExecutable,
       [
         'run',
-        Platform.script
-            .resolve('../example/dtd_stream_example.dart')
-            .toString(),
+        getExampleFileUri('dtd_stream_example.dart').toString(),
         toolingDaemonProcess.uri.toString(),
       ],
     );
     final lines = <String>[];
     streamProcess.handle(
       stdoutLines: (line) {
-        stdout.write('streamProcess stdout: $line');
+        print('streamProcess stdout: $line');
         lines.add(line);
         final json = jsonDecode(line) as Map<String, Object?>;
         if (json['step'] == 'Event A received') {
@@ -67,9 +76,7 @@
       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 @@
     final stdoutMessages = <Map<String, Object?>>[];
     serviceExampleProcess.handle(
       stdoutLines: (line) {
-        stdout.write('serviceExample stdout: $line');
+        print('serviceExample stdout: $line');
         stdoutMessages.add(jsonDecode(line) as Map<String, Object?>);
       },
       stderrLines: (line) => stderr.write('serviceExample stderr: $line'),
@@ -132,9 +139,7 @@
         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 @@
       final lines = <String>[];
       fileSystemServiceExampleProcess.handle(
         stdoutLines: (line) {
-          stdout.write('fileSystemServiceProcess stdout: $line');
+          print('fileSystemServiceProcess stdout: $line');
           lines.add(line);
           final json = jsonDecode(line) as Map<String, Object?>;
           if (json['step'] == 'read') {
diff --git a/pkg/dtd/test/utils.dart b/pkg/dtd/test/utils.dart
index 9cdaa71..67d844c 100644
--- a/pkg/dtd/test/utils.dart
+++ b/pkg/dtd/test/utils.dart
@@ -28,7 +28,7 @@
     );
     process!.handle(
       stdoutLines: (line) {
-        stdout.write('DTD stdout: $line');
+        print('DTD stdout: $line');
         try {
           final json = jsonDecode(line) as Map<String, Object?>;
           final toolingDaemonDetails =