|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const { describe, it } = require('mocha') |
| 4 | +const assert = require('assert') |
| 5 | +const path = require('path') |
| 6 | +const os = require('os') |
| 7 | +const gyp = require('../lib/node-gyp') |
| 8 | +const requireInject = require('require-inject') |
| 9 | +const semver = require('semver') |
| 10 | + |
| 11 | +const versionSemver = semver.parse(process.version) |
| 12 | + |
| 13 | +const configure = requireInject('../lib/configure', { |
| 14 | + 'graceful-fs': { |
| 15 | + openSync: () => 0, |
| 16 | + closeSync: () => {}, |
| 17 | + existsSync: () => true, |
| 18 | + readFileSync: () => '#define NODE_MAJOR_VERSION ' + versionSemver.major + '\n' + |
| 19 | + '#define NODE_MINOR_VERSION ' + versionSemver.minor + '\n' + |
| 20 | + '#define NODE_PATCH_VERSION ' + versionSemver.patch + '\n', |
| 21 | + promises: { |
| 22 | + stat: async () => ({}), |
| 23 | + mkdir: async () => {}, |
| 24 | + writeFile: async () => {} |
| 25 | + } |
| 26 | + } |
| 27 | +}) |
| 28 | + |
| 29 | +const configure2 = requireInject('../lib/configure', { |
| 30 | + 'graceful-fs': { |
| 31 | + openSync: () => 0, |
| 32 | + closeSync: () => {}, |
| 33 | + existsSync: () => true, |
| 34 | + readFileSync: () => '#define NODE_MAJOR_VERSION 8\n' + |
| 35 | + '#define NODE_MINOR_VERSION 0\n' + |
| 36 | + '#define NODE_PATCH_VERSION 0\n', |
| 37 | + promises: { |
| 38 | + stat: async () => ({}), |
| 39 | + mkdir: async () => {}, |
| 40 | + writeFile: async () => {} |
| 41 | + } |
| 42 | + } |
| 43 | +}) |
| 44 | + |
| 45 | +const SPAWN_RESULT = cb => ({ on: function () { cb() } }) |
| 46 | + |
| 47 | +const driveLetter = os.platform() === 'win32' ? `${process.cwd().split(path.sep)[0]}` : '' |
| 48 | +function checkTargetPath (target, value) { |
| 49 | + let targetPath = path.join(path.sep, target, 'include', |
| 50 | + 'node', 'common.gypi') |
| 51 | + if (process.platform === 'win32') { |
| 52 | + targetPath = driveLetter + targetPath |
| 53 | + } |
| 54 | + |
| 55 | + return targetPath.localeCompare(value) === 0 |
| 56 | +} |
| 57 | + |
| 58 | +describe('configure-nodedir', function () { |
| 59 | + it('configure nodedir with node-gyp command line', function (done) { |
| 60 | + const prog = gyp() |
| 61 | + prog.parseArgv(['dummy_prog', 'dummy_script', '--nodedir=' + path.sep + 'usr']) |
| 62 | + |
| 63 | + prog.spawn = function (program, args) { |
| 64 | + for (let i = 0; i < args.length; i++) { |
| 65 | + if (checkTargetPath('usr', args[i])) { |
| 66 | + return SPAWN_RESULT(done) |
| 67 | + } |
| 68 | + }; |
| 69 | + assert.fail() |
| 70 | + } |
| 71 | + configure(prog, [], assert.fail) |
| 72 | + }) |
| 73 | + |
| 74 | + if (process.config.variables.use_prefix_to_find_headers) { |
| 75 | + it('use-prefix-to-find-headers build time option - match', function (done) { |
| 76 | + const prog = gyp() |
| 77 | + prog.parseArgv(['dummy_prog', 'dummy_script']) |
| 78 | + |
| 79 | + prog.spawn = function (program, args) { |
| 80 | + for (let i = 0; i < args.length; i++) { |
| 81 | + const nodedir = process.config.variables.node_prefix |
| 82 | + if (checkTargetPath(nodedir, args[i])) { |
| 83 | + return SPAWN_RESULT(done) |
| 84 | + } |
| 85 | + }; |
| 86 | + assert.fail() |
| 87 | + } |
| 88 | + configure(prog, [], assert.fail) |
| 89 | + }) |
| 90 | + |
| 91 | + it('use-prefix-to-find-headers build time option - no match', function (done) { |
| 92 | + const prog = gyp() |
| 93 | + prog.parseArgv(['dummy_prog', 'dummy_script']) |
| 94 | + |
| 95 | + prog.spawn = function (program, args) { |
| 96 | + for (let i = 0; i < args.length; i++) { |
| 97 | + const nodedir = process.config.variables.node_prefix |
| 98 | + if (checkTargetPath(nodedir, args[i])) { |
| 99 | + assert.fail() |
| 100 | + } |
| 101 | + }; |
| 102 | + return SPAWN_RESULT(done) |
| 103 | + } |
| 104 | + configure2(prog, [], assert.fail) |
| 105 | + }) |
| 106 | + |
| 107 | + it('use-prefix-to-find-headers build time option, target specified', function (done) { |
| 108 | + const prog = gyp() |
| 109 | + prog.parseArgv(['dummy_prog', 'dummy_script', '--target=8.0.0']) |
| 110 | + |
| 111 | + prog.spawn = function (program, args) { |
| 112 | + for (let i = 0; i < args.length; i++) { |
| 113 | + const nodedir = process.config.variables.node_prefix |
| 114 | + if (checkTargetPath(nodedir, args[i])) { |
| 115 | + assert.fail() |
| 116 | + } |
| 117 | + }; |
| 118 | + return SPAWN_RESULT(done) |
| 119 | + } |
| 120 | + configure(prog, [], assert.fail) |
| 121 | + }) |
| 122 | + } |
| 123 | +}) |
0 commit comments