diff --git a/composer/src/test/kotlin/com/gojuno/composer/InstrumentationSpec.kt b/composer/src/test/kotlin/com/gojuno/composer/InstrumentationSpec.kt index db37a58..d9540fe 100644 --- a/composer/src/test/kotlin/com/gojuno/composer/InstrumentationSpec.kt +++ b/composer/src/test/kotlin/com/gojuno/composer/InstrumentationSpec.kt @@ -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) @@ -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) @@ -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 ), @@ -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) @@ -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( @@ -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) @@ -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 ) @@ -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) @@ -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 ) )) @@ -722,5 +736,4 @@ at android.app.Instrumentation${'$'}InstrumentationThread.run(Instrumentation.ja ) } } -}) - +}) \ No newline at end of file diff --git a/composer/src/test/kotlin/com/gojuno/composer/JUnitReportSpec.kt b/composer/src/test/kotlin/com/gojuno/composer/JUnitReportSpec.kt index a10c9fe..da1c9fa 100644 --- a/composer/src/test/kotlin/com/gojuno/composer/JUnitReportSpec.kt +++ b/composer/src/test/kotlin/com/gojuno/composer/JUnitReportSpec.kt @@ -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) } @@ -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(), @@ -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(), @@ -86,7 +88,7 @@ class JUnitReportSpec : Spek({ } it("produces correct xml report") { - assertThat(outputFile.readText()).isEqualTo(""" + var expected = """ @@ -111,7 +113,9 @@ class JUnitReportSpec : Spek({ """.trimIndent() + "\n" - ) + expected = normalizeLinefeed(expected) + val actual = outputFile.readText() + assertThat(actual).isEqualTo(expected) } it("emits completion") { diff --git a/composer/src/test/kotlin/com/gojuno/composer/test.kt b/composer/src/test/kotlin/com/gojuno/composer/test.kt index 7ae15a7..99ae482 100644 --- a/composer/src/test/kotlin/com/gojuno/composer/test.kt +++ b/composer/src/test/kotlin/com/gojuno/composer/test.kt @@ -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"));