UNPKG

1.78 kBJavaScriptView Raw
1var exec = require('child_process').exec,
2 t = require('tap')
3
4var fixture = require.resolve('./fixtures/change-code.js')
5var expect = require('./fixtures/change-code-expect.json')
6
7// process.exitCode has problems prior to:
8// https://github.com/joyent/node/commit/c0d81f90996667a658aa4403123e02161262506a
9function isZero10 () {
10 return /^v0\.10\..+$/.test(process.version)
11}
12
13// process.exit(code), process.exitCode = code, normal exit
14var types = [ 'explicit', 'normal' ]
15if (!isZero10()) types.push('code')
16
17// initial code that is set. Note, for 'normal' exit, there's no
18// point doing these, because we just exit without modifying code
19var codes = [ 0, 2, 'null' ]
20
21// do not change, change to 5 with exit(), change to 5 with exitCode,
22// change to 5 and then to 2 with exit(), change twice with exitcode
23var changes = [ 'nochange', 'change', 'twice']
24if (!isZero10()) changes.push('code', 'twicecode')
25
26// use signal-exit, use process.on('exit')
27var handlers = [ 'sigexit', 'nosigexit' ]
28
29var opts = []
30types.forEach(function (type) {
31 var testCodes = type === 'normal' ? [0] : codes
32 testCodes.forEach(function (code) {
33 changes.forEach(function (change) {
34 handlers.forEach(function (handler) {
35 opts.push([type, code, change, handler].join(' '))
36 })
37 })
38 })
39})
40
41opts.forEach(function (opt) {
42 t.test(opt, function (t) {
43 var cmd = process.execPath + ' ' + fixture + ' ' + opt
44 exec(cmd, function (err, stdout, stderr) {
45 var res = JSON.parse(stdout)
46 if (err) {
47 res.actualCode = err.code
48 res.actualSignal = err.signal
49 } else {
50 res.actualCode = 0
51 res.actualSignal = null
52 }
53 res.stderr = stderr.trim().split('\n')
54 t.same(res, expect[opt])
55 t.end()
56 })
57 })
58})