1 | 'use strict'
|
2 |
|
3 | const test = require('tap').test
|
4 | const os = require('os')
|
5 | const path = require('path')
|
6 | const async = require('async')
|
7 | const { spawn } = require('child_process')
|
8 | const endpoint = require('endpoint')
|
9 | const CollectAndRead = require('./collect-and-read.js')
|
10 |
|
11 | const isWindows = os.platform() === 'win32'
|
12 | let skip
|
13 |
|
14 | skip = isWindows ? 'Skip test as we cannot easily send SIGINT on Windows' : false
|
15 | test('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 |
|
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 |
|
40 | test('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 |
|
49 |
|
50 | skip = isWindows ? 'Skip test as SIGKILL also sends exit code 1 on Windows' : false
|
51 | test('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 | })
|