From 5624a6f8a77b8215b25e7ca27d4ac4ed66f72aee Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Sun, 20 May 2018 22:19:39 -0700 Subject: [PATCH] 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. PR-URL: https://github.com/nodejs/node/pull/20861 Reviewed-By: Sakthipriyan Vairamani Reviewed-By: Ruben Bridgewater Reviewed-By: James M Snell Reviewed-By: Colin Ihrig Reviewed-By: Trivikram Kamat --- ...ssert-builtins-not-read-from-filesystem.js | 47 +++++++++++-------- 1 file changed, 27 insertions(+), 20 deletions(-) diff --git a/test/parallel/test-assert-builtins-not-read-from-filesystem.js b/test/parallel/test-assert-builtins-not-read-from-filesystem.js index 154dc3b33ef87a..000798aca267a3 100644 --- a/test/parallel/test-assert-builtins-not-read-from-filesystem.js +++ b/test/parallel/test-assert-builtins-not-read-from-filesystem.js @@ -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); @@ -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); }