deno.land / std@0.91.0 / node / process_test.ts

process_test.ts
نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// deno-lint-ignore-file no-undef// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
import "./global.ts";import { assert, assertEquals, assertThrows } from "../testing/asserts.ts";import { stripColor } from "../fmt/colors.ts";import * as path from "../path/mod.ts";import { delay } from "../async/delay.ts";import { env } from "./process.ts";
Deno.test({ name: "process.cwd and process.chdir success", fn() { assertEquals(process.cwd(), Deno.cwd());
const currentDir = Deno.cwd();
const tempDir = Deno.makeTempDirSync(); process.chdir(tempDir); assertEquals( Deno.realPathSync(process.cwd()), Deno.realPathSync(tempDir), );
process.chdir(currentDir); },});
Deno.test({ name: "process.chdir failure", fn() { assertThrows( () => { process.chdir("non-existent-directory-name"); }, Deno.errors.NotFound, "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. ); },});
Deno.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"); },});
Deno.test({ name: "process.platform", fn() { assertEquals(typeof process.platform, "string"); },});
Deno.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); },});
Deno.test({ name: "process.pid", fn() { assertEquals(typeof process.pid, "number"); assertEquals(process.pid, Deno.pid); },});
Deno.test({ name: "process.on", async fn() { assertEquals(typeof process.on, "function"); assertThrows( () => { process.on("uncaughtException", (_err: Error) => {}); }, Error, "implemented", );
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", "--quiet", "./testdata/process_exit.ts", ], cwd, stdout: "piped", });
const decoder = new TextDecoder(); const rawOutput = await p.output(); assertEquals( stripColor(decoder.decode(rawOutput).trim()), "1\n2", ); p.close(); },});
Deno.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]", ); assertEquals( process.argv[1], path.fromFileUrl(Deno.mainModule), ); // argv supports array methods. assert(Array.isArray(process.argv.slice(2))); assertEquals(process.argv.indexOf(Deno.execPath()), 0); assertEquals(process.argv.indexOf(path.fromFileUrl(Deno.mainModule)), 1); },});
Deno.test({ name: "process.env", fn() { Deno.env.set("HELLO", "WORLD");
assertEquals(typeof (process.env.HELLO), "string"); assertEquals(process.env.HELLO, "WORLD");
assertEquals(typeof env.HELLO, "string"); assertEquals(env.HELLO, "WORLD"); },});
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); },});
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); },});
Deno.test({ name: "process.hrtime", fn() { const [sec0, nano0] = process.hrtime(); // seconds and nano seconds are positive integers. assert(sec0 > 0); assert(Number.isInteger(sec0)); assert(nano0 > 0); assert(Number.isInteger(nano0));
const [sec1, nano1] = process.hrtime(); // the later call returns bigger value assert(sec1 >= sec0); assert(nano1 > nano0);
const [sec2, nano2] = process.hrtime([sec1, nano1]); // the difference of the 2 calls is a small positive value. assertEquals(sec2, 0); assert(nano2 > 0); },});
std

Version Info

Tagged at
3 years ago

External Dependencies

No external dependencies 🎉