Skip to content

Commit 0112357

Browse files
TrottMylesBorins
authored andcommitted
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: #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]>
1 parent 88f9a39 commit 0112357

File tree

1 file changed

+27
-20
lines changed

1 file changed

+27
-20
lines changed
Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
1-
// Flags: --expose-internals
2-
31
'use strict';
42

5-
require('../common');
3+
// Do not read filesystem when creating AssertionError messages for code in
4+
// builtin modules.
65

6+
require('../common');
77
constassert=require('assert');
8-
constEventEmitter=require('events');
9-
const{ errorCache }=require('internal/assert');
10-
const{ writeFileSync, unlinkSync }=require('fs');
118

12-
// Do not read filesystem for error messages in builtin modules.
13-
{
9+
if(process.argv[2]!=='child'){
10+
consttmpdir=require('../common/tmpdir');
11+
tmpdir.refresh();
12+
const{ spawnSync }=require('child_process');
13+
const{ output, status, error }=
14+
spawnSync(process.execPath,
15+
['--expose-internals',process.argv[1],'child'],
16+
{cwd: tmpdir.path,env: process.env});
17+
assert.ifError(error);
18+
assert.strictEqual(status,0,`Exit code: ${status}\n${output}`);
19+
}else{
20+
constEventEmitter=require('events');
21+
const{ errorCache }=require('internal/assert');
22+
const{ writeFileSync }=require('fs');
1423
conste=newEventEmitter();
1524

1625
e.on('hello',assert);
@@ -27,18 +36,16 @@ const{writeFileSync, unlinkSync } = require('fs');
2736
assert.strictEqual(errorCache.size,size-1);
2837
constdata=`${'\n'.repeat(line-1)}${' '.repeat(column-1)}`+
2938
'ok(failed(badly));';
30-
try{
31-
writeFileSync(filename,data);
32-
assert.throws(
33-
()=>e.emit('hello',false),
34-
{
35-
message: 'false == true'
36-
}
37-
);
38-
threw=true;
39-
}finally{
40-
unlinkSync(filename);
41-
}
39+
40+
writeFileSync(filename,data);
41+
assert.throws(
42+
()=>e.emit('hello',false),
43+
{
44+
message: 'false == true'
45+
}
46+
);
47+
threw=true;
48+
4249
}
4350
assert(threw);
4451
}

0 commit comments

Comments
(0)