forked from VirtusLab/scala-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RunScalaJsTestDefinitions.scala
253 lines (236 loc) · 7.57 KB
/
RunScalaJsTestDefinitions.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package scala.cli.integration
import com.eed3si9n.expecty.Expecty.expect
import scala.cli.integration.TestUtil.removeAnsiColors
trait RunScalaJsTestDefinitions { _: RunTestDefinitions =>
def simpleJsTest(extraArgs: String*): Unit = {
val fileName = "simple.sc"
val message = "Hello"
val inputs = TestInputs(
os.rel / fileName ->
s"""import scala.scalajs.js
|val console = js.Dynamic.global.console
|val msg = "$message"
|console.log(msg)
|""".stripMargin
)
inputs.fromRoot { root =>
val output = os.proc(TestUtil.cli, extraOptions, fileName, "--js", extraArgs).call(cwd =
root
).out.trim()
expect(output.linesIterator.toSeq.last == message)
}
}
test("simple script JS") {
simpleJsTest()
}
test("simple script JS in release mode") {
simpleJsTest("--js-mode", "release")
}
test("without node on the PATH") {
val fileName = "simple.sc"
val message = "Hello"
val inputs = TestInputs(
os.rel / fileName ->
s"""import scala.scalajs.js
|val console = js.Dynamic.global.console
|val msg = "$message"
|console.log(msg)
|""".stripMargin
)
inputs.fromRoot { root =>
val thrown = os.proc(TestUtil.cli, extraOptions, fileName, "--js", "--server=false")
.call(
cwd = root,
env = Map("PATH" -> "", "PATHEXT" -> ""),
check = false,
mergeErrIntoOut = true
)
val output = thrown.out.trim()
assert(thrown.exitCode == 1)
assert(output.contains("Node was not found on the PATH"))
}
}
test("JS arguments") {
val inputs = TestInputs(
os.rel / "simple.sc" ->
s"""// FIXME Ideally, 'args' should contain 'argv' here out-of-the-box.
|import scala.scalajs.js
|import scala.scalajs.js.Dynamic.global
|val process = global.require("process")
|val argv = Option(process.argv)
| .filterNot(js.isUndefined)
| .map(_.asInstanceOf[js.Array[String]].drop(2).toSeq)
| .getOrElse(Nil)
|val console = global.console
|console.log(argv.mkString(" "))
|""".stripMargin
)
val messageArgs = Seq("Hello", "foo")
inputs.fromRoot { root =>
val output = os.proc(TestUtil.cli, extraOptions, ".", "--js", "--", messageArgs)
.call(cwd = root)
.out.trim()
expect(output == messageArgs.mkString(" "))
}
}
test("simple script JS command") {
val fileName = "simple.sc"
val message = "Hello"
val inputs = TestInputs(
os.rel / fileName ->
s"""import scala.scalajs.js
|val console = js.Dynamic.global.console
|val msg = "$message"
|console.log(msg)
|""".stripMargin
)
inputs.fromRoot { root =>
val output = os.proc(
TestUtil.cli,
extraOptions,
fileName,
"--js",
"--command",
"--scratch-dir",
root / "stuff"
)
.call(cwd = root).out.trim()
val command = output.linesIterator.toVector
val actualOutput = os.proc(command).call(cwd = root).out.trim()
expect(actualOutput.linesIterator.toSeq.last == message)
}
}
test("esmodule import JS") {
val fileName = "simple.sc"
val message = "Hello"
val inputs = TestInputs(
os.rel / fileName ->
s"""//> using jsModuleKind "es"
|import scala.scalajs.js
|import scala.scalajs.js.annotation._
|
|@js.native
|@JSImport("console", JSImport.Namespace)
|object console extends js.Object {
| def log(msg: js.Any): Unit = js.native
|}
|
|val msg = "$message"
|console.log(msg)
|""".stripMargin
)
inputs.fromRoot { root =>
val output = os.proc(TestUtil.cli, extraOptions, fileName, "--js")
.call(cwd = root).out.trim()
expect(output == message)
}
}
test("simple script JS via config file") {
val message = "Hello"
val inputs = TestInputs(
os.rel / "simple.sc" ->
s"""//> using platform "scala-js"
|import scala.scalajs.js
|val console = js.Dynamic.global.console
|val msg = "$message"
|console.log(msg)
|""".stripMargin
)
inputs.fromRoot { root =>
val output = os.proc(TestUtil.cli, extraOptions, ".").call(cwd = root).out.trim()
expect(output == message)
}
}
test("simple script JS via platform option") {
val message = "Hello"
val inputs = TestInputs(
os.rel / "simple.sc" ->
s"""//> using platform "scala-native"
|import scala.scalajs.js
|val console = js.Dynamic.global.console
|val msg = "$message"
|console.log(msg)
|""".stripMargin
)
inputs.fromRoot { root =>
val output =
os.proc(TestUtil.cli, extraOptions, ".", "--platform", "js").call(cwd = root).out.trim()
expect(output == message)
}
}
test("Multiple scripts JS") {
val message = "Hello"
val inputs = TestInputs(
os.rel / "messages.sc" ->
s"""def msg = "$message"
|""".stripMargin,
os.rel / "print.sc" ->
s"""import scala.scalajs.js
|val console = js.Dynamic.global.console
|console.log(messages.msg)
|""".stripMargin
)
inputs.fromRoot { root =>
val output = os.proc(TestUtil.cli, extraOptions, "print.sc", "messages.sc", "--js").call(cwd =
root
).out.trim()
expect(output == message)
}
}
def jsDomTest(): Unit = {
val inputs = TestInputs(
os.rel / "JsDom.scala" ->
s"""|//> using dep "org.scala-js::scalajs-dom::2.1.0"
|
|import org.scalajs.dom.document
|
|object JsDom extends App {
| val pSize = document.querySelectorAll("p")
| println("Hello from js dom")
|}
|""".stripMargin
)
inputs.fromRoot { root =>
// install jsdom library
val npmPath = TestUtil.fromPath("npm").getOrElse("npm")
os.proc(npmPath, "init", "private").call(cwd = root)
os.proc(npmPath, "install", "jsdom").call(cwd = root)
val output = os.proc(TestUtil.cli, extraOptions, ".", "--js", "--js-dom")
.call(cwd = root)
.out.text()
expect(output.contains("Hello from js dom"))
}
}
if (TestUtil.isCI)
test("Js DOM") {
jsDomTest()
}
test("help js") {
val helpJsOption = "--help-js"
val helpJs = os.proc(TestUtil.cli, "run", helpJsOption).call(check = false)
val lines = removeAnsiColors(helpJs.out.trim()).linesIterator.toVector
val jsVersionHelp = lines.find(_.contains("--js-version")).getOrElse("")
expect(jsVersionHelp.contains(s"(${Constants.scalaJsVersion} by default)"))
expect(lines.exists(_.contains("Scala.js options")))
expect(!lines.exists(_.contains("Scala Native options")))
}
test("Directory JS") {
val message = "Hello"
val inputs = TestInputs(
os.rel / "dir" / "messages.sc" ->
s"""def msg = "$message"
|""".stripMargin,
os.rel / "dir" / "print.sc" ->
s"""import scala.scalajs.js
|val console = js.Dynamic.global.console
|console.log(messages.msg)
|""".stripMargin
)
inputs.fromRoot { root =>
val output = os.proc(TestUtil.cli, extraOptions, "dir", "--js", "--main-class", "print_sc")
.call(cwd = root)
.out.trim()
expect(output == message)
}
}
}