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.DefaultReporterEnvironment
21 import com.android.tools.metalava.ENV_VAR_METALAVA_DUMP_ARGV
22 import com.android.tools.metalava.ReporterEnvironment
23 import com.android.tools.metalava.model.source.SourceModelProvider
24 import java.io.OutputStreamWriter
25 import java.io.PrintWriter
26 import java.io.StringWriter
27 
28 /**
29  * Encapsulates information provided by the execution environment.
30  *
31  * This supports two environments:
32  * 1. The standard command line application.
33  * 2. Tests.
34  */
35 data class ExecutionEnvironment(
36     val stdout: PrintWriter = PrintWriter(OutputStreamWriter(System.out)),
37     val stderr: PrintWriter = PrintWriter(OutputStreamWriter(System.err)),
38     val reporterEnvironment: ReporterEnvironment = DefaultReporterEnvironment(),
39     val testEnvironment: TestEnvironment? = null,
40 ) {
41     /** Whether metalava is being invoked as part of an Android platform build */
isBuildingAndroidnull42     fun isBuildingAndroid() = System.getenv("ANDROID_BUILD_TOP") != null && !isUnderTest()
43 
44     /** Whether to suppress dumping of information to stderr by a [SourceModelProvider]. */
45     fun disableStderrDumping(): Boolean {
46         return !assertionsEnabled() &&
47             System.getenv(ENV_VAR_METALAVA_DUMP_ARGV) == null &&
48             !isUnderTest()
49     }
50 
51     /** Whether metalava is running unit tests */
isUnderTestnull52     fun isUnderTest() = testEnvironment != null
53 
54     companion object {
55         /** Get an [ExecutionEnvironment] suitable for use by tests. */
56         fun forTest(): Triple<ExecutionEnvironment, StringWriter, StringWriter> {
57             val stdoutString = StringWriter()
58             val stderrString = StringWriter()
59             val stdout = PrintWriter(stdoutString)
60             val stderr = PrintWriter(stderrString)
61             return Triple(
62                 ExecutionEnvironment(
63                     stdout = stdout,
64                     stderr = stderr,
65                     reporterEnvironment =
66                         DefaultReporterEnvironment(
67                             stdout = stdout,
68                             stderr = stderr,
69                         ),
70                 ),
71                 stdoutString,
72                 stderrString,
73             )
74         }
75     }
76 }
77