Skip to content

Commit

Permalink
Merge branch 'exitCode' of https://github.com/rlidwka/npm
Browse files Browse the repository at this point in the history
  • Loading branch information
rlidwka committed Mar 8, 2014
2 parents 60b7dc4 + f39ce0b commit 5d6bffc
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/utils/error-handler.js
Expand Up @@ -37,7 +37,16 @@ process.on("exit", function (code) {
if (exitCode === 0 && !itWorked) {
exitCode = 1
}
if (exitCode !== 0) process.exitCode = exitCode
if (typeof(process.exitCode) === 'undefined') {
// script was executed by old node.js version (< 0.11.7),
// which does not have process.exitCode, so we're
// exitting right away
//
// this will not call any other process.exit handlers
if (exitCode !== 0) process.exit(exitCode)
} else {
if (exitCode !== 0) process.exitCode = exitCode
}
} else {
itWorked = false // ready for next exit
}
Expand Down
44 changes: 44 additions & 0 deletions test/tap/cleanup-on-exit.js
@@ -0,0 +1,44 @@
if (process.argv[2] === undefined) {
// parent

var test = require('tap').test
, path = require('path')
, fs = require('fs')
, spawn = require('child_process').spawn
, node = process.execPath

test('clean lock files on exit', function (t) {
if (process.versions.node < '0.11') return t.end()

var lockFile = path.join(__dirname, Math.random() + '.lock')
var child = spawn(node, [ __filename, lockFile ], { stdio: 'pipe' })
var stdout = ''
child.stdout.on('data', function(d) {
stdout += d
})
child.on('exit', function(code) {
t.ok(!fs.existsSync(lockFile))
t.equal(code, 1)
t.equal(stdout, 'loaded\n')
t.end()
})
})

} else {
// child

var npm = require('../../')
, path = require('path')
, errorHandler = require('../../lib/utils/error-handler')

npm.load({}, function () {
var f = process.argv[2]
require('lockfile').lock(f, function(err) {
if (err) console.log(err)
console.log('loaded')
errorHandler(new Error('foo'))
process.exit()
})
})
}

0 comments on commit 5d6bffc

Please sign in to comment.