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

Commit

Permalink
Fix Logcat Parsing (#133)
Browse files Browse the repository at this point in the history
* Fix Logcat Parsing
  • Loading branch information
christopherperry authored and yunikkk committed Apr 19, 2018
1 parent c095a4a commit 5319795
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 9 deletions.
25 changes: 16 additions & 9 deletions composer/src/main/kotlin/com/gojuno/composer/TestRun.kt
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,20 @@ private fun pullTestFiles(adbDevice: AdbDevice, test: InstrumentationTest, outpu
)
}

internal fun String.parseTestClassAndName(): Pair<String, String>? {
val index = indexOf("TestRunner")
if (index < 0) return null

val tokens = substring(index, length).split(':')
if (tokens.size != 3) return null

val startedOrFinished = tokens[1].trimStart()
if (startedOrFinished == "started" || startedOrFinished == "finished") {
return tokens[2].substringAfter("(").removeSuffix(")") to tokens[2].substringBefore("(").trim()
}
return null
}

private fun saveLogcat(adbDevice: AdbDevice, logsDir: File): Observable<Pair<String, String>> = Observable
.just(logsDir to logcatFileForDevice(logsDir))
.flatMap { (logsDir, fullLogcatFile) -> adbDevice.redirectLogcatToFile(fullLogcatFile).toObservable().map { logsDir to fullLogcatFile } }
Expand All @@ -194,17 +208,10 @@ private fun saveLogcat(adbDevice: AdbDevice, logsDir: File): Observable<Pair<Str
false -> "${previous.logcat}\n$newline"
}

fun String.parseTestClassAndName(prefix: String): Pair<String, String>? = this.substringAfter(prefix, missingDelimiterValue = "").let {
when (it) {
"" -> null
else -> it.substringAfter("(").removeSuffix(")") to it.substringBefore("(")
}
}

// Implicitly expecting to see logs from `android.support.test.internal.runner.listener.LogRunListener`.
// Was not able to find more reliable solution to capture logcat per test.
val startedTest: Pair<String, String>? = newline.parseTestClassAndName("TestRunner: started: ")
val finishedTest: Pair<String, String>? = newline.parseTestClassAndName("TestRunner: finished: ")
val startedTest: Pair<String, String>? = newline.parseTestClassAndName()
val finishedTest: Pair<String, String>? = newline.parseTestClassAndName()

result(
logcat = logcat,
Expand Down
69 changes: 69 additions & 0 deletions composer/src/test/kotlin/com/gojuno/composer/LogLineParserSpec.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.gojuno.composer

import org.assertj.core.api.Assertions.assertThat
import org.jetbrains.spek.api.Spek
import org.jetbrains.spek.api.dsl.context
import org.jetbrains.spek.api.dsl.it

class LogLineParserSpec : Spek({

context("parse TestRunner log line with long prefix") {

context("parse started log") {
val args by memoized {
"04-06 00:25:49.747 28632 28650 I TestRunner: started: someTestMethod(com.example.SampleClass)".parseTestClassAndName()
}

it("extracts test class and method") {
assertThat(args).isEqualTo("com.example.SampleClass" to "someTestMethod")
}
}

context("parse finished log") {
val args by memoized {
"04-06 00:25:49.747 28632 28650 I TestRunner: finished: someTestMethod(com.example.SampleClass)".parseTestClassAndName()
}

it("extracts test class and method") {
assertThat(args).isEqualTo("com.example.SampleClass" to "someTestMethod")
}
}
}

context("parse TestRunner log line with short prefix") {

context("parse started log") {

val args by memoized {
"I/TestRunner( 123): started: someTestMethod(com.example.SampleClass)".parseTestClassAndName()
}

it("extracts test class and method") {
assertThat(args).isEqualTo("com.example.SampleClass" to "someTestMethod")
}
}

context("parse finished log") {

val args by memoized {
"I/TestRunner( 123): finished: someTestMethod(com.example.SampleClass)".parseTestClassAndName()
}

it("extracts test class and method") {
assertThat(args).isEqualTo("com.example.SampleClass" to "someTestMethod")
}
}
}

context("parse non TestRunner started/finished logs") {

it("does not parse empty log") {
assertThat("".parseTestClassAndName()).isNull()
}

it("does not parse TestRunner logs without started/finished") {
assertThat("I/TestRunner( 123):".parseTestClassAndName()).isNull()
assertThat("04-06 00:25:49.747 28632 28650 I TestRunner:".parseTestClassAndName()).isNull()
}
}
})

0 comments on commit 5319795

Please sign in to comment.