mirror of
https://github.com/denoland/deno.git
synced 2025-01-10 16:11:13 -05:00
feat: std/node/process (#3368)
This commit is contained in:
parent
542ec461c4
commit
4ca624a355
2 changed files with 145 additions and 0 deletions
42
std/node/process.ts
Normal file
42
std/node/process.ts
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
import { notImplemented } from "./_utils.ts";
|
||||||
|
|
||||||
|
const version = `v${Deno.version.deno}`;
|
||||||
|
|
||||||
|
const versions = {
|
||||||
|
node: Deno.version.deno,
|
||||||
|
...Deno.version
|
||||||
|
};
|
||||||
|
|
||||||
|
const osToPlatform = (os: Deno.OperatingSystem): string =>
|
||||||
|
os === "win" ? "win32" : os === "mac" ? "darwin" : os;
|
||||||
|
|
||||||
|
const platform = osToPlatform(Deno.build.os);
|
||||||
|
|
||||||
|
const { arch } = Deno.build;
|
||||||
|
|
||||||
|
const { pid, cwd, chdir, exit } = Deno;
|
||||||
|
|
||||||
|
function on(_event: string, _callback: Function): void {
|
||||||
|
// TODO(rsp): to be implemented
|
||||||
|
notImplemented();
|
||||||
|
}
|
||||||
|
|
||||||
|
export const process = {
|
||||||
|
version,
|
||||||
|
versions,
|
||||||
|
platform,
|
||||||
|
arch,
|
||||||
|
pid,
|
||||||
|
cwd,
|
||||||
|
chdir,
|
||||||
|
exit,
|
||||||
|
on,
|
||||||
|
get env(): { [index: string]: string } {
|
||||||
|
// using getter to avoid --allow-env unless it's used
|
||||||
|
return Deno.env();
|
||||||
|
},
|
||||||
|
get argv(): string[] {
|
||||||
|
// Deno.execPath() also requires --allow-env
|
||||||
|
return [Deno.execPath(), ...Deno.args];
|
||||||
|
}
|
||||||
|
};
|
103
std/node/process_test.ts
Normal file
103
std/node/process_test.ts
Normal file
|
@ -0,0 +1,103 @@
|
||||||
|
import { test } from "../testing/mod.ts";
|
||||||
|
import { assert, assertThrows, assertEquals } from "../testing/asserts.ts";
|
||||||
|
import { process } from "./process.ts";
|
||||||
|
|
||||||
|
// NOTE: Deno.execPath() (and thus process.argv) currently requires --allow-env
|
||||||
|
// (Also Deno.env() (and process.env) requires --allow-env but it's more obvious)
|
||||||
|
|
||||||
|
test({
|
||||||
|
name: "process.cwd and process.chdir success",
|
||||||
|
fn() {
|
||||||
|
// this should be run like other tests from directory up
|
||||||
|
assert(process.cwd().match(/\Wstd$/));
|
||||||
|
process.chdir("node");
|
||||||
|
assert(process.cwd().match(/\Wnode$/));
|
||||||
|
process.chdir("..");
|
||||||
|
assert(process.cwd().match(/\Wstd$/));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
test({
|
||||||
|
name: "process.chdir failure",
|
||||||
|
fn() {
|
||||||
|
assertThrows(
|
||||||
|
() => {
|
||||||
|
process.chdir("non-existent-directory-name");
|
||||||
|
},
|
||||||
|
Deno.DenoError,
|
||||||
|
"file"
|
||||||
|
// 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.
|
||||||
|
// TODO(rsp): Crazy idea: 404 for things like this?
|
||||||
|
// It would be nice to have error codes like 404 or 403 in addition to strings.
|
||||||
|
);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
test({
|
||||||
|
name: "process.version",
|
||||||
|
fn() {
|
||||||
|
assertEquals(typeof process, "object");
|
||||||
|
assertEquals(typeof process.version, "string");
|
||||||
|
assertEquals(typeof process.versions, "object");
|
||||||
|
assertEquals(typeof process.versions.node, "string");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
test({
|
||||||
|
name: "process.platform",
|
||||||
|
fn() {
|
||||||
|
assertEquals(typeof process.platform, "string");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
test({
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
test({
|
||||||
|
name: "process.pid",
|
||||||
|
fn() {
|
||||||
|
assertEquals(typeof process.pid, "number");
|
||||||
|
assertEquals(process.pid, Deno.pid);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
test({
|
||||||
|
name: "process.on",
|
||||||
|
fn() {
|
||||||
|
assertEquals(typeof process.on, "function");
|
||||||
|
assertThrows(
|
||||||
|
() => {
|
||||||
|
process.on("uncaughtException", (_err: Error) => {});
|
||||||
|
},
|
||||||
|
Error,
|
||||||
|
"implemented"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
test({
|
||||||
|
name: "process.argv",
|
||||||
|
fn() {
|
||||||
|
assert(Array.isArray(process.argv));
|
||||||
|
assert(
|
||||||
|
process.argv[0].match(/[^/\\]*deno[^/\\]*$/),
|
||||||
|
"deno included in the file name of argv[0]"
|
||||||
|
);
|
||||||
|
// we cannot test for anything else (we see test runner arguments here)
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
test({
|
||||||
|
name: "process.env",
|
||||||
|
fn() {
|
||||||
|
assertEquals(typeof process.env.PATH, "string");
|
||||||
|
}
|
||||||
|
});
|
Loading…
Reference in a new issue