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

Commit

Permalink
update parsing to fix issue with long prefix parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
christopherperry committed Apr 6, 2018
1 parent 65cbadf commit ce4d929
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 32 deletions.
24 changes: 12 additions & 12 deletions composer/src/main/kotlin/com/gojuno/composer/TestRun.kt
Original file line number Diff line number Diff line change
Expand Up @@ -181,21 +181,21 @@ private fun pullTestFiles(adbDevice: AdbDevice, test: InstrumentationTest, outpu
)
}

class LogLineParser {
fun parseTestClassAndName(logLine: String): Pair<String, String>? {
if (!logLine.trimStart('I', '/').startsWith("TestRunner")) return null
internal fun String.parseTestClassAndName(): Pair<String, String>? {
val index = indexOf("TestRunner")
if (index < 0) return null

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

if (tokens[1] == " started" || tokens[1] == " finished") {
return tokens[2].substringAfter("(").removeSuffix(")") to tokens[2].substringBefore("(").trim()
}
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, logLineParser: LogLineParser = LogLineParser()): Observable<Pair<String, String>> = Observable
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 } }
.flatMap { (logsDir, fullLogcatFile) ->
Expand All @@ -210,8 +210,8 @@ private fun saveLogcat(adbDevice: AdbDevice, logsDir: File, logLineParser: LogLi

// 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>? = logLineParser.parseTestClassAndName(newline)
val finishedTest: Pair<String, String>? = logLineParser.parseTestClassAndName(newline)
val startedTest: Pair<String, String>? = newline.parseTestClassAndName()
val finishedTest: Pair<String, String>? = newline.parseTestClassAndName()

result(
logcat = logcat,
Expand Down
67 changes: 47 additions & 20 deletions composer/src/test/kotlin/com/gojuno/composer/LogLineParserSpec.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,63 @@ import org.jetbrains.spek.api.dsl.it

class LogLineParserSpec : Spek({

val parser = LogLineParser()
context("parse TestRunner log line with long prefix") {

context("parse test class and name") {
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("parses old TestRunner start logs") {
assertThat(parser.parseTestClassAndName("I/TestRunner( 123): started: someTestMethod(com.example.SampleClass)"))
.isEqualTo(Pair("com.example.SampleClass", "someTestMethod"))
it("extracts test class and method") {
assertThat(args).isEqualTo(Pair("com.example.SampleClass", "someTestMethod"))
}
}

it("parses old TestRunner finish logs") {
assertThat(parser.parseTestClassAndName("I/TestRunner( 123): finished: someTestMethod(com.example.SampleClass)"))
.isEqualTo(Pair("com.example.SampleClass", "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(Pair("com.example.SampleClass", "someTestMethod"))
}
}
}

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

it("parses new TestRunner start logs") {
assertThat(parser.parseTestClassAndName("TestRunner: started: someTestMethod(com.example.SampleClass)"))
.isEqualTo(Pair("com.example.SampleClass", "someTestMethod"))
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(Pair("com.example.SampleClass", "someTestMethod"))
}
}

it("parses new TestRunner finish logs") {
assertThat(parser.parseTestClassAndName("TestRunner: finished: someTestMethod(com.example.SampleClass)"))
.isEqualTo(Pair("com.example.SampleClass", "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(Pair("com.example.SampleClass", "someTestMethod"))
}
}
}

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

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

it("does not parse other logs") {
assertThat(parser.parseTestClassAndName("")).isNull()
assertThat(parser.parseTestClassAndName("blah: blah: blah")).isNull()
assertThat(parser.parseTestClassAndName("aldkjf ;aldkj ffha a;ldjfoioihfads")).isNull()
assertThat(parser.parseTestClassAndName("I/TestRunner( 123):")).isNull()
assertThat(parser.parseTestClassAndName("TestRunner")).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 ce4d929

Please sign in to comment.