Skip to content

Commit

Permalink
fix: cleanup temp script after running
Browse files Browse the repository at this point in the history
  • Loading branch information
nlf committed Jun 21, 2022
1 parent 24c5165 commit 7963ab5
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 68 deletions.
11 changes: 9 additions & 2 deletions lib/make-spawn-args.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint camelcase: "off" */
const isWindows = require('./is-windows.js')
const setPATH = require('./set-path.js')
const { chmodSync: chmod, writeFileSync: writeFile } = require('fs')
const { chmodSync: chmod, unlinkSync: unlink, writeFileSync: writeFile } = require('fs')
const { tmpdir } = require('os')
const { resolve } = require('path')
const which = require('which')
Expand Down Expand Up @@ -55,7 +55,14 @@ const makeSpawnArgs = options => {
...(isCmd ? { windowsVerbatimArguments: true } : {}),
}

return [scriptShell, spawnArgs, spawnOpts]
const cleanup = () => {
// delete the script, this is just a best effort
try {
unlink(scriptFile)
} catch (err) {}
}

return [scriptShell, spawnArgs, spawnOpts, cleanup]
}

module.exports = makeSpawnArgs
8 changes: 5 additions & 3 deletions lib/run-script-pkg.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const runScriptPkg = async options => {
console.log(bruce(pkg._id, event, cmd))
}

const p = promiseSpawn(...makeSpawnArgs({
const [spawnShell, spawnArgs, spawnOpts, cleanup] = makeSpawnArgs({
event,
path,
scriptShell,
Expand All @@ -63,7 +63,9 @@ const runScriptPkg = async options => {
cmd,
args,
stdioString,
}), {
})

const p = promiseSpawn(spawnShell, spawnArgs, spawnOpts, {
event,
script: cmd,
pkgid: pkg._id,
Expand All @@ -89,7 +91,7 @@ const runScriptPkg = async options => {
} else {
throw er
}
})
}).finally(cleanup)
}

