2020-09-13 08:15:38 -04:00
|
|
|
// deno-lint-ignore-file no-undef
|
2021-01-10 21:59:07 -05:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
2020-09-13 08:15:38 -04:00
|
|
|
|
2021-01-25 11:30:31 -05:00
|
|
|
import "./global.ts";
|
2020-09-27 06:22:32 -04:00
|
|
|
import { assert, assertEquals, assertThrows } from "../testing/asserts.ts";
|
2021-01-25 11:30:31 -05:00
|
|
|
import { stripColor } from "../fmt/colors.ts";
|
2020-09-08 11:37:58 -04:00
|
|
|
import * as path from "../path/mod.ts";
|
2020-11-16 14:44:37 -05:00
|
|
|
import { delay } from "../async/delay.ts";
|
2021-01-25 11:30:31 -05:00
|
|
|
import { env } from "./process.ts";
|
2020-06-23 16:00:47 -04:00
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test({
|
2019-11-18 18:30:24 -05:00
|
|
|
name: "process.cwd and process.chdir success",
|
|
|
|
fn() {
|
2020-09-08 11:37:58 -04:00
|
|
|
assertEquals(process.cwd(), Deno.cwd());
|
|
|
|
|
2021-01-18 07:39:35 -05:00
|
|
|
const currentDir = Deno.cwd();
|
2020-09-15 00:26:57 -04:00
|
|
|
|
2021-01-18 07:39:35 -05:00
|
|
|
const tempDir = Deno.makeTempDirSync();
|
|
|
|
process.chdir(tempDir);
|
|
|
|
assertEquals(
|
|
|
|
Deno.realPathSync(process.cwd()),
|
|
|
|
Deno.realPathSync(tempDir),
|
|
|
|
);
|
2020-09-08 11:37:58 -04:00
|
|
|
|
2021-01-18 07:39:35 -05:00
|
|
|
process.chdir(currentDir);
|
2020-03-28 13:03:49 -04:00
|
|
|
},
|
2019-11-18 18:30:24 -05:00
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test({
|
2019-11-18 18:30:24 -05:00
|
|
|
name: "process.chdir failure",
|
|
|
|
fn() {
|
|
|
|
assertThrows(
|
|
|
|
() => {
|
|
|
|
process.chdir("non-existent-directory-name");
|
|
|
|
},
|
2020-02-24 15:48:35 -05:00
|
|
|
Deno.errors.NotFound,
|
2020-07-14 15:24:17 -04:00
|
|
|
"file",
|
2019-11-18 18:30:24 -05:00
|
|
|
// On every OS Deno returns: "No such file" except for Windows, where it's:
|
|
|
|
// "The system cannot find the file specified. (os error 2)" so "file" is
|
|
|
|
// the only common string here.
|
|
|
|
);
|
2020-03-28 13:03:49 -04:00
|
|
|
},
|
2019-11-18 18:30:24 -05:00
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test({
|
2019-11-18 18:30:24 -05:00
|
|
|
name: "process.version",
|
|
|
|
fn() {
|
|
|
|
assertEquals(typeof process, "object");
|
|
|
|
assertEquals(typeof process.version, "string");
|
|
|
|
assertEquals(typeof process.versions, "object");
|
|
|
|
assertEquals(typeof process.versions.node, "string");
|
2020-03-28 13:03:49 -04:00
|
|
|
},
|
2019-11-18 18:30:24 -05:00
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test({
|
2019-11-18 18:30:24 -05:00
|
|
|
name: "process.platform",
|
|
|
|
fn() {
|
|
|
|
assertEquals(typeof process.platform, "string");
|
2020-03-28 13:03:49 -04:00
|
|
|
},
|
2019-11-18 18:30:24 -05:00
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test({
|
2019-11-18 18:30:24 -05:00
|
|
|
name: "process.arch",
|
|
|
|
fn() {
|
|
|
|
assertEquals(typeof process.arch, "string");
|
|
|
|
// TODO(rsp): make sure that the arch strings should be the same in Node and Deno:
|
|
|
|
assertEquals(process.arch, Deno.build.arch);
|
2020-03-28 13:03:49 -04:00
|
|
|
},
|
2019-11-18 18:30:24 -05:00
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test({
|
2019-11-18 18:30:24 -05:00
|
|
|
name: "process.pid",
|
|
|
|
fn() {
|
|
|
|
assertEquals(typeof process.pid, "number");
|
|
|
|
assertEquals(process.pid, Deno.pid);
|
2020-03-28 13:03:49 -04:00
|
|
|
},
|
2019-11-18 18:30:24 -05:00
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test({
|
2019-11-18 18:30:24 -05:00
|
|
|
name: "process.on",
|
2021-01-25 11:30:31 -05:00
|
|
|
async fn() {
|
2019-11-18 18:30:24 -05:00
|
|
|
assertEquals(typeof process.on, "function");
|
|
|
|
assertThrows(
|
|
|
|
() => {
|
|
|
|
process.on("uncaughtException", (_err: Error) => {});
|
|
|
|
},
|
|
|
|
Error,
|
2020-07-14 15:24:17 -04:00
|
|
|
"implemented",
|
2019-11-18 18:30:24 -05:00
|
|
|
);
|
2021-01-25 11:30:31 -05:00
|
|
|
|
|
|
|
let triggered = false;
|
|
|
|
process.on("exit", () => {
|
|
|
|
triggered = true;
|
|
|
|
});
|
|
|
|
process.emit("exit");
|
|
|
|
assert(triggered);
|
|
|
|
|
|
|
|
const cwd = path.dirname(path.fromFileUrl(import.meta.url));
|
|
|
|
|
|
|
|
const p = Deno.run({
|
|
|
|
cmd: [
|
|
|
|
Deno.execPath(),
|
|
|
|
"run",
|
|
|
|
"./process_exit_test.ts",
|
|
|
|
],
|
|
|
|
cwd,
|
|
|
|
stdout: "piped",
|
|
|
|
});
|
|
|
|
|
|
|
|
const decoder = new TextDecoder();
|
|
|
|
const rawOutput = await p.output();
|
|
|
|
assertEquals(
|
|
|
|
stripColor(decoder.decode(rawOutput).trim()),
|
|
|
|
"1\n2",
|
|
|
|
);
|
|
|
|
p.close();
|
2020-03-28 13:03:49 -04:00
|
|
|
},
|
2019-11-18 18:30:24 -05:00
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test({
|
2019-11-18 18:30:24 -05:00
|
|
|
name: "process.argv",
|
2020-06-25 07:18:01 -04:00
|
|
|
fn() {
|
2019-11-18 18:30:24 -05:00
|
|
|
assert(Array.isArray(process.argv));
|
|
|
|
assert(
|
|
|
|
process.argv[0].match(/[^/\\]*deno[^/\\]*$/),
|
2020-07-14 15:24:17 -04:00
|
|
|
"deno included in the file name of argv[0]",
|
2019-11-18 18:30:24 -05:00
|
|
|
);
|
2021-01-25 11:30:31 -05:00
|
|
|
assertEquals(
|
|
|
|
process.argv[1],
|
|
|
|
path.fromFileUrl(Deno.mainModule),
|
|
|
|
);
|
2020-03-28 13:03:49 -04:00
|
|
|
},
|
2019-11-18 18:30:24 -05:00
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test({
|
2019-11-18 18:30:24 -05:00
|
|
|
name: "process.env",
|
2020-06-25 07:18:01 -04:00
|
|
|
fn() {
|
2021-01-18 10:01:38 -05:00
|
|
|
Deno.env.set("HELLO", "WORLD");
|
|
|
|
|
|
|
|
assertEquals(typeof (process.env.HELLO), "string");
|
|
|
|
assertEquals(process.env.HELLO, "WORLD");
|
|
|
|
|
2021-01-25 11:30:31 -05:00
|
|
|
assertEquals(typeof env.HELLO, "string");
|
|
|
|
assertEquals(env.HELLO, "WORLD");
|
2020-03-28 13:03:49 -04:00
|
|
|
},
|
2019-11-18 18:30:24 -05:00
|
|
|
});
|
2020-09-17 23:31:50 -04:00
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "process.stdin",
|
|
|
|
fn() {
|
|
|
|
assertEquals(typeof process.stdin.fd, "number");
|
|
|
|
assertEquals(process.stdin.fd, Deno.stdin.rid);
|
|
|
|
// TODO(jayhelton) Uncomment out this assertion once PTY is supported
|
|
|
|
//assert(process.stdin.isTTY);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "process.stdout",
|
|
|
|
fn() {
|
|
|
|
assertEquals(typeof process.stdout.fd, "number");
|
|
|
|
assertEquals(process.stdout.fd, Deno.stdout.rid);
|
|
|
|
// TODO(jayhelton) Uncomment out this assertion once PTY is supported
|
|
|
|
// assert(process.stdout.isTTY);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "process.stderr",
|
|
|
|
fn() {
|
|
|
|
assertEquals(typeof process.stderr.fd, "number");
|
|
|
|
assertEquals(process.stderr.fd, Deno.stderr.rid);
|
|
|
|
// TODO(jayhelton) Uncomment out this assertion once PTY is supported
|
|
|
|
// assert(process.stderr.isTTY);
|
|
|
|
},
|
|
|
|
});
|
2020-11-16 14:44:37 -05:00
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "process.nextTick",
|
|
|
|
async fn() {
|
|
|
|
let withoutArguments = false;
|
|
|
|
process.nextTick(() => {
|
|
|
|
withoutArguments = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
const expected = 12;
|
|
|
|
let result;
|
|
|
|
process.nextTick((x: number) => {
|
|
|
|
result = x;
|
|
|
|
}, 12);
|
|
|
|
|
|
|
|
await delay(10);
|
|
|
|
assert(withoutArguments);
|
|
|
|
assertEquals(result, expected);
|
|
|
|
},
|
|
|
|
});
|