|
| 1 | +import pytest |
| 2 | +import hypothesis |
| 3 | +import json |
| 4 | +import subprocess |
| 5 | +import tempfile |
| 6 | +import os |
| 7 | +import re |
| 8 | + |
| 9 | +hypothesis.settings.register_profile("ci", max_examples=500) |
| 10 | +hypothesis.settings.register_profile("fast", max_examples=10) |
| 11 | + |
| 12 | + |
| 13 | + |
| 14 | +def pytest_addoption(parser): |
| 15 | + parser.addoption("--compiler", action="store", help="C++ compiler", default=None, required=False) |
| 16 | + parser.addoption("--compiler-args", action="store", help="C++ compiler arguments", default="", required=False) |
| 17 | + parser.addoption("--includes", action="store", help="C++ include directories", default="", required=False) |
| 18 | + |
| 19 | + parser.addoption("--compile-commands", action="store", help="cmake compiler commands", default=None, required=False) |
| 20 | + parser.addoption("--prototype-driver", action="store", help="Prototype .cpp filename to gather compilation command from", default=None, required=False) |
| 21 | + |
| 22 | +@pytest.fixture(scope="module") |
| 23 | +def cmake_compilation_command(pytestconfig): |
| 24 | + compile_commands_filename = pytestconfig.getoption("compile_commands") |
| 25 | + prototype_driver_filename = pytestconfig.getoption("prototype_driver") |
| 26 | + |
| 27 | + if compile_commands_filename is None or prototype_driver_filename is None: |
| 28 | + return None |
| 29 | + |
| 30 | + def f(filename): |
| 31 | + with open(compile_commands_filename, "r") as f: |
| 32 | + db = json.load(f) |
| 33 | + for obj in db: |
| 34 | + if obj["file"] == prototype_driver_filename: |
| 35 | + cmd = obj["command"] |
| 36 | + cmd = cmd.replace(prototype_driver_filename, filename) |
| 37 | + cmd = re.sub(r"-o .*?\.cpp\.o", f"-o {filename}.o", cmd) |
| 38 | + cmd = re.sub(r" -c ", f" ", cmd) |
| 39 | + return cmd.split(" ") |
| 40 | + |
| 41 | + return f |
| 42 | + |
| 43 | +@pytest.fixture(scope="module") |
| 44 | +def args_compilation_command(pytestconfig): |
| 45 | + compiler = pytestconfig.getoption("compiler") |
| 46 | + if compiler is None: |
| 47 | + return None |
| 48 | + |
| 49 | + include_dirs = [f"-I{i}" for i in pytestconfig.getoption("includes").split(",") if i] |
| 50 | + compiler_args = [i for i in pytestconfig.getoption("compiler_args").split(",") if i] |
| 51 | + |
| 52 | + def f(filename): |
| 53 | + compile_command = [ |
| 54 | + compiler, temp_cpp_file_path, |
| 55 | + "-o", temp_cpp_file_path + ".out" |
| 56 | + ] + compiler_args + include_args |
| 57 | + return compile_command |
| 58 | + |
| 59 | + return f |
| 60 | + |
| 61 | + |
| 62 | + |
| 63 | +@pytest.fixture(scope="module") |
| 64 | +def compile(cmake_compilation_command, args_compilation_command): |
| 65 | + cmd = cmake_compilation_command |
| 66 | + if cmd is None: |
| 67 | + cmd = args_compilation_command |
| 68 | + |
| 69 | + def f(code_str): |
| 70 | + code_str += "\n" |
| 71 | + with tempfile.NamedTemporaryFile(delete=False, suffix=".cpp") as temp_cpp_file: |
| 72 | + temp_cpp_file.write(code_str.encode('utf-8')) |
| 73 | + temp_cpp_file_path = temp_cpp_file.name |
| 74 | + |
| 75 | + try: |
| 76 | + compile_command = cmd(temp_cpp_file_path) |
| 77 | + result = subprocess.run(compile_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 78 | + |
| 79 | + if result.returncode == 0: |
| 80 | + os.chmod(temp_cpp_file_path + ".o", 0o700) |
| 81 | + return temp_cpp_file_path + ".o" |
| 82 | + else: |
| 83 | + error_message = ( |
| 84 | + f"Compiler returned non-zero exit code: {result.returncode}\n" |
| 85 | + f"Compilation command: {' '.join(compile_command)}\n" |
| 86 | + f"Source code:\n{code_str}\n" |
| 87 | + f"Compiler stderr:\n{result.stderr.decode('utf-8')}\n" |
| 88 | + f"Compiler stdout:\n{result.stdout.decode('utf-8')}\n" |
| 89 | + ) |
| 90 | + pytest.fail(error_message) |
| 91 | + |
| 92 | + except Exception as e: |
| 93 | + pytest.fail(str(e)) |
| 94 | + finally: |
| 95 | + os.remove(temp_cpp_file_path) |
| 96 | + |
| 97 | + return f |
| 98 | + |
0 commit comments