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