Update testForMainDexListGenerator to support options modification

Bug: 202173862
Change-Id: I9cecf3e1fea6984bcc3031feb91ea4768d6a5303
diff --git a/src/main/java/com/android/tools/r8/GenerateMainDexList.java b/src/main/java/com/android/tools/r8/GenerateMainDexList.java
index 2a794ab..1edb900 100644
--- a/src/main/java/com/android/tools/r8/GenerateMainDexList.java
+++ b/src/main/java/com/android/tools/r8/GenerateMainDexList.java
@@ -118,12 +118,9 @@
    */
   public static List<String> run(GenerateMainDexListCommand command)
       throws CompilationFailedException {
-    ExecutorService executorService = ThreadUtils.getExecutorService(command.getInternalOptions());
-    try {
-      return run(command, executorService);
-    } finally {
-      executorService.shutdown();
-    }
+    AndroidApp app = command.getInputApp();
+    InternalOptions options = command.getInternalOptions();
+    return runForTesting(app, options);
   }
 
   /**
@@ -145,28 +142,43 @@
     InternalOptions options = command.getInternalOptions();
     List<String> result = new ArrayList<>();
     ExceptionUtils.withMainDexListHandler(
-        command.getReporter(),
+        command.getReporter(), () -> run(app, executor, options, result));
+    return result;
+  }
+
+  static List<String> runForTesting(AndroidApp app, InternalOptions options)
+      throws CompilationFailedException {
+    ExecutorService executorService = ThreadUtils.getExecutorService(options);
+    List<String> result = new ArrayList<>();
+    ExceptionUtils.withMainDexListHandler(
+        options.reporter,
         () -> {
           try {
-            new GenerateMainDexList(options)
-                .run(
-                    app,
-                    executor,
-                    new SortingStringConsumer(
-                        new ForwardingConsumer(options.mainDexListConsumer) {
-                          @Override
-                          public void accept(String string, DiagnosticsHandler handler) {
-                            result.add(string);
-                            super.accept(string, handler);
-                          }
-                        }));
+            run(app, executorService, options, result);
           } finally {
-            executor.shutdown();
+            executorService.shutdown();
           }
         });
     return result;
   }
 
+  private static void run(
+      AndroidApp app, ExecutorService executor, InternalOptions options, List<String> result)
+      throws IOException {
+    new GenerateMainDexList(options)
+        .run(
+            app,
+            executor,
+            new SortingStringConsumer(
+                new ForwardingConsumer(options.mainDexListConsumer) {
+                  @Override
+                  public void accept(String string, DiagnosticsHandler handler) {
+                    result.add(string);
+                    super.accept(string, handler);
+                  }
+                }));
+  }
+
   public static void main(String[] args) throws CompilationFailedException {
     GenerateMainDexListCommand.Builder builder = GenerateMainDexListCommand.parse(args);
     GenerateMainDexListCommand command = builder.build();
diff --git a/src/test/java/com/android/tools/r8/GenerateMainDexListTestBuilder.java b/src/test/java/com/android/tools/r8/GenerateMainDexListTestBuilder.java
index 03ba4b8..c0665cf 100644
--- a/src/test/java/com/android/tools/r8/GenerateMainDexListTestBuilder.java
+++ b/src/test/java/com/android/tools/r8/GenerateMainDexListTestBuilder.java
@@ -7,12 +7,14 @@
 import com.android.tools.r8.debug.DebugTestConfig;
 import com.android.tools.r8.errors.Unimplemented;
 import com.android.tools.r8.origin.Origin;
+import com.android.tools.r8.utils.InternalOptions;
 import java.io.IOException;
 import java.nio.file.Path;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.List;
+import java.util.function.Consumer;
 
 public class GenerateMainDexListTestBuilder
     extends TestBaseBuilder<
@@ -22,6 +24,10 @@
         GenerateMainDexListRunResult,
         GenerateMainDexListTestBuilder> {
 
+  public static final Consumer<InternalOptions> DEFAULT_OPTIONS = options -> {};
+
+  private Consumer<InternalOptions> optionsConsumer = DEFAULT_OPTIONS;
+
   private GenerateMainDexListTestBuilder(TestState state, Builder builder) {
     super(state, builder);
   }
@@ -68,8 +74,12 @@
     throw new Unimplemented("No support for class path");
   }
 
-  public GenerateMainDexListRunResult run() throws CompilationFailedException {
-    return new GenerateMainDexListRunResult(GenerateMainDexList.run(builder.build()), getState());
+  public GenerateMainDexListRunResult run() throws CompilationFailedException, IOException {
+    GenerateMainDexListCommand command = builder.build();
+    InternalOptions internalOptions = command.getInternalOptions();
+    optionsConsumer.accept(internalOptions);
+    return new GenerateMainDexListRunResult(
+        GenerateMainDexList.runForTesting(command.getInputApp(), internalOptions), getState());
   }
 
   public GenerateMainDexListTestBuilder addMainDexRules(Collection<String> rules) {
@@ -94,4 +104,12 @@
     builder.setMainDexListOutputPath(output);
     return self();
   }
+
+  public GenerateMainDexListTestBuilder addOptionsModification(
+      Consumer<InternalOptions> optionsConsumer) {
+    if (optionsConsumer != null) {
+      this.optionsConsumer = this.optionsConsumer.andThen(optionsConsumer);
+    }
+    return self();
+  }
 }