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

Test overhaul #71

Merged
merged 3 commits into from
Sep 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/current/src/main/scala/io/scalajs/nodejs/Process.scala
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ trait Process extends IEventEmitter {
* v0.8 allowed for longer process title strings by also overwriting the environ memory but that was
* potentially insecure/confusing in some (rather obscure) cases.
*/
var title: js.Any = js.native
var title: String = js.native

/**
* A compiled-in property that exposes NODE_VERSION.
Expand Down
1 change: 0 additions & 1 deletion app/current/src/test/resources/fileA2.txt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package nodejs.buffer
package io.scalajs.nodejs.buffer

import io.scalajs.nodejs.buffer.Buffer
import org.scalatest.FunSpec
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package nodejs
package io.scalajs.nodejs

import io.scalajs.nodejs.console_module.{Console, ConsoleOptions}
import io.scalajs.nodejs.fs.Fs
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package nodejs.assertion
package io.scalajs.nodejs.assertion

import org.scalatest.FunSpec
import io.scalajs.nodejs.{ Assert => NodeAssert }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import org.scalatest.FunSpec
class PathTest extends FunSpec {
describe("Path") {
it("supports join()") {
assert(Path.win32.toNamespacedPath("c:\\foo\\bar") == "\\\\?\\c:\\foo\\bar")
assert(Path.posix.toNamespacedPath("c:\\foo\\bar") == "c:\\foo\\bar")
assert(Path.win32.toNamespacedPath("c:\\foo\\bar") === "\\\\?\\c:\\foo\\bar")
assert(Path.posix.toNamespacedPath("c:\\foo\\bar") === "c:\\foo\\bar")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class UtilTest extends FunSpec {
}

it("have inspect.custom added in v10.12.0") {
assert(Util.inspect.custom != null)
assert(Util.inspect.custom !== null)
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package nodejs
package io.scalajs.nodejs

import io.scalajs.nodejs.console_module.Console
import io.scalajs.nodejs.fs.Fs
Expand Down
45 changes: 45 additions & 0 deletions app/nodejs-v8/src/test/scala/io/scalajs/nodejs/ProcessTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package io.scalajs.nodejs

import io.scalajs.nodejs.Process.ProcessEnvExtensions
import io.scalajs.nodejs.os.OS
import org.scalatest.FunSpec

import scala.scalajs.js

/**
* Process Tests
*/
class ProcessTest extends FunSpec {

describe("Process") {
val versionPrefix =
if (TestEnvironment.isExecutedInExactNode8) "v8."
else if (TestEnvironment.isExecutedInExactNode10) "v10."
else if (TestEnvironment.isExecutedInExactNode12) "v12."
else "Unknown node.js version"

it("contains the following properties") {
assert(process.arch.isInstanceOf[String])
assert(process.argv.length === 1)
assert(process.argv(0).endsWith("node"))
assert(process.config("variables").asInstanceOf[js.Dictionary[String]]("host_arch") === OS.arch())
assert(process.connected.isEmpty)
assert(process.cwd().nonEmpty)
assert(process.env("PATH").nonEmpty)
assert(process.env.PATH === process.env("PATH"))
assert(process.execArgv.length === 0)
assert(process.execPath.endsWith("node"))
assert(process.features.contains("debug"))
assert(process.moduleLoadList.length > 0)
assert(process.title.isInstanceOf[String])
assert(process.version.startsWith(versionPrefix))
assert(process.versions.node.map(v => s"v${v}").getOrElse("").startsWith(versionPrefix))

// TODO: actually undefined in test
// assert(process.stdout.isTTY)
// assert(process.stderr.isTTY)
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,15 @@ package io.scalajs.nodejs
import io.scalajs.nodejs.buffer.Buffer
import org.scalatest.FunSpec

/**
* StringDecoder Tests
*/
class StringDecoderTest extends FunSpec {

describe("StringDecoder") {

it("should decode strings or buffer") {
val decoder = new StringDecoder("utf8")

info(decoder.write(Buffer.from("Hello ")))
info(decoder.write(Buffer.from("World")))
info(decoder.end(Buffer.from("!")))
assert(decoder.write(Buffer.from("Hello ")) === "Hello ")
assert(decoder.write(Buffer.from("World")) === "World")
assert(decoder.end(Buffer.from("!")) === "!")
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package nodejs
package io.scalajs.nodejs

import io.scalajs.nodejs.buffer.Buffer
import io.scalajs.nodejs.child_process.ChildProcess

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

def isWindows: Boolean = os.OS.platform().startsWith("win")

def isExecutedInExactNode12: Boolean = nodeMajorVersion == 12
def isExecutedInExactNode10: Boolean = nodeMajorVersion == 10
def isExecutedInExactNode8: Boolean = nodeMajorVersion == 8
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
package io.scalajs.nodejs.buffer

import io.scalajs.collection.Iterator.Entry
import io.scalajs.nodejs.buffer
import nodejs.TestEnvironment
import io.scalajs.nodejs.{TestEnvironment, buffer}
import org.scalatest.FunSpec

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

/**
* Buffer Tests
*/
class BufferTest extends FunSpec {

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

assert(buf1.compare(buf1) == 0)
assert(buf1.compare(buf2) == -1)
assert(buf1.compare(buf3) == -1)
assert(buf2.compare(buf1) == 1)
assert(buf2.compare(buf3) == 1)
assert(buf1.compare(buf1) === 0)
assert(buf1.compare(buf2) === -1)
assert(buf1.compare(buf3) === -1)
assert(buf2.compare(buf1) === 1)
assert(buf2.compare(buf3) === 1)
}

it("should support iterating entries [classic]") {
val buf = Buffer.from("Hello!")
val it = buf.entries()
var result: Entry[js.Any] = null
do {
result = it.next()
if (!result.done) info(s"value: ${result.value}")
} while (!result.done)
}

it("should support iterating entries [Scala]") {
it("should support iterating entries") {
val buf = Buffer.from("Hello!")
for (value <- buf.entries()) info(s"value: $value")
val it = buf.entries()
assert(
it.toSeq.map(_.toSeq) === Seq(
Seq(0, 72),
Seq(1, 101),
Seq(2, 108),
Seq(3, 108),
Seq(4, 111),
Seq(5, 33)
)
)
}

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

it("should support byteLength for specific types") {
assert(Buffer.byteLength("\u00bd + \u00bc = \u00be") == 12)
assert(Buffer.byteLength("\u00bd + \u00bc = \u00be", "utf8") == 12)
assert(Buffer.byteLength(Buffer.alloc(12)) == 12)
assert(Buffer.byteLength(new Uint8Array(12)) == 12)
assert(Buffer.byteLength(new DataView(new ArrayBuffer(12))) == 12)
assert(Buffer.byteLength(new ArrayBuffer(12)) == 12)
assert(Buffer.byteLength("\u00bd + \u00bc = \u00be") === 12)
assert(Buffer.byteLength("\u00bd + \u00bc = \u00be", "utf8") === 12)
assert(Buffer.byteLength(Buffer.alloc(12)) === 12)
assert(Buffer.byteLength(new Uint8Array(12)) === 12)
assert(Buffer.byteLength(new DataView(new ArrayBuffer(12))) === 12)
assert(Buffer.byteLength(new ArrayBuffer(12)) === 12)
}

it("should support fill") {
val otherBuf = Buffer.from("abcdef")
assert(Buffer.alloc(10).fill(otherBuf).toString() == "abcdefabcd")
assert(Buffer.alloc(10).fill(otherBuf).toString() === "abcdefabcd")
}
}

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

it("should create buffers from strings") {
val bufA = Buffer.from("Hello ")
info(s"bufA => ${bufA.toString()}")
val bufB = Buffer.from("World")
info(s"bufB => ${bufB.toString()}")
val bufC = bufA + bufB
info(s"bufC => ${bufC.toString()}, length = ${bufC.byteLength()}")

assert(bufA.toString() == "Hello ")
assert(bufB.toString() == "World")
assert(bufC.byteLength() == 11)
assert(bufA.toString() === "Hello ")
assert(bufB.toString() === "World")
assert(bufC.byteLength === 11)
}

it("should create buffers from buffers") {
val buffer = Buffer.from("hello")
assert(Buffer.from(buffer).toString() == "hello")
assert(Buffer.from(buffer).toString() === "hello")

// TODO: when Scala.js added TypedArray.from
// val uints = Uint8Array.from(???)
// assert(Buffer.from(uints).toString() == "worlds")
val uints = Uint8Array.from(js.Array(72, 101, 108, 108, 111))
assert(Buffer.from(uints).toString() === "Hello")
}

it("should support concat") {
val buffers = js.Array(Buffer.from("abc"), Buffer.from("def"), Buffer.from("ghijk"))
assert(Buffer.compare(Buffer.concat(buffers), Buffer.from("abcdefghijk")) == 0)
assert(Buffer.compare(Buffer.concat(buffers, 5), Buffer.from("abcde")) == 0)
assert(Buffer.compare(Buffer.concat(buffers), Buffer.from("abcdefghijk")) === 0)
assert(Buffer.compare(Buffer.concat(buffers, 5), Buffer.from("abcde")) === 0)

val uints: js.Array[Uint8Array] = js.Array(Buffer.from("abc"), Buffer.from("def"), Buffer.from("ghijk"))
assert(Buffer.compare(Buffer.concat(uints), Buffer.from("abcdefghijk")) == 0)
assert(Buffer.compare(Buffer.concat(uints, 5), Buffer.from("abcde")) == 0)
assert(Buffer.compare(Buffer.concat(uints), Buffer.from("abcdefghijk")) === 0)
assert(Buffer.compare(Buffer.concat(uints, 5), Buffer.from("abcde")) === 0)
}

it("should support isBufrer") {
Expand All @@ -130,12 +121,12 @@ class BufferTest extends FunSpec {
describe("module members") {
it("should support transcode") {
// package object method
assert(buffer.transcode(Buffer.from("hello"), "utf8", "ascii").toString("ascii") == "hello")
assert(buffer.transcode(Buffer.from("€"), "utf8", "ascii").toString("ascii") == "?")
assert(buffer.transcode(Buffer.from("hello"), "utf8", "ascii").toString("ascii") === "hello")
assert(buffer.transcode(Buffer.from("€"), "utf8", "ascii").toString("ascii") === "?")

// extension method
assert(Buffer.transcode(Buffer.from("hello"), "utf8", "ascii").toString("ascii") == "hello")
assert(Buffer.transcode(Buffer.from("€"), "utf8", "ascii").toString("ascii") == "?")
assert(Buffer.transcode(Buffer.from("hello"), "utf8", "ascii").toString("ascii") === "hello")
assert(Buffer.transcode(Buffer.from("€"), "utf8", "ascii").toString("ascii") === "?")
}

it("should support fields") {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,46 +1,69 @@
package io.scalajs.nodejs
package child_process

import io.scalajs.nodejs.Error
import io.scalajs.nodejs.buffer.Buffer
import io.scalajs.util.ScalaJsHelper._
import org.scalatest.FunSpec
import org.scalatest.AsyncFunSpec

import scala.concurrent.{ExecutionContext, Promise}
import scala.scalajs.js
import scala.scalajs.js.|

/**
* ChildProcess Test
*
*/
class ChildProcessTest extends FunSpec {
class ChildProcessTest extends AsyncFunSpec {
override implicit val executionContext = ExecutionContext.Implicits.global

describe("Extension") {
it("supports execFuture(...)") {
for {
r <- ChildProcess.execFuture("cat ./package.json | wc -l")
} yield {
assert(r._1.asInstanceOf[Buffer].toString().trim.toInt > 0)
}
}
}

describe("ChildProcess") {
it("supports exec(...)") {
val promise = Promise[(Output, Output)]()
ChildProcess.exec(
"cat ./package.json | wc -l",
callback = (error: Error, stdout: Buffer | String, stderr: Buffer | String) => {
callback = (error: Error, stdout: Output, stderr: Output) => {
if (isDefined(error)) {
console.error(s"exec error: $error")
promise.failure(error)
} else {
promise.success((stdout, stderr))
}
info(s"stdout: $stdout")
info(s"stderr: $stderr")
}
)
promise.future.map {
case (stdout, stderr) =>
assert(stdout.toString.trim.toInt === 19)
assert(stderr.toString.trim === "")
}
}

it("supports execFile(...)") {
val promise = Promise[(Output, Output)]()
ChildProcess.execFile(
"ls",
js.Array("-l"),
callback = (error: Error, stdout: Buffer | String, stderr: Buffer | String) => {
callback = (error: Error, stdout: Output, stderr: Output) => {
if (isDefined(error)) {
console.error(s"exec error: $error")
promise.failure(error)
} else {
promise.success((stdout, stderr))
}
info(s"stdout: $stdout")
info(s"stderr: $stderr")
}
)
promise.future.map {
case (stdout, stderr) =>
assert(stdout.toString.trim.linesIterator.length > 10)
assert(stderr.toString.trim === "")
}
}

it("supports execSync(...)") {
Expand Down
Loading