Skip to content

Commit

Permalink
Merge pull request #63 from badoo/remove_uninstalled_apps_from_hashmap
Browse files Browse the repository at this point in the history
Remove app from installed apps cache
  • Loading branch information
idyatlov authored Jul 4, 2024
2 parents 298e63c + e8b4a80 commit 9858db7
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.malinskiy.marathon
import com.malinskiy.marathon.android.AndroidConfiguration
import com.malinskiy.marathon.android.DEFAULT_APPLICATION_PM_CLEAR
import com.malinskiy.marathon.android.DEFAULT_AUTO_GRANT_PERMISSION
import com.malinskiy.marathon.android.DEFAULT_DEVICE_CLEANUP_SCRIPT
import com.malinskiy.marathon.android.DEFAULT_INSTALL_OPTIONS
import com.malinskiy.marathon.android.DEFAULT_USED_STORAGE_THRESHOLD_PERCENTS
import com.malinskiy.marathon.android.defaultInitTimeoutMillis
Expand Down Expand Up @@ -98,7 +97,6 @@ private fun createAndroidConfiguration(
}
}
?: SerialStrategy.AUTOMATIC
val cleanupDeviceScript = extension.cleanupDeviceScript ?: DEFAULT_DEVICE_CLEANUP_SCRIPT
val usedStorageThresholdInPercents = extension.usedStorageThresholdInPercents ?: DEFAULT_USED_STORAGE_THRESHOLD_PERCENTS

return AndroidConfiguration(
Expand All @@ -114,7 +112,6 @@ private fun createAndroidConfiguration(
installOptions,
preferableRecorderType,
serialStrategy,
cleanupDeviceScript,
usedStorageThresholdInPercents
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ open class MarathonExtension {
//Android specific for now
var autoGrantPermission: Boolean? = null
var instrumentationArgs: MutableMap<String, String> = mutableMapOf()
var cleanupDeviceScript: String? = null
var usedStorageThresholdInPercents: Int? = null

//Kotlin way
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ const val DEFAULT_AUTO_GRANT_PERMISSION = false
const val DEFAULT_APPLICATION_PM_CLEAR = false
const val DEFAULT_TEST_APPLICATION_PM_CLEAR = false
const val DEFAULT_INSTALL_OPTIONS = ""
const val DEFAULT_USED_STORAGE_THRESHOLD_PERCENTS = 80
const val DEFAULT_DEVICE_CLEANUP_SCRIPT = "pm list packages -3 | grep -E '\\.test\$' | tr -d '\\r' | cut -d ':' -f 2 | xargs -n1 -t pm uninstall"
const val DEFAULT_USED_STORAGE_THRESHOLD_PERCENTS = 85

data class AndroidConfiguration(
val androidSdk: File,
Expand All @@ -29,7 +28,6 @@ data class AndroidConfiguration(
val installOptions: String = DEFAULT_INSTALL_OPTIONS,
val preferableRecorderType: DeviceFeature? = null,
val serialStrategy: SerialStrategy = SerialStrategy.AUTOMATIC,
val cleanupDeviceScript: String = DEFAULT_DEVICE_CLEANUP_SCRIPT,
val usedStorageThresholdInPercents: Int = DEFAULT_USED_STORAGE_THRESHOLD_PERCENTS
) : VendorConfiguration {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class AndroidAppInstaller(
private const val MAX_RETIRES = 3
private const val MARSHMALLOW_VERSION_CODE = 23
private const val MD5_HASH_SIZE = 32
private const val INSTALLED_TEST_APPS_SCRIPT = "pm list packages -3 | grep -E '\\.test\$' | tr -d '\\r' | cut -d ':' -f 2"
private const val PACKAGE_PREFIX = "package:"
}

Expand Down Expand Up @@ -79,9 +80,20 @@ class AndroidAppInstaller(
val usedStorageThresholdInPercents = androidConfiguration.usedStorageThresholdInPercents
if (storageUsedPercentage > usedStorageThresholdInPercents) {
logger.warn { "On ${device.serialNumber} used more than $usedStorageThresholdInPercents% of storage" }
androidConfiguration.cleanupDeviceScript.let {
logger.info { "Launching cleanup shell script `$it`" }
device.safeExecuteShellCommand(it).let { logger.info { it } }
val appsToClean = device.safeExecuteShellCommand(INSTALLED_TEST_APPS_SCRIPT).lines()
logger.info { "Removing ${appsToClean.size} apps on ${device.serialNumber}" }
appsToClean.forEach {
try {
val result = device.safeUninstallPackage(it)
if (result == "Success") {
logger.info { "Uninstalled $it - $result" }
installedApps[device.serialNumber]?.remove(it)
} else {
logger.error { "Error while uninstalling $it on ${device.serialNumber} : $result" }
}
} catch (ignored: Throwable) {
logger.error(ignored) { "Error while uninstalling $it on ${device.serialNumber}" }
}
}
}
}
Expand Down

0 comments on commit 9858db7

Please sign in to comment.