2323const common = require ( '../common' ) ;
2424const hijackstdio = require ( '../common/hijackstdio' ) ;
2525const fixtures = require ( '../common/fixtures' ) ;
26+ const tmpdir = require ( '../common/tmpdir' ) ;
2627const assert = require ( 'assert' ) ;
2728const { execFile } = require ( 'child_process' ) ;
29+ const { writeFileSync, existsSync } = require ( 'fs' ) ;
30+ const { join } = require ( 'path' ) ;
2831
2932// Test for leaked global detection
3033{
@@ -124,25 +127,38 @@ const HIJACK_TEST_ARRAY = [ 'foo\n', 'bar\n', 'baz\n' ];
124127 assert . strictEqual ( originalWrite , stream . write ) ;
125128} ) ;
126129
127- // hijackStderr and hijackStdout again
128- // for console
129- [ [ 'err' , 'error' ] , [ 'out' , 'log' ] ] . forEach ( ( [ type , method ] ) => {
130- hijackstdio [ `hijackStd${ type } ` ] ( common . mustCall ( function ( data ) {
131- assert . strictEqual ( data , 'test\n' ) ;
130+ // Test `tmpdir`.
131+ {
132+ tmpdir . refresh ( ) ;
133+ assert . ok ( / \. t m p \. \d + / . test ( tmpdir . path ) ) ;
134+ const sentinelPath = join ( tmpdir . path , 'gaga' ) ;
135+ writeFileSync ( sentinelPath , 'googoo' ) ;
136+ tmpdir . refresh ( ) ;
137+ assert . strictEqual ( existsSync ( tmpdir . path ) , true ) ;
138+ assert . strictEqual ( existsSync ( sentinelPath ) , false ) ;
139+ }
132140
133- // throw an error
134- throw new Error ( `console ${ type } error` ) ;
135- } ) ) ;
141+ // hijackStderr and hijackStdout again for console
142+ // Must be last, since it uses `process.on('uncaughtException')`
143+ {
144+ [ [ 'err' , 'error' ] , [ 'out' , 'log' ] ] . forEach ( ( [ type , method ] ) => {
145+ hijackstdio [ `hijackStd${ type } ` ] ( common . mustCall ( function ( data ) {
146+ assert . strictEqual ( data , 'test\n' ) ;
136147
137- console [ method ] ( 'test' ) ;
138- hijackstdio [ `restoreStd${ type } ` ] ( ) ;
139- } ) ;
148+ // throw an error
149+ throw new Error ( `console ${ type } error` ) ;
150+ } ) ) ;
151+
152+ console [ method ] ( 'test' ) ;
153+ hijackstdio [ `restoreStd${ type } ` ] ( ) ;
154+ } ) ;
140155
141- let uncaughtTimes = 0 ;
142- process . on ( 'uncaughtException' , common . mustCallAtLeast ( function ( e ) {
143- assert . strictEqual ( uncaughtTimes < 2 , true ) ;
144- assert . strictEqual ( e instanceof Error , true ) ;
145- assert . strictEqual (
146- e . message ,
147- `console ${ ( [ 'err' , 'out' ] ) [ uncaughtTimes ++ ] } error` ) ;
148- } , 2 ) ) ;
156+ let uncaughtTimes = 0 ;
157+ process . on ( 'uncaughtException' , common . mustCallAtLeast ( function ( e ) {
158+ assert . strictEqual ( uncaughtTimes < 2 , true ) ;
159+ assert . strictEqual ( e instanceof Error , true ) ;
160+ assert . strictEqual (
161+ e . message ,
162+ `console ${ ( [ 'err' , 'out' ] ) [ uncaughtTimes ++ ] } error` ) ;
163+ } , 2 ) ) ;
164+ } // End of "Must be last".
0 commit comments