Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F2916104
child_process.js
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
3 KB
Referenced Files
None
Subscribers
None
child_process.js
View Options
import
{
test
,
testThrows
,
summary
}
from
'./helpers.js'
;
import
{
spawn
,
exec
,
execSync
,
spawnSync
}
from
'child_process'
;
console
.
log
(
'Child Process Tests\n'
);
test
(
'execSync returns stdout'
,
execSync
(
'echo hello'
).
trim
(),
'hello'
);
test
(
'execSync with spaces'
,
execSync
(
'echo "hello world"'
).
trim
(),
'hello world'
);
test
(
'execSync exit code 0'
,
typeof
execSync
(
'true'
),
'string'
);
testThrows
(
'execSync throws on non-zero exit'
,
()
=>
{
execSync
(
'exit 1'
);
});
const
syncResult
=
spawnSync
(
'echo'
,
[
'hello'
,
'world'
]);
test
(
'spawnSync stdout'
,
syncResult
.
stdout
.
trim
(),
'hello world'
);
test
(
'spawnSync status 0'
,
syncResult
.
status
,
0
);
test
(
'spawnSync signal null'
,
syncResult
.
signal
,
null
);
test
(
'spawnSync has pid'
,
syncResult
.
pid
>
0
,
true
);
const
failSync
=
spawnSync
(
'sh'
,
[
'-c'
,
'exit 42'
]);
test
(
'spawnSync non-zero status'
,
failSync
.
status
,
42
);
const
stderrSync
=
spawnSync
(
'sh'
,
[
'-c'
,
'echo error >&2'
]);
test
(
'spawnSync captures stderr'
,
stderrSync
.
stderr
.
trim
(),
'error'
);
const
inputSync
=
spawnSync
(
'cat'
,
[],
{
input
:
'test input'
});
test
(
'spawnSync with input'
,
inputSync
.
stdout
,
'test input'
);
let
execDone
=
false
;
const
execDoneP
=
exec
(
'echo async'
).
then
(
result
=>
{
test
(
'exec resolves with stdout'
,
result
.
stdout
.
trim
(),
'async'
);
test
(
'exec exitCode'
,
result
.
exitCode
,
0
);
execDone
=
true
;
});
let
execFailDone
=
false
;
const
execFailDoneP
=
exec
(
'exit 1'
).
catch
(
err
=>
{
test
(
'exec rejects on non-zero exit'
,
typeof
err
,
'string'
);
execFailDone
=
true
;
});
let
spawnStdout
=
''
;
let
spawnExitCode
=
null
;
let
spawnClosed
=
false
;
const
child
=
spawn
(
'sh'
,
[
'-c'
,
'echo line1; echo line2'
]);
test
(
'spawn returns object'
,
typeof
child
,
'object'
);
test
(
'spawn has pid'
,
child
.
pid
>
0
,
true
);
test
(
'spawn has on method'
,
typeof
child
.
on
,
'function'
);
test
(
'spawn has once method'
,
typeof
child
.
once
,
'function'
);
test
(
'spawn has kill method'
,
typeof
child
.
kill
,
'function'
);
child
.
on
(
'stdout'
,
data
=>
{
spawnStdout
+=
data
;
});
child
.
on
(
'exit'
,
code
=>
{
spawnExitCode
=
code
;
});
const
childClosedP
=
new
Promise
(
resolve
=>
child
.
on
(
'close'
,
()
=>
{
spawnClosed
=
true
;
test
(
'spawn collects stdout'
,
spawnStdout
.
trim
(),
'line1\nline2'
);
test
(
'spawn exit code'
,
spawnExitCode
,
0
);
test
(
'spawn close event fired'
,
spawnClosed
,
true
);
resolve
();
}));
const
shellChildDoneP
=
childClosedP
.
then
(()
=>
new
Promise
(
resolve
=>
{
const
shellChild
=
spawn
(
'echo $HOME'
,
[],
{
shell
:
true
});
shellChild
.
on
(
'close'
,
()
=>
{
test
(
'spawn with shell option'
,
shellChild
.
stdout
.
length
>
0
,
true
);
resolve
();
});
}));
const
stderrChild
=
spawn
(
'sh'
,
[
'-c'
,
'echo err >&2'
]);
let
stderrData
=
''
;
stderrChild
.
on
(
'stderr'
,
data
=>
{
stderrData
+=
data
;
});
stderrChild
.
on
(
'close'
,
()
=>
{
test
(
'spawn captures stderr'
,
stderrData
.
trim
(),
'err'
);
});
const
longChild
=
spawn
(
'sleep'
,
[
'10'
]);
longChild
.
on
(
'close'
,
()
=>
{});
const
killResult
=
longChild
.
kill
(
'SIGTERM'
);
test
(
'spawn kill returns true'
,
killResult
,
true
);
Promise
.
all
([
execDoneP
,
execFailDoneP
,
shellChildDoneP
]).
then
(()
=>
{
test
(
'exec async completed'
,
execDone
,
true
);
test
(
'exec fail async completed'
,
execFailDone
,
true
);
summary
();
});
File Metadata
Details
Attached
Mime Type
application/javascript
Expires
Thu, Mar 26, 4:41 PM (1 d, 18 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
511827
Default Alt Text
child_process.js (3 KB)
Attached To
Mode
rANT Ant
Attached
Detach File
Event Timeline
Log In to Comment