liboqs/tests/helpers.py
2019-07-30 20:54:47 -04:00

33 lines
1.1 KiB
Python

import os
import subprocess
def run_subprocess(command, working_dir='.', env=None, expected_returncode=0, input=None):
"""
Helper function to run a shell command and report success/failure
depending on the exit status of the shell command.
"""
if env is not None:
env_ = os.environ.copy()
env_.update(env)
env = env_
# Note we need to capture stdout/stderr from the subprocess,
# then print it, which nose/unittest will then capture and
# buffer appropriately
print(working_dir + " > " + " ".join(command))
result = subprocess.run(
command,
input=input,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
cwd=working_dir,
env=env,
)
print(result.stdout.decode('utf-8'))
if expected_returncode is not None:
assert result.returncode == expected_returncode, \
"Got unexpected return code {}".format(result.returncode)
else:
return (result.returncode, result.stdout.decode('utf-8'))
return result.stdout.decode('utf-8')