105 lines · python
1import argparse2import subprocess3import sys4import unittest5from pathlib import Path6 7 8class TestHeaderGenIntegration(unittest.TestCase):9 def setUp(self):10 self.output_dir = TestHeaderGenIntegration.output_dir11 self.source_dir = Path(__file__).parent12 self.main_script = self.source_dir.parent / "main.py"13 self.maxDiff = 80 * 10014 15 def run_script(self, yaml_file, output_file, entry_points=[], switches=[]):16 command = [17 "python3",18 str(self.main_script),19 str(yaml_file),20 "--output",21 str(output_file),22 ] + switches23 24 for entry_point in entry_points:25 command.extend(["--entry-point", entry_point])26 27 result = subprocess.run(28 command,29 capture_output=True,30 text=True,31 )32 33 print("STDOUT:", result.stdout)34 print("STDERR:", result.stderr)35 result.check_returncode()36 37 def compare_files(self, generated_file, expected_file):38 with generated_file.open("r") as gen_file:39 gen_content = gen_file.read()40 with expected_file.open("r") as exp_file:41 exp_content = exp_file.read()42 43 self.assertEqual(gen_content, exp_content)44 45 def test_generate_header(self):46 yaml_file = self.source_dir / "input/test_small.yaml"47 expected_output_file = self.source_dir / "expected_output/test_header.h"48 output_file = self.output_dir / "test_small.h"49 entry_points = {"func_b", "func_a", "func_c", "func_d", "func_e"}50 51 self.run_script(yaml_file, output_file, entry_points)52 53 self.compare_files(output_file, expected_output_file)54 55 def test_generate_subdir_header(self):56 yaml_file = self.source_dir / "input" / "subdir" / "test.yaml"57 expected_output_file = self.source_dir / "expected_output" / "subdir" / "test.h"58 output_file = self.output_dir / "subdir" / "test.h"59 self.run_script(yaml_file, output_file)60 self.compare_files(output_file, expected_output_file)61 62 def test_custom_license_and_standards(self):63 yaml_file = self.source_dir / "input" / "custom.yaml"64 expected_output_file = self.source_dir / "expected_output" / "custom.h"65 output_file = self.output_dir / "custom.h"66 self.run_script(yaml_file, output_file)67 self.compare_files(output_file, expected_output_file)68 69 def test_generate_json(self):70 yaml_file = self.source_dir / "input/test_small.yaml"71 expected_output_file = self.source_dir / "expected_output/test_small.json"72 output_file = self.output_dir / "test_small.json"73 74 self.run_script(yaml_file, output_file, switches=["--json"])75 76 self.compare_files(output_file, expected_output_file)77 78 def test_sorting(self):79 yaml_file = self.source_dir / "input" / "sorting.yaml"80 expected_output_file = self.source_dir / "expected_output" / "sorting.h"81 output_file = self.output_dir / "sorting.h"82 self.run_script(yaml_file, output_file)83 self.compare_files(output_file, expected_output_file)84 85 86def main():87 parser = argparse.ArgumentParser(description="TestHeaderGenIntegration arguments")88 parser.add_argument(89 "--output_dir",90 type=Path,91 help="Output directory for generated headers",92 required=True,93 )94 args, remaining_argv = parser.parse_known_args()95 96 TestHeaderGenIntegration.output_dir = args.output_dir97 98 sys.argv[1:] = remaining_argv99 100 unittest.main()101 102 103if __name__ == "__main__":104 main()105