diff options
author | Eli Bendersky <eliben@gmail.com> | 2021-10-29 09:50:16 -0700 |
---|---|---|
committer | Eli Bendersky <eliben@gmail.com> | 2021-10-29 09:50:16 -0700 |
commit | 7beef72dea11cf421f6ab7fce81d8592776cf79f (patch) | |
tree | 88fd6b4dd43585adea3da64d053ddf0a5c415125 /tests | |
parent | 615317a473f09b9b9d444313ac15e52e7763c7c1 (diff) | |
download | pycparser-7beef72dea11cf421f6ab7fce81d8592776cf79f.tar.gz |
Add test that runs all examples and makes sure they don't crash
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test_examples.py | 33 | ||||
-rw-r--r-- | tests/test_util.py | 23 |
2 files changed, 56 insertions, 0 deletions
diff --git a/tests/test_examples.py b/tests/test_examples.py new file mode 100644 index 0000000..24cf16e --- /dev/null +++ b/tests/test_examples.py @@ -0,0 +1,33 @@ +import os +import sys +import time +import unittest + +sys.path.insert(0, '.') +from tests.test_util import run_exe + +EMIT_ELAPSED_TIME = False + +# Runs all pycparser examples with no command-line arguments and makes sure they +# run successfully (return code = 0), without actually verifying their output. +class TestExamplesSucceed(unittest.TestCase): + def test_all_examples(self): + root = './examples' + for filename in os.listdir(root): + if os.path.splitext(filename)[1] == '.py': + # TODO: It would be nice to use subTest here, but that's not + # available in Python 2.7 + # Use it when we finally drop Python 2... + path = os.path.join(root, filename) + t1 = time.time() + rc, stdout = run_exe(path) + elapsed = time.time() - t1 + if EMIT_ELAPSED_TIME: + print('{}... elapsed: {}'.format(filename, elapsed)) + self.assertEqual( + rc, 0, 'example "{}" failed with stdout =\n{}'.format(filename, stdout)) + + +if __name__ == '__main__': + EMIT_ELAPSED_TIME = True + unittest.main() diff --git a/tests/test_util.py b/tests/test_util.py index 3ac3886..435962d 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -7,7 +7,10 @@ # This file contributed by vit9696@users.noreply.github.com # License: BSD #------------------------------------------------------------------------------ +import os import platform +import subprocess +import sys def cpp_supported(): @@ -29,3 +32,23 @@ def cpp_args(args=[]): if platform.system() == 'Darwin': return ['-E'] + args return args + +def _bytes2str(b): + if sys.version_info[0] == 3: + return b.decode('latin-1') + else: + return b + +def run_exe(exe_path, args=[], echo=False): + """ Runs the given executable as a subprocess, given the + list of arguments. Captures its return code (rc) and stdout and + returns a pair: rc, stdout_str + """ + popen_cmd = [exe_path] + args + if os.path.splitext(exe_path)[1] == '.py': + popen_cmd.insert(0, sys.executable) + if echo: + print('[cmd]', ' '.join(popen_cmd)) + proc = subprocess.Popen(popen_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + proc_stdout = proc.communicate()[0] + return proc.returncode, _bytes2str(proc_stdout) |