Skip to content

Commit

Permalink
test: isolate unusual assert test in its own file
Browse files Browse the repository at this point in the history
test-assert.js contains a test that writes to the source tree, requires
an internal module, and depends on modules not needed by the plethora of
other test cases in the file. Move it to its own file so that there are
not side effects in test-assert.js and so that it can be refactored to
not write to the source tree.

PR-URL: nodejs#20861
Reviewed-By: Sakthipriyan Vairamani <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Trivikram Kamat <[email protected]>
  • Loading branch information
Trott committed May 28, 2018
1 parent cfc3866 commit 6e1324e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 39 deletions.
44 changes: 44 additions & 0 deletions test/parallel/test-assert-builtins-not-read-from-filesystem.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Flags: --expose-internals

'use strict';

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.
{
const e = new EventEmitter();

e.on('hello', assert);

let threw = false;
try {
e.emit('hello', false);
} catch (err) {
const frames = err.stack.split('\n');
const [, filename, line, column] = frames[1].match(/\((.+):(\d+):(\d+)\)/);
// Reset the cache to check again
const size = errorCache.size;
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));';
try {
writeFileSync(filename, data);
assert.throws(
() => e.emit('hello', false),
{
message: 'false == true'
}
);
threw = true;
} finally {
unlinkSync(filename);
}
}
assert(threw);
}
39 changes: 0 additions & 39 deletions test/parallel/test-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,12 @@
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.

// Flags: --expose-internals

'use strict';

/* eslint-disable node-core/prefer-common-expectserror */

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

Expand Down Expand Up @@ -793,40 +788,6 @@ common.expectsError(
}
);

// Do not try to check Node.js modules.
{
const e = new EventEmitter();

e.on('hello', assert);

let threw = false;
try {
e.emit('hello', false);
} catch (err) {
const frames = err.stack.split('\n');
const [, filename, line, column] = frames[1].match(/\((.+):(\d+):(\d+)\)/);
// Reset the cache to check again
const size = errorCache.size;
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));';
try {
writeFileSync(filename, data);
assert.throws(
() => e.emit('hello', false),
{
message: 'false == true'
}
);
threw = true;
} finally {
unlinkSync(filename);
}
}
assert(threw);
}

common.expectsError(
// eslint-disable-next-line no-restricted-syntax
() => assert.throws(() => {}, 'Error message', 'message'),
Expand Down

0 comments on commit 6e1324e

Please sign in to comment.