1# -*- coding: utf-8 -*- 2# The LLVM Compiler Infrastructure 3# 4# This file is distributed under the University of Illinois Open Source 5# License. See LICENSE.TXT for details. 6 7import re 8import os.path 9import subprocess 10 11 12def load_tests(loader, suite, pattern): 13 from . import test_from_cdb 14 suite.addTests(loader.loadTestsFromModule(test_from_cdb)) 15 from . import test_from_cmd 16 suite.addTests(loader.loadTestsFromModule(test_from_cmd)) 17 from . import test_create_cdb 18 suite.addTests(loader.loadTestsFromModule(test_create_cdb)) 19 from . import test_exec_anatomy 20 suite.addTests(loader.loadTestsFromModule(test_exec_anatomy)) 21 return suite 22 23 24def make_args(target): 25 this_dir, _ = os.path.split(__file__) 26 path = os.path.normpath(os.path.join(this_dir, '..', 'src')) 27 return ['make', 'SRCDIR={}'.format(path), 'OBJDIR={}'.format(target), '-f', 28 os.path.join(path, 'build', 'Makefile')] 29 30 31def silent_call(cmd, *args, **kwargs): 32 kwargs.update({'stdout': subprocess.PIPE, 'stderr': subprocess.STDOUT}) 33 return subprocess.call(cmd, *args, **kwargs) 34 35 36def silent_check_call(cmd, *args, **kwargs): 37 kwargs.update({'stdout': subprocess.PIPE, 'stderr': subprocess.STDOUT}) 38 return subprocess.check_call(cmd, *args, **kwargs) 39 40 41def call_and_report(analyzer_cmd, build_cmd): 42 child = subprocess.Popen(analyzer_cmd + ['-v'] + build_cmd, 43 universal_newlines=True, 44 stdout=subprocess.PIPE, 45 stderr=subprocess.STDOUT) 46 47 pattern = re.compile('Report directory created: (.+)') 48 directory = None 49 for line in child.stdout.readlines(): 50 match = pattern.search(line) 51 if match and match.lastindex == 1: 52 directory = match.group(1) 53 break 54 child.stdout.close() 55 child.wait() 56 57 return (child.returncode, directory) 58 59 60def check_call_and_report(analyzer_cmd, build_cmd): 61 exit_code, result = call_and_report(analyzer_cmd, build_cmd) 62 if exit_code != 0: 63 raise subprocess.CalledProcessError( 64 exit_code, analyzer_cmd + build_cmd, None) 65 else: 66 return result 67 68 69def create_empty_file(filename): 70 with open(filename, 'a') as handle: 71 pass 72