-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
433 lines (376 loc) · 17.9 KB
/
test.py
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
# -*- coding: utf-8 -*-
# This is the Winpackit test suite.
# Be aware that running the suite will produce a lot of output.
# We recommend redirecting both stdout and stderr, eg:
# $ python test.py > output.txt 2>&1
import unittest
from unittest import mock
import os, sys, shutil
from pathlib import Path
from pprint import pprint
from winpackit import *
class _Cfg:
def __init__(self):
self.HERE = Path(__file__).resolve().parent
self.PROJECTS = []
self.PYTHON_VERSION = '3'
self.DELAYED_INSTALL = False
self.PIP_REQUIRED = False
self.DEPENDENCIES = []
self.REQUIREMENTS = ''
self.PIP_CACHE = True
self.PIP_ARGS = []
self.PIP_INSTALL_ARGS = []
self.PROJECT_FILES_IGNORE_PATTERNS = []
self.COMPILE = False
self.PYC_ONLY_DISTRIBUTION = False
self.COPY_DIRS = []
self.USE_CACHE = True
self.VERBOSE = 2
self.WELCOME_MESSAGE = 'starting...'
self.GOODBYE_MESSAGE = "done, press enter to quit"
self.custom_action = lambda i: True
class BasicTestCase(unittest.TestCase):
def setUp(self):
self.cfg = _Cfg()
self.basedir = self.cfg.HERE / 'testoutput'
self.basedir.mkdir(exist_ok=True)
self.cfg.VERBOSE = 0
self.cfg.PROJECTS = []
self.packit = Packit(settings=self.cfg)
self.packit.cache_dir = self.basedir / 'test_cachedir'
self.packit.build_dir = self.basedir / 'BasicTestCase_build'
self.packit.prepare_dirs()
def tearDown(self):
shutil.rmtree(self.packit.build_dir)
shutil.rmtree(self.packit.cache_dir)
self.packit = None
del self.cfg
def test_parse_pyversion(self):
intro = f'\n#####\n##### RUNNING TEST parse_pyversion ...\n#####\n'
self.packit.msg(0, intro)
self.cfg.PYTHON_VERSION = '3.6.6-64'
self.assertEqual(self.packit.parse_pyversion(), (3, 6, 6, 64))
self.cfg.PYTHON_VERSION = '3.5.3'
self.assertEqual(self.packit.parse_pyversion(), (3, 5, 3, 64))
self.cfg.PYTHON_VERSION = 'bogus'
with mock.patch('sys.version_info', (3, 6, 2)):
self.assertEqual(self.packit.parse_pyversion(), (3, 6, 2, 64))
self.cfg.PYTHON_VERSION = '2.7'
with mock.patch('sys.version_info', (3, 7, 1)):
self.assertEqual(self.packit.parse_pyversion(), (3, 7, 1, 64))
def test_parse_pyversion_special_case(self): # see SPECIAL_CASE_VERSIONS
intro = f'\n#####\n##### RUNNING TEST parse_pyversion_special_case ...\n#####\n'
self.packit.msg(0, intro)
self.cfg.PYTHON_VERSION = '3.9.3-32'
self.assertEqual(self.packit.parse_pyversion(), (3, 9, 4, 32))
self.cfg.PYTHON_VERSION = '3.9.3-64'
self.assertEqual(self.packit.parse_pyversion(), (3, 9, 4, 64))
self.cfg.PYTHON_VERSION = '3.9.3'
self.assertEqual(self.packit.parse_pyversion(), (3, 9, 4, 64))
def test_parse_pyversion_max_version(self):
# NOTE: these yield *current* most recent versions...
# will need to be updated as new versions are released...
intro = f'\n#####\n##### RUNNING TEST parse_pyversion_max_version ...\n#####\n'
self.packit.msg(0, intro)
self.cfg.PYTHON_VERSION = '3.5.20'
self.assertEqual(self.packit.parse_pyversion(), (3, 5, 4, 64))
self.cfg.PYTHON_VERSION = '3.7'
self.assertEqual(self.packit.parse_pyversion(), (3, 7, 9, 64))
self.cfg.PYTHON_VERSION = '3.7-32'
self.assertEqual(self.packit.parse_pyversion(), (3, 7, 9, 32))
self.cfg.PYTHON_VERSION = '3.18'
self.assertEqual(self.packit.parse_pyversion(), (3, 10, 0, 64))
self.cfg.PYTHON_VERSION = '3'
self.assertEqual(self.packit.parse_pyversion(), (3, 10, 0, 64))
self.cfg.PYTHON_VERSION = '3-32'
self.assertEqual(self.packit.parse_pyversion(), (3, 10, 0, 32))
self.cfg.PYTHON_VERSION = '4'
self.assertEqual(self.packit.parse_pyversion(), (3, 10, 0, 64))
self.cfg.PYTHON_VERSION = 'bogus'
with mock.patch('sys.version_info', (2, 7, 10)):
self.assertEqual(self.packit.parse_pyversion(), (3, 5, 4, 64))
def test_getfile(self):
intro = f'\n#####\n##### RUNNING TEST getfile ...\n#####\n'
self.packit.msg(0, intro)
with self.assertRaises(Exception):
self.packit.getfile('bogus', on_error_abort=True)
testpath = self.packit.cache_dir / 'testfile'
open(testpath, 'a').close()
self.cfg.USE_CACHE = True
self.assertEqual(self.packit.getfile('bogus/dir/testfile'), testpath)
@unittest.skip('this will download and check *all* the pythons...')
def test_get_pythons(self):
intro = f'\n#####\n##### RUNNING TEST get_pythons ...\n#####\n'
self.packit.msg(0, intro)
for url, checksum in PY_URL.values():
self.assertTrue(self.packit.getfile(url, checksum))
class BaseBuildTestCase(unittest.TestCase):
def setUp(self):
self.cfg = _Cfg()
self.basedir = self.cfg.HERE / 'testoutput'
self.basedir.mkdir(exist_ok=True)
def tearDown(self):
del self.cfg
def start(self, buildir):
self.packit = Packit(settings=self.cfg)
intro = f'\n#####\n##### RUNNING TEST {buildir.stem} ...\n#####\n'
self.packit.msg(1, intro)
self.packit.cache_dir = self.basedir / 'test_cachedir'
self.packit.build_dir = self.basedir / buildir
# skip md5 check to save time
with mock.patch('winpackit._md5compare', lambda i, j: True):
ret = self.packit.main()
with open((self.packit.build_dir / '_testconfig.txt'), 'a') as f:
f.write('This test build was made from this configuration:\n\n')
pprint(self.cfg.__dict__, stream=f)
return ret
class BuildTestCase(BaseBuildTestCase):
# builds various example projects
def test_build_no_project(self):
buildir = Path('BuildTestCase_build_no_project')
ret = self.start(buildir)
self.assertTrue(all(ret))
def test_build0(self):
self.cfg.PROJECTS = [['examples/project0', ('main.py', 'main'),
('readme.txt', 'readme')]]
buildir = Path('BuildTestCase_build0')
ret = self.start(buildir)
self.assertTrue(all(ret))
def test_build1(self):
self.cfg.PROJECTS = [['examples/project1', ('main.py', 'main'),
('readme.txt', 'readme')]]
self.cfg.COMPILE = True
self.cfg.PYC_ONLY_DISTRIBUTION = True
buildir = Path('BuildTestCase_build1')
ret = self.start(buildir)
self.assertTrue(all(ret))
def test_build2(self):
self.cfg.PROJECTS = [
['examples/project1', ('main.py', 'main_project1'),
('readme.txt', 'readme_project1')],
['examples/project2', ('code/main.py', 'main_project2'),
('code/side.py', 'codeside'),
('code/foo/side.py', 'fooside'),
('docs/readme.txt', 'readme_project2')]]
buildir = Path('BuildTestCase_build2')
ret = self.start(buildir)
self.assertTrue(all(ret))
def test_build3(self):
self.cfg.PROJECTS = [['examples/project3/code with spaces',
('main.py', 'main')]]
self.cfg.COPY_DIRS = [['examples/project3/docs with spaces',
('readme.txt', 'readme')]]
buildir = Path('BuildTestCase_build3')
ret = self.start(buildir)
self.assertTrue(all(ret))
@unittest.skip('This one is going to pip-install *many* packages')
def test_build4(self):
self.cfg.PROJECTS = [['examples/project4', ('main.py', 'main'),
('readme.txt', 'readme')]]
self.cfg.PIP_REQUIRED = True
self.cfg.PIP_ARGS = ['--no-color']
self.cfg.PIP_INSTALL_ARGS = ['--no-compile']
self.cfg.DEPENDENCIES = ['wxpython', 'numpy']
self.cfg.REQUIREMENTS = 'examples/project4/requirements.txt'
buildir = Path('BuildTestCase_build4')
ret = self.start(buildir)
self.assertTrue(all(ret))
def test_build5(self):
self.cfg.PROJECTS = [['examples/project5/one', ('main.py', 'Main')],
['examples/project5/two']]
buildir = Path('BuildTestCase_build5')
ret = self.start(buildir)
self.assertTrue(all(ret))
def test_build6(self):
self.cfg.COMPILE = True
self.cfg.PYC_ONLY_DISTRIBUTION = True
self.cfg.PROJECTS = [['examples/project6', ('main.pyw', 'Main'),
('readme.txt', 'readme')]]
buildir = Path('BuildTestCase_build6')
ret = self.start(buildir)
self.assertTrue(all(ret))
def test_build_ignore_patterns(self):
self.cfg.PROJECTS = [
['examples/project1', ('main.py', 'main_project1'),
('readme.txt', 'readme_project1')]]
self.cfg.PROJECT_FILES_IGNORE_PATTERNS = ['a.gif', 'foo']
buildir = Path('BuildTestCase_build_ignore_patterns')
ret = self.start(buildir)
self.assertTrue(all(ret))
class FailBuildTestCase(BaseBuildTestCase):
# test various failures
def test_fail1(self): # this obtains a bogus get_pip.py
self.cfg.PIP_REQUIRED = True
buildir = Path('BuildTestCase_fail1')
with mock.patch('winpackit.Packit.obtain_getpip', lambda i: 'bogus'):
ret = self.start(buildir)
self.assertEqual(ret, [True, False, True, True, True, True, True, True, False])
def test_fail2(self): # this installs a bogus dependency
self.cfg.PIP_REQUIRED = True
self.cfg.DEPENDENCIES = ['total_bogus_packet_wont_install']
buildir = Path('BuildTestCase_fail2')
ret = self.start(buildir)
self.assertEqual(ret, [True, True, False, True, True, True, True, True, True])
def test_fail3(self): # this packs a non-existent project
self.cfg.PROJECTS = [['examples/project0', ('main.py', 'main'),
('readme.txt', 'readme')],
['examples/BOGUS']]
buildir = Path('BuildTestCase_fail3')
ret = self.start(buildir)
self.assertEqual(ret, [True, True, True, False, True, True, True, True, True])
def test_fail4(self): # this will hit a compile error
self.cfg.PROJECTS = [['examples/project7', ('main.py', 'main')]]
self.cfg.COMPILE = True
buildir = Path('BuildTestCase_fail4')
ret = self.start(buildir)
self.assertEqual(ret, [True, True, True, True, False, True, True, True, True])
def test_fail5(self): # this packs a non-existent "other" dir
self.cfg.PROJECTS = [['examples/project0', ('main.py', 'main'),
('readme.txt', 'readme')]]
self.cfg.COPY_DIRS = [['examples/BOGUS']]
buildir = Path('BuildTestCase_fail5')
ret = self.start(buildir)
self.assertEqual(ret, [True, True, True, True, True, False, True, True, True])
def test_fail6(self): # this has a bogus entrypoint
self.cfg.PROJECTS = [['examples/project0', ('main.py', 'main'),
('BOGUS', 'readme')]]
buildir = Path('BuildTestCase_fail6')
ret = self.start(buildir)
self.assertEqual(ret, [True, True, True, True, True, True, False, True, True])
class BuildTestCaseAllPythons(BaseBuildTestCase):
# builds the same project with the latest version of all Pythons
def setUp(self):
self.cfg = _Cfg()
self.basedir = self.cfg.HERE / 'testoutput'
self.basedir.mkdir(exist_ok=True)
self.cfg.PIP_REQUIRED = True
self.cfg.PROJECTS = [['examples/project5/one', ('main.py', 'Main')],
['examples/project5/two'],
['examples/project1', ('main.py', 'main_project1'),
('readme.txt', 'readme_project1')]]
self.cfg.COPY_DIRS = [['examples/project3/docs with spaces',
('readme.txt', 'docs_readme')]]
self.cfg.DEPENDENCIES = ['arrow']
self.cfg.COMPILE = True
self.cfg.PYC_ONLY_DISTRIBUTION = True
def test_build_py35_32(self):
self.cfg.PYTHON_VERSION = '3.5-32'
buildir = Path('BuildTestCase1_build_py35_32')
ret = self.start(buildir)
self.assertTrue(all(ret))
def test_build_py35_64(self):
self.cfg.PYTHON_VERSION = '3.5'
buildir = Path('BuildTestCase1_build_py35_64')
ret = self.start(buildir)
self.assertTrue(all(ret))
def test_build_py36_32(self):
self.cfg.PYTHON_VERSION = '3.6-32'
buildir = Path('BuildTestCase1_build_py36_32')
ret = self.start(buildir)
self.assertTrue(all(ret))
def test_build_py36_64(self):
self.cfg.PYTHON_VERSION = '3.6'
buildir = Path('BuildTestCase1_build_py36_64')
ret = self.start(buildir)
self.assertTrue(all(ret))
def test_build_py37_32(self):
self.cfg.PYTHON_VERSION = '3.7-32'
buildir = Path('BuildTestCase1_build_py37_32')
ret = self.start(buildir)
self.assertTrue(all(ret))
def test_build_py37_64(self):
self.cfg.PYTHON_VERSION = '3.7'
buildir = Path('BuildTestCase1_build_py37_64')
ret = self.start(buildir)
self.assertTrue(all(ret))
def test_build_py38_32(self):
self.cfg.PYTHON_VERSION = '3.8-32'
buildir = Path('BuildTestCase1_build_py38_32')
ret = self.start(buildir)
self.assertTrue(all(ret))
def test_build_py38_64(self):
self.cfg.PYTHON_VERSION = '3.8'
buildir = Path('BuildTestCase1_build_py38_64')
ret = self.start(buildir)
self.assertTrue(all(ret))
def test_build_py39_32(self):
self.cfg.PYTHON_VERSION = '3.9-32'
buildir = Path('BuildTestCase1_build_py39_32')
ret = self.start(buildir)
self.assertTrue(all(ret))
def test_build_py39_64(self):
self.cfg.PYTHON_VERSION = '3.9'
buildir = Path('BuildTestCase1_build_py39_64')
ret = self.start(buildir)
self.assertTrue(all(ret))
def test_build_py310_32(self):
self.cfg.PYTHON_VERSION = '3.10-32'
buildir = Path('BuildTestCase1_build_py310_32')
ret = self.start(buildir)
self.assertTrue(all(ret))
def test_build_py310_64(self):
self.cfg.PYTHON_VERSION = '3.10'
buildir = Path('BuildTestCase1_build_py310_64')
ret = self.start(buildir)
self.assertTrue(all(ret))
class DelayedBuildTestCase(BaseBuildTestCase):
# a few "delayed build" tests
def setUp(self):
super().setUp()
self.cfg.DELAYED_INSTALL = True
def test_delay1(self): # this installs a delayed Pip
self.cfg.PIP_REQUIRED = True
self.cfg.PROJECTS = [['examples/project0', ('main.py', 'main'),
('readme.txt', 'readme')]]
buildir = Path('DelayedBuildTestCase_build1')
ret = self.start(buildir)
self.assertTrue(all(ret))
def test_delay2(self): # this installs delayed dependencies from a req file
self.cfg.PIP_REQUIRED = True
self.cfg.PROJECTS = [['examples/project4', ('main.py', 'main'),
('readme.txt', 'readme')]]
self.cfg.REQUIREMENTS = 'examples/project4/requirements_small.txt'
buildir = Path('DelayedBuildTestCase_build2')
ret = self.start(buildir)
self.assertTrue(all(ret))
def test_delay3(self): # this installs delayed dependencies from a list
self.cfg.PIP_REQUIRED = True
self.cfg.PROJECTS = [['examples/project4', ('main.py', 'main'),
('readme.txt', 'readme')]]
self.cfg.DEPENDENCIES = ['arrow']
buildir = Path('DelayedBuildTestCase_build3')
ret = self.start(buildir)
self.assertTrue(all(ret))
def test_delay4(self): # this installs delayed dependencies from both
self.cfg.PIP_REQUIRED = True
self.cfg.PROJECTS = [['examples/project4', ('main.py', 'main'),
('readme.txt', 'readme')]]
self.cfg.DEPENDENCIES = ['arrow']
self.cfg.REQUIREMENTS = 'examples/project4/requirements_small.txt'
buildir = Path('DelayedBuildTestCase_build4')
ret = self.start(buildir)
self.assertTrue(all(ret))
def test_delay5(self): # this installs with delayed compile pycs
self.cfg.COMPILE = True
self.cfg.PROJECTS = [['examples/project5/one', ('main.py', 'main_project5')],
['examples/project5/two'],
['examples/project6', ('main.pyw', 'main_project6'),
('readme.txt', 'readme_project6')]]
buildir = Path('DelayedBuildTestCase_build5')
ret = self.start(buildir)
self.assertTrue(all(ret))
def test_delay6(self): # this installs a delayed pyc-only distribution
self.cfg.COMPILE = True
self.cfg.PYC_ONLY_DISTRIBUTION = True
self.cfg.PROJECTS = [['examples/project5/one', ('main.py', 'main_project5')],
['examples/project5/two'],
['examples/project6', ('main.pyw', 'main_project6'),
('readme.txt', 'readme_project6')],
['examples/project3/code with spaces',
('main.py', 'main_project3')]]
buildir = Path('DelayedBuildTestCase_build6')
ret = self.start(buildir)
self.assertTrue(all(ret))
if __name__ == '__main__':
unittest.main()