Skip to content

Commit 045d8ec

Browse files
committed
Fix tests
1 parent 3cc5a42 commit 045d8ec

File tree

5 files changed

+50
-66
lines changed

5 files changed

+50
-66
lines changed

frontend/src/test/scala/bloop/TestSpec.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ abstract class BaseTestSpec(val projectName: String, buildName: String)
2929
build.state.test(project)
3030
try assert(logger.errors.size == 0)
3131
catch { case _: AssertionError => logger.dump() }
32-
assertNoDiff(
32+
assertEndsWith(
3333
logger.renderTimeInsensitiveTestInfos,
3434
expectedFullTestsOutput
3535
)

frontend/src/test/scala/bloop/dap/DebugProtocolSpec.scala

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ object DebugProtocolSpec extends DebugBspBaseSuite {
4040
} yield output
4141
}
4242

43-
assertNoDiff(output, "Hello, World!\n")
43+
assertNoDiff(output.linesIterator.toSeq.last, "Hello, World!")
4444
}
4545
}
4646
}
@@ -76,8 +76,7 @@ object DebugProtocolSpec extends DebugBspBaseSuite {
7676
previousSessionOutput <- previousSession.takeCurrentOutput
7777
} yield previousSessionOutput
7878
}
79-
80-
assertNoDiff(output, "")
79+
assert(output.linesIterator.size == 5)
8180
}
8281
}
8382
}
@@ -107,7 +106,7 @@ object DebugProtocolSpec extends DebugBspBaseSuite {
107106
val project = TestProject(workspace, "p", List(main))
108107

109108
loadBspState(workspace, List(project), logger) { state =>
110-
// start debug session and the immediately disconnect from it
109+
// start debug session and then immediately disconnect from it
111110
val blockingSessionOutput = state.withDebugSession(project, mainClassParams("Main")) {
112111
client =>
113112
for {
@@ -120,7 +119,7 @@ object DebugProtocolSpec extends DebugBspBaseSuite {
120119
} yield output
121120
}
122121

123-
assertNoDiff(blockingSessionOutput, "Blocking Hello!")
122+
assertNoDiff(blockingSessionOutput.linesIterator.toSeq.last, "Blocking Hello!")
124123

125124
// fix the main class
126125
val sources = state.toTestState.getProjectFor(project).sources
@@ -139,7 +138,7 @@ object DebugProtocolSpec extends DebugBspBaseSuite {
139138
} yield output
140139
}
141140

142-
assertNoDiff(output, "Non-blocking Hello!")
141+
assertNoDiff(output.linesIterator.toSeq.last, "Non-blocking Hello!")
143142
}
144143
}
145144
}

