[macros] Tests find SDK out instead of hard coding it.

R=whesse@google.com

Change-Id: I0ecdf8ef53e79234f8d2957ac680e8ec103e496a
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/356042
Auto-Submit: Morgan :) <davidmorgan@google.com>
Reviewed-by: William Hesse <whesse@google.com>
Commit-Queue: Morgan :) <davidmorgan@google.com>
diff --git a/tests/macro_build/analyzer_lt_test.dart b/tests/macro_build/analyzer_lt_test.dart
index 7a962df..52b3123 100644
--- a/tests/macro_build/analyzer_lt_test.dart
+++ b/tests/macro_build/analyzer_lt_test.dart
@@ -8,8 +8,7 @@
   testMacroBuild([
     r'$DART pub get',
     r'$DART '
-        // TODO(davidmorgan): find this programmatically.
-        r'$DART_SDK/out/DebugX64/gen/dartanalyzer.dart.snapshot '
+        r'$DART_SDK_OUT/gen/dartanalyzer.dart.snapshot '
         '-Dtest_runner.configuration=analyzer-asserts-linux '
         '--enable-experiment=macros '
         '--ignore-unrecognized-flags '
diff --git a/tests/macro_build/cfe_lt_test.dart b/tests/macro_build/cfe_lt_test.dart
index 02065e1..639f717 100644
--- a/tests/macro_build/cfe_lt_test.dart
+++ b/tests/macro_build/cfe_lt_test.dart
@@ -12,8 +12,7 @@
         '--verify '
         '--skip-platform-verification -o out.dill '
         '--platform '
-        // TODO(davidmorgan): find this programmatically.
-        r'$DART_SDK/out/DebugX64/vm_platform_strong.dill '
+        r'$DART_SDK_OUT/vm_platform_strong.dill '
         '-Dtest_runner.configuration=cfe-strong-linux '
         '--enable-experiment=macros '
         '--nnbd-strong '
diff --git a/tests/macro_build/tester/tester.dart b/tests/macro_build/tester/tester.dart
index 4bb9ab5..97ecfb9 100644
--- a/tests/macro_build/tester/tester.dart
+++ b/tests/macro_build/tester/tester.dart
@@ -10,6 +10,21 @@
 import 'package:path/path.dart' as p;
 import 'package:test/test.dart';
 
+final dartSdkOutDirectory = _findSdkOutDirectory();
+
+String _findSdkOutDirectory() {
+  var dartSdkOutDirectory = File(Platform.resolvedExecutable).parent;
+  if (!Directory.fromUri(dartSdkOutDirectory.uri.resolve('gen')).existsSync()) {
+    // Not next to `dart`, try two levels up for `dart-sdk/bin` suffix.
+    dartSdkOutDirectory = dartSdkOutDirectory.parent.parent;
+  }
+  if (!Directory.fromUri(dartSdkOutDirectory.uri.resolve('gen')).existsSync()) {
+    fail("Can't find SDK 'gen' directory from ${Platform.resolvedExecutable}, "
+        'please run from an SDK build out.');
+  }
+  return dartSdkOutDirectory.path;
+}
+
 /// Tests a macro build specified by [commands].
 ///
 /// The commands are launched with current directory set to a temp folder with
@@ -30,7 +45,7 @@
   var workingDirectory = '${temp.path}/package_under_test';
   await _copyPath(sourceDirectory, workingDirectory);
 
-  var dartSdkPath = Directory.current.path;
+  final dartSdkPath = Directory.current.path;
   final dartPath = Platform.resolvedExecutable;
 
   // TODO(davidmorgan): run on more platforms.
@@ -51,6 +66,10 @@
   var failed = false;
   var timedOut = false;
   for (var command in commands) {
+    if (command.contains(r'$DART_SDK_OUT')) {
+      // Only search for SDK out directory if it's needed for this test case.
+      command = command.replaceAll(r'$DART_SDK_OUT', dartSdkOutDirectory);
+    }
     final commandParts = command
         .replaceAll(r'$DART_SDK', dartSdkPath)
         .replaceAll(r'$DART', dartPath)