-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeviceFileManager.kt
More file actions
99 lines (87 loc) · 3.64 KB
/
DeviceFileManager.kt
File metadata and controls
99 lines (87 loc) · 3.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package com.telefonica.androidsnaptesting
import com.android.build.gradle.internal.tasks.DeviceProviderInstrumentTestTask
import com.android.build.gradle.internal.testing.ConnectedDevice
import org.gradle.api.file.RegularFile
import org.gradle.api.provider.ProviderFactory
import java.io.File
import java.util.concurrent.TimeUnit
fun DeviceProviderInstrumentTestTask.deviceFileManager(
applicationId: String,
adbExecutablePath: String,
providerFactory: ProviderFactory,
): DeviceFileManager = DeviceFileManager(this.deviceProviderFactory, applicationId, adbExecutablePath, providerFactory)
class DeviceFileManager(
private val deviceProviderFactory: DeviceProviderInstrumentTestTask.DeviceProviderFactory,
private val applicationId: String,
private val adbExecutablePath: String,
private val providerFactory: ProviderFactory,
) {
fun pullRecordedSnapshots(destinationPath: String) {
pullSnapshots("recorded", destinationPath)
}
fun pullFailuresSnapshots(destinationPath: String) {
pullSnapshots("failures", destinationPath)
}
fun clearAllSnapshots() {
withConnectedDevices { devices ->
devices.forEach { device ->
runAdb(device.serialNumber, "shell", "rm", "-rf", getDeviceAndroidSnaptestingRootAbsolutePath())
}
}
}
private fun getDeviceAndroidSnaptestingRootAbsolutePath(): String =
"/sdcard/Download/android-snaptesting/$applicationId"
private fun getDeviceAndroidSnaptestingSubfolderAbsolutePath(subFolder: String): String =
"${getDeviceAndroidSnaptestingRootAbsolutePath()}/$subFolder"
@Suppress("UnstableApiUsage")
private fun withConnectedDevices(runnable: (List<ConnectedDevice>) -> Unit) {
deviceProviderFactory.getDeviceProvider(
providerFactory.provider {
RegularFile { File(adbExecutablePath) }
},
System.getenv("ANDROID_SERIAL"),
).let {
it.use {
runnable(it.devices.filterIsInstance<ConnectedDevice>())
}
}
}
private fun pullSnapshots(
androidSnaptestingSubFolderInDevice: String,
destinationPath: String,
) {
val remotePath = getDeviceAndroidSnaptestingSubfolderAbsolutePath(androidSnaptestingSubFolderInDevice)
withConnectedDevices { devices ->
devices.forEach { device ->
val serial = device.serialNumber
// List files in the remote folder; ignore errors if the folder doesn't exist yet
val lsOutput = runAdbCapture(serial, "shell", "ls", remotePath)
val fileNames = lsOutput.lines()
.map { it.trim() }
.filter { it.isNotBlank() && !it.startsWith("ls:") && !it.contains("No such file") }
// Pull each file to the local destination
fileNames.forEach { fileName ->
runAdb(serial, "pull", "$remotePath/$fileName", "$destinationPath/$fileName")
}
}
}
}
private fun runAdb(serial: String, vararg args: String) {
val output = runAdbCapture(serial, *args)
println(output)
}
private fun runAdbCapture(serial: String, vararg args: String): String {
val command = buildList {
add(adbExecutablePath)
add("-s")
add(serial)
addAll(args.toList())
}
val process = ProcessBuilder(command)
.redirectErrorStream(true)
.start()
val output = process.inputStream.bufferedReader().readText()
process.waitFor(60, TimeUnit.SECONDS)
return output
}
}