forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest-diagnostics-channel-child-process.js
More file actions
94 lines (85 loc) · 2.85 KB
/
test-diagnostics-channel-child-process.js
File metadata and controls
94 lines (85 loc) · 2.85 KB
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
'use strict';
const common = require('../common');
const assert = require('assert');
const { spawn, ChildProcess } = require('child_process');
const dc = require('diagnostics_channel');
const path = require('path');
const fs = require('fs');
const tmpdir = require('../common/tmpdir');
const isChildProcess = (process) => process instanceof ChildProcess;
function testDiagnosticChannel(subscribers, test, after) {
dc.tracingChannel('child_process.spawn').subscribe(subscribers);
test(common.mustCall(() => {
dc.tracingChannel('child_process.spawn').unsubscribe(subscribers);
after?.();
}));
}
const testSuccessfulSpawn = common.mustCall(() => {
let cb;
testDiagnosticChannel(
{
start: common.mustCall(({ process: childProcess, options }) => {
assert.strictEqual(isChildProcess(childProcess), true);
assert.strictEqual(options.file, process.execPath);
}),
end: common.mustCall(({ process: childProcess }) => {
assert.strictEqual(isChildProcess(childProcess), true);
}),
error: common.mustNotCall(),
},
common.mustCall((callback) => {
cb = callback;
const child = spawn(process.execPath, ['-e', 'process.exit(0)']);
child.on('close', () => {
cb();
});
}),
testFailingSpawnENOENT
);
});
const testFailingSpawnENOENT = common.mustCall(() => {
testDiagnosticChannel(
{
start: common.mustCall(({ process: childProcess, options }) => {
assert.strictEqual(isChildProcess(childProcess), true);
assert.strictEqual(options.file, 'does-not-exist');
}),
end: common.mustNotCall(),
error: common.mustCall(({ process: childProcess, error }) => {
assert.strictEqual(isChildProcess(childProcess), true);
assert.strictEqual(error.code, 'ENOENT');
}),
},
common.mustCall((callback) => {
const child = spawn('does-not-exist');
child.on('error', () => {});
callback();
}),
common.isWindows ? undefined : testFailingSpawnEACCES,
);
});
const testFailingSpawnEACCES = !common.isWindows ? common.mustCall(() => {
tmpdir.refresh();
const noExecFile = path.join(tmpdir.path, 'no-exec');
fs.writeFileSync(noExecFile, '');
fs.chmodSync(noExecFile, 0o644);
testDiagnosticChannel(
{
start: common.mustCall(({ process: childProcess, options }) => {
assert.strictEqual(isChildProcess(childProcess), true);
assert.strictEqual(options.file, noExecFile);
}),
end: common.mustNotCall(),
error: common.mustCall(({ process: childProcess, error }) => {
assert.strictEqual(isChildProcess(childProcess), true);
assert.strictEqual(error.code, 'EACCES');
}),
},
common.mustCall((callback) => {
const child = spawn(noExecFile);
child.on('error', () => {});
callback();
}),
);
}) : undefined;
testSuccessfulSpawn();