From 045d8ec939c2927a98253fbc354df5d0c94eb5ff Mon Sep 17 00:00:00 2001 From: Adrien Piquerez Date: Thu, 16 Nov 2023 17:08:45 +0100 Subject: [PATCH] Fix tests --- frontend/src/test/scala/bloop/TestSpec.scala | 2 +- .../scala/bloop/dap/DebugProtocolSpec.scala | 11 ++-- .../scala/bloop/dap/DebugServerSpec.scala | 23 ++----- .../test/scala/bloop/testing/BaseSuite.scala | 16 +++-- .../scala/bloop/testing/DiffAssertions.scala | 64 ++++++++----------- 5 files changed, 50 insertions(+), 66 deletions(-) diff --git a/frontend/src/test/scala/bloop/TestSpec.scala b/frontend/src/test/scala/bloop/TestSpec.scala index eb3d711f0d..882b25aeeb 100644 --- a/frontend/src/test/scala/bloop/TestSpec.scala +++ b/frontend/src/test/scala/bloop/TestSpec.scala @@ -29,7 +29,7 @@ abstract class BaseTestSpec(val projectName: String, buildName: String) build.state.test(project) try assert(logger.errors.size == 0) catch { case _: AssertionError => logger.dump() } - assertNoDiff( + assertEndsWith( logger.renderTimeInsensitiveTestInfos, expectedFullTestsOutput ) diff --git a/frontend/src/test/scala/bloop/dap/DebugProtocolSpec.scala b/frontend/src/test/scala/bloop/dap/DebugProtocolSpec.scala index d58cebb64a..5e28542ee1 100644 --- a/frontend/src/test/scala/bloop/dap/DebugProtocolSpec.scala +++ b/frontend/src/test/scala/bloop/dap/DebugProtocolSpec.scala @@ -40,7 +40,7 @@ object DebugProtocolSpec extends DebugBspBaseSuite { } yield output } - assertNoDiff(output, "Hello, World!\n") + assertNoDiff(output.linesIterator.toSeq.last, "Hello, World!") } } } @@ -76,8 +76,7 @@ object DebugProtocolSpec extends DebugBspBaseSuite { previousSessionOutput <- previousSession.takeCurrentOutput } yield previousSessionOutput } - - assertNoDiff(output, "") + assert(output.linesIterator.size == 5) } } } @@ -107,7 +106,7 @@ object DebugProtocolSpec extends DebugBspBaseSuite { val project = TestProject(workspace, "p", List(main)) loadBspState(workspace, List(project), logger) { state => - // start debug session and the immediately disconnect from it + // start debug session and then immediately disconnect from it val blockingSessionOutput = state.withDebugSession(project, mainClassParams("Main")) { client => for { @@ -120,7 +119,7 @@ object DebugProtocolSpec extends DebugBspBaseSuite { } yield output } - assertNoDiff(blockingSessionOutput, "Blocking Hello!") + assertNoDiff(blockingSessionOutput.linesIterator.toSeq.last, "Blocking Hello!") // fix the main class val sources = state.toTestState.getProjectFor(project).sources @@ -139,7 +138,7 @@ object DebugProtocolSpec extends DebugBspBaseSuite { } yield output } - assertNoDiff(output, "Non-blocking Hello!") + assertNoDiff(output.linesIterator.toSeq.last, "Non-blocking Hello!") } } } diff --git a/frontend/src/test/scala/bloop/dap/DebugServerSpec.scala b/frontend/src/test/scala/bloop/dap/DebugServerSpec.scala index 6ca1da360c..795cd08350 100644 --- a/frontend/src/test/scala/bloop/dap/DebugServerSpec.scala +++ b/frontend/src/test/scala/bloop/dap/DebugServerSpec.scala @@ -137,10 +137,7 @@ object DebugServerSpec extends DebugBspBaseSuite { } yield { assert(client.socket.isClosed) assertNoDiff( - output.linesIterator - .filterNot(_.contains("ERROR: JDWP Unable to get JNI 1.2 environment")) - .filterNot(_.contains("JDWP exit error AGENT_ERROR_NO_JNI_ENV")) - .mkString(lineSeparator), + output.linesIterator.toSeq.last, "Hello, World!" ) } @@ -190,10 +187,7 @@ object DebugServerSpec extends DebugBspBaseSuite { } yield { assert(client.socket.isClosed) assertNoDiff( - output.linesIterator - .filterNot(_.contains("ERROR: JDWP Unable to get JNI 1.2 environment")) - .filterNot(_.contains("JDWP exit error AGENT_ERROR_NO_JNI_ENV")) - .mkString(lineSeparator), + output.linesIterator.toSeq.takeRight(2).mkString(lineSeparator), "hello\nworld!" ) } @@ -291,7 +285,7 @@ object DebugServerSpec extends DebugBspBaseSuite { } yield { assert(client.socket.isClosed) assertNoDiff( - finalOutput, + finalOutput.linesIterator.toSeq.takeRight(7).mkString(lineSeparator), """|Breakpoint in main method |Breakpoint in hello class |Breakpoint in hello inner class @@ -545,6 +539,7 @@ object DebugServerSpec extends DebugBspBaseSuite { for { port <- startRemoteProcess(buildProject, testState) client <- server.startConnection + initOutput <- client.takeCurrentOutput _ <- client.initialize() _ <- client.attach("localhost", port) breakpoints <- client.setBreakpoints(breakpoints) @@ -560,12 +555,8 @@ object DebugServerSpec extends DebugBspBaseSuite { } yield { assert(client.socket.isClosed) - assertNoDiff(outputOnBreakpoint, "") - - assertNoDiff( - finalOutput, - "" - ) + assertNoDiff(outputOnBreakpoint, initOutput) + assertNoDiff(finalOutput, initOutput) } } } @@ -667,7 +658,7 @@ object DebugServerSpec extends DebugBspBaseSuite { _ <- Task.fromFuture(client.closedPromise.future) } yield { assert(client.socket.isClosed) - assertNoDiff(finalOutput, workspace.toString) + assertNoDiff(finalOutput.linesIterator.toSeq.last, workspace.toString) } } } diff --git a/frontend/src/test/scala/bloop/testing/BaseSuite.scala b/frontend/src/test/scala/bloop/testing/BaseSuite.scala index 3bb9aa6414..939b2b8084 100644 --- a/frontend/src/test/scala/bloop/testing/BaseSuite.scala +++ b/frontend/src/test/scala/bloop/testing/BaseSuite.scala @@ -437,12 +437,11 @@ abstract class BaseSuite extends TestSuite with BloopHelpers { expectedTitle: String )(implicit filename: sourcecode.File, line: sourcecode.Line): Unit = { colored { - DiffAssertions.assertNoDiffOrPrintExpected( + DiffAssertions.assertNoDiffOrPrintObtained( obtained, expected, obtainedTitle, - expectedTitle, - true + expectedTitle ) () } @@ -455,11 +454,20 @@ abstract class BaseSuite extends TestSuite with BloopHelpers { print: Boolean = true )(implicit filename: sourcecode.File, line: sourcecode.Line): Unit = { colored { - DiffAssertions.assertNoDiffOrPrintExpected(obtained, expected, title, title, print) + if (print) DiffAssertions.assertNoDiffOrPrintObtained(obtained, expected, title, title) + else DiffAssertions.assertNoDiff(obtained, expected, title, title) () } } + def assertEndsWith( + obtained: String, + expected: String, + title: String = "" + )(implicit filename: sourcecode.File, line: sourcecode.Line): Unit = { + colored { DiffAssertions.assertEndsWithOrPrintObtained(obtained, expected, title, title) } + } + def colored[T]( thunk: => T )(implicit filename: sourcecode.File, line: sourcecode.Line): T = { diff --git a/frontend/src/test/scala/bloop/testing/DiffAssertions.scala b/frontend/src/test/scala/bloop/testing/DiffAssertions.scala index 0f9f006c88..e47f07fe93 100644 --- a/frontend/src/test/scala/bloop/testing/DiffAssertions.scala +++ b/frontend/src/test/scala/bloop/testing/DiffAssertions.scala @@ -15,43 +15,19 @@ object DiffAssertions { obtainedTitle: String, expectedTitle: String )(implicit source: sourcecode.Line): Unit = { - orPrintObtained( - () => { assertNoDiff(obtained, expected, obtainedTitle, expectedTitle); () }, - obtained - ) + orPrintObtained(obtained) { () => + assertNoDiff(obtained, expected, obtainedTitle, expectedTitle); () + } } - def assertNoDiffOrPrintExpected( + def assertEndsWithOrPrintObtained( obtained: String, expected: String, obtainedTitle: String, - expectedTitle: String, - print: Boolean = true - )(implicit - source: sourcecode.Line - ): Boolean = { - try assertNoDiff(obtained, expected, obtainedTitle, expectedTitle) - catch { - case ex: Exception => - if (print) { - obtained.linesIterator.toList match { - case head +: tail => - val b = new StringBuilder() - b.++=(" \"\"\"|" + head).++=(System.lineSeparator()) - tail.foreach { line => - b.++=(" |") - .++=(line) - .++=(System.lineSeparator()) - } - b.++=(" |\"\"\".stripMargin").++=(System.lineSeparator()) - println(b.mkString) - case head +: Nil => - println(head) - case Nil => - println("obtained is empty") - } - } - throw ex + expectedTitle: String + )(implicit source: sourcecode.Line): Unit = { + orPrintObtained(obtained) { () => + assertEndsWith(obtained, expected, obtainedTitle, expectedTitle); () } } @@ -66,12 +42,22 @@ object DiffAssertions { if (result.isEmpty) true else { throw new TestFailedException( - error2message( - obtained, - expected, - obtainedTitle, - expectedTitle - ) + error2message(obtained, expected, obtainedTitle, expectedTitle) + ) + } + } + + def assertEndsWith( + obtained: String, + expected: String, + obtainedTitle: String, + expectedTitle: String + )(implicit source: sourcecode.Line): Boolean = colored { + if (obtained.isEmpty && !expected.isEmpty) fail("Obtained empty output!") + if (obtained.endsWith(expected)) true + else { + throw new TestFailedException( + error2message(obtained, expected, obtainedTitle, expectedTitle) ) } } @@ -126,7 +112,7 @@ object DiffAssertions { } } - def orPrintObtained(thunk: () => Unit, obtained: String): Unit = { + def orPrintObtained(obtained: String)(thunk: () => Unit): Unit = { try thunk() catch { case ex: Exception =>