[infra] Add --keep-files flag to tools/test.py

This allows one to actually run the commands which are printed in a
failure.

Change-Id: I62c8a387c37d3563ea3c0f803c107e2e9d8262bb
Reviewed-on: https://dart-review.googlesource.com/c/85483
Commit-Queue: Martin Kustermann <kustermann@google.com>
Reviewed-by: Vyacheslav Egorov <vegorov@google.com>
Reviewed-by: Alexander Thomas <athom@google.com>
diff --git a/pkg/smith/lib/configuration.dart b/pkg/smith/lib/configuration.dart
index ea0b8d1..9bd0e37 100644
--- a/pkg/smith/lib/configuration.dart
+++ b/pkg/smith/lib/configuration.dart
@@ -259,6 +259,7 @@
         useAnalyzerCfe: boolOption("use-cfe"),
         useAnalyzerFastaParser: boolOption("analyzer-use-fasta-parser"),
         useBlobs: boolOption("use-blobs"),
+        keepGeneratedFiles: boolOption("keep-generated-files"),
         useDart2JSWithKernel: boolOption("dart2js-with-kernel"),
         useDart2JSOldFrontEnd: boolOption("dart2js-old-frontend"),
         useFastStartup: boolOption("fast-startup"),
@@ -314,6 +315,9 @@
   // TODO(rnystrom): What is this?
   final bool useBlobs;
 
+  // Keep generated files (instead of deleting them).
+  final bool keepGeneratedFiles;
+
   // TODO(rnystrom): Remove these when Dart 1.0 is no longer supported.
   final bool useDart2JSWithKernel;
   final bool useDart2JSOldFrontEnd;
@@ -339,6 +343,7 @@
       bool useAnalyzerCfe,
       bool useAnalyzerFastaParser,
       bool useBlobs,
+      bool keepGeneratedFiles,
       bool useDart2JSWithKernel,
       bool useDart2JSOldFrontEnd,
       bool useFastStartup,
@@ -357,6 +362,7 @@
         useAnalyzerCfe = useAnalyzerCfe ?? false,
         useAnalyzerFastaParser = useAnalyzerFastaParser ?? false,
         useBlobs = useBlobs ?? false,
+        keepGeneratedFiles = keepGeneratedFiles ?? false,
         useDart2JSWithKernel = useDart2JSWithKernel ?? false,
         useDart2JSOldFrontEnd = useDart2JSOldFrontEnd ?? false,
         useFastStartup = useFastStartup ?? false,
@@ -384,6 +390,7 @@
       useAnalyzerCfe == other.useAnalyzerCfe &&
       useAnalyzerFastaParser == other.useAnalyzerFastaParser &&
       useBlobs == other.useBlobs &&
+      keepGeneratedFiles == other.keepGeneratedFiles &&
       useDart2JSWithKernel == other.useDart2JSWithKernel &&
       useDart2JSOldFrontEnd == other.useDart2JSOldFrontEnd &&
       useFastStartup == other.useFastStartup &&
@@ -418,7 +425,8 @@
       (useFastStartup ? 2048 : 0) ^
       (useHotReload ? 4096 : 0) ^
       (useHotReloadRollback ? 8192 : 0) ^
