|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var helpers = require('../helpers'); |
| 4 | +var runMocha = helpers.runMocha; |
| 5 | +var os = require('node:os'); |
| 6 | + |
| 7 | +const EXIT_SUCCESS = 0; |
| 8 | +const EXIT_FAILURE = 1; |
| 9 | +const SIGNAL_OFFSET = 128; |
| 10 | + |
| 11 | +describe('--posix-exit-codes', function () { |
| 12 | + if (os.platform() !== 'win32') { |
| 13 | + describe('when enabled', function () { |
| 14 | + describe('when mocha is run as a child process', () => { |
| 15 | + // 'no-warnings' node option makes mocha run as a child process |
| 16 | + const args = ['--no-warnings', '--posix-exit-codes']; |
| 17 | + |
| 18 | + it('should exit with correct POSIX shell code on SIGABRT', function (done) { |
| 19 | + var fixture = 'signals-sigabrt.fixture.js'; |
| 20 | + runMocha(fixture, args, function postmortem(err, res) { |
| 21 | + if (err) { |
| 22 | + return done(err); |
| 23 | + } |
| 24 | + expect( |
| 25 | + res.code, |
| 26 | + 'to be', |
| 27 | + SIGNAL_OFFSET + os.constants.signals.SIGABRT |
| 28 | + ); |
| 29 | + done(); |
| 30 | + }); |
| 31 | + }); |
| 32 | + |
| 33 | + it('should exit with correct POSIX shell code on SIGTERM', function (done) { |
| 34 | + // SIGTERM is not supported on Windows |
| 35 | + if (os.platform() !== 'win32') { |
| 36 | + var fixture = 'signals-sigterm.fixture.js'; |
| 37 | + runMocha(fixture, args, function postmortem(err, res) { |
| 38 | + if (err) { |
| 39 | + return done(err); |
| 40 | + } |
| 41 | + expect( |
| 42 | + res.code, |
| 43 | + 'to be', |
| 44 | + SIGNAL_OFFSET + os.constants.signals.SIGTERM |
| 45 | + ); |
| 46 | + done(); |
| 47 | + }); |
| 48 | + } else { |
| 49 | + done(); |
| 50 | + } |
| 51 | + }); |
| 52 | + |
| 53 | + it('should exit with the correct POSIX shell code on numeric fatal signal', function (done) { |
| 54 | + // not supported on Windows |
| 55 | + if (os.platform() !== 'win32') { |
| 56 | + var fixture = 'signals-sigterm-numeric.fixture.js'; |
| 57 | + runMocha(fixture, args, function postmortem(err, res) { |
| 58 | + if (err) { |
| 59 | + return done(err); |
| 60 | + } |
| 61 | + expect( |
| 62 | + res.code, |
| 63 | + 'to be', |
| 64 | + SIGNAL_OFFSET + os.constants.signals.SIGTERM |
| 65 | + ); |
| 66 | + done(); |
| 67 | + }); |
| 68 | + } else { |
| 69 | + done(); |
| 70 | + } |
| 71 | + }); |
| 72 | + |
| 73 | + it('should exit with code 1 if there are test failures', function (done) { |
| 74 | + var fixture = 'failing.fixture.js'; |
| 75 | + runMocha(fixture, args, function postmortem(err, res) { |
| 76 | + if (err) { |
| 77 | + return done(err); |
| 78 | + } |
| 79 | + expect(res.code, 'to be', EXIT_FAILURE); |
| 80 | + done(); |
| 81 | + }); |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + describe('when mocha is run in-process', () => { |
| 86 | + // Without node-specific cli options, mocha runs in-process |
| 87 | + const args = ['--posix-exit-codes']; |
| 88 | + |
| 89 | + it('should exit with the correct POSIX shell code on SIGABRT', function (done) { |
| 90 | + var fixture = 'signals-sigabrt.fixture.js'; |
| 91 | + runMocha(fixture, args, function postmortem(err, res) { |
| 92 | + if (err) { |
| 93 | + return done(err); |
| 94 | + } |
| 95 | + expect( |
| 96 | + res.code, |
| 97 | + 'to be', |
| 98 | + SIGNAL_OFFSET + os.constants.signals.SIGABRT |
| 99 | + ); |
| 100 | + done(); |
| 101 | + }); |
| 102 | + }); |
| 103 | + |
| 104 | + it('should exit with the correct POSIX shell code on SIGTERM', function (done) { |
| 105 | + // SIGTERM is not supported on Windows |
| 106 | + if (os.platform() !== 'win32') { |
| 107 | + var fixture = 'signals-sigterm.fixture.js'; |
| 108 | + runMocha(fixture, args, function postmortem(err, res) { |
| 109 | + if (err) { |
| 110 | + return done(err); |
| 111 | + } |
| 112 | + expect( |
| 113 | + res.code, |
| 114 | + 'to be', |
| 115 | + SIGNAL_OFFSET + os.constants.signals.SIGTERM |
| 116 | + ); |
| 117 | + done(); |
| 118 | + }); |
| 119 | + } else { |
| 120 | + done(); |
| 121 | + } |
| 122 | + }); |
| 123 | + |
| 124 | + it('should exit with code 1 if there are test failures', function (done) { |
| 125 | + var fixture = 'failing.fixture.js'; |
| 126 | + runMocha(fixture, args, function postmortem(err, res) { |
| 127 | + if (err) { |
| 128 | + return done(err); |
| 129 | + } |
| 130 | + expect(res.code, 'to be', EXIT_FAILURE); |
| 131 | + done(); |
| 132 | + }); |
| 133 | + }); |
| 134 | + }); |
| 135 | + }); |
| 136 | + |
| 137 | + describe('when not enabled', function () { |
| 138 | + describe('when mocha is run as a child process', () => { |
| 139 | + // any node-specific option makes mocha run as a child process |
| 140 | + var args = ['--no-warnings']; |
| 141 | + |
| 142 | + it('should exit with the number of failed tests', function (done) { |
| 143 | + var fixture = 'failing.fixture.js'; |
| 144 | + var numFailures = 3; |
| 145 | + runMocha(fixture, args, function postmortem(err, res) { |
| 146 | + if (err) { |
| 147 | + return done(err); |
| 148 | + } |
| 149 | + expect(res.code, 'to be', numFailures); |
| 150 | + done(); |
| 151 | + }); |
| 152 | + }); |
| 153 | + }); |
| 154 | + |
| 155 | + describe('when mocha is run in-process', () => { |
| 156 | + var args = []; |
| 157 | + |
| 158 | + it('should exit with the number of failed tests', function (done) { |
| 159 | + var fixture = 'failing.fixture.js'; |
| 160 | + var numFailures = 3; |
| 161 | + runMocha(fixture, args, function postmortem(err, res) { |
| 162 | + if (err) { |
| 163 | + return done(err); |
| 164 | + } |
| 165 | + expect(res.code, 'to be', numFailures); |
| 166 | + done(); |
| 167 | + }); |
| 168 | + }); |
| 169 | + }); |
| 170 | + }); |
| 171 | + } |
| 172 | +}); |
0 commit comments