test: remove internal errorCache property

The internal `assert` modules `errorCache` property is exposed only for
testing. The one test that used it is rewritten here to not use it.

This has the following advantages:

* The test now makes sure that there is an empty cache in a more robust
  way. Instead of relying on the internal implementation of
  `errorCache`, it simply spawns a separate process.
* One less test using the `--expose-internals` flag.

PR-URL: https://github.com/nodejs/node/pull/23304
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
This commit is contained in:
Rich Trott 2018-10-06 18:27:28 -07:00
parent 157d507d64
commit 1f94b85069

View File

@ -5,47 +5,44 @@
require('../common'); require('../common');
const assert = require('assert'); const assert = require('assert');
const EventEmitter = require('events');
const e = new EventEmitter();
e.on('hello', assert);
if (process.argv[2] !== 'child') { if (process.argv[2] !== 'child') {
const tmpdir = require('../common/tmpdir'); const tmpdir = require('../common/tmpdir');
tmpdir.refresh(); tmpdir.refresh();
const { spawnSync } = require('child_process'); const { spawnSync } = require('child_process');
const { output, status, error } =
spawnSync(process.execPath,
['--expose-internals', process.argv[1], 'child'],
{ cwd: tmpdir.path, env: process.env });
assert.ifError(error);
assert.strictEqual(status, 0, `Exit code: ${status}\n${output}`);
} else {
const EventEmitter = require('events');
const { errorCache } = require('internal/assert');
const { writeFileSync } = require('fs');
const e = new EventEmitter();
e.on('hello', assert);
let threw = false; let threw = false;
try { try {
e.emit('hello', false); e.emit('hello', false);
} catch (err) { } catch (err) {
const frames = err.stack.split('\n'); const frames = err.stack.split('\n');
const [, filename, line, column] = frames[1].match(/\((.+):(\d+):(\d+)\)/); const [, filename, , ] = frames[1].match(/\((.+):(\d+):(\d+)\)/);
// Reset the cache to check again // Spawn a child process to avoid the error having been cached in the assert
const size = errorCache.size; // module's `errorCache` Map.
errorCache.delete(`${filename}${line - 1}${column - 1}`);
assert.strictEqual(errorCache.size, size - 1);
const data = `${'\n'.repeat(line - 1)}${' '.repeat(column - 1)}` +
'ok(failed(badly));';
writeFileSync(filename, data); const { output, status, error } =
assert.throws( spawnSync(process.execPath,
() => e.emit('hello', false), [process.argv[1], 'child', filename],
{ { cwd: tmpdir.path, env: process.env });
message: 'false == true' assert.ifError(error);
} assert.strictEqual(status, 0, `Exit code: ${status}\n${output}`);
);
threw = true; threw = true;
} }
assert(threw); assert.ok(threw);
} else {
const { writeFileSync } = require('fs');
const [, , , filename, line, column] = process.argv;
const data = `${'\n'.repeat(line - 1)}${' '.repeat(column - 1)}` +
'ok(failed(badly));';
writeFileSync(filename, data);
assert.throws(
() => e.emit('hello', false),
{
message: 'false == true'
}
);
} }