forked from scala-js/scala-js-js-envs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExternalJSRunTest.scala
156 lines (125 loc) · 3.84 KB
/
ExternalJSRunTest.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
/*
* Scala.js JS Envs (https://github.com/scala-js/scala-js-js-envs)
*
* Copyright EPFL.
*
* Licensed under Apache License 2.0
* (https://www.apache.org/licenses/LICENSE-2.0).
*
* See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
*/
package org.scalajs.jsenv
import org.junit.Test
import org.junit.Assert._
import scala.concurrent.{Await, Future}
import scala.concurrent.duration._
import scala.util.Failure
class ExternalJSRunTest {
private def assertFails(future: Future[Unit])(
pf: PartialFunction[Throwable, Unit]): Unit = {
Await.ready(future, 1.seconds).value.get match {
case Failure(t) if pf.isDefinedAt(t) => // OK
case result =>
result.get
fail("run succeeded unexpectedly")
}
}
private val silentConfig = {
val runConfig = RunConfig()
.withInheritOut(false)
.withInheritErr(false)
.withOnOutputStream((_, _) => ())
ExternalJSRun.Config()
.withRunConfig(runConfig)
}
@Test
def nonExistentCommand: Unit = {
val cmd = List("nonexistent-cmd")
val run = ExternalJSRun.start(cmd, silentConfig) { _ =>
fail("unexpected call to input")
}
assertFails(run.future) {
case ExternalJSRun.FailedToStartException(`cmd`, _) => // OK
}
}
@Test
def failingCommand: Unit = {
val run = ExternalJSRun.start(List("node", "non-existent-file.js"),
silentConfig)(_.close())
assertFails(run.future) {
case ExternalJSRun.NonZeroExitException(_) => // OK
}
}
@Test
def abortedCommand: Unit = {
val run = ExternalJSRun.start(List("node"), silentConfig) { _ =>
// Do not close stdin so node keeps running
}
run.close()
assertFails(run.future) {
case ExternalJSRun.ClosedException() => // OK
}
}
@Test
def abortedCommandOK: Unit = {
val config = silentConfig
.withClosingFails(false)
val run = ExternalJSRun.start(List("node"), config) { _ =>
// Do not close stdin so node keeps running
}
run.close()
Await.result(run.future, 1.second)
}
private def checkEnvRun(name: String, want: String, config: ExternalJSRun.Config) = {
ExternalJSRun.start(List("node"), config) { stdin =>
val p = new java.io.PrintStream(stdin)
p.println("""const process = require("process");""");
p.println(s"""process.exit(process.env["$name"] !== "$want");""");
p.close()
}
}
@Test
def setEnv: Unit = {
val config = silentConfig
.withEnv(Map("EXTERNAL_JS_RUN_TEST" -> "witness"))
val run = checkEnvRun("EXTERNAL_JS_RUN_TEST", "witness", config)
Await.result(run.future, 1.second)
}
@Test
def setEnvOnRunConfig: Unit = {
val runConfig = RunConfig()
.withEnv(Map("EXTERNAL_JS_RUN_TEST" -> "witness"))
val config = silentConfig
.withRunConfig(runConfig)
val run = checkEnvRun("EXTERNAL_JS_RUN_TEST", "witness", config)
Await.result(run.future, 1.second)
}
@Test
def envOverrides: Unit = {
val runConfig = RunConfig()
.withEnv(Map("EXTERNAL_JS_RUN_TEST" -> "override"))
val config = silentConfig
.withEnv(Map("EXTERNAL_JS_RUN_TEST" -> "witness"))
.withRunConfig(runConfig)
val run = checkEnvRun("EXTERNAL_JS_RUN_TEST", "override", config)
Await.result(run.future, 1.second)
}
// Confidence tests for checkEnvRun.
@Test
def setEnvWrong: Unit = {
val config = silentConfig
.withEnv(Map("EXTERNAL_JS_RUN_TEST" -> "not-witness"))
val run = checkEnvRun("EXTERNAL_JS_RUN_TEST", "witness", config)
assertFails(run.future) {
case ExternalJSRun.NonZeroExitException(1) => // OK
}
}
@Test
def setEnvMissing: Unit = {
val run = checkEnvRun("EXTERNAL_JS_RUN_TEST", "witness", silentConfig)
assertFails(run.future) {
case ExternalJSRun.NonZeroExitException(1) => // OK
}
}
}