• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.tools.metalava.cli.common
18 
19 import com.android.tools.lint.detector.api.assertionsEnabled
20 import com.android.tools.metalava.ENV_VAR_METALAVA_DUMP_ARGV
21 import com.android.tools.metalava.model.source.SourceModelProvider
22 import com.android.tools.metalava.reporter.DefaultReporterEnvironment
23 import com.android.tools.metalava.reporter.ReporterEnvironment
24 import java.io.InputStream
25 import java.io.OutputStreamWriter
26 import java.io.PrintWriter
27 import java.io.StringWriter
28 
29 /**
30  * Encapsulates information provided by the execution environment.
31  *
32  * This supports two environments:
33  * 1. The standard command line application.
34  * 2. Tests.
35  */
36 data class ExecutionEnvironment(
37     val stdout: PrintWriter = PrintWriter(OutputStreamWriter(System.out)),
38     val stderr: PrintWriter = PrintWriter(OutputStreamWriter(System.err)),
39     val stdin: InputStream = System.`in`,
40     val reporterEnvironment: ReporterEnvironment = DefaultReporterEnvironment(),
41     val testEnvironment: TestEnvironment? = null,
42 ) {
43     /** Whether metalava is being invoked as part of an Android platform build */
isBuildingAndroidnull44     fun isBuildingAndroid() = System.getenv("ANDROID_BUILD_TOP") != null && !isUnderTest()
45 
46     /** Whether to suppress dumping of information to stderr by a [SourceModelProvider]. */
47     fun disableStderrDumping(): Boolean {
48         return !assertionsEnabled() &&
49             System.getenv(ENV_VAR_METALAVA_DUMP_ARGV) == null &&
50             !isUnderTest()
51     }
52 
53     /** Whether metalava is running unit tests */
isUnderTestnull54     fun isUnderTest() = testEnvironment != null
55 
56     companion object {
57         /** Get an [ExecutionEnvironment] suitable for use by tests. */
58         fun forTest(stdin: String = ""): Triple<ExecutionEnvironment, StringWriter, StringWriter> {
59             val stdoutString = StringWriter()
60             val stderrString = StringWriter()
61             val stdout = PrintWriter(stdoutString)
62             val stderr = PrintWriter(stderrString)
63             return Triple(
64                 ExecutionEnvironment(
65                     stdout = stdout,
66                     stderr = stderr,
67                     stdin = stdin.byteInputStream(),
68                     reporterEnvironment =
69                         DefaultReporterEnvironment(
70                             stdout = stdout,
71                             stderr = stderr,
72                         ),
73                 ),
74                 stdoutString,
75                 stderrString,
76             )
77         }
78     }
79 }
80