xref: /llvm-project/clang/tools/scan-build-py/tests/functional/cases/test_exec_anatomy.py (revision dd3c26a045c081620375a878159f536758baba6e)
1# -*- coding: utf-8 -*-
2# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
3# See https://llvm.org/LICENSE.txt for license information.
4# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5
6import libear
7import unittest
8
9import os.path
10import subprocess
11import json
12
13
14def run(source_dir, target_dir):
15    def execute(cmd):
16        return subprocess.check_call(
17            cmd, cwd=target_dir, stdout=subprocess.PIPE, stderr=subprocess.STDOUT
18        )
19
20    execute(["cmake", source_dir])
21    execute(["make"])
22
23    result_file = os.path.join(target_dir, "result.json")
24    expected_file = os.path.join(target_dir, "expected.json")
25    execute(["intercept-build", "--cdb", result_file, "./exec", expected_file])
26    return (expected_file, result_file)
27
28
29class ExecAnatomyTest(unittest.TestCase):
30    def assertEqualJson(self, expected, result):
31        def read_json(filename):
32            with open(filename) as handler:
33                return json.load(handler)
34
35        lhs = read_json(expected)
36        rhs = read_json(result)
37        for item in lhs:
38            self.assertTrue(rhs.count(item))
39        for item in rhs:
40            self.assertTrue(lhs.count(item))
41
42    def test_all_exec_calls(self):
43        this_dir, _ = os.path.split(__file__)
44        source_dir = os.path.abspath(os.path.join(this_dir, "..", "exec"))
45        with libear.TemporaryDirectory() as tmp_dir:
46            expected, result = run(source_dir, tmp_dir)
47            self.assertEqualJson(expected, result)
48