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

Commit f4134b3

Browse files
Only match instrumentation entries with different status codes, add tests for instrumentation entries parsing. (#20)
1 parent fcddb77 commit f4134b3

File tree

6 files changed

+586
-428
lines changed

6 files changed

+586
-428
lines changed

ci/build.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ BUILD_COMMAND+="apt-get update && apt-get --assume-yes install git && "
1515

1616
if [ "$USER_ID" == "0" ]; then
1717
echo "Warning: running as r00t."
18-
else
18+
else
1919
BUILD_COMMAND+="apt-get --assume-yes install sudo && "
2020
BUILD_COMMAND+="groupadd --gid $USER_ID build_user && "
2121
BUILD_COMMAND+="useradd --shell /bin/bash --uid $USER_ID --gid $USER_ID --create-home build_user && "

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

+29-9
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ private fun String.substringBetween(first: String, second: String): String {
5454
return substring(startIndex, endIndex)
5555
}
5656

57-
private fun String.parseInstrumentationStatusValue(key: String): String =
58-
substringBetween("INSTRUMENTATION_STATUS: $key=", "INSTRUMENTATION_STATUS").trim()
57+
private fun String.parseInstrumentationStatusValue(key: String): String = substringBetween("INSTRUMENTATION_STATUS: $key=", "INSTRUMENTATION_STATUS")
58+
.trim()
5959

6060
private fun parseInstrumentationEntry(str: String): InstrumentationEntry =
6161
InstrumentationEntry(
@@ -74,7 +74,7 @@ private fun parseInstrumentationEntry(str: String): InstrumentationEntry =
7474
}
7575
.let { statusCode ->
7676
when (statusCode) {
77-
null -> throw IllegalStateException("Unknown test result status code, please report that to Composer maintainers $str")
77+
null -> throw IllegalStateException("Unknown test result status code [$statusCode], please report that to Composer maintainers $str")
7878
else -> statusCode
7979
}
8080
},
@@ -86,12 +86,13 @@ fun readInstrumentationOutput(output: File): Observable<InstrumentationEntry> {
8686
data class result(val buffer: String = "", val readyForProcessing: Boolean = false)
8787

8888
return tail(output)
89+
.map { it.trim() }
8990
// `INSTRUMENTATION_CODE: -1` is last line printed by instrumentation, even if 0 tests were run.
9091
.takeWhile { !it.startsWith("INSTRUMENTATION_CODE") }
9192
.scan(result()) { previousResult, newLine ->
9293
val buffer = when (previousResult.readyForProcessing) {
9394
true -> newLine
94-
false -> "${previousResult.buffer}\n$newLine"
95+
false -> "${previousResult.buffer}${System.lineSeparator()}$newLine"
9596
}
9697

9798
result(buffer = buffer, readyForProcessing = newLine.startsWith("INSTRUMENTATION_STATUS_CODE"))
@@ -101,7 +102,7 @@ fun readInstrumentationOutput(output: File): Observable<InstrumentationEntry> {
101102
}
102103

103104
fun Observable<InstrumentationEntry>.asTests(): Observable<Test> {
104-
data class result(val entries: List<InstrumentationEntry> = emptyList(), val tests: List<Test> = emptyList())
105+
data class result(val entries: List<InstrumentationEntry> = emptyList(), val tests: List<Test> = emptyList(), val totalTestsCount: Int = 0)
105106

106107
return this
107108
.scan(result()) { previousResult, newEntry ->
@@ -110,7 +111,15 @@ fun Observable<InstrumentationEntry>.asTests(): Observable<Test> {
110111
.mapIndexed { index, first ->
111112
val second = entries
112113
.subList(index + 1, entries.size)
113-
.firstOrNull { first.clazz == it.clazz && first.test == it.test && first.current == it.current }
114+
.firstOrNull {
115+
first.clazz == it.clazz
116+
&&
117+
first.test == it.test
118+
&&
119+
first.current == it.current
120+
&&
121+
first.statusCode != it.statusCode
122+
}
114123

115124
if (second == null) null else first to second
116125
}
@@ -123,18 +132,29 @@ fun Observable<InstrumentationEntry>.asTests(): Observable<Test> {
123132
StatusCode.Ok -> Passed
124133
StatusCode.Ignored -> Ignored
125134
StatusCode.Failure, StatusCode.AssumptionFailure -> Failed(stacktrace = second.stack)
126-
StatusCode.Start -> throw IllegalStateException("Unexpected Start code in second entry, please report that to Composer maintainers ($first, $second)")
135+
StatusCode.Start -> throw IllegalStateException("Unexpected status code [${second.statusCode}] in second entry, please report that to Composer maintainers ($first, $second)")
127136
},
128137
durationNanos = second.timestampNanos - first.timestampNanos
129138
)
130139
}
131140

132141
result(
133142
entries = entries.filter { entry -> tests.firstOrNull { it.className == entry.clazz && it.testName == entry.test } == null },
134-
tests = tests
143+
tests = tests,
144+
totalTestsCount = previousResult.totalTestsCount + tests.size
135145
)
136146
}
137-
.takeUntil { it.entries.count { it.current == it.numTests } >= 2 }
147+
.takeUntil {
148+
if (it.entries.count { it.current == it.numTests } == 2) {
149+
if (it.totalTestsCount < it.entries.first().numTests) {
150+
throw IllegalStateException("Less tests were emitted than Instrumentation reported: $it")
151+
}
152+
153+
true
154+
} else {
155+
false
156+
}
157+
}
138158
.filter { it.tests.isNotEmpty() }
139159
.flatMap { Observable.from(it.tests) }
140160
}

0 commit comments

Comments
 (0)