|
1 | 1 | 'use strict'
|
2 | 2 |
|
3 | 3 | const { test } = require('brittle')
|
| 4 | +const path = require('bare-path') |
| 5 | +const fs = require('bare-fs') |
| 6 | +const os = require('bare-os') |
4 | 7 |
|
5 |
| -test('crasher', async function (t) { |
6 |
| - // TODO: write tests |
| 8 | +const Helper = require('./helper') |
| 9 | + |
| 10 | +const dirname = __dirname |
| 11 | + |
| 12 | +test('crasher uncaught exception', async function (t) { |
| 13 | + const dir = path.join(dirname, 'fixtures', 'run-crasher-uncaught-exception') |
| 14 | + |
| 15 | + const teardown = Helper.rig({ runtimeArgv: [dir] }) |
| 16 | + t.teardown(teardown) |
| 17 | + |
| 18 | + const swap = path.join(os.tmpdir(), `${Date.now()}`) |
| 19 | + await fs.promises.mkdir(swap) |
| 20 | + const pipe = Pear.run(dir, [swap]) |
| 21 | + pipe.on('error', (err) => { |
| 22 | + if (err.code === 'ENOTCONN') return // when the other side destroys the pipe |
| 23 | + throw err |
| 24 | + }) |
| 25 | + |
| 26 | + const pid = await Helper.untilResult(pipe) |
| 27 | + await Helper.untilExit(pid) |
| 28 | + |
| 29 | + const crashlogPath = path.join(swap, 'testProcess.crash.log') |
| 30 | + await Helper.untilExists(crashlogPath) |
| 31 | + t.ok(fs.existsSync(crashlogPath), 'Crash log file should be created') |
| 32 | + |
| 33 | + await Helper.untilHandler(async () => { |
| 34 | + const logContent = await fs.promises.readFile(crashlogPath, 'utf8') |
| 35 | + return logContent.includes('Test uncaught exception') |
| 36 | + }) |
| 37 | + const logContent = await fs.promises.readFile(crashlogPath, 'utf8') |
| 38 | + t.ok(logContent.includes('Test uncaught exception'), 'Log should contain the error message') |
| 39 | +}) |
| 40 | + |
| 41 | +test('crasher unhandled rejection', async function (t) { |
| 42 | + const dir = path.join(dirname, 'fixtures', 'run-crasher-unhandled-rejection') |
| 43 | + |
| 44 | + const teardown = Helper.rig({ runtimeArgv: [dir] }) |
| 45 | + t.teardown(teardown) |
| 46 | + |
| 47 | + const swap = path.join(os.tmpdir(), `${Date.now()}`) |
| 48 | + await fs.promises.mkdir(swap) |
| 49 | + const pipe = Pear.run(dir, [swap]) |
| 50 | + pipe.on('error', (err) => { |
| 51 | + if (err.code === 'ENOTCONN') return // when the other side destroys the pipe |
| 52 | + throw err |
| 53 | + }) |
| 54 | + |
| 55 | + const pid = await Helper.untilResult(pipe) |
| 56 | + await Helper.untilExit(pid) |
| 57 | + |
| 58 | + const crashlogPath = path.join(swap, 'testProcess.crash.log') |
| 59 | + await Helper.untilExists(crashlogPath) |
| 60 | + t.ok(fs.existsSync(crashlogPath), 'Crash log file should be created') |
| 61 | + |
| 62 | + await Helper.untilHandler(async () => { |
| 63 | + const logContent = await fs.promises.readFile(crashlogPath, 'utf8') |
| 64 | + return logContent.includes('Test unhandled rejection') |
| 65 | + }) |
| 66 | + const logContent = await fs.promises.readFile(crashlogPath, 'utf8') |
| 67 | + t.ok(logContent.includes('Test unhandled rejection'), 'Log should contain the error message') |
7 | 68 | })
|
0 commit comments