Skip to content

Commit cb9997b

Browse files
committed
Fix tests
1 parent 808f4ba commit cb9997b

File tree

5 files changed

+59
-78
lines changed

5 files changed

+59
-78
lines changed

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

+1-1
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

+13-17
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,21 +106,18 @@ 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
111-
val blockingSessionOutput = state.withDebugSession(project, mainClassParams("Main")) {
112-
client =>
113-
for {
114-
_ <- client.initialize()
115-
_ <- client.launch(noDebug = true)
116-
_ <- client.initialized
117-
_ <- client.configurationDone()
118-
output <- client.takeCurrentOutput.restartUntil(!_.isEmpty)
119-
_ <- client.disconnect()
120-
} yield output
109+
// start debug session and then immediately disconnect from it
110+
state.withDebugSession(project, mainClassParams("Main")) { client =>
111+
for {
112+
_ <- client.initialize()
113+
_ <- client.launch(noDebug = true)
114+
_ <- client.initialized
115+
_ <- client.configurationDone()
116+
_ <- client.takeCurrentOutput.restartUntil(_.contains("Blocking Hello!"))
117+
_ <- client.disconnect()
118+
} yield ()
121119
}
122120

123-
assertNoDiff(blockingSessionOutput, "Blocking Hello!")
124-
125121
// fix the main class
126122
val sources = state.toTestState.getProjectFor(project).sources
127123
val mainFile = sources.head.resolve("Main.scala")
@@ -139,7 +135,7 @@ object DebugProtocolSpec extends DebugBspBaseSuite {
139135
} yield output
140136
}
141137

142-
assertNoDiff(output, "Non-blocking Hello!")
138+
assertNoDiff(output.linesIterator.toSeq.last, "Non-blocking Hello!")
143139
}
144140
}
145141
}

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

+8-17
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,7 @@ object DebugServerSpec extends DebugBspBaseSuite {
141141
} yield {
142142
assert(client.socket.isClosed)
143143
assertNoDiff(
144-
output.linesIterator
145-
.filterNot(_.contains("ERROR: JDWP Unable to get JNI 1.2 environment"))
146-
.filterNot(_.contains("JDWP exit error AGENT_ERROR_NO_JNI_ENV"))
147-
.mkString(lineSeparator),
144+
output.linesIterator.toSeq.last,
148145
"Hello, World!"
149146
)
150147
}
@@ -177,7 +174,7 @@ object DebugServerSpec extends DebugBspBaseSuite {
177174
project,
178175
state,
179176
arguments = List("hello"),
180-
jvmOptions = List("-J-Dworld=world"),
177+
jvmOptions = List("-Dworld=world"),
181178
environmentVariables = List("EXCL=!")
182179
)
183180

@@ -194,10 +191,7 @@ object DebugServerSpec extends DebugBspBaseSuite {
194191
} yield {
195192
assert(client.socket.isClosed)
196193
assertNoDiff(
197-
output.linesIterator
198-
.filterNot(_.contains("ERROR: JDWP Unable to get JNI 1.2 environment"))
199-
.filterNot(_.contains("JDWP exit error AGENT_ERROR_NO_JNI_ENV"))
200-
.mkString(lineSeparator),
194+
output.linesIterator.toSeq.takeRight(2).mkString(lineSeparator),
201195
"hello\nworld!"
202196
)
203197
}
@@ -370,7 +364,7 @@ object DebugServerSpec extends DebugBspBaseSuite {
370364
} yield {
371365
assert(client.socket.isClosed)
372366
assertNoDiff(
373-
finalOutput,
367+
finalOutput.linesIterator.toSeq.takeRight(7).mkString(lineSeparator),
374368
"""|Breakpoint in main method
375369
|Breakpoint in hello class
376370
|Breakpoint in hello inner class
@@ -624,6 +618,7 @@ object DebugServerSpec extends DebugBspBaseSuite {
624618
for {
625619
port <- startRemoteProcess(buildProject, testState)
626620
client <- server.startConnection
621+
initOutput <- client.takeCurrentOutput
627622
_ <- client.initialize()
628623
_ <- client.attach("localhost", port)
629624
breakpoints <- client.setBreakpoints(breakpoints)
@@ -639,12 +634,8 @@ object DebugServerSpec extends DebugBspBaseSuite {
639634
} yield {
640635
assert(client.socket.isClosed)
641636

642-
assertNoDiff(outputOnBreakpoint, "")
643-
644-
assertNoDiff(
645-
finalOutput,
646-
""
647-
)
637+
assertNoDiff(outputOnBreakpoint, initOutput)
638+
assertNoDiff(finalOutput, initOutput)
648639
}
649640
}
650641
}
@@ -746,7 +737,7 @@ object DebugServerSpec extends DebugBspBaseSuite {
746737
_ <- Task.fromFuture(client.closedPromise.future)
747738
} yield {
748739
assert(client.socket.isClosed)
749-
assertNoDiff(finalOutput, workspace.toString)
740+
assertNoDiff(finalOutput.linesIterator.toSeq.last, workspace.toString)
750741
}
751742
}
752743
}

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

+12-4
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

+25-39
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.stripLineEnd.endsWith(expected.stripLineEnd)) 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)