From 4c2380af5c8561fc0a5f8279c1c7336680d026ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Thu, 16 Feb 2023 14:30:14 +0100 Subject: test: add unit tests from std/node (#17794) Adds two test files: "cli/tests/unit_node/process_test.ts" and "cli/tests/unit_node/child_process_test.ts" --------- Co-authored-by: Yoshiya Hinosawa --- cli/tests/unit_node/testdata/binary_stdio.js | 11 +++++++++++ cli/tests/unit_node/testdata/child_process_unref.js | 9 +++++++++ cli/tests/unit_node/testdata/exec_file_text_error.js | 2 ++ cli/tests/unit_node/testdata/exec_file_text_output.js | 1 + cli/tests/unit_node/testdata/infinite_loop.js | 3 +++ .../unit_node/testdata/node_modules/foo/index.js | 4 ++++ .../unit_node/testdata/node_modules/foo/package.json | 3 +++ cli/tests/unit_node/testdata/process_exit.ts | 19 +++++++++++++++++++ cli/tests/unit_node/testdata/process_exit2.ts | 4 ++++ cli/tests/unit_node/testdata/process_stdin.ts | 11 +++++++++++ cli/tests/unit_node/testdata/process_stdin_dummy.txt | 2 ++ 11 files changed, 69 insertions(+) create mode 100644 cli/tests/unit_node/testdata/binary_stdio.js create mode 100644 cli/tests/unit_node/testdata/child_process_unref.js create mode 100644 cli/tests/unit_node/testdata/exec_file_text_error.js create mode 100644 cli/tests/unit_node/testdata/exec_file_text_output.js create mode 100644 cli/tests/unit_node/testdata/infinite_loop.js create mode 100644 cli/tests/unit_node/testdata/node_modules/foo/index.js create mode 100644 cli/tests/unit_node/testdata/node_modules/foo/package.json create mode 100644 cli/tests/unit_node/testdata/process_exit.ts create mode 100644 cli/tests/unit_node/testdata/process_exit2.ts create mode 100644 cli/tests/unit_node/testdata/process_stdin.ts create mode 100644 cli/tests/unit_node/testdata/process_stdin_dummy.txt (limited to 'cli/tests/unit_node/testdata') diff --git a/cli/tests/unit_node/testdata/binary_stdio.js b/cli/tests/unit_node/testdata/binary_stdio.js new file mode 100644 index 000000000..aa370a933 --- /dev/null +++ b/cli/tests/unit_node/testdata/binary_stdio.js @@ -0,0 +1,11 @@ +const buffer = new Uint8Array(10); +const nread = await Deno.stdin.read(buffer); + +if (nread != 10) { + throw new Error("Too little data read"); +} + +const nwritten = await Deno.stdout.write(buffer); +if (nwritten != 10) { + throw new Error("Too little data written"); +} diff --git a/cli/tests/unit_node/testdata/child_process_unref.js b/cli/tests/unit_node/testdata/child_process_unref.js new file mode 100644 index 000000000..cc7815d97 --- /dev/null +++ b/cli/tests/unit_node/testdata/child_process_unref.js @@ -0,0 +1,9 @@ +import cp from "node:child_process"; +import * as path from "node:path"; + +const script = path.join( + path.dirname(path.fromFileUrl(import.meta.url)), + "infinite_loop.js", +); +const childProcess = cp.spawn(Deno.execPath(), ["run", script]); +childProcess.unref(); diff --git a/cli/tests/unit_node/testdata/exec_file_text_error.js b/cli/tests/unit_node/testdata/exec_file_text_error.js new file mode 100644 index 000000000..9697e6044 --- /dev/null +++ b/cli/tests/unit_node/testdata/exec_file_text_error.js @@ -0,0 +1,2 @@ +console.error("yikes!"); +Deno.exit(1); diff --git a/cli/tests/unit_node/testdata/exec_file_text_output.js b/cli/tests/unit_node/testdata/exec_file_text_output.js new file mode 100644 index 000000000..019c0f4bc --- /dev/null +++ b/cli/tests/unit_node/testdata/exec_file_text_output.js @@ -0,0 +1 @@ +console.log("Hello World!"); diff --git a/cli/tests/unit_node/testdata/infinite_loop.js b/cli/tests/unit_node/testdata/infinite_loop.js new file mode 100644 index 000000000..0e6540a7b --- /dev/null +++ b/cli/tests/unit_node/testdata/infinite_loop.js @@ -0,0 +1,3 @@ +while (true) { + await new Promise((resolve) => setTimeout(resolve, 1000)); +} diff --git a/cli/tests/unit_node/testdata/node_modules/foo/index.js b/cli/tests/unit_node/testdata/node_modules/foo/index.js new file mode 100644 index 000000000..24faba789 --- /dev/null +++ b/cli/tests/unit_node/testdata/node_modules/foo/index.js @@ -0,0 +1,4 @@ +console.log("foo"); +console.log(typeof require === "function"); +console.log(typeof module === "object"); +console.log(typeof exports === "object"); diff --git a/cli/tests/unit_node/testdata/node_modules/foo/package.json b/cli/tests/unit_node/testdata/node_modules/foo/package.json new file mode 100644 index 000000000..bde99de92 --- /dev/null +++ b/cli/tests/unit_node/testdata/node_modules/foo/package.json @@ -0,0 +1,3 @@ +{ + "name": "foo" +} diff --git a/cli/tests/unit_node/testdata/process_exit.ts b/cli/tests/unit_node/testdata/process_exit.ts new file mode 100644 index 000000000..57351c087 --- /dev/null +++ b/cli/tests/unit_node/testdata/process_exit.ts @@ -0,0 +1,19 @@ +import process from "node:process"; + +//deno-lint-ignore no-undef +process.on("exit", () => { + console.log(1); +}); + +function unexpected() { + console.log(null); +} +//deno-lint-ignore no-undef +process.on("exit", unexpected); +//deno-lint-ignore no-undef +process.removeListener("exit", unexpected); + +//deno-lint-ignore no-undef +process.on("exit", () => { + console.log(2); +}); diff --git a/cli/tests/unit_node/testdata/process_exit2.ts b/cli/tests/unit_node/testdata/process_exit2.ts new file mode 100644 index 000000000..3731f745a --- /dev/null +++ b/cli/tests/unit_node/testdata/process_exit2.ts @@ -0,0 +1,4 @@ +import process from "node:process"; + +process.on("exit", () => console.log("exit")); +process.exit(); diff --git a/cli/tests/unit_node/testdata/process_stdin.ts b/cli/tests/unit_node/testdata/process_stdin.ts new file mode 100644 index 000000000..23562b090 --- /dev/null +++ b/cli/tests/unit_node/testdata/process_stdin.ts @@ -0,0 +1,11 @@ +import process from "node:process"; + +console.log(process.stdin.readableHighWaterMark); + +process.stdin.setEncoding("utf8"); +process.stdin.on("readable", () => { + console.log(process.stdin.read()); +}); +process.stdin.on("end", () => { + console.log("end"); +}); diff --git a/cli/tests/unit_node/testdata/process_stdin_dummy.txt b/cli/tests/unit_node/testdata/process_stdin_dummy.txt new file mode 100644 index 000000000..a907ec3f4 --- /dev/null +++ b/cli/tests/unit_node/testdata/process_stdin_dummy.txt @@ -0,0 +1,2 @@ +foo +bar \ No newline at end of file -- cgit v1.2.3