11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../../common');
41cb0ef41Sopenharmony_ciconst assert = require('assert');
51cb0ef41Sopenharmony_ciconst path = require('path');
61cb0ef41Sopenharmony_ciconst spawnSync = require('child_process').spawnSync;
71cb0ef41Sopenharmony_ciconst helper = require('../../common/report.js');
81cb0ef41Sopenharmony_ciconst tmpdir = require('../../common/tmpdir');
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ciconst binding = path.resolve(__dirname, `./build/${common.buildType}/binding`);
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ciif (process.argv[2] === 'child') {
131cb0ef41Sopenharmony_ci  (function childMain() {
141cb0ef41Sopenharmony_ci    const addon = require(binding);
151cb0ef41Sopenharmony_ci    addon.triggerFatalError();
161cb0ef41Sopenharmony_ci  })();
171cb0ef41Sopenharmony_ci  return;
181cb0ef41Sopenharmony_ci}
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ciconst ARGS = [
211cb0ef41Sopenharmony_ci  __filename,
221cb0ef41Sopenharmony_ci  'child',
231cb0ef41Sopenharmony_ci];
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci{
261cb0ef41Sopenharmony_ci  // Verify that --report-on-fatalerror is respected when set.
271cb0ef41Sopenharmony_ci  tmpdir.refresh();
281cb0ef41Sopenharmony_ci  const args = ['--report-on-fatalerror', ...ARGS];
291cb0ef41Sopenharmony_ci  const child = spawnSync(process.execPath, args, { cwd: tmpdir.path });
301cb0ef41Sopenharmony_ci  assert.notStrictEqual(child.status, 0, 'Process exited unexpectedly');
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci  const reports = helper.findReports(child.pid, tmpdir.path);
331cb0ef41Sopenharmony_ci  assert.strictEqual(reports.length, 1);
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ci  const report = reports[0];
361cb0ef41Sopenharmony_ci  helper.validate(report);
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci  const content = require(report);
391cb0ef41Sopenharmony_ci  assert.strictEqual(content.header.trigger, 'FatalError');
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ci  // Check that the javascript stack is present.
421cb0ef41Sopenharmony_ci  assert.strictEqual(content.javascriptStack.stack.findIndex((frame) => frame.match('childMain')), 0);
431cb0ef41Sopenharmony_ci}
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ci{
461cb0ef41Sopenharmony_ci  // Verify that --report-on-fatalerror is respected when not set.
471cb0ef41Sopenharmony_ci  const args = ARGS;
481cb0ef41Sopenharmony_ci  const child = spawnSync(process.execPath, args, { cwd: tmpdir.path });
491cb0ef41Sopenharmony_ci  assert.notStrictEqual(child.status, 0, 'Process exited unexpectedly');
501cb0ef41Sopenharmony_ci  const reports = helper.findReports(child.pid, tmpdir.path);
511cb0ef41Sopenharmony_ci  assert.strictEqual(reports.length, 0);
521cb0ef41Sopenharmony_ci}
53