python: clean-up detection

Try everything until Python is found.

PR-URL: https://github.com/nodejs/node-gyp/pull/1582
Reviewed-By: Rod Vagg <rod@vagg.org>
This commit is contained in:
João Reis 2018-10-24 05:33:55 +01:00 committed by Rod Vagg
parent 49ab79d221
commit 43031fadcb
No known key found for this signature in database
GPG key ID: C273792F7D83545D
3 changed files with 437 additions and 305 deletions

View file

@ -1,7 +1,6 @@
'use strict'
var test = require('tape')
var path = require('path')
var configure = require('../lib/configure')
var execFile = require('child_process').execFile
var PythonFinder = configure.test.PythonFinder
@ -9,7 +8,7 @@ var PythonFinder = configure.test.PythonFinder
test('find python', function (t) {
t.plan(4)
configure.test.findPython('python', function (err, found) {
configure.test.findPython(null, function (err, found) {
t.strictEqual(err, null)
var proc = execFile(found, ['-V'], function (err, stdout, stderr) {
t.strictEqual(err, null)
@ -23,183 +22,188 @@ test('find python', function (t) {
function poison(object, property) {
function fail() {
throw new Error('Property ' + property + ' should not have been accessed.')
console.error(Error(`Property ${property} should not have been accessed.`))
process.abort()
}
var descriptor = {
configurable: true,
configurable: false,
enumerable: false,
writable: true,
getter: fail,
setter: fail,
get: fail,
set: fail,
}
Object.defineProperty(object, property, descriptor)
}
// Work around a v0.10.x CI issue where path.resolve() on UNIX systems prefixes
// Windows paths with the current working directory. v0.12 and up are free of
// this issue because they use path.win32.resolve() which does the right thing.
var resolve = path.win32 && path.win32.resolve || function() {
function rstrip(s) { return s.replace(/\\+$/, '') }
return [].slice.call(arguments).map(rstrip).join('\\')
}
function TestPythonFinder() { PythonFinder.apply(this, arguments) }
TestPythonFinder.prototype = Object.create(PythonFinder.prototype)
poison(TestPythonFinder.prototype, 'env')
poison(TestPythonFinder.prototype, 'execFile')
poison(TestPythonFinder.prototype, 'resolve')
poison(TestPythonFinder.prototype, 'stat')
poison(TestPythonFinder.prototype, 'which')
poison(TestPythonFinder.prototype, 'win')
// Silence npmlog - remove for debugging
TestPythonFinder.prototype.log = {
silly: () => {},
verbose: () => {},
info: () => {},
warn: () => {},
error: () => {},
}
test('find python - python', function (t) {
t.plan(5)
t.plan(6)
var f = new TestPythonFinder('python', done)
f.which = function(program, cb) {
t.strictEqual(program, 'python')
cb(null, program)
}
f.execFile = function(program, args, opts, cb) {
t.strictEqual(program, 'python')
t.ok(/import sys/.test(args[1]))
cb(null, '2.7.0')
f.execFile = function(program, args, opts, cb) {
poison(f, 'execFile')
t.strictEqual(program, '/path/python')
t.ok(/sys\.version_info/.test(args[1]))
cb(null, '2.7.15')
}
t.strictEqual(program,
process.platform === 'win32' ? '"python"' : 'python')
t.ok(/sys\.executable/.test(args[1]))
cb(null, '/path/python')
}
f.checkPython()
f.findPython()
function done(err, python) {
t.strictEqual(err, null)
t.strictEqual(python, 'python')
t.strictEqual(python, '/path/python')
}
})
test('find python - python too old', function (t) {
t.plan(5)
t.plan(2)
var f = new TestPythonFinder('python', done)
f.which = function(program, cb) {
t.strictEqual(program, 'python')
cb(null, program)
}
var f = new TestPythonFinder(null, done)
f.execFile = function(program, args, opts, cb) {
t.strictEqual(program, 'python')
t.ok(/import sys/.test(args[1]))
cb(null, '2.3.4')
if (/sys\.executable/.test(args[args.length-1])) {
cb(null, '/path/python')
} else if (/sys\.version_info/.test(args[args.length-1])) {
cb(null, '2.3.4')
} else {
t.fail()
}
}
f.checkPython()
f.findPython()
function done(err) {
t.ok(/this version is not supported by GYP/.test(err))
t.ok(/For more information consult the documentation/.test(err))
t.ok(/Could not find any Python/.test(err))
t.ok(/not supported/i.test(f.errorLog))
}
})
test('find python - python too new', function (t) {
t.plan(5)
t.plan(2)
var f = new TestPythonFinder('python', done)
f.which = function(program, cb) {
t.strictEqual(program, 'python')
cb(null, program)
}
var f = new TestPythonFinder(null, done)
f.execFile = function(program, args, opts, cb) {
t.strictEqual(program, 'python')
t.ok(/import sys/.test(args[1]))
cb(null, '3.0.0')
if (/sys\.executable/.test(args[args.length-1])) {
cb(null, '/path/python')
} else if (/sys\.version_info/.test(args[args.length-1])) {
cb(null, '3.0.0')
} else {
t.fail()
}
}
f.checkPython()
f.findPython()
function done(err) {
t.ok(/this version is not supported by GYP/.test(err))
t.ok(/For more information consult the documentation/.test(err))
t.ok(/Could not find any Python/.test(err))
t.ok(/not supported/i.test(f.errorLog))
}
})
test('find python - no python', function (t) {
t.plan(2)
var f = new TestPythonFinder('python', done)
f.which = function(program, cb) {
t.strictEqual(program, 'python')
cb(new Error('not found'))
var f = new TestPythonFinder(null, done)
f.execFile = function(program, args, opts, cb) {
if (/sys\.executable/.test(args[args.length-1])) {
cb(new Error('not found'))
} else if (/sys\.version_info/.test(args[args.length-1])) {
cb(new Error('not a Python executable'))
} else {
t.fail()
}
}
f.checkPython()
f.findPython()
function done(err) {
t.ok(/For more information consult the documentation/.test(err))
t.ok(/Could not find any Python/.test(err))
t.ok(/not in PATH/.test(f.errorLog))
}
})
test('find python - no python2', function (t) {
t.plan(6)
t.plan(2)
var f = new TestPythonFinder('python2', done)
f.which = function(program, cb) {
f.which = function(program, cb) {
t.strictEqual(program, 'python')
cb(null, program)
}
t.strictEqual(program, 'python2')
cb(new Error('not found'))
}
var f = new TestPythonFinder(null, done)
f.execFile = function(program, args, opts, cb) {
t.strictEqual(program, 'python')
t.ok(/import sys/.test(args[1]))
cb(null, '2.7.0')
if (/sys\.executable/.test(args[args.length-1])) {
if (program == 'python2') {
cb(new Error('not found'))
} else {
cb(null, '/path/python')
}
} else if (/sys\.version_info/.test(args[args.length-1])) {
cb(null, '2.7.14')
} else {
t.fail()
}
}
f.checkPython()
f.findPython()
function done(err, python) {
t.strictEqual(err, null)
t.strictEqual(python, 'python')
t.strictEqual(python, '/path/python')
}
})
test('find python - no python2, no python, unix', function (t) {
t.plan(3)
t.plan(2)
var f = new TestPythonFinder('python2', done)
poison(f, 'checkPythonLauncher')
var f = new TestPythonFinder(null, done)
f.checkPyLauncher = t.fail
f.win = false
f.which = function(program, cb) {
f.which = function(program, cb) {
t.strictEqual(program, 'python')
f.execFile = function(program, args, opts, cb) {
if (/sys\.executable/.test(args[args.length-1])) {
cb(new Error('not found'))
} else {
t.fail()
}
t.strictEqual(program, 'python2')
cb(new Error('not found'))
}
f.checkPython()
f.findPython()
function done(err) {
t.ok(/For more information consult the documentation/.test(err))
t.ok(/Could not find any Python/.test(err))
t.ok(/not in PATH/.test(f.errorLog))
}
})
test('find python - no python, use python launcher', function (t) {
t.plan(8)
t.plan(4)
var f = new TestPythonFinder('python', done)
f.env = {}
var f = new TestPythonFinder(null, done)
f.win = true
f.which = function(program, cb) {
t.strictEqual(program, 'python')
cb(new Error('not found'))
}
f.execFile = function(program, args, opts, cb) {
f.execFile = function(program, args, opts, cb) {
t.strictEqual(program, 'Z:\\snake.exe')
t.ok(/import sys/.test(args[1]))
cb(null, '2.7.0')
if (program === 'py.exe') {
t.notEqual(args.indexOf('-2'), -1)
t.notEqual(args.indexOf('-c'), -1)
return cb(null, 'Z:\\snake.exe')
}
if (/sys\.executable/.test(args[args.length-1])) {
cb(new Error('not found'))
} else if (/sys\.version_info/.test(args[args.length-1])) {
if (program === 'Z:\\snake.exe') {
cb(null, '2.7.14')
} else {
t.fail()
}
} else {
t.fail()
}
t.strictEqual(program, 'py.exe')
t.notEqual(args.indexOf('-2'), -1)
t.notEqual(args.indexOf('-c'), -1)
cb(null, 'Z:\\snake.exe')
}
f.checkPython()
f.findPython()
function done(err, python) {
t.strictEqual(err, null)
@ -208,33 +212,34 @@ test('find python - no python, use python launcher', function (t) {
})
test('find python - python 3, use python launcher', function (t) {
t.plan(10)
t.plan(4)
var f = new TestPythonFinder('python', done)
f.env = {}
var f = new TestPythonFinder(null, done)
f.win = true
f.which = function(program, cb) {
t.strictEqual(program, 'python')
cb(null, program)
}
f.execFile = function(program, args, opts, cb) {
f.execFile = function(program, args, opts, cb) {
if (program === 'py.exe') {
f.execFile = function(program, args, opts, cb) {
t.strictEqual(program, 'Z:\\snake.exe')
t.ok(/import sys/.test(args[1]))
cb(null, '2.7.0')
poison(f, 'execFile')
if (/sys\.version_info/.test(args[args.length-1])) {
cb(null, '2.7.14')
} else {
t.fail()
}
}
t.strictEqual(program, 'py.exe')
t.notEqual(args.indexOf('-2'), -1)
t.notEqual(args.indexOf('-c'), -1)
cb(null, 'Z:\\snake.exe')
return cb(null, 'Z:\\snake.exe')
}
if (/sys\.executable/.test(args[args.length-1])) {
cb(null, '/path/python')
} else if (/sys\.version_info/.test(args[args.length-1])) {
cb(null, '3.0.0')
} else {
t.fail()
}
t.strictEqual(program, 'python')
t.ok(/import sys/.test(args[1]))
cb(null, '3.0.0')
}
f.checkPython()
f.findPython()
function done(err, python) {
t.strictEqual(err, null)
@ -244,99 +249,100 @@ test('find python - python 3, use python launcher', function (t) {
test('find python - python 3, use python launcher, python 2 too old',
function (t) {
t.plan(10)
t.plan(6)
var f = new TestPythonFinder('python', done)
f.checkedPythonLauncher = false
f.env = {}
var f = new TestPythonFinder(null, done)
f.win = true
f.which = function(program, cb) {
t.strictEqual(program, 'python')
cb(null, program)
}
f.execFile = function(program, args, opts, cb) {
f.execFile = function(program, args, opts, cb) {
if (program === 'py.exe') {
f.execFile = function(program, args, opts, cb) {
t.strictEqual(program, 'Z:\\snake.exe')
t.ok(/import sys/.test(args[1]))
cb(null, '2.3.4')
if (/sys\.version_info/.test(args[args.length-1])) {
f.execFile = function(program, args, opts, cb) {
if (/sys\.version_info/.test(args[args.length-1])) {
poison(f, 'execFile')
t.strictEqual(program, f.defaultLocation)
cb(new Error('not found'))
} else {
t.fail()
}
}
t.strictEqual(program, 'Z:\\snake.exe')
cb(null, '2.3.4')
} else {
t.fail()
}
}
t.strictEqual(program, 'py.exe')
t.notEqual(args.indexOf('-2'), -1)
t.notEqual(args.indexOf('-c'), -1)
cb(null, 'Z:\\snake.exe')
return cb(null, 'Z:\\snake.exe')
}
if (/sys\.executable/.test(args[args.length-1])) {
cb(null, '/path/python')
} else if (/sys\.version_info/.test(args[args.length-1])) {
cb(null, '3.0.0')
} else {
t.fail()
}
t.strictEqual(program, 'python')
t.ok(/import sys/.test(args[1]))
cb(null, '3.0.0')
}
f.checkPython()
f.findPython()
function done(err) {
t.ok(/this version is not supported by GYP/.test(err))
t.ok(/For more information consult the documentation/.test(err))
t.ok(/Could not find any Python/.test(err))
t.ok(/not supported/i.test(f.errorLog))
}
})
test('find python - no python, no python launcher, good guess', function (t) {
t.plan(6)
t.plan(4)
var re = /C:[\\\/]Python27[\\\/]python[.]exe/
var f = new TestPythonFinder('python', done)
f.env = {}
var f = new TestPythonFinder(null, done)
f.win = true
f.which = function(program, cb) {
t.strictEqual(program, 'python')
cb(new Error('not found'))
}
f.execFile = function(program, args, opts, cb) {
f.execFile = function(program, args, opts, cb) {
t.ok(re.test(program))
t.ok(/import sys/.test(args[1]))
cb(null, '2.7.0')
if (program === 'py.exe') {
f.execFile = function(program, args, opts, cb) {
poison(f, 'execFile')
t.ok(re.test(program))
t.ok(/sys\.version_info/.test(args[args.length-1]))
cb(null, '2.7.14')
}
return cb(new Error('not found'))
}
if (/sys\.executable/.test(args[args.length-1])) {
cb(new Error('not found'))
} else {
t.fail()
}
t.strictEqual(program, 'py.exe')
cb(new Error('not found'))
}
f.resolve = resolve
f.stat = function(path, cb) {
t.ok(re.test(path))
cb(null, {})
}
f.checkPython()
f.findPython()
function done(err, python) {
t.strictEqual(err, null)
t.ok(re.test(python))
}
})
test('find python - no python, no python launcher, bad guess', function (t) {
t.plan(4)
t.plan(2)
var f = new TestPythonFinder('python', done)
f.env = { SystemDrive: 'Z:\\' }
var f = new TestPythonFinder(null, done)
f.win = true
f.which = function(program, cb) {
t.strictEqual(program, 'python')
cb(new Error('not found'))
}
f.execFile = function(program, args, opts, cb) {
t.strictEqual(program, 'py.exe')
cb(new Error('not found'))
if (/sys\.executable/.test(args[args.length-1])) {
cb(new Error('not found'))
} else if (/sys\.version_info/.test(args[args.length-1])) {
cb(new Error('not a Python executable'))
} else {
t.fail()
}
}
f.resolve = resolve
f.stat = function(path, cb) {
t.ok(/Z:[\\\/]Python27[\\\/]python.exe/.test(path))
var err = new Error('not found')
err.code = 'ENOENT'
cb(err)
}
f.checkPython()
f.findPython()
function done(err) {
t.ok(/For more information consult the documentation/.test(err))
t.ok(/Could not find any Python/.test(err))
t.ok(/not in PATH/.test(f.errorLog))
}
})