UNPKG

1.94 kBJavaScriptView Raw
1'use strict'
2
3const test = require('tap').test
4const os = require('os')
5const path = require('path')
6const async = require('async')
7const { spawn } = require('child_process')
8const endpoint = require('endpoint')
9const CollectAndRead = require('./collect-and-read.js')
10
11const isWindows = os.platform() === 'win32'
12let skip
13
14skip = isWindows ? 'Skip test as we cannot easily send SIGINT on Windows' : false
15test('cmd - collect - external SIGINT is relayed', { skip }, function (t) {
16 const child = spawn(
17 process.execPath, [
18 path.resolve(__dirname, 'cmd-collect-exit-sigint.script.js')
19 ], {
20 cwd: __dirname
21 }
22 )
23
24 child.stdout.once('data', () => child.kill('SIGINT'))
25
26 async.parallel({
27 stdout (done) { child.stdout.pipe(endpoint(done)) },
28 stderr (done) { child.stderr.pipe(endpoint(done)) }
29 }, function (err, output) {
30 if (err) return t.ifError(err)
31
32 // Expect the WARNING output to be shown
33 t.ok(output.stderr.toString().split('\n').length, 1)
34 t.strictEqual(output.stdout.toString(),
35 'listening for SIGINT\nSIGINT received\n')
36 t.end()
37 })
38})
39
40test('cmd - collect - non-success exit code should not throw', function (t) {
41 const cmd = new CollectAndRead({}, '--expose-gc', '-e', 'process.exit(1)')
42 cmd.on('error', t.ifError.bind(t))
43 cmd.on('ready', function () {
44 t.end()
45 })
46})
47
48// On Windows, process.exit(1) and'process.kill(pid, 'SIGKILL') both give exit code 1
49// TODO: test Windows SIGKILL behaviour, if it should error, find reliable detection
50skip = isWindows ? 'Skip test as SIGKILL also sends exit code 1 on Windows' : false
51test('cmd - collect - SIGKILL causes error', { skip }, function (t) {
52 const cmd = new CollectAndRead(
53 {},
54 '--expose-gc', '-e', 'process.kill(process.pid, "SIGKILL")'
55 )
56
57 cmd.once('error', function (err) {
58 cmd.cleanup()
59 t.strictDeepEqual(err, new Error('process exited by signal SIGKILL'))
60 t.end()
61 })
62})