| 1 |
import subprocess |
| 2 |
import unittest |
| 3 |
from pathlib import Path |
| 4 |
import os |
| 5 |
import argparse |
| 6 |
import sys |
| 7 |
|
| 8 |
|
| 9 |
class TestHeaderGenIntegration(unittest.TestCase): |
| 10 |
def setUp(self): |
| 11 |
self.output_dir = Path( |
| 12 |
args.output_dir if args.output_dir else "libc/newhdrgen/tests/output" |
| 13 |
) |
| 14 |
|
| 15 |
self.maxDiff = None |
| 16 |
|
| 17 |
self.source_dir = Path(__file__).resolve().parent.parent.parent.parent |
| 18 |
|
| 19 |
def run_script(self, yaml_file, h_def_file, output_dir, entry_points): |
| 20 |
yaml_file = self.source_dir / yaml_file |
| 21 |
h_def_file = self.source_dir / h_def_file |
| 22 |
command = [ |
| 23 |
"python3", |
| 24 |
str(self.source_dir / "libc/newhdrgen/yaml_to_classes.py"), |
| 25 |
str(yaml_file), |
| 26 |
"--h_def_file", |
| 27 |
str(h_def_file), |
| 28 |
"--output_dir", |
| 29 |
str(output_dir), |
| 30 |
] |
| 31 |
|
| 32 |
for entry_point in entry_points: |
| 33 |
command.extend(["--e", entry_point]) |
| 34 |
|
| 35 |
result = subprocess.run( |
| 36 |
command, |
| 37 |
capture_output=True, |
| 38 |
text=True, |
| 39 |
) |
| 40 |
|
| 41 |
print("STDOUT:", result.stdout) |
| 42 |
print("STDERR:", result.stderr) |
| 43 |
result.check_returncode() |
| 44 |
|
| 45 |
def compare_files(self, generated_file, expected_file): |
| 46 |
with generated_file.open("r") as gen_file: |
| 47 |
gen_content = gen_file.read() |
| 48 |
with expected_file.open("r") as exp_file: |
| 49 |
exp_content = exp_file.read() |
| 50 |
|
| 51 |
self.assertEqual(gen_content, exp_content) |
| 52 |
|
| 53 |
def test_generate_header(self): |
| 54 |
yaml_file = "libc/newhdrgen/tests/input/test_small.yaml" |
| 55 |
h_def_file = "libc/newhdrgen/tests/input/test_small.h.def" |
| 56 |
expected_output_file = ( |
| 57 |
self.source_dir / "libc/newhdrgen/tests/expected_output/test_header.h" |
| 58 |
) |
| 59 |
output_file = self.output_dir / "test_small.h" |
| 60 |
entry_points = {"func_b", "func_a", "func_c", "func_d", "func_e"} |
| 61 |
|
| 62 |
if not self.output_dir.exists(): |
| 63 |
self.output_dir.mkdir(parents=True) |
| 64 |
|
| 65 |
self.run_script(yaml_file, h_def_file, self.output_dir, entry_points) |
| 66 |
|
| 67 |
self.compare_files(output_file, expected_output_file) |
| 68 |
|
| 69 |
|
| 70 |
if __name__ == "__main__": |
| 71 |
parser = argparse.ArgumentParser(description="TestHeaderGenIntegration arguments") |
| 72 |
parser.add_argument( |
| 73 |
"--output_dir", type=str, help="Output directory for generated headers" |
| 74 |
) |
| 75 |
args, remaining_argv = parser.parse_known_args() |
| 76 |
|
| 77 |
TestHeaderGenIntegration.output_dir = args.output_dir |
| 78 |
|
| 79 |
sys.argv[1:] = remaining_argv |
| 80 |
|
| 81 |
unittest.main() |