forked from VirtusLab/scala-cli
/
BloopTests.scala
172 lines (142 loc) · 5.55 KB
/
BloopTests.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package scala.cli.integration
import com.eed3si9n.expecty.Expecty.expect
import scala.cli.integration.util.BloopUtil
import scala.concurrent.ExecutionContext
import scala.concurrent.duration.Duration
import scala.util.Properties
class BloopTests extends ScalaCliSuite {
def runScalaCli(args: String*): os.proc = os.proc(TestUtil.cli, args)
private lazy val bloopDaemonDir =
BloopUtil.bloopDaemonDir(runScalaCli("--power", "directories").call().out.text())
val dummyInputs: TestInputs = TestInputs(
os.rel / "Test.scala" ->
"""//> using scala "2.13"
|object Test {
| def main(args: Array[String]): Unit =
| println("Hello " + "from test")
|}
|""".stripMargin
)
def testScalaTermination(
currentBloopVersion: String,
shouldRestart: Boolean
): Unit = TestUtil.retryOnCi() {
dummyInputs.fromRoot { root =>
BloopUtil.killBloop()
val bloop = BloopUtil.bloop(currentBloopVersion, bloopDaemonDir)
bloop(Seq("about")).call(cwd = root, stdout = os.Inherit)
val output = os.proc(TestUtil.cli, "run", ".")
.call(cwd = root, stderr = os.Pipe, mergeErrIntoOut = true)
.out.text()
expect(output.contains("Hello from test"))
if (shouldRestart)
output.contains("Shutting down unsupported Bloop")
else
output.contains("No need to restart Bloop")
val versionLine = bloop(Seq("about")).call(cwd = root).out.lines()(0)
expect(versionLine == "bloop v" + Constants.bloopVersion)
}
}
// Disabled until we have at least 2 Bleep releases
// test("scala-cli terminates incompatible bloop") {
// testScalaTermination("1.4.8-122-794af022", shouldRestart = true)
// }
test("scala-cli keeps compatible bloop running") {
testScalaTermination(Constants.bloopVersion, shouldRestart = false)
}
test("invalid bloop options passed via global bloop config json file cause bloop start failure") {
val inputs = TestInputs(
os.rel / "bloop.json" ->
"""|{
| "javaOptions" : ["-Xmx1k"]
| }""".stripMargin
)
inputs.fromRoot { root =>
runScalaCli("--power", "bloop", "exit").call()
val res = runScalaCli(
"--power",
"bloop",
"start",
"--bloop-global-options-file",
(root / "bloop.json").toString()
).call(cwd = root, stderr = os.Pipe, check = false)
expect(res.exitCode == 1)
expect(res.err.text().contains("Server failed with exit code 1") || res.err.text().contains(
"java.lang.OutOfMemoryError: Garbage-collected heap size exceeded"
))
}
}
test("bloop exit works") {
def bloopRunning(): Boolean = {
val javaProcesses = os.proc("jps", "-l").call().out.text()
javaProcesses.contains("bloop.Bloop")
}
val inputs = TestInputs.empty
inputs.fromRoot { _ =>
BloopUtil.killBloop()
TestUtil.retry()(assert(!bloopRunning()))
val res = runScalaCli("--power", "bloop", "start").call(check = false)
assert(res.exitCode == 0, clues(res.out.text()))
assert(bloopRunning(), clues(res.out.text()))
val resExit = runScalaCli("--power", "bloop", "exit").call(check = false)
assert(resExit.exitCode == 0, clues(resExit.out.text()))
assert(!bloopRunning())
}
}
test("bloop projects and bloop compile works") {
val inputs = TestInputs(
os.rel / "Hello.scala" ->
"""object Hello {
| def main(args: Array[String]): Unit =
| println("Hello")
|}
|""".stripMargin
)
inputs.fromRoot { root =>
os.proc(TestUtil.cli, "compile", ".")
.call(cwd = root, stdin = os.Inherit, stdout = os.Inherit)
val projRes = os.proc(TestUtil.cli, "--power", "bloop", "projects")
.call(cwd = root / Constants.workspaceDirName)
val projList = projRes.out.trim().linesIterator.toVector
expect(projList.length == 1)
val proj = projList.head
os.proc(TestUtil.cli, "--power", "bloop", "compile", proj)
.call(cwd = root / Constants.workspaceDirName)
val failRes = os.proc(TestUtil.cli, "--power", "bloop", "foo")
.call(cwd = root / Constants.workspaceDirName, check = false, mergeErrIntoOut = true)
val failOutput = failRes.out.text()
expect(failRes.exitCode == 4)
expect(failOutput.contains("Command not found: foo"))
}
}
if (!Properties.isMac || !TestUtil.isNativeCli || !TestUtil.isCI)
// TODO make this pass reliably on Mac CI
test("Restart Bloop server while watching") {
TestUtil.withThreadPool("bloop-restart-test", 2) { pool =>
val timeout = Duration("90 seconds")
val ec = ExecutionContext.fromExecutorService(pool)
def content(message: String) =
s"""object Hello {
| def main(args: Array[String]): Unit =
| println("$message")
|}
|""".stripMargin
val sourcePath = os.rel / "Hello.scala"
val inputs = TestInputs(
sourcePath -> content("Hello")
)
inputs.fromRoot { root =>
val proc = os.proc(TestUtil.cli, "run", "--power", "--offline", "-w", ".")
.spawn(cwd = root)
val firstLine = TestUtil.readLine(proc.stdout, ec, timeout)
expect(firstLine == "Hello")
os.proc(TestUtil.cli, "--power", "bloop", "exit")
.call(cwd = root)
os.write.over(root / sourcePath, content("Foo"))
val secondLine = TestUtil.readLine(proc.stdout, ec, timeout)
expect(secondLine == "Foo")
proc.destroy()
}
}
}
}