module.exports = runScriptPkg
162 changes: 99 additions & 63 deletions test/make-spawn-args.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,16 @@ if (isWindows) {
t.teardown(() => {
whichPaths.delete('cmd')
})
t.match(makeSpawnArgs({
event: 'event',
path: 'path',
cmd: 'script "quoted parameter"; second command',
}), [
'cmd',
['/d', '/s', '/c', /\.cmd$/],
{

t.test('simple script', (t) => {
const [shell, args, opts, cleanup] = makeSpawnArgs({
event: 'event',
path: 'path',
cmd: 'script "quoted parameter"; second command',
})
t.equal(shell, 'cmd', 'default shell applies')
t.match(args, ['/d', '/s', '/c', /\.cmd$/], 'got expected args')
t.match(opts, {
env: {
npm_package_json: /package\.json$/,
npm_lifecycle_event: 'event',
Expand All @@ -77,23 +79,29 @@ if (isWindows) {
stdio: undefined,
cwd: 'path',
windowsVerbatimArguments: true,
},
])
}, 'got expected options')

// with a funky ComSpec
process.env.ComSpec = 'blrorp'
whichPaths.set('blrorp', '/bin/blrorp')
t.teardown(() => {
whichPaths.delete('blrorp')
t.ok(fs.existsSync(args[args.length - 1]), 'script file was written')
cleanup()
t.not(fs.existsSync(args[args.length - 1]), 'cleanup removes script file')

t.end()
})
t.match(makeSpawnArgs({
event: 'event',
path: 'path',
cmd: 'script "quoted parameter"; second command',
}), [
'blrorp',
['-c', /\.sh$/],
{

t.test('with a funky ComSpec', (t) => {
process.env.ComSpec = 'blrorp'
whichPaths.set('blrorp', '/bin/blrorp')
t.teardown(() => {
whichPaths.delete('blrorp')
})
const [shell, args, opts, cleanup] = makeSpawnArgs({
event: 'event',
path: 'path',
cmd: 'script "quoted parameter"; second command',
})
t.equal(shell, 'blrorp', 'used ComSpec as default shell')
t.match(args, ['-c', /\.sh$/], 'got expected args')
t.match(opts, {
env: {
npm_package_json: /package\.json$/,
npm_lifecycle_event: 'event',
Expand All @@ -102,19 +110,26 @@ if (isWindows) {
stdio: undefined,
cwd: 'path',
windowsVerbatimArguments: undefined,
},
])

t.match(makeSpawnArgs({
event: 'event',
path: 'path',
cmd: 'script',
args: ['"quoted parameter";', 'second command'],
scriptShell: 'cmd.exe',
}), [
'cmd.exe',
['/d', '/s', '/c', /\.cmd$/],
{
}, 'got expected options')

t.ok(fs.existsSync(args[args.length - 1]), 'script file was written')
cleanup()
t.not(fs.existsSync(args[args.length - 1]), 'cleanup removes script file')

t.end()
})

t.test('with cmd.exe as scriptShell', (t) => {
const [shell, args, opts, cleanup] = makeSpawnArgs({
event: 'event',
path: 'path',
cmd: 'script',
args: ['"quoted parameter";', 'second command'],
scriptShell: 'cmd.exe',
})
t.equal(shell, 'cmd.exe', 'kept cmd.exe')
t.match(args, ['/d', '/s', '/c', /\.cmd$/], 'got expected args')
t.match(opts, {
env: {
npm_package_json: /package\.json$/,
npm_lifecycle_event: 'event',
Expand All @@ -123,8 +138,14 @@ if (isWindows) {
stdio: undefined,
cwd: 'path',
windowsVerbatimArguments: true,
},
])
}, 'got expected options')

t.ok(fs.existsSync(args[args.length - 1]), 'script file was written')
cleanup()
t.not(fs.existsSync(args[args.length - 1]), 'cleanup removes script file')

t.end()
})

t.end()
})
Expand All @@ -134,15 +155,17 @@ if (isWindows) {
t.teardown(() => {
whichPaths.delete('sh')
})
t.match(makeSpawnArgs({
event: 'event',
path: 'path',
cmd: 'script',
args: ['"quoted parameter";', 'second command'],
}), [
'sh',
['-c', /\.sh$/],
{

t.test('simple script', (t) => {
const [shell, args, opts, cleanup] = makeSpawnArgs({
event: 'event',
path: 'path',
cmd: 'script',
args: ['"quoted parameter";', 'second command'],
})
t.equal(shell, 'sh', 'defaults to sh')
t.match(args, ['-c', /\.sh$/], 'got expected args')
t.match(opts, {
env: {
npm_package_json: /package\.json$/,
npm_lifecycle_event: 'event',
Expand All @@ -151,20 +174,27 @@ if (isWindows) {
stdio: undefined,
cwd: 'path',
windowsVerbatimArguments: undefined,
},
])

// test that we can explicitly run in cmd.exe, even on posix systems
// relevant for running under WSL
t.match(makeSpawnArgs({
event: 'event',
path: 'path',
cmd: 'script "quoted parameter"; second command',
scriptShell: 'cmd.exe',
}), [
'cmd.exe',
['/d', '/s', '/c', /\.cmd$/],
{
}, 'got expected options')

t.ok(fs.existsSync(args[args.length - 1]), 'script file was written')
cleanup()
t.not(fs.existsSync(args[args.length - 1]), 'cleanup removes script file')

t.end()
})

t.test('can use cmd.exe', (t) => {
// test that we can explicitly run in cmd.exe, even on posix systems
// relevant for running under WSL
const [shell, args, opts, cleanup] = makeSpawnArgs({
event: 'event',
path: 'path',
cmd: 'script "quoted parameter"; second command',
scriptShell: 'cmd.exe',
})
t.equal(shell, 'cmd.exe', 'kept cmd.exe')
t.match(args, ['/d', '/s', '/c', /\.cmd$/], 'got expected args')
t.match(opts, {
env: {
npm_package_json: /package\.json$/,
npm_lifecycle_event: 'event',
Expand All @@ -173,8 +203,14 @@ if (isWindows) {
stdio: undefined,
cwd: 'path',
windowsVerbatimArguments: true,
},
])
}, 'got expected options')

t.ok(fs.existsSync(args[args.length - 1]), 'script file was written')
cleanup()
t.not(fs.existsSync(args[args.length - 1]), 'cleanup removes script file')

t.end()
})

t.end()
})
Expand Down

0 comments on commit 7963ab5

Please sign in to comment.