xref: /llvm-project/clang/tools/scan-build-py/tests/functional/cases/__init__.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 re
7import os.path
8import subprocess
9
10
11def load_tests(loader, suite, pattern):
12    from . import test_from_cdb
13
14    suite.addTests(loader.loadTestsFromModule(test_from_cdb))
15    from . import test_from_cmd
16
17    suite.addTests(loader.loadTestsFromModule(test_from_cmd))
18    from . import test_create_cdb
19
20    suite.addTests(loader.loadTestsFromModule(test_create_cdb))
21    from . import test_exec_anatomy
22
23    suite.addTests(loader.loadTestsFromModule(test_exec_anatomy))
24    return suite
25
26
27def make_args(target):
28    this_dir, _ = os.path.split(__file__)
29    path = os.path.abspath(os.path.join(this_dir, "..", "src"))
30    return [
31        "make",
32        "SRCDIR={}".format(path),
33        "OBJDIR={}".format(target),
34        "-f",
35        os.path.join(path, "build", "Makefile"),
36    ]
37
38
39def silent_call(cmd, *args, **kwargs):
40    kwargs.update({"stdout": subprocess.PIPE, "stderr": subprocess.STDOUT})
41    return subprocess.call(cmd, *args, **kwargs)
42
43
44def silent_check_call(cmd, *args, **kwargs):
45    kwargs.update({"stdout": subprocess.PIPE, "stderr": subprocess.STDOUT})
46    return subprocess.check_call(cmd, *args, **kwargs)
47
48
49def call_and_report(analyzer_cmd, build_cmd):
50    child = subprocess.Popen(
51        analyzer_cmd + ["-v"] + build_cmd,
52        universal_newlines=True,
53        stdout=subprocess.PIPE,
54        stderr=subprocess.STDOUT,
55    )
56
57    pattern = re.compile("Report directory created: (.+)")
58    directory = None
59    for line in child.stdout.readlines():
60        match = pattern.search(line)
61        if match and match.lastindex == 1:
62            directory = match.group(1)
63            break
64    child.stdout.close()
65    child.wait()
66
67    return (child.returncode, directory)
68
69
70def check_call_and_report(analyzer_cmd, build_cmd):
71    exit_code, result = call_and_report(analyzer_cmd, build_cmd)
72    if exit_code != 0:
73        raise subprocess.CalledProcessError(exit_code, analyzer_cmd + build_cmd, None)
74    else:
75        return result
76
77
78def create_empty_file(filename):
79    with open(filename, "a") as handle:
80        pass
81