forked from sio/Makefile.venv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.py
87 lines (74 loc) · 2.83 KB
/
common.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
'''
Common utilities for testing Makefile.venv
'''
import os
import sys
from shutil import copyfile
from subprocess import run, PIPE
from tempfile import TemporaryDirectory
from unittest import TestCase, skipIf
slow_test = skipIf(
os.environ.get('SKIP_SLOW_TESTS', False),
'slow test'
)
class MakefileTestCase(TestCase):
'''Base class for Makefile.venv tests'''
MAKEFILE = 'Makefile.venv'
MAKE = 'make'
TIMEOUT = int(os.getenv('TEST_SUBPROCESS_TIMEOUT', 60)) # seconds
TMPPREFIX = 'Makefile.venv_test_'
def make(self, *args, makefile=None, debug=False, dry_run=False, returncode=0):
'''Execute Makefile.venv with GNU Make in temporary directory'''
if makefile is None:
makefile = self.MAKEFILE
command = [self.MAKE, '-C', self.tmpdir.name, '-f', os.path.abspath(makefile)]
if debug:
command.append('-drR')
if dry_run:
command.append('-n')
command.extend(args)
process = run(command, stdout=PIPE, stderr=PIPE, timeout=self.TIMEOUT)
process.stdout, process.stderr = (output.decode(sys.stdout.encoding)
for output in (process.stdout, process.stderr))
if returncode is not None:
self.check_returncode(process, returncode)
return process
def check_returncode(self, process, returncode=0):
'''Check subprocess return code in automated tests'''
self.assertEqual(
process.returncode,
returncode,
msg='\n'.join(
part for part in (
'{} exited with code {} (expected {})'.format(self.MAKE, process.returncode, returncode),
'\nstdout:',
process.stdout,
'stderr:',
process.stderr,
)
if part.strip()
)
)
def copy_data(self, name, makefile=False, data_dir='tests/data'):
'''Copy test data to temporary directory. Return full path to resulting file'''
src = os.path.join(data_dir, name)
dest = os.path.join(self.tmpdir.name, name)
if makefile:
with open(src) as source:
content = source.read()
with open(dest, 'w') as output:
output.write(content.replace(
'{{ Makefile.venv }}',
os.path.abspath(self.MAKEFILE)
))
else:
copyfile(src, dest)
return dest
def setUp(self):
self.tmpdir = TemporaryDirectory(prefix=self.TMPPREFIX)
for variable in ('WORKDIR', 'VENVDIR', 'REQUIREMENTS_TXT'):
if variable in os.environ: # Clear environment variables for tests
os.environ.pop(variable)
def tearDown(self):
self.tmpdir.cleanup()
del self.tmpdir