test-addon.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. 'use strict'
  2. const test = require('tap').test
  3. const path = require('path')
  4. const fs = require('graceful-fs')
  5. const childProcess = require('child_process')
  6. const os = require('os')
  7. const addonPath = path.resolve(__dirname, 'node_modules', 'hello_world')
  8. const nodeGyp = path.resolve(__dirname, '..', 'bin', 'node-gyp.js')
  9. const execFileSync = childProcess.execFileSync || require('./process-exec-sync')
  10. const execFile = childProcess.execFile
  11. function runHello (hostProcess) {
  12. if (!hostProcess) {
  13. hostProcess = process.execPath
  14. }
  15. var testCode = "console.log(require('hello_world').hello())"
  16. return execFileSync(hostProcess, ['-e', testCode], { cwd: __dirname }).toString()
  17. }
  18. function getEncoding () {
  19. var code = 'import locale;print(locale.getdefaultlocale()[1])'
  20. return execFileSync('python', ['-c', code]).toString().trim()
  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. }
  33. test('build simple addon', function (t) {
  34. t.plan(3)
  35. // Set the loglevel otherwise the output disappears when run via 'npm test'
  36. var cmd = [nodeGyp, 'rebuild', '-C', addonPath, '--loglevel=verbose']
  37. var proc = execFile(process.execPath, cmd, function (err, stdout, stderr) {
  38. var logLines = stderr.toString().trim().split(/\r?\n/)
  39. var lastLine = logLines[logLines.length - 1]
  40. t.strictEqual(err, null)
  41. t.strictEqual(lastLine, 'gyp info ok', 'should end in ok')
  42. t.strictEqual(runHello().trim(), 'world')
  43. })
  44. proc.stdout.setEncoding('utf-8')
  45. proc.stderr.setEncoding('utf-8')
  46. })
  47. test('build simple addon in path with non-ascii characters', function (t) {
  48. t.plan(1)
  49. if (!checkCharmapValid()) {
  50. return t.skip('python console app can\'t encode non-ascii character.')
  51. }
  52. var testDirNames = {
  53. cp936: '文件夹',
  54. cp1252: 'Latīna',
  55. cp932: 'フォルダ'
  56. }
  57. // Select non-ascii characters by current encoding
  58. var testDirName = testDirNames[getEncoding()]
  59. // If encoding is UTF-8 or other then no need to test
  60. if (!testDirName) {
  61. return t.skip('no need to test')
  62. }
  63. t.plan(3)
  64. var data
  65. var configPath = path.join(addonPath, 'build', 'config.gypi')
  66. try {
  67. data = fs.readFileSync(configPath, 'utf8')
  68. } catch (err) {
  69. t.error(err)
  70. return
  71. }
  72. var config = JSON.parse(data.replace(/#.+\n/, ''))
  73. var nodeDir = config.variables.nodedir
  74. var testNodeDir = path.join(addonPath, testDirName)
  75. // Create symbol link to path with non-ascii characters
  76. try {
  77. fs.symlinkSync(nodeDir, testNodeDir, 'dir')
  78. } catch (err) {
  79. switch (err.code) {
  80. case 'EEXIST': break
  81. case 'EPERM':
  82. t.error(err, 'Please try to running console as an administrator')
  83. return
  84. default:
  85. t.error(err)
  86. return
  87. }
  88. }
  89. var cmd = [
  90. nodeGyp,
  91. 'rebuild',
  92. '-C',
  93. addonPath,
  94. '--loglevel=verbose',
  95. '-nodedir=' + testNodeDir
  96. ]
  97. var proc = execFile(process.execPath, cmd, function (err, stdout, stderr) {
  98. try {
  99. fs.unlink(testNodeDir)
  100. } catch (err) {
  101. t.error(err)
  102. }
  103. var logLines = stderr.toString().trim().split(/\r?\n/)
  104. var lastLine = logLines[logLines.length - 1]
  105. t.strictEqual(err, null)
  106. t.strictEqual(lastLine, 'gyp info ok', 'should end in ok')
  107. t.strictEqual(runHello().trim(), 'world')
  108. })
  109. proc.stdout.setEncoding('utf-8')
  110. proc.stderr.setEncoding('utf-8')
  111. })
  112. test('addon works with renamed host executable', function (t) {
  113. // No `fs.copyFileSync` before node8.
  114. if (process.version.substr(1).split('.')[0] < 8) {
  115. t.skip('skipping test for old node version')
  116. t.end()
  117. return
  118. }
  119. t.plan(3)
  120. var notNodePath = path.join(os.tmpdir(), 'notnode' + path.extname(process.execPath))
  121. fs.copyFileSync(process.execPath, notNodePath)
  122. var cmd = [nodeGyp, 'rebuild', '-C', addonPath, '--loglevel=verbose']
  123. var proc = execFile(process.execPath, cmd, function (err, stdout, stderr) {
  124. var logLines = stderr.toString().trim().split(/\r?\n/)
  125. var lastLine = logLines[logLines.length - 1]
  126. t.strictEqual(err, null)
  127. t.strictEqual(lastLine, 'gyp info ok', 'should end in ok')
  128. t.strictEqual(runHello(notNodePath).trim(), 'world')
  129. fs.unlinkSync(notNodePath)
  130. })
  131. proc.stdout.setEncoding('utf-8')
  132. proc.stderr.setEncoding('utf-8')
  133. })