3 var test = require('tape')
4 var configure = require('../lib/configure')
5 var execFile = require('child_process').execFile
6 var PythonFinder = configure.test.PythonFinder
8 test('find python', function (t) {
11 configure.test.findPython('python', function (err, found) {
12 t.strictEqual(err, null)
13 var proc = execFile(found, ['-V'], function (err, stdout, stderr) {
14 t.strictEqual(err, null)
15 t.strictEqual(stdout, '')
16 t.ok(/Python 2/.test(stderr))
18 proc.stdout.setEncoding('utf-8')
19 proc.stderr.setEncoding('utf-8')
23 function poison(object, property) {
25 throw new Error('Property ' + property + ' should not have been accessed.')
34 Object.defineProperty(object, property, descriptor)
37 function TestPythonFinder() { PythonFinder.apply(this, arguments) }
38 TestPythonFinder.prototype = Object.create(PythonFinder.prototype)
39 poison(TestPythonFinder.prototype, 'env')
40 poison(TestPythonFinder.prototype, 'execFile')
41 poison(TestPythonFinder.prototype, 'stat')
42 poison(TestPythonFinder.prototype, 'which')
43 poison(TestPythonFinder.prototype, 'win')
45 test('find python - python', function (t) {
48 var f = new TestPythonFinder('python', done)
49 f.which = function(program, cb) {
50 t.strictEqual(program, 'python')
53 f.execFile = function(program, args, opts, cb) {
54 t.strictEqual(program, 'python')
55 t.ok(/import platform/.test(args[1]))
60 function done(err, python) {
61 t.strictEqual(err, null)
62 t.strictEqual(python, 'python')
66 test('find python - python too old', function (t) {
69 var f = new TestPythonFinder('python', done)
70 f.which = function(program, cb) {
71 t.strictEqual(program, 'python')
74 f.execFile = function(program, args, opts, cb) {
75 t.strictEqual(program, 'python')
76 t.ok(/import platform/.test(args[1]))
81 function done(err, python) {
82 t.ok(/is not supported by gyp/.test(err))
86 test('find python - python too new', function (t) {
89 var f = new TestPythonFinder('python', done)
90 f.which = function(program, cb) {
91 t.strictEqual(program, 'python')
94 f.execFile = function(program, args, opts, cb) {
95 t.strictEqual(program, 'python')
96 t.ok(/import platform/.test(args[1]))
101 function done(err, python) {
102 t.ok(/is not supported by gyp/.test(err))
106 test('find python - no python', function (t) {
109 var f = new TestPythonFinder('python', done)
110 f.which = function(program, cb) {
111 t.strictEqual(program, 'python')
112 cb(new Error('not found'))
116 function done(err, python) {
117 t.ok(/Can't find Python executable/.test(err))
121 test('find python - no python2', function (t) {
124 var f = new TestPythonFinder('python2', done)
125 f.which = function(program, cb) {
126 f.which = function(program, cb) {
127 t.strictEqual(program, 'python')
130 t.strictEqual(program, 'python2')
131 cb(new Error('not found'))
133 f.execFile = function(program, args, opts, cb) {
134 t.strictEqual(program, 'python')
135 t.ok(/import platform/.test(args[1]))
140 function done(err, python) {
141 t.strictEqual(err, null)
142 t.strictEqual(python, 'python')
146 test('find python - no python2, no python, unix', function (t) {
149 var f = new TestPythonFinder('python2', done)
150 poison(f, 'checkPythonLauncher')
153 f.which = function(program, cb) {
154 f.which = function(program, cb) {
155 t.strictEqual(program, 'python')
156 cb(new Error('not found'))
158 t.strictEqual(program, 'python2')
159 cb(new Error('not found'))
163 function done(err, python) {
164 t.ok(/Can't find Python executable/.test(err))
168 test('find python - no python, use python launcher', function (t) {
171 var f = new TestPythonFinder('python', done)
175 f.which = function(program, cb) {
176 t.strictEqual(program, 'python')
177 cb(new Error('not found'))
179 f.execFile = function(program, args, opts, cb) {
180 f.execFile = function(program, args, opts, cb) {
181 t.strictEqual(program, 'Z:\\snake.exe')
182 t.ok(/import platform/.test(args[1]))
185 t.strictEqual(program, 'py.exe')
186 t.notEqual(args.indexOf('-2'), -1)
187 t.notEqual(args.indexOf('-c'), -1)
188 cb(null, 'Z:\\snake.exe')
192 function done(err, python) {
193 t.strictEqual(err, null)
194 t.strictEqual(python, 'Z:\\snake.exe')
198 test('find python - python 3, use python launcher', function (t) {
201 var f = new TestPythonFinder('python', done)
205 f.which = function(program, cb) {
206 t.strictEqual(program, 'python')
209 f.execFile = function(program, args, opts, cb) {
210 f.execFile = function(program, args, opts, cb) {
211 f.execFile = function(program, args, opts, cb) {
212 t.strictEqual(program, 'Z:\\snake.exe')
213 t.ok(/import platform/.test(args[1]))
216 t.strictEqual(program, 'py.exe')
217 t.notEqual(args.indexOf('-2'), -1)
218 t.notEqual(args.indexOf('-c'), -1)
219 cb(null, 'Z:\\snake.exe')
221 t.strictEqual(program, 'python')
222 t.ok(/import platform/.test(args[1]))
227 function done(err, python) {
228 t.strictEqual(err, null)
229 t.strictEqual(python, 'Z:\\snake.exe')
233 test('find python - python 3, use python launcher, python 2 too old',
237 var f = new TestPythonFinder('python', done)
238 f.checkedPythonLauncher = false
242 f.which = function(program, cb) {
243 t.strictEqual(program, 'python')
246 f.execFile = function(program, args, opts, cb) {
247 f.execFile = function(program, args, opts, cb) {
248 f.execFile = function(program, args, opts, cb) {
249 t.strictEqual(program, 'Z:\\snake.exe')
250 t.ok(/import platform/.test(args[1]))
253 t.strictEqual(program, 'py.exe')
254 t.notEqual(args.indexOf('-2'), -1)
255 t.notEqual(args.indexOf('-c'), -1)
256 cb(null, 'Z:\\snake.exe')
258 t.strictEqual(program, 'python')
259 t.ok(/import platform/.test(args[1]))
264 function done(err, python) {
265 t.ok(/is not supported by gyp/.test(err))
269 test('find python - no python, no python launcher, good guess', function (t) {
272 var re = /C:[\\\/]Python27[\\\/]python[.]exe/
273 var f = new TestPythonFinder('python', done)
277 f.which = function(program, cb) {
278 t.strictEqual(program, 'python')
279 cb(new Error('not found'))
281 f.execFile = function(program, args, opts, cb) {
282 f.execFile = function(program, args, opts, cb) {
283 t.ok(re.test(program))
284 t.ok(/import platform/.test(args[1]))
287 t.strictEqual(program, 'py.exe')
288 cb(new Error('not found'))
290 f.stat = function(path, cb) {
296 function done(err, python) {
297 t.ok(re.test(python))
301 test('find python - no python, no python launcher, bad guess', function (t) {
304 var f = new TestPythonFinder('python', done)
305 f.env = { SystemDrive: 'Z:\\' }
308 f.which = function(program, cb) {
309 t.strictEqual(program, 'python')
310 cb(new Error('not found'))
312 f.execFile = function(program, args, opts, cb) {
313 t.strictEqual(program, 'py.exe')
314 cb(new Error('not found'))
316 f.stat = function(path, cb) {
317 t.ok(/Z:[\\\/]Python27[\\\/]python.exe/.test(path))
318 var err = new Error('not found')
324 function done(err, python) {
325 t.ok(/Can't find Python executable/.test(err))