Skip to content
This repository was archived by the owner on Jul 30, 2024. It is now read-only.

Commit dec0f76

Browse files
authored
Merge pull request #71 from exoego/test-overhaul
Test overhaul
2 parents 0b80fad + f42c207 commit dec0f76

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+456
-442
lines changed

app/current/src/main/scala/io/scalajs/nodejs/Process.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ trait Process extends IEventEmitter {
144144
* v0.8 allowed for longer process title strings by also overwriting the environ memory but that was
145145
* potentially insecure/confusing in some (rather obscure) cases.
146146
*/
147-
var title: js.Any = js.native
147+
var title: String = js.native
148148

149149
/**
150150
* A compiled-in property that exposes NODE_VERSION.

app/current/src/test/resources/fileA2.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

app/current/src/test/scala/nodejs/buffer/BufferTest.scala renamed to app/current/src/test/scala/io/scalajs/nodejs/buffer/BufferTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package nodejs.buffer
1+
package io.scalajs.nodejs.buffer
22

33
import io.scalajs.nodejs.buffer.Buffer
44
import org.scalatest.FunSpec

app/nodejs-v10/src/test/scala/nodejs/ConsoleTest.scala renamed to app/nodejs-v10/src/test/scala/io/scalajs/nodejs/ConsoleTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package nodejs
1+
package io.scalajs.nodejs
22

33
import io.scalajs.nodejs.console_module.{Console, ConsoleOptions}
44
import io.scalajs.nodejs.fs.Fs

app/nodejs-v10/src/test/scala/nodejs/assertion/AssertTest.scala renamed to app/nodejs-v10/src/test/scala/io/scalajs/nodejs/assertion/AssertTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package nodejs.assertion
1+
package io.scalajs.nodejs.assertion
22

33
import org.scalatest.FunSpec
44
import io.scalajs.nodejs.{ Assert => NodeAssert }

app/nodejs-v10/src/test/scala/nodejs/path/PathTest.scala renamed to app/nodejs-v10/src/test/scala/io/scalajs/nodejs/path/PathTest.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import org.scalatest.FunSpec
66
class PathTest extends FunSpec {
77
describe("Path") {
88
it("supports join()") {
9-
assert(Path.win32.toNamespacedPath("c:\\foo\\bar") == "\\\\?\\c:\\foo\\bar")
10-
assert(Path.posix.toNamespacedPath("c:\\foo\\bar") == "c:\\foo\\bar")
9+
assert(Path.win32.toNamespacedPath("c:\\foo\\bar") === "\\\\?\\c:\\foo\\bar")
10+
assert(Path.posix.toNamespacedPath("c:\\foo\\bar") === "c:\\foo\\bar")
1111
}
1212
}
1313
}

app/nodejs-v10/src/test/scala/nodejs/util/UtilTest.scala renamed to app/nodejs-v10/src/test/scala/io/scalajs/nodejs/util/UtilTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class UtilTest extends FunSpec {
2121
}
2222

2323
it("have inspect.custom added in v10.12.0") {
24-
assert(Util.inspect.custom != null)
24+
assert(Util.inspect.custom !== null)
2525
}
2626

2727
}

app/nodejs-v8/src/test/scala/nodejs/ConsoleTest.scala renamed to app/nodejs-v8/src/test/scala/io/scalajs/nodejs/ConsoleTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package nodejs
1+
package io.scalajs.nodejs
22

33
import io.scalajs.nodejs.console_module.Console
44
import io.scalajs.nodejs.fs.Fs
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package io.scalajs.nodejs
2+
3+
import io.scalajs.nodejs.Process.ProcessEnvExtensions
4+
import io.scalajs.nodejs.os.OS
5+
import org.scalatest.FunSpec
6+
7+
import scala.scalajs.js
8+
9+
/**
10+
* Process Tests
11+
*/
12+
class ProcessTest extends FunSpec {
13+
14+
describe("Process") {
15+
val versionPrefix =
16+
if (TestEnvironment.isExecutedInExactNode8) "v8."
17+
else if (TestEnvironment.isExecutedInExactNode10) "v10."
18+
else if (TestEnvironment.isExecutedInExactNode12) "v12."
19+
else "Unknown node.js version"
20+
21+
it("contains the following properties") {
22+
assert(process.arch.isInstanceOf[String])
23+
assert(process.argv.length === 1)
24+
assert(process.argv(0).endsWith("node"))
25+
assert(process.config("variables").asInstanceOf[js.Dictionary[String]]("host_arch") === OS.arch())
26+
assert(process.connected.isEmpty)
27+
assert(process.cwd().nonEmpty)
28+
assert(process.env("PATH").nonEmpty)
29+
assert(process.env.PATH === process.env("PATH"))
30+
assert(process.execArgv.length === 0)
31+
assert(process.execPath.endsWith("node"))
32+
assert(process.features.contains("debug"))
33+
assert(process.moduleLoadList.length > 0)
34+
assert(process.title.isInstanceOf[String])
35+
assert(process.version.startsWith(versionPrefix))
36+
assert(process.versions.node.map(v => s"v${v}").getOrElse("").startsWith(versionPrefix))
37+
38+
// TODO: actually undefined in test
39+
// assert(process.stdout.isTTY)
40+
// assert(process.stderr.isTTY)
41+
}
42+
43+
}
44+
45+
}

app/nodejs-v8/src/test/scala/nodejs/StringDecoderTest.scala renamed to app/nodejs-v8/src/test/scala/io/scalajs/nodejs/StringDecoderTest.scala

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,15 @@ package io.scalajs.nodejs
33
import io.scalajs.nodejs.buffer.Buffer
44
import org.scalatest.FunSpec
55

6-
/**
7-
* StringDecoder Tests
8-
*/
96
class StringDecoderTest extends FunSpec {
107

118
describe("StringDecoder") {
129

1310
it("should decode strings or buffer") {
1411
val decoder = new StringDecoder("utf8")
15-
16-
info(decoder.write(Buffer.from("Hello ")))
17-
info(decoder.write(Buffer.from("World")))
18-
info(decoder.end(Buffer.from("!")))
12+
assert(decoder.write(Buffer.from("Hello ")) === "Hello ")
13+
assert(decoder.write(Buffer.from("World")) === "World")
14+
assert(decoder.end(Buffer.from("!")) === "!")
1915
}
2016

2117
}

app/nodejs-v8/src/test/scala/nodejs/TestEnvironment.scala renamed to app/nodejs-v8/src/test/scala/io/scalajs/nodejs/TestEnvironment.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
package nodejs
1+
package io.scalajs.nodejs
2+
23
import io.scalajs.nodejs.buffer.Buffer
34
import io.scalajs.nodejs.child_process.ChildProcess
45

@@ -7,6 +8,8 @@ object TestEnvironment {
78
private lazy val nodeMajorVersion: Int =
89
ChildProcess.execSync("node -v").asInstanceOf[Buffer].toString().drop(1).takeWhile(_.isDigit).toInt
910

11+
def isWindows: Boolean = os.OS.platform().startsWith("win")
12+
1013
def isExecutedInExactNode12: Boolean = nodeMajorVersion == 12
1114
def isExecutedInExactNode10: Boolean = nodeMajorVersion == 10
1215
def isExecutedInExactNode8: Boolean = nodeMajorVersion == 8

app/nodejs-v8/src/test/scala/nodejs/buffer/BufferTest.scala renamed to app/nodejs-v8/src/test/scala/io/scalajs/nodejs/buffer/BufferTest.scala

Lines changed: 40 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
package io.scalajs.nodejs.buffer
22

3-
import io.scalajs.collection.Iterator.Entry
4-
import io.scalajs.nodejs.buffer
5-
import nodejs.TestEnvironment
3+
import io.scalajs.nodejs.{TestEnvironment, buffer}
64
import org.scalatest.FunSpec
75

86
import scala.scalajs.js
97
import scala.scalajs.js.typedarray.{ArrayBuffer, DataView, Uint8Array}
108

11-
/**
12-
* Buffer Tests
13-
*/
149
class BufferTest extends FunSpec {
1510

1611
describe("Buffer") {
@@ -20,26 +15,26 @@ class BufferTest extends FunSpec {
2015
val buf2 = Buffer.from("BCD")
2116
val buf3 = Buffer.from("ABCD")
2217

23-
assert(buf1.compare(buf1) == 0)
24-
assert(buf1.compare(buf2) == -1)
25-
assert(buf1.compare(buf3) == -1)
26-
assert(buf2.compare(buf1) == 1)
27-
assert(buf2.compare(buf3) == 1)
18+
assert(buf1.compare(buf1) === 0)
19+
assert(buf1.compare(buf2) === -1)
20+
assert(buf1.compare(buf3) === -1)
21+
assert(buf2.compare(buf1) === 1)
22+
assert(buf2.compare(buf3) === 1)
2823
}
2924

30-
it("should support iterating entries [classic]") {
31-
val buf = Buffer.from("Hello!")
32-
val it = buf.entries()
33-
var result: Entry[js.Any] = null
34-
do {
35-
result = it.next()
36-
if (!result.done) info(s"value: ${result.value}")
37-
} while (!result.done)
38-
}
39-
40-
it("should support iterating entries [Scala]") {
25+
it("should support iterating entries") {
4126
val buf = Buffer.from("Hello!")
42-
for (value <- buf.entries()) info(s"value: $value")
27+
val it = buf.entries()
28+
assert(
29+
it.toSeq.map(_.toSeq) === Seq(
30+
Seq(0, 72),
31+
Seq(1, 101),
32+
Seq(2, 108),
33+
Seq(3, 108),
34+
Seq(4, 111),
35+
Seq(5, 33)
36+
)
37+
)
4338
}
4439

4540
it("should support buffer property") {
@@ -52,21 +47,21 @@ class BufferTest extends FunSpec {
5247
import scala.scalajs.js.typedarray.Int8Array
5348
val nodeBuffer = Buffer.from(js.Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9))
5449
val typedarray = new Int8Array(nodeBuffer.buffer, nodeBuffer.byteOffset, nodeBuffer.length)
55-
assert(typedarray.mkString == new Int8Array(js.Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)).mkString)
50+
assert(typedarray.mkString === new Int8Array(js.Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)).mkString)
5651
}
5752

5853
it("should support byteLength for specific types") {
59-
assert(Buffer.byteLength("\u00bd + \u00bc = \u00be") == 12)
60-
assert(Buffer.byteLength("\u00bd + \u00bc = \u00be", "utf8") == 12)
61-
assert(Buffer.byteLength(Buffer.alloc(12)) == 12)
62-
assert(Buffer.byteLength(new Uint8Array(12)) == 12)
63-
assert(Buffer.byteLength(new DataView(new ArrayBuffer(12))) == 12)
64-
assert(Buffer.byteLength(new ArrayBuffer(12)) == 12)
54+
assert(Buffer.byteLength("\u00bd + \u00bc = \u00be") === 12)
55+
assert(Buffer.byteLength("\u00bd + \u00bc = \u00be", "utf8") === 12)
56+
assert(Buffer.byteLength(Buffer.alloc(12)) === 12)
57+
assert(Buffer.byteLength(new Uint8Array(12)) === 12)
58+
assert(Buffer.byteLength(new DataView(new ArrayBuffer(12))) === 12)
59+
assert(Buffer.byteLength(new ArrayBuffer(12)) === 12)
6560
}
6661

6762
it("should support fill") {
6863
val otherBuf = Buffer.from("abcdef")
69-
assert(Buffer.alloc(10).fill(otherBuf).toString() == "abcdefabcd")
64+
assert(Buffer.alloc(10).fill(otherBuf).toString() === "abcdefabcd")
7065
}
7166
}
7267

@@ -77,34 +72,30 @@ class BufferTest extends FunSpec {
7772

7873
it("should create buffers from strings") {
7974
val bufA = Buffer.from("Hello ")
80-
info(s"bufA => ${bufA.toString()}")
8175
val bufB = Buffer.from("World")
82-
info(s"bufB => ${bufB.toString()}")
8376
val bufC = bufA + bufB
84-
info(s"bufC => ${bufC.toString()}, length = ${bufC.byteLength()}")
8577

86-
assert(bufA.toString() == "Hello ")
87-
assert(bufB.toString() == "World")
88-
assert(bufC.byteLength() == 11)
78+
assert(bufA.toString() === "Hello ")
79+
assert(bufB.toString() === "World")
80+
assert(bufC.byteLength === 11)
8981
}
9082

9183
it("should create buffers from buffers") {
9284
val buffer = Buffer.from("hello")
93-
assert(Buffer.from(buffer).toString() == "hello")
85+
assert(Buffer.from(buffer).toString() === "hello")
9486

95-
// TODO: when Scala.js added TypedArray.from
96-
// val uints = Uint8Array.from(???)
97-
// assert(Buffer.from(uints).toString() == "worlds")
87+
val uints = Uint8Array.from(js.Array(72, 101, 108, 108, 111))
88+
assert(Buffer.from(uints).toString() === "Hello")
9889
}
9990

10091
it("should support concat") {
10192
val buffers = js.Array(Buffer.from("abc"), Buffer.from("def"), Buffer.from("ghijk"))
102-
assert(Buffer.compare(Buffer.concat(buffers), Buffer.from("abcdefghijk")) == 0)
103-
assert(Buffer.compare(Buffer.concat(buffers, 5), Buffer.from("abcde")) == 0)
93+
assert(Buffer.compare(Buffer.concat(buffers), Buffer.from("abcdefghijk")) === 0)
94+
assert(Buffer.compare(Buffer.concat(buffers, 5), Buffer.from("abcde")) === 0)
10495

10596
val uints: js.Array[Uint8Array] = js.Array(Buffer.from("abc"), Buffer.from("def"), Buffer.from("ghijk"))
106-
assert(Buffer.compare(Buffer.concat(uints), Buffer.from("abcdefghijk")) == 0)
107-
assert(Buffer.compare(Buffer.concat(uints, 5), Buffer.from("abcde")) == 0)
97+
assert(Buffer.compare(Buffer.concat(uints), Buffer.from("abcdefghijk")) === 0)
98+
assert(Buffer.compare(Buffer.concat(uints, 5), Buffer.from("abcde")) === 0)
10899
}
109100

110101
it("should support isBufrer") {
@@ -130,12 +121,12 @@ class BufferTest extends FunSpec {
130121
describe("module members") {
131122
it("should support transcode") {
132123
// package object method
133-
assert(buffer.transcode(Buffer.from("hello"), "utf8", "ascii").toString("ascii") == "hello")
134-
assert(buffer.transcode(Buffer.from(""), "utf8", "ascii").toString("ascii") == "?")
124+
assert(buffer.transcode(Buffer.from("hello"), "utf8", "ascii").toString("ascii") === "hello")
125+
assert(buffer.transcode(Buffer.from(""), "utf8", "ascii").toString("ascii") === "?")
135126

136127
// extension method
137-
assert(Buffer.transcode(Buffer.from("hello"), "utf8", "ascii").toString("ascii") == "hello")
138-
assert(Buffer.transcode(Buffer.from(""), "utf8", "ascii").toString("ascii") == "?")
128+
assert(Buffer.transcode(Buffer.from("hello"), "utf8", "ascii").toString("ascii") === "hello")
129+
assert(Buffer.transcode(Buffer.from(""), "utf8", "ascii").toString("ascii") === "?")
139130
}
140131

141132
it("should support fields") {

app/nodejs-v8/src/test/scala/nodejs/child_process/ChildProcessTest.scala renamed to app/nodejs-v8/src/test/scala/io/scalajs/nodejs/child_process/ChildProcessTest.scala

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,69 @@
11
package io.scalajs.nodejs
22
package child_process
33

4-
import io.scalajs.nodejs.Error
54
import io.scalajs.nodejs.buffer.Buffer
65
import io.scalajs.util.ScalaJsHelper._
7-
import org.scalatest.FunSpec
6+
import org.scalatest.AsyncFunSpec
87

8+
import scala.concurrent.{ExecutionContext, Promise}
99
import scala.scalajs.js
1010
import scala.scalajs.js.|
1111

1212
/**
1313
* ChildProcess Test
1414
*
1515
*/
16-
class ChildProcessTest extends FunSpec {
16+
class ChildProcessTest extends AsyncFunSpec {
17+
override implicit val executionContext = ExecutionContext.Implicits.global
18+
19+
describe("Extension") {
20+
it("supports execFuture(...)") {
21+
for {
22+
r <- ChildProcess.execFuture("cat ./package.json | wc -l")
23+
} yield {
24+
assert(r._1.asInstanceOf[Buffer].toString().trim.toInt > 0)
25+
}
26+
}
27+
}
1728

1829
describe("ChildProcess") {
1930
it("supports exec(...)") {
31+
val promise = Promise[(Output, Output)]()
2032
ChildProcess.exec(
2133
"cat ./package.json | wc -l",
22-
callback = (error: Error, stdout: Buffer | String, stderr: Buffer | String) => {
34+
callback = (error: Error, stdout: Output, stderr: Output) => {
2335
if (isDefined(error)) {
24-
console.error(s"exec error: $error")
36+
promise.failure(error)
37+
} else {
38+
promise.success((stdout, stderr))
2539
}
26-
info(s"stdout: $stdout")
27-
info(s"stderr: $stderr")
2840
}
2941
)
42+
promise.future.map {
43+
case (stdout, stderr) =>
44+
assert(stdout.toString.trim.toInt === 19)
45+
assert(stderr.toString.trim === "")
46+
}
3047
}
3148

3249
it("supports execFile(...)") {
50+
val promise = Promise[(Output, Output)]()
3351
ChildProcess.execFile(
3452
"ls",
3553
js.Array("-l"),
36-
callback = (error: Error, stdout: Buffer | String, stderr: Buffer | String) => {
54+
callback = (error: Error, stdout: Output, stderr: Output) => {
3755
if (isDefined(error)) {
38-
console.error(s"exec error: $error")
56+
promise.failure(error)
57+
} else {
58+
promise.success((stdout, stderr))
3959
}
40-
info(s"stdout: $stdout")
41-
info(s"stderr: $stderr")
4260
}
4361
)
62+
promise.future.map {
63+
case (stdout, stderr) =>
64+
assert(stdout.toString.trim.linesIterator.length > 10)
65+
assert(stderr.toString.trim === "")
66+
}
4467
}
4568

4669
it("supports execSync(...)") {

0 commit comments

Comments
 (0)