Initial commit
[yaffs-website] / node_modules / node-gyp / test / test-find-python.js
1 'use strict'
2
3 var test = require('tape')
4 var configure = require('../lib/configure')
5 var execFile = require('child_process').execFile
6 var PythonFinder = configure.test.PythonFinder
7
8 test('find python', function (t) {
9   t.plan(4)
10
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))
17     })
18     proc.stdout.setEncoding('utf-8')
19     proc.stderr.setEncoding('utf-8')
20   })
21 })
22
23 function poison(object, property) {
24   function fail() {
25     throw new Error('Property ' + property + ' should not have been accessed.')
26   }
27   var descriptor = {
28     configurable: true,
29     enumerable: false,
30     writable: true,
31     getter: fail,
32     setter: fail,
33   }
34   Object.defineProperty(object, property, descriptor)
35 }
36
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')
44
45 test('find python - python', function (t) {
46   t.plan(5)
47
48   var f = new TestPythonFinder('python', done)
49   f.which = function(program, cb) {
50     t.strictEqual(program, 'python')
51     cb(null, program)
52   }
53   f.execFile = function(program, args, opts, cb) {
54     t.strictEqual(program, 'python')
55     t.ok(/import platform/.test(args[1]))
56     cb(null, '2.7.0')
57   }
58   f.checkPython()
59
60   function done(err, python) {
61     t.strictEqual(err, null)
62     t.strictEqual(python, 'python')
63   }
64 })
65
66 test('find python - python too old', function (t) {
67   t.plan(4)
68
69   var f = new TestPythonFinder('python', done)
70   f.which = function(program, cb) {
71     t.strictEqual(program, 'python')
72     cb(null, program)
73   }
74   f.execFile = function(program, args, opts, cb) {
75     t.strictEqual(program, 'python')
76     t.ok(/import platform/.test(args[1]))
77     cb(null, '2.3.4')
78   }
79   f.checkPython()
80
81   function done(err, python) {
82     t.ok(/is not supported by gyp/.test(err))
83   }
84 })
85
86 test('find python - python too new', function (t) {
87   t.plan(4)
88
89   var f = new TestPythonFinder('python', done)
90   f.which = function(program, cb) {
91     t.strictEqual(program, 'python')
92     cb(null, program)
93   }
94   f.execFile = function(program, args, opts, cb) {
95     t.strictEqual(program, 'python')
96     t.ok(/import platform/.test(args[1]))
97     cb(null, '3.0.0')
98   }
99   f.checkPython()
100
101   function done(err, python) {
102     t.ok(/is not supported by gyp/.test(err))
103   }
104 })
105
106 test('find python - no python', function (t) {
107   t.plan(2)
108
109   var f = new TestPythonFinder('python', done)
110   f.which = function(program, cb) {
111     t.strictEqual(program, 'python')
112     cb(new Error('not found'))
113   }
114   f.checkPython()
115
116   function done(err, python) {
117     t.ok(/Can't find Python executable/.test(err))
118   }
119 })
120
121 test('find python - no python2', function (t) {
122   t.plan(6)
123
124   var f = new TestPythonFinder('python2', done)
125   f.which = function(program, cb) {
126     f.which = function(program, cb) {
127       t.strictEqual(program, 'python')
128       cb(null, program)
129     }
130     t.strictEqual(program, 'python2')
131     cb(new Error('not found'))
132   }
133   f.execFile = function(program, args, opts, cb) {
134     t.strictEqual(program, 'python')
135     t.ok(/import platform/.test(args[1]))
136     cb(null, '2.7.0')
137   }
138   f.checkPython()
139
140   function done(err, python) {
141     t.strictEqual(err, null)
142     t.strictEqual(python, 'python')
143   }
144 })
145
146 test('find python - no python2, no python, unix', function (t) {
147   t.plan(3)
148
149   var f = new TestPythonFinder('python2', done)
150   poison(f, 'checkPythonLauncher')
151   f.win = false
152
153   f.which = function(program, cb) {
154     f.which = function(program, cb) {
155       t.strictEqual(program, 'python')
156       cb(new Error('not found'))
157     }
158     t.strictEqual(program, 'python2')
159     cb(new Error('not found'))
160   }
161   f.checkPython()
162
163   function done(err, python) {
164     t.ok(/Can't find Python executable/.test(err))
165   }
166 })
167
168 test('find python - no python, use python launcher', function (t) {
169   t.plan(8)
170
171   var f = new TestPythonFinder('python', done)
172   f.env = {}
173   f.win = true
174
175   f.which = function(program, cb) {
176     t.strictEqual(program, 'python')
177     cb(new Error('not found'))
178   }
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]))
183       cb(null, '2.7.0')
184     }
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')
189   }
190   f.checkPython()
191
192   function done(err, python) {
193     t.strictEqual(err, null)
194     t.strictEqual(python, 'Z:\\snake.exe')
195   }
196 })
197
198 test('find python - python 3, use python launcher', function (t) {
199   t.plan(10)
200
201   var f = new TestPythonFinder('python', done)
202   f.env = {}
203   f.win = true
204
205   f.which = function(program, cb) {
206     t.strictEqual(program, 'python')
207     cb(null, program)
208   }
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]))
214         cb(null, '2.7.0')
215       }
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')
220     }
221     t.strictEqual(program, 'python')
222     t.ok(/import platform/.test(args[1]))
223     cb(null, '3.0.0')
224   }
225   f.checkPython()
226
227   function done(err, python) {
228     t.strictEqual(err, null)
229     t.strictEqual(python, 'Z:\\snake.exe')
230   }
231 })
232
233 test('find python - python 3, use python launcher, python 2 too old',
234      function (t) {
235   t.plan(9)
236
237   var f = new TestPythonFinder('python', done)
238   f.checkedPythonLauncher = false
239   f.env = {}
240   f.win = true
241
242   f.which = function(program, cb) {
243     t.strictEqual(program, 'python')
244     cb(null, program)
245   }
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]))
251         cb(null, '2.3.4')
252       }
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')
257     }
258     t.strictEqual(program, 'python')
259     t.ok(/import platform/.test(args[1]))
260     cb(null, '3.0.0')
261   }
262   f.checkPython()
263
264   function done(err, python) {
265     t.ok(/is not supported by gyp/.test(err))
266   }
267 })
268
269 test('find python - no python, no python launcher, good guess', function (t) {
270   t.plan(6)
271
272   var re = /C:[\\\/]Python27[\\\/]python[.]exe/
273   var f = new TestPythonFinder('python', done)
274   f.env = {}
275   f.win = true
276
277   f.which = function(program, cb) {
278     t.strictEqual(program, 'python')
279     cb(new Error('not found'))
280   }
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]))
285       cb(null, '2.7.0')
286     }
287     t.strictEqual(program, 'py.exe')
288     cb(new Error('not found'))
289   }
290   f.stat = function(path, cb) {
291     t.ok(re.test(path))
292     cb(null, {})
293   }
294   f.checkPython()
295
296   function done(err, python) {
297     t.ok(re.test(python))
298   }
299 })
300
301 test('find python - no python, no python launcher, bad guess', function (t) {
302   t.plan(4)
303
304   var f = new TestPythonFinder('python', done)
305   f.env = { SystemDrive: 'Z:\\' }
306   f.win = true
307
308   f.which = function(program, cb) {
309     t.strictEqual(program, 'python')
310     cb(new Error('not found'))
311   }
312   f.execFile = function(program, args, opts, cb) {
313     t.strictEqual(program, 'py.exe')
314     cb(new Error('not found'))
315   }
316   f.stat = function(path, cb) {
317     t.ok(/Z:[\\\/]Python27[\\\/]python.exe/.test(path))
318     var err = new Error('not found')
319     err.code = 'ENOENT'
320     cb(err)
321   }
322   f.checkPython()
323
324   function done(err, python) {
325     t.ok(/Can't find Python executable/.test(err))
326   }
327 })