@@ -4,55 +4,56 @@ const { describe, it } = require('mocha')
44const assert = require ( 'assert' )
55const path = require ( 'path' )
66const fs = require ( 'graceful-fs' )
7- const { execFileSync, execFile } = require ( 'child_process' )
87const os = require ( 'os' )
8+ const cp = require ( 'child_process' )
9+ const util = require ( '../lib/util' )
10+
911const addonPath = path . resolve ( __dirname , 'node_modules' , 'hello_world' )
1012const nodeGyp = path . resolve ( __dirname , '..' , 'bin' , 'node-gyp.js' )
1113
12- function runHello ( hostProcess ) {
13- if ( ! hostProcess ) {
14- hostProcess = process . execPath
15- }
14+ const execFileSync = ( ...args ) => cp . execFileSync ( ...args ) . toString ( ) . trim ( )
15+
16+ const execFile = async ( cmd ) => {
17+ const [ err , , stderr ] = await util . execFile ( process . execPath , cmd , {
18+ env : { ...process . env , NODE_GYP_NULL_LOGGER : undefined } ,
19+ encoding : 'utf-8'
20+ } )
21+ return [ err , stderr . toString ( ) . trim ( ) . split ( / \r ? \n / ) ]
22+ }
23+
24+ function runHello ( hostProcess = process . execPath ) {
1625 const testCode = "console.log(require('hello_world').hello())"
17- return execFileSync ( hostProcess , [ '-e' , testCode ] , { cwd : __dirname } ) . toString ( )
26+ return execFileSync ( hostProcess , [ '-e' , testCode ] , { cwd : __dirname } )
1827}
1928
2029function getEncoding ( ) {
2130 const code = 'import locale;print(locale.getdefaultlocale()[1])'
22- return execFileSync ( 'python' , [ '-c' , code ] ) . toString ( ) . trim ( )
31+ return execFileSync ( 'python' , [ '-c' , code ] )
2332}
2433
2534function checkCharmapValid ( ) {
26- let data
2735 try {
28- data = execFileSync ( 'python' , [ 'fixtures/test-charmap.py' ] ,
29- { cwd : __dirname } )
30- } catch ( err ) {
36+ const data = execFileSync ( 'python' , [ 'fixtures/test-charmap.py' ] , { cwd : __dirname } )
37+ return data . split ( '\n' ) . pop ( ) === 'True'
38+ } catch {
3139 return false
3240 }
33- const lines = data . toString ( ) . trim ( ) . split ( '\n' )
34- return lines . pop ( ) === 'True'
3541}
3642
3743describe ( 'addon' , function ( ) {
3844 this . timeout ( 300000 )
3945
40- it ( 'build simple addon' , function ( done ) {
46+ it ( 'build simple addon' , async function ( ) {
4147 // Set the loglevel otherwise the output disappears when run via 'npm test'
4248 const cmd = [ nodeGyp , 'rebuild' , '-C' , addonPath , '--loglevel=verbose' ]
43- const proc = execFile ( process . execPath , cmd , function ( err , stdout , stderr ) {
44- const logLines = stderr . toString ( ) . trim ( ) . split ( / \r ? \n / )
45- const lastLine = logLines [ logLines . length - 1 ]
46- assert . strictEqual ( err , null )
47- assert . strictEqual ( lastLine , 'gyp info ok' , 'should end in ok' )
48- assert . strictEqual ( runHello ( ) . trim ( ) , 'world' )
49- done ( )
50- } )
51- proc . stdout . setEncoding ( 'utf-8' )
52- proc . stderr . setEncoding ( 'utf-8' )
49+ const [ err , logLines ] = await execFile ( cmd )
50+ const lastLine = logLines [ logLines . length - 1 ]
51+ assert . strictEqual ( err , null )
52+ assert . strictEqual ( lastLine , 'gyp info ok' , 'should end in ok' )
53+ assert . strictEqual ( runHello ( ) , 'world' )
5354 } )
5455
55- it ( 'build simple addon in path with non-ascii characters' , function ( done ) {
56+ it ( 'build simple addon in path with non-ascii characters' , async function ( ) {
5657 if ( ! checkCharmapValid ( ) ) {
5758 return this . skip ( 'python console app can\'t encode non-ascii character.' )
5859 }
@@ -105,46 +106,30 @@ describe('addon', function () {
105106 '--loglevel=verbose' ,
106107 '-nodedir=' + testNodeDir
107108 ]
108- const proc = execFile ( process . execPath , cmd , function ( err , stdout , stderr ) {
109- try {
110- fs . unlink ( testNodeDir )
111- } catch ( err ) {
112- assert . fail ( err )
113- }
114-
115- const logLines = stderr . toString ( ) . trim ( ) . split ( / \r ? \n / )
116- const lastLine = logLines [ logLines . length - 1 ]
117- assert . strictEqual ( err , null )
118- assert . strictEqual ( lastLine , 'gyp info ok' , 'should end in ok' )
119- assert . strictEqual ( runHello ( ) . trim ( ) , 'world' )
120- done ( )
121- } )
122- proc . stdout . setEncoding ( 'utf-8' )
123- proc . stderr . setEncoding ( 'utf-8' )
124- } )
125-
126- it ( 'addon works with renamed host executable' , function ( done ) {
127- // No `fs.copyFileSync` before node8.
128- if ( process . version . substr ( 1 ) . split ( '.' ) [ 0 ] < 8 ) {
129- return this . skip ( 'skipping test for old node version' )
109+ const [ err , logLines ] = await execFile ( cmd )
110+ try {
111+ fs . unlink ( testNodeDir )
112+ } catch ( err ) {
113+ assert . fail ( err )
130114 }
115+ const lastLine = logLines [ logLines . length - 1 ]
116+ assert . strictEqual ( err , null )
117+ assert . strictEqual ( lastLine , 'gyp info ok' , 'should end in ok' )
118+ assert . strictEqual ( runHello ( ) , 'world' )
119+ } )
131120
121+ it ( 'addon works with renamed host executable' , async function ( ) {
132122 this . timeout ( 300000 )
133123
134124 const notNodePath = path . join ( os . tmpdir ( ) , 'notnode' + path . extname ( process . execPath ) )
135125 fs . copyFileSync ( process . execPath , notNodePath )
136126
137127 const cmd = [ nodeGyp , 'rebuild' , '-C' , addonPath , '--loglevel=verbose' ]
138- const proc = execFile ( process . execPath , cmd , function ( err , stdout , stderr ) {
139- const logLines = stderr . toString ( ) . trim ( ) . split ( / \r ? \n / )
140- const lastLine = logLines [ logLines . length - 1 ]
141- assert . strictEqual ( err , null )
142- assert . strictEqual ( lastLine , 'gyp info ok' , 'should end in ok' )
143- assert . strictEqual ( runHello ( notNodePath ) . trim ( ) , 'world' )
144- fs . unlinkSync ( notNodePath )
145- done ( )
146- } )
147- proc . stdout . setEncoding ( 'utf-8' )
148- proc . stderr . setEncoding ( 'utf-8' )
128+ const [ err , logLines ] = await execFile ( cmd )
129+ const lastLine = logLines [ logLines . length - 1 ]
130+ assert . strictEqual ( err , null )
131+ assert . strictEqual ( lastLine , 'gyp info ok' , 'should end in ok' )
132+ assert . strictEqual ( runHello ( notNodePath ) , 'world' )
133+ fs . unlinkSync ( notNodePath )
149134 } )
150135} )
0 commit comments