frontend/src/test/scala/bloop/dap/DebugServerSpec.scala

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,7 @@ object DebugServerSpec extends DebugBspBaseSuite {
137137
} yield {
138138
assert(client.socket.isClosed)
139139
assertNoDiff(
140-
output.linesIterator
141-
.filterNot(_.contains("ERROR: JDWP Unable to get JNI 1.2 environment"))
142-
.filterNot(_.contains("JDWP exit error AGENT_ERROR_NO_JNI_ENV"))
143-
.mkString(lineSeparator),
140+
output.linesIterator.toSeq.last,
144141
"Hello, World!"
145142
)
146143
}
@@ -190,10 +187,7 @@ object DebugServerSpec extends DebugBspBaseSuite {
190187
} yield {
191188
assert(client.socket.isClosed)
192189
assertNoDiff(
193-
output.linesIterator
194-
.filterNot(_.contains("ERROR: JDWP Unable to get JNI 1.2 environment"))
195-
.filterNot(_.contains("JDWP exit error AGENT_ERROR_NO_JNI_ENV"))
196-
.mkString(lineSeparator),
190+
output.linesIterator.toSeq.takeRight(2).mkString(lineSeparator),
197191
"hello\nworld!"
198192
)
199193
}
@@ -291,7 +285,7 @@ object DebugServerSpec extends DebugBspBaseSuite {
291285
} yield {
292286
assert(client.socket.isClosed)
293287
assertNoDiff(
294-
finalOutput,
288+
finalOutput.linesIterator.toSeq.takeRight(7).mkString(lineSeparator),
295289
"""|Breakpoint in main method
296290
|Breakpoint in hello class
297291
|Breakpoint in hello inner class
@@ -545,6 +539,7 @@ object DebugServerSpec extends DebugBspBaseSuite {
545539
for {
546540
port <- startRemoteProcess(buildProject, testState)
547541
client <- server.startConnection
542+
initOutput <- client.takeCurrentOutput
548543
_ <- client.initialize()
549544
_ <- client.attach("localhost", port)
550545
breakpoints <- client.setBreakpoints(breakpoints)
@@ -560,12 +555,8 @@ object DebugServerSpec extends DebugBspBaseSuite {
560555
} yield {
561556
assert(client.socket.isClosed)
562557

563-
assertNoDiff(outputOnBreakpoint, "")
564-
565-
assertNoDiff(
566-
finalOutput,
567-
""
568-
)
558+
assertNoDiff(outputOnBreakpoint, initOutput)
559+
assertNoDiff(finalOutput, initOutput)
569560
}
570561
}
571562
}
@@ -667,7 +658,7 @@ object DebugServerSpec extends DebugBspBaseSuite {
667658
_ <- Task.fromFuture(client.closedPromise.future)
668659
} yield {
669660
assert(client.socket.isClosed)
670-
assertNoDiff(finalOutput, workspace.toString)
661+
assertNoDiff(finalOutput.linesIterator.toSeq.last, workspace.toString)
671662
}
672663
}
673664
}

frontend/src/test/scala/bloop/testing/BaseSuite.scala

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -437,12 +437,11 @@ abstract class BaseSuite extends TestSuite with BloopHelpers {
437437
expectedTitle: String
438438
)(implicit filename: sourcecode.File, line: sourcecode.Line): Unit = {
439439
colored {
440-
DiffAssertions.assertNoDiffOrPrintExpected(
440+
DiffAssertions.assertNoDiffOrPrintObtained(
441441
obtained,
442442
expected,
443443
obtainedTitle,
444-
expectedTitle,
445-
true
444+
expectedTitle
446445
)
447446
()
448447
}
@@ -455,11 +454,20 @@ abstract class BaseSuite extends TestSuite with BloopHelpers {
455454
print: Boolean = true
456455
)(implicit filename: sourcecode.File, line: sourcecode.Line): Unit = {
457456
colored {
458-
DiffAssertions.assertNoDiffOrPrintExpected(obtained, expected, title, title, print)
457+
if (print) DiffAssertions.assertNoDiffOrPrintObtained(obtained, expected, title, title)
458+
else DiffAssertions.assertNoDiff(obtained, expected, title, title)
459459
()
460460
}
461461
}
462462

463+
def assertEndsWith(
464+
obtained: String,
465+
expected: String,
466+
title: String = ""
467+
)(implicit filename: sourcecode.File, line: sourcecode.Line): Unit = {
468+
colored { DiffAssertions.assertEndsWithOrPrintObtained(obtained, expected, title, title) }
469+
}
470+
463471
def colored[T](
464472
thunk: => T
465473
)(implicit filename: sourcecode.File, line: sourcecode.Line): T = {

frontend/src/test/scala/bloop/testing/DiffAssertions.scala

Lines changed: 25 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -15,43 +15,19 @@ object DiffAssertions {
1515
obtainedTitle: String,
1616
expectedTitle: String
1717
)(implicit source: sourcecode.Line): Unit = {
18-
orPrintObtained(
19-
() => { assertNoDiff(obtained, expected, obtainedTitle, expectedTitle); () },
20-
obtained
21-
)
18+
orPrintObtained(obtained) { () =>
19+
assertNoDiff(obtained, expected, obtainedTitle, expectedTitle); ()
20+
}
2221
}
2322

24-
def assertNoDiffOrPrintExpected(
23+
def assertEndsWithOrPrintObtained(
2524
obtained: String,
2625
expected: String,
2726
obtainedTitle: String,
28-
expectedTitle: String,
29-
print: Boolean = true
30-
)(implicit
31-
source: sourcecode.Line
32-
): Boolean = {
33-
try assertNoDiff(obtained, expected, obtainedTitle, expectedTitle)
34-
catch {
35-
case ex: Exception =>
36-
if (print) {
37-
obtained.linesIterator.toList match {
38-
case head +: tail =>
39-
val b = new StringBuilder()
40-
b.++=(" \"\"\"|" + head).++=(System.lineSeparator())
41-
tail.foreach { line =>
42-
b.++=(" |")
43-
.++=(line)
44-
.++=(System.lineSeparator())
45-
}
46-
b.++=(" |\"\"\".stripMargin").++=(System.lineSeparator())
47-
println(b.mkString)
48-
case head +: Nil =>
49-
println(head)
50-
case Nil =>
51-
println("obtained is empty")
52-
}
53-
}
54-
throw ex
27+
expectedTitle: String
28+
)(implicit source: sourcecode.Line): Unit = {
29+
orPrintObtained(obtained) { () =>
30+
assertEndsWith(obtained, expected, obtainedTitle, expectedTitle); ()
5531
}
5632
}
5733

@@ -66,12 +42,22 @@ object DiffAssertions {
6642
if (result.isEmpty) true
6743
else {
6844
throw new TestFailedException(
69-
error2message(
70-
obtained,
71-
expected,
72-
obtainedTitle,
73-
expectedTitle
74-
)
45+
error2message(obtained, expected, obtainedTitle, expectedTitle)
46+
)
47+
}
48+
}
49+
50+
def assertEndsWith(
51+
obtained: String,
52+
expected: String,
53+
obtainedTitle: String,
54+
expectedTitle: String
55+
)(implicit source: sourcecode.Line): Boolean = colored {
56+
if (obtained.isEmpty && !expected.isEmpty) fail("Obtained empty output!")
57+
if (obtained.endsWith(expected)) true
58+
else {
59+
throw new TestFailedException(
60+
error2message(obtained, expected, obtainedTitle, expectedTitle)
7561
)
7662
}
7763
}
@@ -126,7 +112,7 @@ object DiffAssertions {
126112
}
127113
}
128114

129-
def orPrintObtained(thunk: () => Unit, obtained: String): Unit = {
115+
def orPrintObtained(obtained: String)(thunk: () => Unit): Unit = {
130116
try thunk()
131117
catch {
132118
case ex: Exception =>

0 commit comments

Comments
 (0)