test-find-python.js
5.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
'use strict'
delete process.env.PYTHON
const test = require('tap').test
const findPython = require('../lib/find-python')
const execFile = require('child_process').execFile
const PythonFinder = findPython.test.PythonFinder
require('npmlog').level = 'warn'
test('find python', function (t) {
t.plan(4)
findPython.test.findPython(null, function (err, found) {
t.strictEqual(err, null)
var proc = execFile(found, ['-V'], function (err, stdout, stderr) {
t.strictEqual(err, null)
t.ok(/Python 3/.test(stdout))
t.strictEqual(stderr, '')
})
proc.stdout.setEncoding('utf-8')
proc.stderr.setEncoding('utf-8')
})
})
function poison (object, property) {
function fail () {
console.error(Error(`Property ${property} should not have been accessed.`))
process.abort()
}
var descriptor = {
configurable: false,
enumerable: false,
get: fail,
set: fail
}
Object.defineProperty(object, property, descriptor)
}
function TestPythonFinder () {
PythonFinder.apply(this, arguments)
}
TestPythonFinder.prototype = Object.create(PythonFinder.prototype)
// Silence npmlog - remove for debugging
TestPythonFinder.prototype.log = {
silly: () => {},
verbose: () => {},
info: () => {},
warn: () => {},
error: () => {}
}
delete TestPythonFinder.prototype.env.NODE_GYP_FORCE_PYTHON
test('find python - python', function (t) {
t.plan(6)
var f = new TestPythonFinder('python', done)
f.execFile = function (program, args, opts, cb) {
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, '3.9.1')
}
t.strictEqual(program,
process.platform === 'win32' ? '"python"' : 'python')
t.ok(/sys\.executable/.test(args[1]))
cb(null, '/path/python')
}
f.findPython()
function done (err, python) {
t.strictEqual(err, null)
t.strictEqual(python, '/path/python')
}
})
test('find python - python too old', function (t) {
t.plan(2)
var f = new TestPythonFinder(null, done)
f.execFile = function (program, args, opts, cb) {
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.findPython()
function done (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(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.findPython()
function done (err) {
t.ok(/Could not find any Python/.test(err))
t.ok(/not in PATH/.test(f.errorLog))
}
})
test('find python - no python2, no python, unix', function (t) {
t.plan(2)
var f = new TestPythonFinder(null, done)
f.checkPyLauncher = t.fail
f.win = false
f.execFile = function (program, args, opts, cb) {
if (/sys\.executable/.test(args[args.length - 1])) {
cb(new Error('not found'))
} else {
t.fail()
}
}
f.findPython()
function done (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(4)
var f = new TestPythonFinder(null, done)
f.win = true
f.execFile = function (program, args, opts, cb) {
if (program === 'py.exe') {
t.notEqual(args.indexOf('-3'), -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 (f.winDefaultLocations.includes(program)) {
cb(new Error('not found'))
} else if (/sys\.version_info/.test(args[args.length - 1])) {
if (program === 'Z:\\snake.exe') {
cb(null, '3.9.0')
} else {
t.fail()
}
} else {
t.fail()
}
}
f.findPython()
function done (err, python) {
t.strictEqual(err, null)
t.strictEqual(python, 'Z:\\snake.exe')
}
})
test('find python - no python, no python launcher, good guess', function (t) {
t.plan(2)
var f = new TestPythonFinder(null, done)
f.win = true
const expectedProgram = f.winDefaultLocations[0]
f.execFile = function (program, args, opts, cb) {
if (program === 'py.exe') {
return cb(new Error('not found'))
}
if (/sys\.executable/.test(args[args.length - 1])) {
cb(new Error('not found'))
} else if (program === expectedProgram &&
/sys\.version_info/.test(args[args.length - 1])) {
cb(null, '3.7.3')
} else {
t.fail()
}
}
f.findPython()
function done (err, python) {
t.strictEqual(err, null)
t.ok(python === expectedProgram)
}
})
test('find python - no python, no python launcher, bad guess', function (t) {
t.plan(2)
var f = new TestPythonFinder(null, done)
f.win = true
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.findPython()
function done (err) {
t.ok(/Could not find any Python/.test(err))
t.ok(/not in PATH/.test(f.errorLog))
}
})