Skip to content
This repository was archived by the owner on Aug 11, 2021. It is now read-only.

Commit 9986486

Browse files
committed
test: add test for getting spawn args
1 parent 051cf20 commit 9986486

File tree

3 files changed

+154
-15
lines changed

3 files changed

+154
-15
lines changed

index.js

+22-15
Original file line numberDiff line numberDiff line change
@@ -261,14 +261,8 @@ function runCmd (note, cmd, pkg, env, stage, wd, opts, cb) {
261261
}
262262
}
263263

264-
function runCmd_ (cmd, pkg, env, wd, opts, stage, unsafe, uid, gid, cb_) {
265-
function cb (er) {
266-
cb_.apply(null, arguments)
267-
opts.log.resume()
268-
process.nextTick(dequeue)
269-
}
270-
271-
var conf = {
264+
const getSpawnArgs = ({ cmd, wd, opts, uid, gid, unsafe, env }) => {
265+
const conf = {
272266
cwd: wd,
273267
env: env,
274268
stdio: opts.stdio || [ 0, 1, 2 ]
@@ -279,14 +273,13 @@ function runCmd_ (cmd, pkg, env, wd, opts, stage, unsafe, uid, gid, cb_) {
279273
conf.gid = gid ^ 0
280274
}
281275

282-
var sh = 'sh'
283-
var shFlag = '-c'
284-
285-
var customShell = opts.scriptShell
276+
const customShell = opts.scriptShell
286277

278+
let sh = 'sh'
279+
let shFlag = '-c'
287280
if (customShell) {
288281
sh = customShell
289-
} else if (isWindows) {
282+
} else if (isWindows || opts._TESTING_FAKE_WINDOWS_) {
290283
sh = process.env.comspec || 'cmd'
291284
// '/d /s /c' is used only for cmd.exe.
292285
if (/^(?:.*\\)?cmd(?:\.exe)?$/i.test(sh)) {
@@ -295,11 +288,25 @@ function runCmd_ (cmd, pkg, env, wd, opts, stage, unsafe, uid, gid, cb_) {
295288
}
296289
}
297290

291+
return [sh, [shFlag, cmd], conf]
292+
}
293+
294+
exports._getSpawnArgs = getSpawnArgs
295+
296+
function runCmd_ (cmd, pkg, env, wd, opts, stage, unsafe, uid, gid, cb_) {
297+
function cb (er) {
298+
cb_.apply(null, arguments)
299+
opts.log.resume()
300+
process.nextTick(dequeue)
301+
}
302+
303+
const [sh, args, conf] = getSpawnArgs({ cmd, wd, opts, uid, gid, unsafe, env })
304+
298305
opts.log.verbose('lifecycle', logid(pkg, stage), 'PATH:', env[PATH])
299306
opts.log.verbose('lifecycle', logid(pkg, stage), 'CWD:', wd)
300-
opts.log.silly('lifecycle', logid(pkg, stage), 'Args:', [shFlag, cmd])
307+
opts.log.silly('lifecycle', logid(pkg, stage), 'Args:', args)
301308

302-
var proc = spawn(sh, [shFlag, cmd], conf, opts.log)
309+
var proc = spawn(sh, args, conf, opts.log)
303310

304311
proc.on('error', procError)
305312
proc.on('close', function (code, signal) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/* IMPORTANT
2+
* This snapshot file is auto-generated, but designed for humans.
3+
* It should be checked into source control and tracked carefully.
4+
* Re-generate by setting TAP_SNAPSHOT=1 and running tests.
5+
* Make sure to inspect the output below. Do not ignore changes!
6+
*/
7+
'use strict'
8+
exports[`test/get-spawn-args.js TAP > custom windows script shell 1`] = `
9+
[
10+
'flerbbyderb',
11+
[ '-c', 'cmd' ],
12+
{
13+
cwd: '/working/dir',
14+
env: { env: 'iron', men: 'tal' },
15+
stdio: [ 0, 1, 2 ],
16+
uid: 123,
17+
gid: 432
18+
}
19+
]
20+
`
21+
22+
exports[`test/get-spawn-args.js TAP > just basics 1`] = `
23+
[
24+
'sh',
25+
[ '-c', 'cmd' ],
26+
{
27+
cwd: '/working/dir',
28+
env: { env: 'iron', men: 'tal' },
29+
stdio: [ 0, 1, 2 ],
30+
uid: 123,
31+
gid: 432
32+
}
33+
]
34+
`
35+
36+
exports[`test/get-spawn-args.js TAP > stdio and numeric string uid 1`] = `
37+
[
38+
'sh',
39+
[ '-c', 'cmd' ],
40+
{
41+
cwd: '/working/dir',
42+
env: { env: 'iron', men: 'tal' },
43+
stdio: [ 3, 2, 1 ],
44+
uid: 123,
45+
gid: 432
46+
}
47+
]
48+
`
49+
50+
exports[`test/get-spawn-args.js TAP > unsafe numeric string uid 1`] = `
51+
[
52+
'sh',
53+
[ '-c', 'cmd' ],
54+
{
55+
cwd: '/working/dir',
56+
env: { env: 'iron', men: 'tal' },
57+
stdio: [ 3, 2, 1 ]
58+
}
59+
]
60+
`
61+
62+
exports[`test/get-spawn-args.js TAP > weird comspec 1`] = `
63+
[
64+
'flerbbyderb',
65+
[ '-c', 'cmd' ],
66+
{
67+
cwd: '/working/dir',
68+
env: { env: 'iron', men: 'tal' },
69+
stdio: [ 0, 1, 2 ],
70+
uid: 123,
71+
gid: 432
72+
}
73+
]
74+
`
75+
76+
exports[`test/get-spawn-args.js TAP > windows 1`] = `
77+
[
78+
'CMD.exe',
79+
[ '/d /s /c', 'cmd' ],
80+
{
81+
cwd: '/working/dir',
82+
env: { env: 'iron', men: 'tal' },
83+
stdio: [ 0, 1, 2 ],
84+
uid: 123,
85+
gid: 432,
86+
windowsVerbatimArguments: true
87+
}
88+
]
89+
`

test/get-spawn-args.js

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
const t = require('tap')
2+
const getSpawnArgs = require('../')._getSpawnArgs
3+
4+
// these show up in the result, but aren't forked on
5+
// just set the same for every case.
6+
const cmd = 'cmd'
7+
const wd = '/working/dir'
8+
const env = { env: 'iron', men: 'tal' }
9+
const b = { cmd, wd, env, uid: 123, gid: 432, opts: {} }
10+
11+
t.matchSnapshot(getSpawnArgs(b), 'just basics')
12+
13+
t.matchSnapshot(getSpawnArgs(Object.assign({}, b, {
14+
opts: { stdio: [3, 2, 1] },
15+
uid: '123'
16+
})), 'stdio and numeric string uid')
17+
18+
t.matchSnapshot(getSpawnArgs(Object.assign({}, b, {
19+
opts: { stdio: [3, 2, 1] },
20+
uid: '123',
21+
unsafe: true
22+
})), 'unsafe numeric string uid')
23+
24+
process.env.comspec = 'CMD.exe'
25+
t.matchSnapshot(getSpawnArgs(Object.assign({}, b, {
26+
opts: {
27+
_TESTING_FAKE_WINDOWS_: true
28+
}
29+
})), 'windows')
30+
31+
t.matchSnapshot(getSpawnArgs(Object.assign({}, b, {
32+
opts: {
33+
_TESTING_FAKE_WINDOWS_: true,
34+
scriptShell: 'flerbbyderb'
35+
}
36+
})), 'custom windows script shell')
37+
38+
process.env.comspec = 'flerbbyderb'
39+
t.matchSnapshot(getSpawnArgs(Object.assign({}, b, {
40+
opts: {
41+
_TESTING_FAKE_WINDOWS_: true
42+
}
43+
})), 'weird comspec')

0 commit comments

Comments
 (0)