-
Notifications
You must be signed in to change notification settings - Fork 47
/
ArtifactsTestListener.kt
86 lines (77 loc) · 2.94 KB
/
ArtifactsTestListener.kt
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
package com.avito.runner.scheduler.listener
import com.avito.android.Result
import com.avito.logger.LoggerFactory
import com.avito.logger.create
import com.avito.runner.service.listener.TestListener
import com.avito.runner.service.model.TestCase
import com.avito.runner.service.model.TestCaseRun
import com.avito.runner.service.model.TestCaseRun.Result.Failed
import com.avito.runner.service.model.TestCaseRun.Result.Failed.InfrastructureError
import com.avito.runner.service.model.TestCaseRun.Result.Ignored
import com.avito.runner.service.model.TestCaseRun.Result.Passed
import com.avito.runner.service.worker.device.Device
import com.avito.utils.deleteRecursively
import java.io.File
import kotlin.io.path.ExperimentalPathApi
import kotlin.io.path.createTempDirectory
import kotlin.io.path.div
internal class ArtifactsTestListener(
private val lifecycleListener: TestLifecycleListener,
loggerFactory: LoggerFactory
) : TestListener {
private val logger = loggerFactory.create<ArtifactsTestListener>()
override fun started(
device: Device,
targetPackage: String,
test: TestCase,
executionNumber: Int
) {
lifecycleListener.started(
test = test,
device = device,
executionNumber = executionNumber
)
}
@ExperimentalPathApi
override fun finished(
device: Device,
test: TestCase,
targetPackage: String,
result: TestCaseRun.Result,
durationMilliseconds: Long,
executionNumber: Int,
testArtifactsDir: Result<File>
) {
val tempDirectory = createTempDirectory()
val testResult = when (result) {
Passed,
is Failed.InRun -> {
val artifacts = testArtifactsDir.flatMap { dir ->
// last /. means to adb to copy recursively, and do not copy the last
// example:
// - from: /sdcard/Android/someDir/ to: /xx ; will copy to /xx/someDir/ and not recursive
// - from: /sdcard/android/someDir/. to: /xx ; will copy to /xx and recursive
// todo move this knowledge under adb layer
device.pull(from = dir.toPath() / ".", to = tempDirectory)
}
TestResult.Complete(artifacts)
}
is InfrastructureError ->
TestResult.Incomplete(result)
Ignored ->
TestResult.Incomplete(
InfrastructureError.Unexpected(
IllegalStateException("Instrumentation executed Ignored test")
)
)
}
lifecycleListener.finished(
result = testResult,
test = test,
executionNumber = executionNumber,
)
tempDirectory.deleteRecursively().onFailure { error ->
logger.warn("Can't clear temp directory: $tempDirectory", error)
}
}
}