Merge "Fixed running pgrep on api 36 and above for Shell" into androidx-main am: c4e614557a
Original change: https://android-review.googlesource.com/c/platform/frameworks/support/+/3528062
Change-Id: Ifbf80b2fe84e40cbee2ec171ac35368a6176e64e
Signed-off-by: Automerger Merge Worker <[email protected]>
diff --git a/benchmark/benchmark-common/src/androidTest/java/androidx/benchmark/ShellBehaviorTest.kt b/benchmark/benchmark-common/src/androidTest/java/androidx/benchmark/ShellBehaviorTest.kt
index 0b43fce..9f0ebafe 100644
--- a/benchmark/benchmark-common/src/androidTest/java/androidx/benchmark/ShellBehaviorTest.kt
+++ b/benchmark/benchmark-common/src/androidTest/java/androidx/benchmark/ShellBehaviorTest.kt
@@ -29,20 +29,32 @@
/**
* This class collects tests of strange shell behavior, for the purpose of documenting and
- * validating how general the problems are.
+ * validating how general the problems are. For this reason, these tests call directly shell
+ * commands without referring to the actual implementations in [Shell]. To test the [Shell]
+ * implementations, please add to [ShellTest].
*/
@MediumTest
@SdkSuppress(minSdkVersion = 21)
@RunWith(AndroidJUnit4::class)
class ShellBehaviorTest {
+
/**
* Test validates consistent behavior of pgrep, for usage in discovering processes without
* needing to check stderr
*/
@Test
- fun pgrepLF() {
+ fun pgrepLFExecuteScript() {
+
+ val apiSpecificArgs =
+ setOfNotNull(
+ // aosp/3507001 -> needed to print full command line (so full package name)
+ if (Build.VERSION.SDK_INT >= 36) "-a" else null
+ )
+ .joinToString(" ")
+
// Should only be one process - this one!
- val pgrepOutput = Shell.executeScriptCaptureStdoutStderr("pgrep -l -f ${Packages.TEST}")
+ val pgrepOutput =
+ Shell.executeScriptCaptureStdoutStderr("pgrep -l -f $apiSpecificArgs ${Packages.TEST}")
if (Build.VERSION.SDK_INT >= 23) {
// API 23 has trailing whitespace after the package name for some reason :shrug:
diff --git a/benchmark/benchmark-common/src/androidTest/java/androidx/benchmark/ShellTest.kt b/benchmark/benchmark-common/src/androidTest/java/androidx/benchmark/ShellTest.kt
index 4df3dc0..1a1fac3 100644
--- a/benchmark/benchmark-common/src/androidTest/java/androidx/benchmark/ShellTest.kt
+++ b/benchmark/benchmark-common/src/androidTest/java/androidx/benchmark/ShellTest.kt
@@ -39,6 +39,7 @@
@SmallTest
@RunWith(AndroidJUnit4::class)
class ShellTest {
+
@Before
@After
fun setup() {
@@ -532,6 +533,26 @@
assertFalse(Shell.fullProcessNameMatchesProcess("example.app:ui", "example.ap"))
}
+ @Test
+ @SdkSuppress(minSdkVersion = 36)
+ fun pgrepLF() {
+ val processPids = Shell.pgrepLF(Packages.TEST)
+ assertTrue(
+ processPids.any { it.processName == Packages.TEST },
+ "expected package name to be contained in output:\n${processPids.joinToString("\n")}"
+ )
+ }
+
+ @Test
+ @SdkSuppress(minSdkVersion = 23, maxSdkVersion = 35)
+ fun pgrepLFBelowApi36() {
+ val processPids = Shell.pgrepLF(Packages.TEST)
+ assertTrue(
+ processPids.any { it.processName == Packages.TEST },
+ "expected package name to be contained in output:\n${processPids.joinToString("\n")}"
+ )
+ }
+
private fun pidof(packageName: String): Int? {
return Shell.getPidsForProcess(packageName).firstOrNull()
}
diff --git a/benchmark/benchmark-common/src/main/java/androidx/benchmark/Shell.kt b/benchmark/benchmark-common/src/main/java/androidx/benchmark/Shell.kt
index 31b3d41..25f4507 100644
--- a/benchmark/benchmark-common/src/main/java/androidx/benchmark/Shell.kt
+++ b/benchmark/benchmark-common/src/main/java/androidx/benchmark/Shell.kt
@@ -500,7 +500,14 @@
fun pgrepLF(pattern: String): List<ProcessPid> {
// Note: we use the unsafe variant for performance, since this is a
// common operation, and pgrep is stable after API 23 see [ShellBehaviorTest#pgrep]
- return ShellImpl.executeCommandUnsafe("pgrep -l -f $pattern")
+ val apiSpecificArgs =
+ setOfNotNull(
+ // aosp/3507001 -> needed to print full command line (so full package name)
+ if (Build.VERSION.SDK_INT >= 36) "-a" else null
+ )
+ .joinToString(" ")
+
+ return ShellImpl.executeCommandUnsafe("pgrep -l -f $apiSpecificArgs $pattern")
.split(Regex("\r?\n"))
.filter { it.isNotEmpty() }
.map {