Fix AndroidJarsToSignatureCommand so it does not generate empty files

When run as a standalone command this produces empty signature files.
That is because it does not set options.showUnannotated = true. That
behavior is tested but the test infrastructure sets that up hence why
the test works but it fails in the real world.

This change:
1. Makes the test more realistic by remove the `options` setup, it is
   the `parse(..)` call that sets `showUnannotated = true`.
2. Fixes the test failure of `AndroidJarsToSignatureCommand` by
   explicitly setting `options.showUnannotated = true`.
3. Also, fixes the test failure of `SignatureToJDiffCommand` by setting
   `options` to the defaults.

Bug: 298200709
Test: ./gradlew
Change-Id: I1e9be84ab1ed392e9939b696d32b48e16208d3eb
diff --git a/src/main/java/com/android/tools/metalava/AndroidJarsToSignaturesCommand.kt b/src/main/java/com/android/tools/metalava/AndroidJarsToSignaturesCommand.kt
index f3a0bca..2bc216e 100644
--- a/src/main/java/com/android/tools/metalava/AndroidJarsToSignaturesCommand.kt
+++ b/src/main/java/com/android/tools/metalava/AndroidJarsToSignaturesCommand.kt
@@ -68,6 +68,12 @@
         val sourceModelProvider = SourceModelProvider.getImplementation("psi")
         sourceModelProvider.createEnvironmentManager(disableStderrDumping()).use {
             environmentManager ->
+
+            // Some code that this calls still accesses options, but it only needs the default
+            // values apart from `showUnannotated` which it requires to be `true`.
+            @Suppress("DEPRECATION")
+            options = Options().apply { showUnannotated = true }
+
             ConvertJarsToSignatureFiles(
                     stderr,
                     stdout,
diff --git a/src/main/java/com/android/tools/metalava/SignatureToJDiffCommand.kt b/src/main/java/com/android/tools/metalava/SignatureToJDiffCommand.kt
index 5e287fd..a63ed8b 100644
--- a/src/main/java/com/android/tools/metalava/SignatureToJDiffCommand.kt
+++ b/src/main/java/com/android/tools/metalava/SignatureToJDiffCommand.kt
@@ -80,6 +80,10 @@
             .newFile()
 
     override fun run() {
+        // Some code that this calls still accesses options, but it only needs the default values.
+        @Suppress("DEPRECATION")
+        options = Options()
+
         val convertFile = ConvertFile(apiFile, xmlFile, baseApiFile, strip)
         convertFile.process(progressTracker)
     }
diff --git a/src/test/java/com/android/tools/metalava/cli/common/BaseCommandTest.kt b/src/test/java/com/android/tools/metalava/cli/common/BaseCommandTest.kt
index 397a796..12ecffe 100644
--- a/src/test/java/com/android/tools/metalava/cli/common/BaseCommandTest.kt
+++ b/src/test/java/com/android/tools/metalava/cli/common/BaseCommandTest.kt
@@ -16,9 +16,8 @@
 
 package com.android.tools.metalava.cli.common
 
-import com.android.tools.metalava.Options
+import com.android.tools.metalava.OptionsDelegate
 import com.android.tools.metalava.ProgressTracker
-import com.android.tools.metalava.options
 import com.android.tools.metalava.run
 import com.android.tools.metalava.testing.TemporaryFolderOwner
 import com.github.ajalt.clikt.core.CliktCommand
@@ -26,7 +25,9 @@
 import java.io.File
 import java.io.PrintWriter
 import java.io.StringWriter
+import org.junit.After
 import org.junit.Assert
+import org.junit.Before
 import org.junit.Rule
 import org.junit.rules.ErrorCollector
 import org.junit.rules.TemporaryFolder
@@ -53,6 +54,16 @@
     /** Provides access to temporary files. */
     @get:Rule override val temporaryFolder = TemporaryFolder()
 
+    @Before
+    fun ensureTestDoesNotAccessOptionsLeakedFromAnotherTest() {
+        OptionsDelegate.disallowAccess()
+    }
+
+    @After
+    fun ensureTestDoesNotLeakOptionsToAnotherTest() {
+        OptionsDelegate.disallowAccess()
+    }
+
     /**
      * Type safe builder for configuring and running a command related test.
      *
@@ -186,7 +197,6 @@
     }
 
     /** Run the test defined by the configuration. */
-    @Suppress("DEPRECATION")
     internal fun runTest() {
         val stdout = StringWriter()
         val stderr = StringWriter()
@@ -194,15 +204,6 @@
         val printOut = PrintWriter(stdout)
         val printErr = PrintWriter(stderr)
 
-        // Make sure that the global options is reset before each test. This is needed because the
-        // options are used throughout the code and extracting it is a time-consuming process. As a
-        // result even though some code being tested does not require options being parsed they do
-        // use code that accesses the options and so the code being tested relies on the options
-        // being set to their default value. This ensures that even if another test that modifies
-        // the global options is run that it does not affect this code.
-        options = Options()
-        options.parse(emptyArray(), printOut, printErr)
-
         // Runs the command
         command = test.commandFactory()
         runCommand(printOut, printErr, command)