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

Commit

Permalink
test: add test for getting spawn args
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Jul 17, 2019
1 parent 051cf20 commit 9986486
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 15 deletions.
37 changes: 22 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,14 +261,8 @@ function runCmd (note, cmd, pkg, env, stage, wd, opts, cb) {
}
}

function runCmd_ (cmd, pkg, env, wd, opts, stage, unsafe, uid, gid, cb_) {
function cb (er) {
cb_.apply(null, arguments)
opts.log.resume()
process.nextTick(dequeue)
}

var conf = {
const getSpawnArgs = ({ cmd, wd, opts, uid, gid, unsafe, env }) => {
const conf = {
cwd: wd,
env: env,
stdio: opts.stdio || [ 0, 1, 2 ]
Expand All @@ -279,14 +273,13 @@ function runCmd_ (cmd, pkg, env, wd, opts, stage, unsafe, uid, gid, cb_) {
conf.gid = gid ^ 0
}

var sh = 'sh'
var shFlag = '-c'

var customShell = opts.scriptShell
const customShell = opts.scriptShell

let sh = 'sh'
let shFlag = '-c'
if (customShell) {
sh = customShell
} else if (isWindows) {
} else if (isWindows || opts._TESTING_FAKE_WINDOWS_) {
sh = process.env.comspec || 'cmd'
// '/d /s /c' is used only for cmd.exe.
if (/^(?:.*\\)?cmd(?:\.exe)?$/i.test(sh)) {
Expand All @@ -295,11 +288,25 @@ function runCmd_ (cmd, pkg, env, wd, opts, stage, unsafe, uid, gid, cb_) {
}
}

return [sh, [shFlag, cmd], conf]
}

exports._getSpawnArgs = getSpawnArgs

function runCmd_ (cmd, pkg, env, wd, opts, stage, unsafe, uid, gid, cb_) {
function cb (er) {
cb_.apply(null, arguments)
opts.log.resume()
process.nextTick(dequeue)
}

const [sh, args, conf] = getSpawnArgs({ cmd, wd, opts, uid, gid, unsafe, env })

opts.log.verbose('lifecycle', logid(pkg, stage), 'PATH:', env[PATH])
opts.log.verbose('lifecycle', logid(pkg, stage), 'CWD:', wd)
opts.log.silly('lifecycle', logid(pkg, stage), 'Args:', [shFlag, cmd])
opts.log.silly('lifecycle', logid(pkg, stage), 'Args:', args)

var proc = spawn(sh, [shFlag, cmd], conf, opts.log)
var proc = spawn(sh, args, conf, opts.log)

proc.on('error', procError)
proc.on('close', function (code, signal) {
Expand Down
89 changes: 89 additions & 0 deletions tap-snapshots/test-get-spawn-args.js-TAP.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/* IMPORTANT
* This snapshot file is auto-generated, but designed for humans.
* It should be checked into source control and tracked carefully.
* Re-generate by setting TAP_SNAPSHOT=1 and running tests.
* Make sure to inspect the output below. Do not ignore changes!
*/
'use strict'
exports[`test/get-spawn-args.js TAP > custom windows script shell 1`] = `
[
'flerbbyderb',
[ '-c', 'cmd' ],
{
cwd: '/working/dir',
env: { env: 'iron', men: 'tal' },
stdio: [ 0, 1, 2 ],
uid: 123,
gid: 432
}
]
`

exports[`test/get-spawn-args.js TAP > just basics 1`] = `
[
'sh',
[ '-c', 'cmd' ],
{
cwd: '/working/dir',
env: { env: 'iron', men: 'tal' },
stdio: [ 0, 1, 2 ],
uid: 123,
gid: 432
}
]
`

exports[`test/get-spawn-args.js TAP > stdio and numeric string uid 1`] = `
[
'sh',
[ '-c', 'cmd' ],
{
cwd: '/working/dir',
env: { env: 'iron', men: 'tal' },
stdio: [ 3, 2, 1 ],
uid: 123,
gid: 432
}
]
`

exports[`test/get-spawn-args.js TAP > unsafe numeric string uid 1`] = `
[
'sh',
[ '-c', 'cmd' ],
{
cwd: '/working/dir',
env: { env: 'iron', men: 'tal' },
stdio: [ 3, 2, 1 ]
}
]
`

exports[`test/get-spawn-args.js TAP > weird comspec 1`] = `
[
'flerbbyderb',
[ '-c', 'cmd' ],
{
cwd: '/working/dir',
env: { env: 'iron', men: 'tal' },
stdio: [ 0, 1, 2 ],
uid: 123,
gid: 432
}
]
`

exports[`test/get-spawn-args.js TAP > windows 1`] = `
[
'CMD.exe',
[ '/d /s /c', 'cmd' ],
{
cwd: '/working/dir',
env: { env: 'iron', men: 'tal' },
stdio: [ 0, 1, 2 ],
uid: 123,
gid: 432,
windowsVerbatimArguments: true
}
]
`
43 changes: 43 additions & 0 deletions test/get-spawn-args.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const t = require('tap')
const getSpawnArgs = require('../')._getSpawnArgs

// these show up in the result, but aren't forked on
// just set the same for every case.
const cmd = 'cmd'
const wd = '/working/dir'
const env = { env: 'iron', men: 'tal' }
const b = { cmd, wd, env, uid: 123, gid: 432, opts: {} }

t.matchSnapshot(getSpawnArgs(b), 'just basics')

t.matchSnapshot(getSpawnArgs(Object.assign({}, b, {
opts: { stdio: [3, 2, 1] },
uid: '123'
})), 'stdio and numeric string uid')

t.matchSnapshot(getSpawnArgs(Object.assign({}, b, {
opts: { stdio: [3, 2, 1] },
uid: '123',
unsafe: true
})), 'unsafe numeric string uid')

process.env.comspec = 'CMD.exe'
t.matchSnapshot(getSpawnArgs(Object.assign({}, b, {
opts: {
_TESTING_FAKE_WINDOWS_: true
}
})), 'windows')

t.matchSnapshot(getSpawnArgs(Object.assign({}, b, {
opts: {
_TESTING_FAKE_WINDOWS_: true,
scriptShell: 'flerbbyderb'
}
})), 'custom windows script shell')

process.env.comspec = 'flerbbyderb'
t.matchSnapshot(getSpawnArgs(Object.assign({}, b, {
opts: {
_TESTING_FAKE_WINDOWS_: true
}
})), 'weird comspec')

0 comments on commit 9986486

Please sign in to comment.