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

Commit

Permalink
Make linefeed OS-agnostic in unit tests (#118)
Browse files Browse the repository at this point in the history
* Make linefeed OS-agnostic in unit tests

Ensure no tests fail due to \n differs to \r\n on Windows,
while maintaining Linux/MacOS compatibility

* Make linefeed OS-agnostic in HtmlReportSpec.kt

Left out of previous commit

* Revert "Make linefeed OS-agnostic in HtmlReportSpec.kt"

Template file is stored with \n and will not be affected
by operating system
  • Loading branch information
Nilzor authored and yunikkk committed Jan 22, 2018
1 parent 16fd284 commit 6e039df
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 67 deletions.
139 changes: 76 additions & 63 deletions composer/src/test/kotlin/com/gojuno/composer/InstrumentationSpec.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,7 @@ class InstrumentationSpec : Spek({
}

it("emits expected entries") {
// We have no control over system time in tests.
assertThat(entriesSubscriber.onNextEvents.map { it.copy(timestampNanos = 0) }).isEqualTo(listOf(
InstrumentationEntry(
numTests = 4,
stream = "com.example.test.TestClass:",
id = "AndroidJUnitRunner",
test = "test1",
clazz = "com.example.test.TestClass",
current = 1,
stack = "",
statusCode = StatusCode.Start,
timestampNanos = 0
),
InstrumentationEntry(
numTests = 4,
stream = """Error in test1(com.example.test.TestClass):
java.net.UnknownHostException: Test Exception
var stacktrace = """java.net.UnknownHostException: Test Exception
at com.example.test.TestClass.test1.1.invoke(TestClass.kt:245)
at com.example.test.TestClass.test1.1.invoke(TestClass.kt:44)
at com.example.test.TestClass.test1(TestClass.kt:238)
Expand Down Expand Up @@ -74,12 +58,11 @@ at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:59)
at android.support.test.runner.JunoAndroidRunner.onStart(JunoAndroidRunner.kt:107)
at android.app.Instrumentation.InstrumentationThread.run(Instrumentation.java:1932)""",
id = "AndroidJUnitRunner",
test = "test1",
clazz = "com.example.test.TestClass",
current = 1,
stack = """java.net.UnknownHostException: Test Exception
at android.app.Instrumentation.InstrumentationThread.run(Instrumentation.java:1932)"""
stacktrace = normalizeLinefeed(stacktrace)

var stream = """Error in test1(com.example.test.TestClass):
java.net.UnknownHostException: Test Exception
at com.example.test.TestClass.test1.1.invoke(TestClass.kt:245)
at com.example.test.TestClass.test1.1.invoke(TestClass.kt:44)
at com.example.test.TestClass.test1(TestClass.kt:238)
Expand Down Expand Up @@ -116,7 +99,30 @@ at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:59)
at android.support.test.runner.JunoAndroidRunner.onStart(JunoAndroidRunner.kt:107)
at android.app.Instrumentation.InstrumentationThread.run(Instrumentation.java:1932)""",
at android.app.Instrumentation.InstrumentationThread.run(Instrumentation.java:1932)"""
stream = normalizeLinefeed(stream)

// We have no control over system time in tests.
assertThat(entriesSubscriber.onNextEvents.map { it.copy(timestampNanos = 0) }).isEqualTo(listOf(
InstrumentationEntry(
numTests = 4,
stream = "com.example.test.TestClass:",
id = "AndroidJUnitRunner",
test = "test1",
clazz = "com.example.test.TestClass",
current = 1,
stack = "",
statusCode = StatusCode.Start,
timestampNanos = 0
),
InstrumentationEntry(
numTests = 4,
stream = stream,
id = "AndroidJUnitRunner",
test = "test1",
clazz = "com.example.test.TestClass",
current = 1,
stack = stacktrace,
statusCode = StatusCode.Failure,
timestampNanos = 0
),
Expand Down Expand Up @@ -208,13 +214,7 @@ at android.app.Instrumentation.InstrumentationThread.run(Instrumentation.java:19

it("emits expected tests") {
// We have no control over system time in tests.
assertThat(testsSubscriber.onNextEvents.map { it.copy(durationNanos = 0) }).isEqualTo(listOf(
InstrumentationTest(
index = 1,
total = 4,
className = "com.example.test.TestClass",
testName = "test1",
status = Failed(stacktrace = """java.net.UnknownHostException: Test Exception
var stacktrace = """java.net.UnknownHostException: Test Exception
at com.example.test.TestClass.test1.1.invoke(TestClass.kt:245)
at com.example.test.TestClass.test1.1.invoke(TestClass.kt:44)
at com.example.test.TestClass.test1(TestClass.kt:238)
Expand Down Expand Up @@ -251,7 +251,16 @@ at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:59)
at android.support.test.runner.JunoAndroidRunner.onStart(JunoAndroidRunner.kt:107)
at android.app.Instrumentation.InstrumentationThread.run(Instrumentation.java:1932)"""),
at android.app.Instrumentation.InstrumentationThread.run(Instrumentation.java:1932)"""

stacktrace = normalizeLinefeed(stacktrace)
assertThat(testsSubscriber.onNextEvents.map { it.copy(durationNanos = 0) }).isEqualTo(listOf(
InstrumentationTest(
index = 1,
total = 4,
className = "com.example.test.TestClass",
testName = "test1",
status = Failed(stacktrace = stacktrace),
durationNanos = 0
),
InstrumentationTest(
Expand Down Expand Up @@ -586,26 +595,7 @@ at android.app.Instrumentation.InstrumentationThread.run(Instrumentation.java:19

it("emits expected entries") {
// We have no control over system time in tests.
assertThat(entriesSubscriber.onNextEvents.map { it.copy(timestampNanos = 0) }).isEqualTo(listOf(
InstrumentationEntry(
numTests = 1,
stream = "com.example.test.TestClass:",
id = "AndroidJUnitRunner",
test = "violated",
clazz = "com.example.test.TestClass",
current = 1,
stack = "",
statusCode = StatusCode.Start,
timestampNanos = 0
),
InstrumentationEntry(
numTests = 1,
stream = "com.example.test.TestClass:",
id = "AndroidJUnitRunner",
test = "violated",
clazz = "com.example.test.TestClass",
current = 1,
stack = """org.junit.AssumptionViolatedException: got: "foo", expected: is "bar"
var stacktrace = """org.junit.AssumptionViolatedException: got: "foo", expected: is "bar"
at org.junit.Assume.assumeThat(Assume.java:95)
at com.example.test.TestClass.violated(TestClass.java:22)
at java.lang.reflect.Method.invoke(Native Method)
Expand Down Expand Up @@ -634,7 +624,29 @@ at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:58)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:375)
at android.app.Instrumentation${'$'}InstrumentationThread.run(Instrumentation.java:2074)""",
at android.app.Instrumentation${'$'}InstrumentationThread.run(Instrumentation.java:2074)"""
stacktrace = normalizeLinefeed(stacktrace)

assertThat(entriesSubscriber.onNextEvents.map { it.copy(timestampNanos = 0) }).isEqualTo(listOf(
InstrumentationEntry(
numTests = 1,
stream = "com.example.test.TestClass:",
id = "AndroidJUnitRunner",
test = "violated",
clazz = "com.example.test.TestClass",
current = 1,
stack = "",
statusCode = StatusCode.Start,
timestampNanos = 0
),
InstrumentationEntry(
numTests = 1,
stream = "com.example.test.TestClass:",
id = "AndroidJUnitRunner",
test = "violated",
clazz = "com.example.test.TestClass",
current = 1,
stack = stacktrace,
statusCode = StatusCode.AssumptionFailure,
timestampNanos = 0
)
Expand All @@ -659,13 +671,7 @@ at android.app.Instrumentation${'$'}InstrumentationThread.run(Instrumentation.ja
}

it("emits expected tests") {
assertThat(testsSubscriber.onNextEvents.map { it.copy(durationNanos = 0) }).isEqualTo(listOf(
InstrumentationTest(
index = 1,
total = 1,
className = "com.example.test.TestClass",
testName = "violated",
status = Ignored(stacktrace = """org.junit.AssumptionViolatedException: got: "foo", expected: is "bar"
var stacktrace = """org.junit.AssumptionViolatedException: got: "foo", expected: is "bar"
at org.junit.Assume.assumeThat(Assume.java:95)
at com.example.test.TestClass.violated(TestClass.java:22)
at java.lang.reflect.Method.invoke(Native Method)
Expand Down Expand Up @@ -694,7 +700,15 @@ at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:58)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:375)
at android.app.Instrumentation${'$'}InstrumentationThread.run(Instrumentation.java:2074)"""),
at android.app.Instrumentation${'$'}InstrumentationThread.run(Instrumentation.java:2074)"""
stacktrace = normalizeLinefeed(stacktrace)
assertThat(testsSubscriber.onNextEvents.map { it.copy(durationNanos = 0) }).isEqualTo(listOf(
InstrumentationTest(
index = 1,
total = 1,
className = "com.example.test.TestClass",
testName = "violated",
status = Ignored(stacktrace = stacktrace),
durationNanos = 0L
)
))
Expand Down Expand Up @@ -722,5 +736,4 @@ at android.app.Instrumentation${'$'}InstrumentationThread.run(Instrumentation.ja
)
}
}
})

})
12 changes: 8 additions & 4 deletions composer/src/test/kotlin/com/gojuno/composer/JUnitReportSpec.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import java.util.concurrent.TimeUnit.SECONDS

class JUnitReportSpec : Spek({

val LF = System.getProperty("line.separator")

context("write test run result as junit4 report to file") {

val adbDevice by memoized { AdbDevice(id = "testDevice", online = true) }
Expand All @@ -38,7 +40,7 @@ class JUnitReportSpec : Spek({
adbDevice = adbDevice,
className = "test.class.name2",
testName = "test2",
status = Failed(stacktrace = "multi\nline\nstacktrace"),
status = Failed(stacktrace = "multi${LF}line${LF}stacktrace"),
durationNanos = MILLISECONDS.toNanos(3250),
logcat = testFile(),
files = emptyList(),
Expand Down Expand Up @@ -68,7 +70,7 @@ class JUnitReportSpec : Spek({
adbDevice = adbDevice,
className = "test.class.name5",
testName = "test5",
status = Ignored("multi\nline\nstacktrace"),
status = Ignored("multi${LF}line${LF}stacktrace"),
durationNanos = SECONDS.toNanos(0),
logcat = testFile(),
files = emptyList(),
Expand All @@ -86,7 +88,7 @@ class JUnitReportSpec : Spek({
}

it("produces correct xml report") {
assertThat(outputFile.readText()).isEqualTo("""
var expected = """
<?xml version="1.0" encoding="UTF-8"?>
<testsuite name="com.gojuno.test" tests="5" failures="1" errors="0" skipped="2" time="6.25" timestamp="2017-03-22T16:29:10" hostname="localhost">
<properties/>
Expand All @@ -111,7 +113,9 @@ class JUnitReportSpec : Spek({
</testcase>
</testsuite>
""".trimIndent() + "\n"
)
expected = normalizeLinefeed(expected)
val actual = outputFile.readText()
assertThat(actual).isEqualTo(expected)
}

it("emits completion") {
Expand Down
3 changes: 3 additions & 0 deletions composer/src/test/kotlin/com/gojuno/composer/test.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ fun testFile(): File = createTempFile().apply { deleteOnExit() }
fun SpecBody.perform(body: () -> Unit) = beforeEachTest(body)

fun SpecBody.cleanup(body: () -> Unit) = afterEachTest(body)

/** Make a Unix-formatted String compliant with current operating system's newline format */
fun normalizeLinefeed(str: String): String = str.replace("\n", System.getProperty("line.separator"));

0 comments on commit 6e039df

Please sign in to comment.