diff --git a/staticlibs/testutils/devicetests/com/android/testutils/ConnectivityDiagnosticsCollector.kt b/staticlibs/testutils/devicetests/com/android/testutils/ConnectivityDiagnosticsCollector.kt index 4b9429b34a..c7d6850625 100644 --- a/staticlibs/testutils/devicetests/com/android/testutils/ConnectivityDiagnosticsCollector.kt +++ b/staticlibs/testutils/devicetests/com/android/testutils/ConnectivityDiagnosticsCollector.kt @@ -430,32 +430,19 @@ class ConnectivityDiagnosticsCollector : BaseMetricListener() { * @param dumpsysCmd The dumpsys command to run (for example "connectivity"). * @param exceptionContext An exception to write a stacktrace to the dump for context. */ - fun collectDumpsys(dumpsysCmd: String, exceptionContext: Throwable? = null) = - collectCommandOutput("dumpsys $dumpsysCmd", exceptionContext = exceptionContext) - - /** - * Add the output of a command to the test data dump. - * - *

The output will be collected immediately, and exported to a test artifact file when the - * test ends. - * @param cmd The command to run. Stdout of the command will be collected. - * @param shell The shell to run the command in. - * @param exceptionContext An exception to write a stacktrace to the dump for context. - */ - fun collectCommandOutput( - cmd: String, - shell: String = "sh", - exceptionContext: Throwable? = null - ) { - Log.i(TAG, "Collecting '$cmd' for test artifacts") + fun collectDumpsys(dumpsysCmd: String, exceptionContext: Throwable? = null) { + Log.i(TAG, "Collecting dumpsys $dumpsysCmd for test artifacts") PrintWriter(buffer).let { - it.println("--- $cmd at ${ZonedDateTime.now()} ---") + it.println("--- Dumpsys $dumpsysCmd at ${ZonedDateTime.now()} ---") maybeWriteExceptionContext(it, exceptionContext) it.flush() } - - runCommandInShell(cmd, shell) { stdout, _ -> - stdout.copyTo(buffer) + ParcelFileDescriptor.AutoCloseInputStream( + InstrumentationRegistry.getInstrumentation().uiAutomation.executeShellCommand( + "dumpsys $dumpsysCmd" + ) + ).use { + it.copyTo(buffer) } } diff --git a/staticlibs/testutils/devicetests/com/android/testutils/ShellUtil.kt b/staticlibs/testutils/devicetests/com/android/testutils/ShellUtil.kt deleted file mode 100644 index fadc2ab4e3..0000000000 --- a/staticlibs/testutils/devicetests/com/android/testutils/ShellUtil.kt +++ /dev/null @@ -1,75 +0,0 @@ -/* - * Copyright (C) 2025 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -@file:JvmName("ShellUtil") - -package com.android.testutils - -import android.app.UiAutomation -import android.os.ParcelFileDescriptor.AutoCloseInputStream -import android.os.ParcelFileDescriptor.AutoCloseOutputStream -import androidx.test.platform.app.InstrumentationRegistry -import java.io.InputStream - -/** - * Run a command in a shell. - * - * Compared to [UiAutomation.executeShellCommand], this allows running commands with pipes and - * redirections. [UiAutomation.executeShellCommand] splits the command on spaces regardless of - * quotes, so it is not able to run commands like `sh -c "echo 123 > some_file"`. - * - * @param cmd Shell command to run. - * @param shell Command used to run the shell. - * @param outputProcessor Function taking stdout, stderr as argument. The streams will be closed - * when this function returns. - * @return Result of [outputProcessor]. - */ -fun runCommandInShell( - cmd: String, - shell: String = "sh", - outputProcessor: (InputStream, InputStream) -> T, -): T { - val (stdout, stdin, stderr) = InstrumentationRegistry.getInstrumentation().uiAutomation - .executeShellCommandRwe(shell) - AutoCloseOutputStream(stdin).bufferedWriter().use { it.write(cmd) } - AutoCloseInputStream(stdout).use { outStream -> - AutoCloseInputStream(stderr).use { errStream -> - return outputProcessor(outStream, errStream) - } - } -} - -/** - * Run a command in a shell. - * - * Overload of [runCommandInShell] that reads and returns stdout as String. - */ -fun runCommandInShell( - cmd: String, - shell: String = "sh", -) = runCommandInShell(cmd, shell) { stdout, _ -> - stdout.reader().use { it.readText() } -} - -/** - * Run a command in a root shell. - * - * This is generally only usable on devices on which [DeviceInfoUtils.isDebuggable] is true. - * @see runCommandInShell - */ -fun runCommandInRootShell( - cmd: String -) = runCommandInShell(cmd, shell = "su root sh")