Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: isolate individual assert test to improve test hygiene #20861

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
test: improve assert test hygiene
Do not pollute the source tree for the test. Instead of writing to the
source tree, spawn a process with the temp dir as cwd and write the file
there.
  • Loading branch information
Trott committed May 24, 2018
commit 564ccacd3d4e6bd19937f94110cc772ffc410236
47 changes: 27 additions & 20 deletions test/parallel/test-assert-builtins-not-read-from-filesystem.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
// Flags: --expose-internals

'use strict';

require('../common');
// Do not read filesystem when creating AssertionError messages for code in
// builtin modules.

require('../common');
const assert = require('assert');
const EventEmitter = require('events');
const { errorCache } = require('internal/assert');
const { writeFileSync, unlinkSync } = require('fs');

// Do not read filesystem for error messages in builtin modules.
{
if (process.argv[2] !== 'child') {
const tmpdir = require('../common/tmpdir');
tmpdir.refresh();
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);
Expand All @@ -27,18 +36,16 @@ const { writeFileSync, unlinkSync } = require('fs');
assert.strictEqual(errorCache.size, size - 1);
const data = `${'\n'.repeat(line - 1)}${' '.repeat(column - 1)}` +
'ok(failed(badly));';
try {
writeFileSync(filename, data);
assert.throws(
() => e.emit('hello', false),
{
message: 'false == true'
}
);
threw = true;
} finally {
unlinkSync(filename);
}

writeFileSync(filename, data);
assert.throws(
() => e.emit('hello', false),
{
message: 'false == true'
}
);
threw = true;

}
assert(threw);
}