From ddac34899147e159a664a4828bfc2be3a55df8b0 Mon Sep 17 00:00:00 2001 From: Michael Hart Date: Tue, 12 Jul 2016 23:36:54 -0400 Subject: [PATCH] Use push on PYTHONPATH and add tests Using push instead of the original unshift makes the logic a little cleaner to read here. PR-URL: https://github.com/nodejs/node-gyp/pull/990 Reviewed-By: Ben Noordhuis --- lib/configure.js | 8 ++-- test/test-configure-python.js | 72 +++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 3 deletions(-) create mode 100644 test/test-configure-python.js diff --git a/lib/configure.js b/lib/configure.js index c828c0f4e8..b275c23753 100644 --- a/lib/configure.js +++ b/lib/configure.js @@ -287,9 +287,11 @@ function configure (gyp, argv, callback) { argv.unshift(gyp_script) // make sure python uses files that came with this particular node package - var pypath = process.env.PYTHONPATH ? [process.env.PYTHONPATH] : [] - pypath.unshift(path.resolve(__dirname, '..', 'gyp', 'pylib')) - process.env.PYTHONPATH = pypath.join(process.platform === 'win32' ? ';' : ':') + var pypath = [path.join(__dirname, '..', 'gyp', 'pylib')] + if (process.env.PYTHONPATH) { + pypath.push(process.env.PYTHONPATH) + } + process.env.PYTHONPATH = pypath.join(win ? ';' : ':') var cp = gyp.spawn(python, argv) cp.on('exit', onCpExit) diff --git a/test/test-configure-python.js b/test/test-configure-python.js new file mode 100644 index 0000000000..2930cacffc --- /dev/null +++ b/test/test-configure-python.js @@ -0,0 +1,72 @@ +'use strict' + +var test = require('tape') +var path = require('path') +var gyp = require('../lib/node-gyp') +var requireInject = require('require-inject') +var configure = requireInject('../lib/configure', { + 'graceful-fs': { + 'writeFile': function (file, data, cb) { cb() }, + 'stat': function (file, cb) { cb(null, {}) } + } +}) + +var EXPECTED_PYPATH = path.join(__dirname, '..', 'gyp', 'pylib') +var SEPARATOR = process.platform == 'win32' ? ';' : ':' +var SPAWN_RESULT = { on: function () { } } + +test('configure PYTHONPATH with no existing env', function (t) { + t.plan(1) + + delete process.env.PYTHONPATH + + var prog = gyp() + prog.parseArgv([]) + prog.spawn = function () { + t.equal(process.env.PYTHONPATH, EXPECTED_PYPATH) + return SPAWN_RESULT + } + configure(prog, []) +}) + +test('configure PYTHONPATH with existing env of one dir', function (t) { + t.plan(2) + + var existingPath = path.join('a', 'b') + process.env.PYTHONPATH = existingPath + + var prog = gyp() + prog.parseArgv([]) + prog.spawn = function () { + + t.equal(process.env.PYTHONPATH, [EXPECTED_PYPATH, existingPath].join(SEPARATOR)) + + var dirs = process.env.PYTHONPATH.split(SEPARATOR) + t.deepEqual(dirs, [EXPECTED_PYPATH, existingPath]) + + return SPAWN_RESULT + } + configure(prog, []) +}) + +test('configure PYTHONPATH with existing env of multiple dirs', function (t) { + t.plan(2) + + var pythonDir1 = path.join('a', 'b') + var pythonDir2 = path.join('b', 'c') + var existingPath = [pythonDir1, pythonDir2].join(SEPARATOR) + process.env.PYTHONPATH = existingPath + + var prog = gyp() + prog.parseArgv([]) + prog.spawn = function () { + + t.equal(process.env.PYTHONPATH, [EXPECTED_PYPATH, existingPath].join(SEPARATOR)) + + var dirs = process.env.PYTHONPATH.split(SEPARATOR) + t.deepEqual(dirs, [EXPECTED_PYPATH, pythonDir1, pythonDir2]) + + return SPAWN_RESULT + } + configure(prog, []) +})