Skip to content
This repository was archived by the owner on Dec 7, 2019. It is now read-only.

Commit 5319795

Browse files
christopherperryyunikkk
authored andcommitted
Fix Logcat Parsing (#133)
* Fix Logcat Parsing
1 parent c095a4a commit 5319795

File tree

2 files changed

+85
-9
lines changed

2 files changed

+85
-9
lines changed

composer/src/main/kotlin/com/gojuno/composer/TestRun.kt

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,20 @@ private fun pullTestFiles(adbDevice: AdbDevice, test: InstrumentationTest, outpu
181181
)
182182
}
183183

184+
internal fun String.parseTestClassAndName(): Pair<String, String>? {
185+
val index = indexOf("TestRunner")
186+
if (index < 0) return null
187+
188+
val tokens = substring(index, length).split(':')
189+
if (tokens.size != 3) return null
190+
191+
val startedOrFinished = tokens[1].trimStart()
192+
if (startedOrFinished == "started" || startedOrFinished == "finished") {
193+
return tokens[2].substringAfter("(").removeSuffix(")") to tokens[2].substringBefore("(").trim()
194+
}
195+
return null
196+
}
197+
184198
private fun saveLogcat(adbDevice: AdbDevice, logsDir: File): Observable<Pair<String, String>> = Observable
185199
.just(logsDir to logcatFileForDevice(logsDir))
186200
.flatMap { (logsDir, fullLogcatFile) -> adbDevice.redirectLogcatToFile(fullLogcatFile).toObservable().map { logsDir to fullLogcatFile } }
@@ -194,17 +208,10 @@ private fun saveLogcat(adbDevice: AdbDevice, logsDir: File): Observable<Pair<Str
194208
false -> "${previous.logcat}\n$newline"
195209
}
196210

197-
fun String.parseTestClassAndName(prefix: String): Pair<String, String>? = this.substringAfter(prefix, missingDelimiterValue = "").let {
198-
when (it) {
199-
"" -> null
200-
else -> it.substringAfter("(").removeSuffix(")") to it.substringBefore("(")
201-
}
202-
}
203-
204211
// Implicitly expecting to see logs from `android.support.test.internal.runner.listener.LogRunListener`.
205212
// Was not able to find more reliable solution to capture logcat per test.
206-
val startedTest: Pair<String, String>? = newline.parseTestClassAndName("TestRunner: started: ")
207-
val finishedTest: Pair<String, String>? = newline.parseTestClassAndName("TestRunner: finished: ")
213+
val startedTest: Pair<String, String>? = newline.parseTestClassAndName()
214+
val finishedTest: Pair<String, String>? = newline.parseTestClassAndName()
208215

209216
result(
210217
logcat = logcat,
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.gojuno.composer
2+
3+
import org.assertj.core.api.Assertions.assertThat
4+
import org.jetbrains.spek.api.Spek
5+
import org.jetbrains.spek.api.dsl.context
6+
import org.jetbrains.spek.api.dsl.it
7+
8+
class LogLineParserSpec : Spek({
9+
10+
context("parse TestRunner log line with long prefix") {
11+
12+
context("parse started log") {
13+
val args by memoized {
14+
"04-06 00:25:49.747 28632 28650 I TestRunner: started: someTestMethod(com.example.SampleClass)".parseTestClassAndName()
15+
}
16+
17+
it("extracts test class and method") {
18+
assertThat(args).isEqualTo("com.example.SampleClass" to "someTestMethod")
19+
}
20+
}
21+
22+
context("parse finished log") {
23+
val args by memoized {
24+
"04-06 00:25:49.747 28632 28650 I TestRunner: finished: someTestMethod(com.example.SampleClass)".parseTestClassAndName()
25+
}
26+
27+
it("extracts test class and method") {
28+
assertThat(args).isEqualTo("com.example.SampleClass" to "someTestMethod")
29+
}
30+
}
31+
}
32+
33+
context("parse TestRunner log line with short prefix") {
34+
35+
context("parse started log") {
36+
37+
val args by memoized {
38+
"I/TestRunner( 123): started: someTestMethod(com.example.SampleClass)".parseTestClassAndName()
39+
}
40+
41+
it("extracts test class and method") {
42+
assertThat(args).isEqualTo("com.example.SampleClass" to "someTestMethod")
43+
}
44+
}
45+
46+
context("parse finished log") {
47+
48+
val args by memoized {
49+
"I/TestRunner( 123): finished: someTestMethod(com.example.SampleClass)".parseTestClassAndName()
50+
}
51+
52+
it("extracts test class and method") {
53+
assertThat(args).isEqualTo("com.example.SampleClass" to "someTestMethod")
54+
}
55+
}
56+
}
57+
58+
context("parse non TestRunner started/finished logs") {
59+
60+
it("does not parse empty log") {
61+
assertThat("".parseTestClassAndName()).isNull()
62+
}
63+
64+
it("does not parse TestRunner logs without started/finished") {
65+
assertThat("I/TestRunner( 123):".parseTestClassAndName()).isNull()
66+
assertThat("04-06 00:25:49.747 28632 28650 I TestRunner:".parseTestClassAndName()).isNull()
67+
}
68+
}
69+
})

0 commit comments

Comments
 (0)