Make main() suitable to being called from python

Don't call sys.exit(), and don't clobber the working directory.

Signed-off-by: Bence Szépkúti <bence.szepkuti@arm.com>
This commit is contained in:
Bence Szépkúti 2021-11-02 13:48:39 +01:00
parent 19a124d677
commit 559f1ce0a3

View File

@ -18,7 +18,7 @@ EXPECTED_FAILURES = {
PSA_ARCH_TESTS_REPO = 'https://github.com/ronald-cron-arm/psa-arch-tests.git' PSA_ARCH_TESTS_REPO = 'https://github.com/ronald-cron-arm/psa-arch-tests.git'
PSA_ARCH_TESTS_REF = 'crypto1.0-3.0' PSA_ARCH_TESTS_REF = 'crypto1.0-3.0'
#pylint: disable=too-many-statements #pylint: disable=too-many-branches,too-many-statements
def main(): def main():
mbedtls_dir = os.getcwd() mbedtls_dir = os.getcwd()
@ -30,81 +30,85 @@ def main():
os.mkdir(psa_arch_tests_dir) os.mkdir(psa_arch_tests_dir)
except FileExistsError: except FileExistsError:
pass pass
os.chdir(psa_arch_tests_dir)
subprocess.check_call(['git', 'init'])
subprocess.check_call(['git', 'fetch', PSA_ARCH_TESTS_REPO, PSA_ARCH_TESTS_REF])
subprocess.check_call(['git', 'checkout', 'FETCH_HEAD'])
build_dir = 'api-tests/build'
try: try:
shutil.rmtree(build_dir) os.chdir(psa_arch_tests_dir)
except FileNotFoundError:
pass
os.mkdir(build_dir)
os.chdir(build_dir)
#pylint: disable=bad-continuation subprocess.check_call(['git', 'init'])
subprocess.check_call([ subprocess.check_call(['git', 'fetch', PSA_ARCH_TESTS_REPO, PSA_ARCH_TESTS_REF])
'cmake', '..', '-GUnix Makefiles', subprocess.check_call(['git', 'checkout', 'FETCH_HEAD'])
'-DTARGET=tgt_dev_apis_stdc',
'-DTOOLCHAIN=HOST_GCC',
'-DSUITE=CRYPTO',
'-DPSA_CRYPTO_LIB_FILENAME={}/library/libmbedcrypto.a'.format(mbedtls_dir),
'-DPSA_INCLUDE_PATHS={}/include'.format(mbedtls_dir)
])
subprocess.check_call(['cmake', '--build', '.'])
proc = subprocess.Popen(['./psa-arch-tests-crypto'], build_dir = 'api-tests/build'
bufsize=1, stdout=subprocess.PIPE, universal_newlines=True) try:
shutil.rmtree(build_dir)
except FileNotFoundError:
pass
os.mkdir(build_dir)
os.chdir(build_dir)
test_re = re.compile( #pylint: disable=bad-continuation
'^TEST: (?P<test_num>[0-9]*)|' subprocess.check_call([
'^TEST RESULT: (?P<test_result>FAILED|PASSED)' 'cmake', '..',
) '-GUnix Makefiles',
test = -1 '-DTARGET=tgt_dev_apis_stdc',
unexpected_successes = set(EXPECTED_FAILURES) '-DTOOLCHAIN=HOST_GCC',
expected_failures = [] '-DSUITE=CRYPTO',
unexpected_failures = [] '-DPSA_CRYPTO_LIB_FILENAME={}/library/libmbedcrypto.a'.format(mbedtls_dir),
for line in proc.stdout: '-DPSA_INCLUDE_PATHS={}/include'.format(mbedtls_dir)
print(line, end='') ])
match = test_re.match(line) subprocess.check_call(['cmake', '--build', '.'])
if match is not None:
groupdict = match.groupdict()
test_num = groupdict['test_num']
if test_num is not None:
test = int(test_num)
elif groupdict['test_result'] == 'FAILED':
try:
unexpected_successes.remove(test)
expected_failures.append(test)
print('Expected failure, ignoring')
except KeyError:
unexpected_failures.append(test)
print('ERROR: Unexpected failure')
elif test in unexpected_successes:
print('ERROR: Unexpected success')
proc.wait()
print() proc = subprocess.Popen(['./psa-arch-tests-crypto'],
print('***** test_psa_compliance.py report ******') bufsize=1, stdout=subprocess.PIPE, universal_newlines=True)
print()
print('Expected failures:', ', '.join(str(i) for i in expected_failures)) test_re = re.compile(
print('Unexpected failures:', ', '.join(str(i) for i in unexpected_failures)) '^TEST: (?P<test_num>[0-9]*)|'
print('Unexpected successes:', ', '.join(str(i) for i in sorted(unexpected_successes))) '^TEST RESULT: (?P<test_result>FAILED|PASSED)'
print() )
if unexpected_successes or unexpected_failures: test = -1
if unexpected_successes: unexpected_successes = set(EXPECTED_FAILURES)
print('Unexpected successes encountered.') expected_failures = []
print('Please remove the corresponding tests from ' unexpected_failures = []
'EXPECTED_FAILURES in tests/scripts/compliance_test.py') for line in proc.stdout:
print() print(line, end='')
print('FAILED') match = test_re.match(line)
sys.exit(1) if match is not None:
else: groupdict = match.groupdict()
test_num = groupdict['test_num']
if test_num is not None:
test = int(test_num)
elif groupdict['test_result'] == 'FAILED':
try:
unexpected_successes.remove(test)
expected_failures.append(test)
print('Expected failure, ignoring')
except KeyError:
unexpected_failures.append(test)
print('ERROR: Unexpected failure')
elif test in unexpected_successes:
print('ERROR: Unexpected success')
proc.wait()
print()
print('***** test_psa_compliance.py report ******')
print()
print('Expected failures:', ', '.join(str(i) for i in expected_failures))
print('Unexpected failures:', ', '.join(str(i) for i in unexpected_failures))
print('Unexpected successes:', ', '.join(str(i) for i in sorted(unexpected_successes)))
print()
if unexpected_successes or unexpected_failures:
if unexpected_successes:
print('Unexpected successes encountered.')
print('Please remove the corresponding tests from '
'EXPECTED_FAILURES in tests/scripts/compliance_test.py')
print()
print('FAILED')
return 1
else:
shutil.rmtree(psa_arch_tests_dir)
print('SUCCESS')
return 0
finally:
os.chdir(mbedtls_dir) os.chdir(mbedtls_dir)
shutil.rmtree(psa_arch_tests_dir)
print('SUCCESS')
if __name__ == '__main__': if __name__ == '__main__':
main() sys.exit(main())