test-addon.js 3.4 KB

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