xref: /llvm-project/libc/utils/hdrgen/tests/test_integration.py (revision 6ad0dcf67f5dccdf8506ce5f51d793062a1c6879)
1*6ad0dcf6SRoland McGrathimport argparse
29abcca5eSRoland McGrathimport subprocess
3*6ad0dcf6SRoland McGrathimport sys
49abcca5eSRoland McGrathimport unittest
59abcca5eSRoland McGrathfrom pathlib import Path
69abcca5eSRoland McGrath
79abcca5eSRoland McGrath
89abcca5eSRoland McGrathclass TestHeaderGenIntegration(unittest.TestCase):
99abcca5eSRoland McGrath    def setUp(self):
10*6ad0dcf6SRoland McGrath        self.output_dir = TestHeaderGenIntegration.output_dir
11*6ad0dcf6SRoland McGrath        self.source_dir = Path(__file__).parent
12*6ad0dcf6SRoland McGrath        self.main_script = self.source_dir.parent / "main.py"
139abcca5eSRoland McGrath
14*6ad0dcf6SRoland McGrath    def run_script(self, yaml_file, output_file, entry_points):
159abcca5eSRoland McGrath        command = [
169abcca5eSRoland McGrath            "python3",
17*6ad0dcf6SRoland McGrath            str(self.main_script),
189abcca5eSRoland McGrath            str(yaml_file),
19*6ad0dcf6SRoland McGrath            "--output",
20*6ad0dcf6SRoland McGrath            str(output_file),
219abcca5eSRoland McGrath        ]
229abcca5eSRoland McGrath
239abcca5eSRoland McGrath        for entry_point in entry_points:
24*6ad0dcf6SRoland McGrath            command.extend(["--entry-point", entry_point])
259abcca5eSRoland McGrath
269abcca5eSRoland McGrath        result = subprocess.run(
279abcca5eSRoland McGrath            command,
289abcca5eSRoland McGrath            capture_output=True,
299abcca5eSRoland McGrath            text=True,
309abcca5eSRoland McGrath        )
319abcca5eSRoland McGrath
329abcca5eSRoland McGrath        print("STDOUT:", result.stdout)
339abcca5eSRoland McGrath        print("STDERR:", result.stderr)
349abcca5eSRoland McGrath        result.check_returncode()
359abcca5eSRoland McGrath
369abcca5eSRoland McGrath    def compare_files(self, generated_file, expected_file):
379abcca5eSRoland McGrath        with generated_file.open("r") as gen_file:
389abcca5eSRoland McGrath            gen_content = gen_file.read()
399abcca5eSRoland McGrath        with expected_file.open("r") as exp_file:
409abcca5eSRoland McGrath            exp_content = exp_file.read()
419abcca5eSRoland McGrath
429abcca5eSRoland McGrath        self.assertEqual(gen_content, exp_content)
439abcca5eSRoland McGrath
449abcca5eSRoland McGrath    def test_generate_header(self):
45*6ad0dcf6SRoland McGrath        yaml_file = self.source_dir / "input/test_small.yaml"
46*6ad0dcf6SRoland McGrath        expected_output_file = self.source_dir / "expected_output/test_header.h"
479abcca5eSRoland McGrath        output_file = self.output_dir / "test_small.h"
489abcca5eSRoland McGrath        entry_points = {"func_b", "func_a", "func_c", "func_d", "func_e"}
499abcca5eSRoland McGrath
50*6ad0dcf6SRoland McGrath        self.run_script(yaml_file, output_file, entry_points)
519abcca5eSRoland McGrath
529abcca5eSRoland McGrath        self.compare_files(output_file, expected_output_file)
539abcca5eSRoland McGrath
549abcca5eSRoland McGrath
55*6ad0dcf6SRoland McGrathdef main():
569abcca5eSRoland McGrath    parser = argparse.ArgumentParser(description="TestHeaderGenIntegration arguments")
579abcca5eSRoland McGrath    parser.add_argument(
58*6ad0dcf6SRoland McGrath        "--output_dir",
59*6ad0dcf6SRoland McGrath        type=Path,
60*6ad0dcf6SRoland McGrath        help="Output directory for generated headers",
61*6ad0dcf6SRoland McGrath        required=True,
629abcca5eSRoland McGrath    )
639abcca5eSRoland McGrath    args, remaining_argv = parser.parse_known_args()
649abcca5eSRoland McGrath
659abcca5eSRoland McGrath    TestHeaderGenIntegration.output_dir = args.output_dir
669abcca5eSRoland McGrath
679abcca5eSRoland McGrath    sys.argv[1:] = remaining_argv
689abcca5eSRoland McGrath
699abcca5eSRoland McGrath    unittest.main()
70*6ad0dcf6SRoland McGrath
71*6ad0dcf6SRoland McGrath
72*6ad0dcf6SRoland McGrathif __name__ == "__main__":
73*6ad0dcf6SRoland McGrath    main()
74