-      (useSdk ? 16384 : 0);
+      (useSdk ? 16384 : 0) ^
+      (keepGeneratedFiles ? 32768 : 0);
 
   String toString() {
     var buffer = new StringBuffer();
@@ -444,6 +452,7 @@
     if (useAnalyzerCfe) fields.add("use-cfe");
     if (useAnalyzerFastaParser) fields.add("analyzer-use-fasta-parser");
     if (useBlobs) fields.add("use-blobs");
+    if (keepGeneratedFiles) fields.add("keep-generated-files");
     if (useDart2JSWithKernel) fields.add("dart2js-with-kernel");
     if (useDart2JSOldFrontEnd) fields.add("dart2js-old-frontend");
     if (useFastStartup) fields.add("fast-startup");
@@ -507,6 +516,10 @@
     if (useBlobs || other.useBlobs) {
       fields.add("useBlobs $useBlobs ${other.useBlobs}");
     }
+    if (keepGeneratedFiles || other.keepGeneratedFiles) {
+      fields.add(
+          "keepGeneratedFiles $keepGeneratedFiles ${other.keepGeneratedFiles}");
+    }
     if (useDart2JSWithKernel || other.useDart2JSWithKernel) {
       fields.add("useDart2JSWithKernel "
           "$useDart2JSWithKernel ${other.useDart2JSWithKernel}");
diff --git a/tools/testing/dart/compiler_configuration.dart b/tools/testing/dart/compiler_configuration.dart
index 8fe7756..d1c68fb 100644
--- a/tools/testing/dart/compiler_configuration.dart
+++ b/tools/testing/dart/compiler_configuration.dart
@@ -622,7 +622,7 @@
     commands.add(
         computeDartBootstrapCommand(tempDir, arguments, environmentOverrides));
 
-    if (previewDart2) {
+    if (previewDart2 && !_configuration.keepGeneratedFiles) {
       commands.add(computeRemoveKernelFileCommand(
           tempDir, arguments, environmentOverrides));
     }
@@ -630,8 +630,10 @@
     if (!_configuration.useBlobs) {
       commands.add(
           computeAssembleCommand(tempDir, arguments, environmentOverrides));
-      commands.add(computeRemoveAssemblyCommand(
-          tempDir, arguments, environmentOverrides));
+      if (!_configuration.keepGeneratedFiles) {
+        commands.add(computeRemoveAssemblyCommand(
+            tempDir, arguments, environmentOverrides));
+      }
     }
 
     return new CommandArtifact(
diff --git a/tools/testing/dart/configuration.dart b/tools/testing/dart/configuration.dart
index 31e1f32..53f95df 100644
--- a/tools/testing/dart/configuration.dart
+++ b/tools/testing/dart/configuration.dart
@@ -112,6 +112,7 @@
   bool get useAnalyzerCfe => configuration.useAnalyzerCfe;
   bool get useAnalyzerFastaParser => configuration.useAnalyzerFastaParser;
   bool get useBlobs => configuration.useBlobs;
+  bool get keepGeneratedFiles => configuration.keepGeneratedFiles;
   bool get useSdk => configuration.useSdk;
   bool get useFastStartup => configuration.useFastStartup;
   bool get useEnableAsserts => configuration.enableAsserts;
diff --git a/tools/testing/dart/options.dart b/tools/testing/dart/options.dart
index 53416a8..3bea2d7 100644
--- a/tools/testing/dart/options.dart
+++ b/tools/testing/dart/options.dart
@@ -191,6 +191,8 @@
     new _Option.bool(
         'use_blobs', 'Use mmap instead of shared libraries for precompilation.',
         hide: true),
+    new _Option.bool('keep_generated_files', 'Keep any generated files.',
+        abbr: 'k'),
     new _Option.int('timeout', 'Timeout in seconds.', abbr: 't'),
     new _Option(
         'progress',
@@ -648,6 +650,7 @@
                     useAnalyzerFastaParser:
                         data["analyzer_use_fasta_parser"] as bool,
                     useBlobs: data["use_blobs"] as bool,
+                    keepGeneratedFiles: data["keep_generated_files"] as bool,
                     useSdk: data["use_sdk"] as bool,
                     useFastStartup: data["fast_startup"] as bool,
                     useDart2JSWithKernel: data["dart2js_with_kernel"] as bool,
diff --git a/tools/testing/dart/test_configurations.dart b/tools/testing/dart/test_configurations.dart
index 051fbb7..00e5ebf 100644
--- a/tools/testing/dart/test_configurations.dart
+++ b/tools/testing/dart/test_configurations.dart
@@ -167,7 +167,9 @@
     }
 
     DebugLogger.close();
-    TestUtils.deleteTempSnapshotDirectory(configurations[0]);
+    if (!firstConf.keepGeneratedFiles) {
+      TestUtils.deleteTempSnapshotDirectory(configurations[0]);
+    }
   }
 
   var eventListener = <EventListener>[];