|
| 1 | +// Copyright Joyent, Inc. and other Node contributors. |
| 2 | +// |
| 3 | +// Permission is hereby granted, free of charge, to any person obtaining a |
| 4 | +// copy of this software and associated documentation files (the |
| 5 | +// "Software"), to deal in the Software without restriction, including |
| 6 | +// without limitation the rights to use, copy, modify, merge, publish, |
| 7 | +// distribute, sublicense, and/or sell copies of the Software, and to permit |
| 8 | +// persons to whom the Software is furnished to do so, subject to the |
| 9 | +// following conditions: |
| 10 | +// |
| 11 | +// The above copyright notice and this permission notice shall be included |
| 12 | +// in all copies or substantial portions of the Software. |
| 13 | +// |
| 14 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 15 | +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 16 | +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN |
| 17 | +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| 18 | +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 19 | +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE |
| 20 | +// USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 21 | + |
| 22 | +var common = require('../common'); |
| 23 | +var assert = require('assert'); |
| 24 | + |
| 25 | +switch (process.argv[2]) { |
| 26 | + case 'child1': |
| 27 | + return child1(); |
| 28 | + case 'child2': |
| 29 | + return child2(); |
| 30 | + case 'child3': |
| 31 | + return child3(); |
| 32 | + case undefined: |
| 33 | + return parent(); |
| 34 | + default: |
| 35 | + throw new Error('wtf'); |
| 36 | +} |
| 37 | + |
| 38 | +function child1() { |
| 39 | + process.exitCode = 42; |
| 40 | + process.on('exit', function(code) { |
| 41 | + assert.equal(code, 42); |
| 42 | + }); |
| 43 | +} |
| 44 | + |
| 45 | +function child2() { |
| 46 | + process.exitCode = 99; |
| 47 | + process.on('exit', function(code) { |
| 48 | + assert.equal(code, 42); |
| 49 | + }); |
| 50 | + process.exit(42); |
| 51 | +} |
| 52 | + |
| 53 | +function child3() { |
| 54 | + process.exitCode = 99; |
| 55 | + process.on('exit', function(code) { |
| 56 | + assert.equal(code, 0); |
| 57 | + }); |
| 58 | + process.exit(0); |
| 59 | +} |
| 60 | + |
| 61 | +function parent() { |
| 62 | + test('child1', 42); |
| 63 | + test('child2', 42); |
| 64 | + test('child3', 0); |
| 65 | +} |
| 66 | + |
| 67 | +function test(arg, exit) { |
| 68 | + var spawn = require('child_process').spawn; |
| 69 | + var node = process.execPath; |
| 70 | + var f = __filename; |
| 71 | + spawn(node, [f, arg], {stdio: 'inherit'}).on('exit', function(code) { |
| 72 | + assert.equal(code, exit, 'wrong exit for ' + |
| 73 | + arg + '\nexpected:' + exit + |
| 74 | + ' but got:' + code); |
| 75 | + console.log('ok - %s exited with %d', arg, exit); |
| 76 | + }); |
| 77 | +} |
0 commit comments