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(cmd,
17                                     cwd=target_dir,
18                                     stdout=subprocess.PIPE,
19                                     stderr=subprocess.STDOUT)
20
21    execute(['cmake', source_dir])
22    execute(['make'])
23
24    result_file = os.path.join(target_dir, 'result.json')
25    expected_file = os.path.join(target_dir, 'expected.json')
26    execute(['intercept-build', '--cdb', result_file, './exec',
27             expected_file])
28    return (expected_file, result_file)
29
30
31class ExecAnatomyTest(unittest.TestCase):
32    def assertEqualJson(self, expected, result):
33        def read_json(filename):
34            with open(filename) as handler:
35                return json.load(handler)
36
37        lhs = read_json(expected)
38        rhs = read_json(result)
39        for item in lhs:
40            self.assertTrue(rhs.count(item))
41        for item in rhs:
42            self.assertTrue(lhs.count(item))
43
44    def test_all_exec_calls(self):
45        this_dir, _ = os.path.split(__file__)
46        source_dir = os.path.abspath(os.path.join(this_dir, '..', 'exec'))
47        with libear.TemporaryDirectory() as tmp_dir:
48            expected, result = run(source_dir, tmp_dir)
49            self.assertEqualJson(expected, result)
50