pqc/test/helpers.py

71 linhas
2.1 KiB
Python
Original Visão normal Histórico

2019-03-04 14:12:38 +00:00
import functools
2019-02-27 11:58:00 +00:00
import os
import subprocess
2019-03-04 14:12:38 +00:00
import unittest
2019-02-18 12:04:59 +00:00
2019-02-27 11:58:00 +00:00
def run_subprocess(command, working_dir='.', env=None, expected_returncode=0):
2019-02-18 12:04:59 +00:00
"""
Helper function to run a shell command and report success/failure
depending on the exit status of the shell command.
"""
2019-02-27 11:58:00 +00:00
if env is not None:
env_ = os.environ.copy()
env_.update(env)
env = env_
2019-02-18 12:04:59 +00:00
# Note we need to capture stdout/stderr from the subprocess,
# then print it, which nose/unittest will then capture and
# buffer appropriately
2019-03-04 14:12:38 +00:00
print(working_dir + " > " + " ".join(command))
result = subprocess.run(
command,
2019-02-18 12:04:59 +00:00
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
2019-02-27 11:58:00 +00:00
cwd=working_dir,
env=env,
)
print(result.stdout.decode('utf-8'))
2019-03-04 15:54:37 +00:00
assert result.returncode == expected_returncode, \
"Got unexpected return code {}".format(result.returncode)
return result.stdout.decode('utf-8')
2019-02-27 11:44:21 +00:00
2019-03-04 15:54:37 +00:00
def make(*args, working_dir='.', env=None, expected_returncode=0, **kwargs):
2019-02-27 11:44:21 +00:00
"""
Runs a make target in the specified working directory
Usage:
make('clean', 'targetb', SCHEME='bla')
"""
2019-03-04 14:12:38 +00:00
if os.name == 'nt':
make_command = ['nmake', '/f', 'Makefile.Microsoft_nmake', '/E']
# we need SCHEME_UPPERCASE and IMPLEMENTATION_UPPERCASE with nmake
for envvar in ['IMPLEMENTATION', 'SCHEME']:
if envvar in kwargs:
kwargs['{}_UPPERCASE'.format(envvar)] = kwargs[envvar].upper().replace('-', '')
else:
make_command = ['make']
2019-02-27 11:44:21 +00:00
return run_subprocess(
[
2019-03-04 14:12:38 +00:00
*make_command,
2019-02-27 11:44:21 +00:00
*['{}={}'.format(k, v) for k, v in kwargs.items()],
2019-03-04 14:12:38 +00:00
*args,
2019-02-27 11:44:21 +00:00
],
working_dir=working_dir,
2019-02-27 11:58:00 +00:00
env=env,
2019-03-04 15:54:37 +00:00
expected_returncode=expected_returncode,
2019-02-27 11:44:21 +00:00
)
2019-03-04 14:12:38 +00:00
def skip_windows(message="This test is not supported on Windows"):
def wrapper(f):
@functools.wraps(f)
def skip_windows(*args, **kwargs):
raise unittest.SkipTest(message)
if os.name == 'nt':
return skip_windows
else:
return f
return wrapper