|
| 1 | +import pytest |
| 2 | +import hypothesis |
| 3 | +import subprocess |
| 4 | +import tempfile |
| 5 | +import os |
| 6 | + |
| 7 | +hypothesis.settings.register_profile("ci", max_examples=500) |
| 8 | +hypothesis.settings.register_profile("fast", max_examples=10) |
| 9 | + |
| 10 | + |
| 11 | +def pytest_addoption(parser): |
| 12 | + parser.addoption("--includes", action="store", help="C++ include directories", default="", required=False) |
| 13 | + parser.addoption("--compiler", action="store", help="C++ compiler", required=True) |
| 14 | + parser.addoption("--compiler-args", action="store", help="C++ compiler arguments", default="", required=False) |
| 15 | + parser.addoption("--compiler-args-file", action="store", help="File with C++ compiler arguments", default=None, required=False) |
| 16 | + |
| 17 | + |
| 18 | +@pytest.fixture(scope="module") |
| 19 | +def compiler(pytestconfig): |
| 20 | + return pytestconfig.getoption("compiler") |
| 21 | + |
| 22 | +@pytest.fixture(scope="module") |
| 23 | +def include_dirs(pytestconfig): |
| 24 | + return [i for i in pytestconfig.getoption("includes").split(",") if i] |
| 25 | + |
| 26 | +@pytest.fixture(scope="module") |
| 27 | +def compiler_args(pytestconfig): |
| 28 | + retval = [] |
| 29 | + |
| 30 | + args_file = pytestconfig.getoption("compiler_args_file") |
| 31 | + if args_file: |
| 32 | + with open(args_file, "r") as f: |
| 33 | + for line in f.readlines(): |
| 34 | + retval += [i for i in line.split(",") if i] |
| 35 | + |
| 36 | + args = pytestconfig.getoption("compiler_args") |
| 37 | + if args: |
| 38 | + retval += [i for i in args.split(",") if i] |
| 39 | + |
| 40 | + return retval |
| 41 | + |
| 42 | + |
| 43 | +@pytest.fixture(scope="module") |
| 44 | +def compile(compiler, compiler_args, include_dirs): |
| 45 | + include_args = [f"-I{i}" for i in include_dirs] |
| 46 | + def f(code_str): |
| 47 | + code_str += "\n" |
| 48 | + with tempfile.NamedTemporaryFile(delete=False, suffix=".cpp") as temp_cpp_file: |
| 49 | + temp_cpp_file.write(code_str.encode('utf-8')) |
| 50 | + temp_cpp_file_path = temp_cpp_file.name |
| 51 | + |
| 52 | + try: |
| 53 | + compile_command = [ |
| 54 | + compiler, temp_cpp_file_path, |
| 55 | + "-o", temp_cpp_file_path + ".out" |
| 56 | + ] + compiler_args + include_args |
| 57 | + |
| 58 | + result = subprocess.run(compile_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 59 | + |
| 60 | + if result.returncode == 0: |
| 61 | + return True |
| 62 | + else: |
| 63 | + error_message = ( |
| 64 | + f"Compiler returned non-zero exit code: {result.returncode}\n" |
| 65 | + f"Compilation command: {' '.join(compile_command)}\n" |
| 66 | + f"Source code:\n{code_str}\n" |
| 67 | + f"Compiler stderr:\n{result.stderr.decode('utf-8')}\n" |
| 68 | + f"Compiler stdout:\n{result.stdout.decode('utf-8')}\n" |
| 69 | + ) |
| 70 | + pytest.fail(error_message) |
| 71 | + |
| 72 | + except Exception as e: |
| 73 | + pytest.fail(str(e)) |
| 74 | + finally: |
| 75 | + os.remove(temp_cpp_file_path) |
| 76 | + if os.path.exists(temp_cpp_file_path + ".out"): |
| 77 | + os.remove(temp_cpp_file_path + ".out") |
| 78 | + |
| 79 | + return f |
| 80 | + |
0 commit comments