-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtest.py
63 lines (52 loc) · 2.04 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import os
import subprocess
import pytest
import json
# Default configurations
ASPIS_SCRIPT = "./aspis.sh" # Path to the ASPIS compilation script
TEST_DIR = "./tests" # Directory containing the test cases
# Load the test configuration
def load_config():
with open("test_config.json", "r") as file:
return json.load(file)
# Utility functions
def run_command(command, cwd=None):
"""Runs a shell command and returns its output, error, and exit code."""
process = subprocess.run(
command, cwd=cwd, text=True, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
return process.stdout, process.stderr, process.returncode
def compile_with_aspis(source_file, output_file, options, llvm_bin, build_dir):
"""Compile a file using ASPIS with specified options."""
command = f"{ASPIS_SCRIPT} --llvm-bin {llvm_bin} {options} {source_file} -o {output_file} --build-dir ./{build_dir} --verbose"
print(command)
stdout, stderr, exit_code = run_command(command)
if exit_code != 0:
raise RuntimeError(f"Compilation failed: {stderr}")
return stdout
def execute_binary(binary_file):
"""Execute the compiled binary and return its output."""
stdout, stderr, exit_code = run_command(binary_file)
if exit_code != 0:
raise RuntimeError(f"Execution failed: {stderr}")
return stdout
# Tests
@pytest.mark.parametrize("test", load_config()["tests"])
def test_aspis(test):
"""Run a single ASPIS test."""
config = load_config()
llvm_bin = config["llvm_bin"]
test_name = test["test_name"]
build_dir = test_name
source_file = test["source_file"]
aspis_options = test["aspis_options"]
expected_output = test["expected_output"]
output_file = f"{test_name}.out"
source_path = os.path.join(TEST_DIR, source_file)
# Compile the source file
compile_with_aspis(source_path, output_file, aspis_options, llvm_bin, build_dir)
# Execute the binary and check output
result = execute_binary(f"./{build_dir}/{output_file}")
assert result == expected_output, f"Test {test_name} failed: {result}"
if __name__ == "__main__":
pytest.main()