You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test-addon.js 4.7KB

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