@@ -6,9 +6,9 @@ if (common.isWindows && (process.env.PROCESSOR_ARCHITEW6432 !== undefined))
66const fs = require ( 'fs' ) ;
77const path = require ( 'path' ) ;
88const assert = require ( 'assert' ) ;
9+ const { fork } = require ( 'child_process' ) ;
910
1011const tmpdir = require ( '../../common/tmpdir' ) ;
11- tmpdir . refresh ( ) ;
1212
1313// Make a path that is more than 260 chars long.
1414// Any given folder cannot have a name longer than 260 characters,
@@ -17,7 +17,6 @@ let addonDestinationDir = path.resolve(tmpdir.path);
1717
1818for ( let i = 0 ; i < 10 ; i ++ ) {
1919 addonDestinationDir = path . join ( addonDestinationDir , 'x' . repeat ( 30 ) ) ;
20- fs . mkdirSync ( addonDestinationDir ) ;
2120}
2221
2322const addonPath = path . join ( __dirname ,
@@ -26,11 +25,29 @@ const addonPath = path.join(__dirname,
2625 'binding.node' ) ;
2726const addonDestinationPath = path . join ( addonDestinationDir , 'binding.node' ) ;
2827
28+ // Loading an addon keeps the file open until the process terminates. Load
29+ // the addon in a child process so that when the parent terminates the file
30+ // is already closed and the tmpdir can be cleaned up.
31+
32+ // Child
33+ if ( process . argv [ 2 ] === 'child' ) {
34+ // Attempt to load at long path destination
35+ const addon = require ( addonDestinationPath ) ;
36+ assert . notStrictEqual ( addon , null ) ;
37+ assert . strictEqual ( addon . hello ( ) , 'world' ) ;
38+ return ;
39+ }
40+
41+ // Parent
42+ tmpdir . refresh ( ) ;
43+
2944// Copy binary to long path destination
45+ fs . mkdirSync ( addonDestinationDir , { recursive : true } ) ;
3046const contents = fs . readFileSync ( addonPath ) ;
3147fs . writeFileSync ( addonDestinationPath , contents ) ;
3248
33- // Attempt to load at long path destination
34- const addon = require ( addonDestinationPath ) ;
35- assert . notStrictEqual ( addon , null ) ;
36- assert . strictEqual ( addon . hello ( ) , 'world' ) ;
49+ // Run test
50+ const child = fork ( __filename , [ 'child' ] , { stdio : 'inherit' } ) ;
51+ child . on ( 'exit' , common . mustCall ( function ( code ) {
52+ assert . strictEqual ( code , 0 ) ;
53+ } ) ) ;
0 commit comments