11'use strict' 
22
33var  test  =  require ( 'tape' ) 
4- var  execFile  =  require ( 'child_process' ) . execFile 
54var  path  =  require ( 'path' ) 
5+ var  fs  =  require ( 'graceful-fs' ) 
6+ var  child_process  =  require ( 'child_process' ) 
67var  addonPath  =  path . resolve ( __dirname ,  'node_modules' ,  'hello_world' ) 
78var  nodeGyp  =  path . resolve ( __dirname ,  '..' ,  'bin' ,  'node-gyp.js' ) 
9+ var  execFileSync  =  child_process . execFileSync 
10+ var  execFile  =  child_process . execFile 
11+ 
12+ function  runHello ( )  { 
13+   var  testCode  =  "console.log(require('hello_world').hello())" 
14+   return  execFileSync ( 'node' ,  [ '-e' ,  testCode ] ,  {  cwd : __dirname  } ) . toString ( ) 
15+ } 
16+ 
17+ function  getEncoding ( )  { 
18+   var  code  =  'import locale;print locale.getdefaultlocale()[1]' 
19+   return  execFileSync ( 'python' ,  [ '-c' ,  code ] ) . toString ( ) . trim ( ) 
20+ } 
21+ 
22+ function  checkCharmapValid ( )  { 
23+   var  data 
24+   try  { 
25+     data  =  execFileSync ( 'python' ,  [ 'fixtures/test-charmap.py' ] , 
26+                         {  cwd : __dirname  } ) 
27+   }  catch  ( err )  { 
28+     return  false 
29+   } 
30+   var  lines  =  data . toString ( ) . trim ( ) . split ( '\n' ) 
31+   return  lines . pop ( )  ===  'True' 
32+ } 
833
934test ( 'build simple addon' ,  function  ( t )  { 
1035  t . plan ( 3 ) 
@@ -16,12 +41,72 @@ test('build simple addon', function (t) {
1641    var  lastLine  =  logLines [ logLines . length - 1 ] 
1742    t . strictEqual ( err ,  null ) 
1843    t . strictEqual ( lastLine ,  'gyp info ok' ,  'should end in ok' ) 
44+     t . strictEqual ( runHello ( ) . trim ( ) ,  'world' ) 
45+   } ) 
46+   proc . stdout . setEncoding ( 'utf-8' ) 
47+   proc . stderr . setEncoding ( 'utf-8' ) 
48+ } ) 
49+ 
50+ test ( 'build simple addon in path with non-ascii characters' ,  function  ( t )  { 
51+   t . plan ( 1 ) 
52+ 
53+   if  ( ! checkCharmapValid ( ) )  { 
54+     return  t . skip ( 'python console app can\'t encode non-ascii character.' ) 
55+   } 
56+ 
57+   var  testDirNames  =  { 
58+     'cp936' : '文件夹' , 
59+     'cp1252' : 'Latīna' , 
60+     'cp932' : 'フォルダ' 
61+   } 
62+   // Select non-ascii characters by current encoding 
63+   var  testDirName  =  testDirNames [ getEncoding ( ) ] 
64+   // If encoding is UTF-8 or other then no need to test 
65+   if  ( ! testDirName )  { 
66+     return  t . skip ( 'no need to test' ) 
67+   } 
68+ 
69+   t . plan ( 3 ) 
70+ 
71+   var  data ,  configPath  =  path . join ( addonPath ,  'build' ,  'config.gypi' ) 
72+   try  { 
73+     data  =  fs . readFileSync ( configPath ,  'utf8' ) 
74+   }  catch  ( err )  { 
75+     t . error ( err ) 
76+     return 
77+   } 
78+   var  config  =  JSON . parse ( data . replace ( / \# .+ \n / ,  '' ) ) 
79+   var  nodeDir  =  config . variables . nodedir 
80+   var  testNodeDir  =  path . join ( addonPath ,  testDirName ) 
81+   // Create symbol link to path with non-ascii characters 
82+   try  { 
83+     fs . symlinkSync ( nodeDir ,  testNodeDir ,  'dir' ) 
84+   }  catch  ( err )  { 
85+     switch  ( err . code )  { 
86+       case  'EEXIST' : break 
87+       case  'EPERM' :
88+         t . error ( err ,  'Please try to running console as an administrator' ) 
89+         return 
90+       default :
91+         t . error ( err ) 
92+         return 
93+     } 
94+   } 
95+ 
96+   var  cmd  =  [ nodeGyp ,  'rebuild' ,  '-C' ,  addonPath , 
97+              '--loglevel=verbose' ,  '-nodedir='  +  testNodeDir ] 
98+   var  proc  =  execFile ( process . execPath ,  cmd ,  function  ( err ,  stdout ,  stderr )  { 
1999    try  { 
20-       var  binding  =  require ( 'hello_world' ) 
21-       t . strictEqual ( binding . hello ( ) ,  'world' ) 
22-     }  catch  ( error )  { 
23-       t . error ( error ,  'load module' ) 
100+       fs . unlink ( testNodeDir ) 
101+     }  catch  ( err )  { 
102+       t . error ( err ) 
24103    } 
104+ 
105+     var  logLines  =  stderr . toString ( ) . trim ( ) . split ( / \r ? \n / ) 
106+     var  lastLine  =  logLines [ logLines . length - 1 ] 
107+     t . strictEqual ( err ,  null ) 
108+     t . strictEqual ( lastLine ,  'gyp info ok' ,  'should end in ok' ) 
109+     t . strictEqual ( runHello ( ) . trim ( ) ,  'world' ) 
25110  } ) 
26111  proc . stdout . setEncoding ( 'utf-8' ) 
27112  proc . stderr . setEncoding ( 'utf-8' ) 
0 commit comments