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