11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci// Do not read filesystem when creating AssertionError messages for code in
41cb0ef41Sopenharmony_ci// builtin modules.
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_cirequire('../common');
71cb0ef41Sopenharmony_ciconst assert = require('assert');
81cb0ef41Sopenharmony_ciconst EventEmitter = require('events');
91cb0ef41Sopenharmony_ciconst e = new EventEmitter();
101cb0ef41Sopenharmony_cie.on('hello', assert);
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ciif (process.argv[2] !== 'child') {
131cb0ef41Sopenharmony_ci  const tmpdir = require('../common/tmpdir');
141cb0ef41Sopenharmony_ci  tmpdir.refresh();
151cb0ef41Sopenharmony_ci  const { spawnSync } = require('child_process');
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ci  let threw = false;
181cb0ef41Sopenharmony_ci  try {
191cb0ef41Sopenharmony_ci    e.emit('hello', false);
201cb0ef41Sopenharmony_ci  } catch (err) {
211cb0ef41Sopenharmony_ci    const frames = err.stack.split('\n');
221cb0ef41Sopenharmony_ci    const [, filename, line, column] = frames[1].match(/\((.+):(\d+):(\d+)\)/);
231cb0ef41Sopenharmony_ci    // Spawn a child process to avoid the error having been cached in the assert
241cb0ef41Sopenharmony_ci    // module's `errorCache` Map.
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ci    const { output, status, error } =
271cb0ef41Sopenharmony_ci      spawnSync(process.execPath,
281cb0ef41Sopenharmony_ci                [process.argv[1], 'child', filename, line, column],
291cb0ef41Sopenharmony_ci                { cwd: tmpdir.path, env: process.env });
301cb0ef41Sopenharmony_ci    assert.ifError(error);
311cb0ef41Sopenharmony_ci    assert.strictEqual(status, 0, `Exit code: ${status}\n${output}`);
321cb0ef41Sopenharmony_ci    threw = true;
331cb0ef41Sopenharmony_ci  }
341cb0ef41Sopenharmony_ci  assert.ok(threw);
351cb0ef41Sopenharmony_ci} else {
361cb0ef41Sopenharmony_ci  const { writeFileSync } = require('fs');
371cb0ef41Sopenharmony_ci  const [, , , filename, line, column] = process.argv;
381cb0ef41Sopenharmony_ci  const data = `${'\n'.repeat(line - 1)}${' '.repeat(column - 1)}` +
391cb0ef41Sopenharmony_ci               'ok(failed(badly));';
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ci  writeFileSync(filename, data);
421cb0ef41Sopenharmony_ci  assert.throws(
431cb0ef41Sopenharmony_ci    () => e.emit('hello', false),
441cb0ef41Sopenharmony_ci    {
451cb0ef41Sopenharmony_ci      message: 'false == true'
461cb0ef41Sopenharmony_ci    }
471cb0ef41Sopenharmony_ci  );
481cb0ef41Sopenharmony_ci}
49