feat: convert all internal functions to async/await

BREAKING CHANGE: All internal functions have been coverted to return
promises and no longer accept callbacks. This is not a breaking change
for users but may be breaking to consumers of `node-gyp` if you are
requiring internal functions directly.
This commit is contained in:
Luke Karrys 2023-10-27 15:52:13 -07:00
parent 1b3bd341b4
commit 355622f4aa
20 changed files with 768 additions and 1062 deletions

View file

@ -4,22 +4,16 @@ delete process.env.PYTHON
const { describe, it } = require('mocha')
const assert = require('assert')
const findPython = require('../lib/find-python')
const execFile = require('child_process').execFile
const PythonFinder = findPython.test.PythonFinder
const { test: { PythonFinder, findPython: testFindPython } } = require('../lib/find-python')
const { execFile } = require('../lib/util')
describe('find-python', function () {
it('find python', function () {
findPython.test.findPython(null, function (err, found) {
assert.strictEqual(err, null)
const proc = execFile(found, ['-V'], function (err, stdout, stderr) {
assert.strictEqual(err, null)
assert.ok(/Python 3/.test(stdout))
assert.strictEqual(stderr, '')
})
proc.stdout.setEncoding('utf-8')
proc.stderr.setEncoding('utf-8')
})
it('find python', async function () {
const found = await testFindPython(null)
const [err, stdout, stderr] = await execFile(found, ['-V'], { encoding: 'utf-8' })
assert.strictEqual(err, null)
assert.ok(/Python 3/.test(stdout))
assert.strictEqual(stderr, '')
})
function poison (object, property) {
@ -36,109 +30,105 @@ describe('find-python', function () {
Object.defineProperty(object, property, descriptor)
}
function TestPythonFinder () {
PythonFinder.apply(this, arguments)
}
function TestPythonFinder () { PythonFinder.apply(this, arguments) }
TestPythonFinder.prototype = Object.create(PythonFinder.prototype)
delete TestPythonFinder.prototype.env.NODE_GYP_FORCE_PYTHON
const findPython = async (f) => {
try {
return { err: null, python: await f.findPython() }
} catch (err) {
return { err, python: null }
}
}
it('find python - python', function () {
const f = new TestPythonFinder('python', done)
f.execFile = function (program, args, opts, cb) {
f.execFile = function (program, args, opts, cb) {
it('find python - python', async function () {
const f = new TestPythonFinder('python')
f.execFile = async function (program, args, opts) {
f.execFile = async function (program, args, opts) {
poison(f, 'execFile')
assert.strictEqual(program, '/path/python')
assert.ok(/sys\.version_info/.test(args[1]))
cb(null, '3.9.1')
return [null, '3.9.1']
}
assert.strictEqual(program,
process.platform === 'win32' ? '"python"' : 'python')
assert.strictEqual(program, process.platform === 'win32' ? '"python"' : 'python')
assert.ok(/sys\.executable/.test(args[1]))
cb(null, '/path/python')
return [null, '/path/python']
}
f.findPython()
function done (err, python) {
assert.strictEqual(err, null)
assert.strictEqual(python, '/path/python')
}
const { err, python } = await findPython(f)
assert.strictEqual(err, null)
assert.strictEqual(python, '/path/python')
})
it('find python - python too old', function () {
const f = new TestPythonFinder(null, done)
f.execFile = function (program, args, opts, cb) {
it('find python - python too old', async function () {
const f = new TestPythonFinder(null)
f.execFile = async function (program, args, opts) {
if (/sys\.executable/.test(args[args.length - 1])) {
cb(null, '/path/python')
return [null, '/path/python']
} else if (/sys\.version_info/.test(args[args.length - 1])) {
cb(null, '2.3.4')
return [null, '2.3.4']
} else {
assert.fail()
}
}
f.findPython()
function done (err) {
assert.ok(/Could not find any Python/.test(err))
assert.ok(/not supported/i.test(f.errorLog))
}
const { err } = await findPython(f)
assert.ok(/Could not find any Python/.test(err))
assert.ok(/not supported/i.test(f.errorLog))
})
it('find python - no python', function () {
const f = new TestPythonFinder(null, done)
f.execFile = function (program, args, opts, cb) {
it('find python - no python', async function () {
const f = new TestPythonFinder(null)
f.execFile = async function (program, args, opts) {
if (/sys\.executable/.test(args[args.length - 1])) {
cb(new Error('not found'))
throw new Error('not found')
} else if (/sys\.version_info/.test(args[args.length - 1])) {
cb(new Error('not a Python executable'))
throw new Error('not a Python executable')
} else {
assert.fail()
}
}
f.findPython()
function done (err) {
assert.ok(/Could not find any Python/.test(err))
assert.ok(/not in PATH/.test(f.errorLog))
}
const { err } = await findPython(f)
assert.ok(/Could not find any Python/.test(err))
assert.ok(/not in PATH/.test(f.errorLog))
})
it('find python - no python2, no python, unix', function () {
const f = new TestPythonFinder(null, done)
it('find python - no python2, no python, unix', async function () {
const f = new TestPythonFinder(null)
f.checkPyLauncher = assert.fail
f.win = false
f.execFile = function (program, args, opts, cb) {
f.execFile = async function (program, args, opts) {
if (/sys\.executable/.test(args[args.length - 1])) {
cb(new Error('not found'))
throw new Error('not found')
} else {
assert.fail()
}
}
f.findPython()
function done (err) {
assert.ok(/Could not find any Python/.test(err))
assert.ok(/not in PATH/.test(f.errorLog))
}
const { err } = await findPython(f)
assert.ok(/Could not find any Python/.test(err))
assert.ok(/not in PATH/.test(f.errorLog))
})
it('find python - no python, use python launcher', function () {
const f = new TestPythonFinder(null, done)
it('find python - no python, use python launcher', async function () {
const f = new TestPythonFinder(null)
f.win = true
f.execFile = function (program, args, opts, cb) {
f.execFile = async function (program, args, opts) {
if (program === 'py.exe') {
assert.notStrictEqual(args.indexOf('-3'), -1)
assert.notStrictEqual(args.indexOf('-c'), -1)
return cb(null, 'Z:\\snake.exe')
return [null, 'Z:\\snake.exe']
}
if (/sys\.executable/.test(args[args.length - 1])) {
cb(new Error('not found'))
throw new Error('not found')
} else if (f.winDefaultLocations.includes(program)) {
cb(new Error('not found'))
throw new Error('not found')
} else if (/sys\.version_info/.test(args[args.length - 1])) {
if (program === 'Z:\\snake.exe') {
cb(null, '3.9.0')
return [null, '3.9.0']
} else {
assert.fail()
}
@ -146,58 +136,49 @@ describe('find-python', function () {
assert.fail()
}
}
f.findPython()
function done (err, python) {
assert.strictEqual(err, null)
assert.strictEqual(python, 'Z:\\snake.exe')
}
const { err, python } = await findPython(f)
assert.strictEqual(err, null)
assert.strictEqual(python, 'Z:\\snake.exe')
})
it('find python - no python, no python launcher, good guess', function () {
const f = new TestPythonFinder(null, done)
it('find python - no python, no python launcher, good guess', async function () {
const f = new TestPythonFinder(null)
f.win = true
const expectedProgram = f.winDefaultLocations[0]
f.execFile = function (program, args, opts, cb) {
f.execFile = async function (program, args, opts) {
if (program === 'py.exe') {
return cb(new Error('not found'))
throw new Error('not found')
}
if (/sys\.executable/.test(args[args.length - 1])) {
cb(new Error('not found'))
throw new Error('not found')
} else if (program === expectedProgram &&
/sys\.version_info/.test(args[args.length - 1])) {
cb(null, '3.7.3')
return [null, '3.7.3']
} else {
assert.fail()
}
}
f.findPython()
function done (err, python) {
assert.strictEqual(err, null)
assert.ok(python === expectedProgram)
}
const { err, python } = await findPython(f)
assert.strictEqual(err, null)
assert.ok(python === expectedProgram)
})
it('find python - no python, no python launcher, bad guess', function () {
const f = new TestPythonFinder(null, done)
it('find python - no python, no python launcher, bad guess', async function () {
const f = new TestPythonFinder(null)
f.win = true
f.execFile = function (program, args, opts, cb) {
f.execFile = async function (program, args, opts) {
if (/sys\.executable/.test(args[args.length - 1])) {
cb(new Error('not found'))
throw new Error('not found')
} else if (/sys\.version_info/.test(args[args.length - 1])) {
cb(new Error('not a Python executable'))
throw new Error('not a Python executable')
} else {
assert.fail()
}
}
f.findPython()
function done (err) {
assert.ok(/Could not find any Python/.test(err))
assert.ok(/not in PATH/.test(f.errorLog))
}
const { err } = await findPython(f)
assert.ok(/Could not find any Python/.test(err))
assert.ok(/not in PATH/.test(f.errorLog))
})
})