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 libear 8from . import make_args, check_call_and_report, create_empty_file 9import unittest 10 11import os 12import os.path 13import glob 14 15 16class OutputDirectoryTest(unittest.TestCase): 17 18 @staticmethod 19 def run_analyzer(outdir, args, cmd): 20 return check_call_and_report( 21 ['scan-build', '--intercept-first', '-o', outdir] + args, 22 cmd) 23 24 def test_regular_keeps_report_dir(self): 25 with libear.TemporaryDirectory() as tmpdir: 26 make = make_args(tmpdir) + ['build_regular'] 27 outdir = self.run_analyzer(tmpdir, [], make) 28 self.assertTrue(os.path.isdir(outdir)) 29 30 def test_clear_deletes_report_dir(self): 31 with libear.TemporaryDirectory() as tmpdir: 32 make = make_args(tmpdir) + ['build_clean'] 33 outdir = self.run_analyzer(tmpdir, [], make) 34 self.assertFalse(os.path.isdir(outdir)) 35 36 def test_clear_keeps_report_dir_when_asked(self): 37 with libear.TemporaryDirectory() as tmpdir: 38 make = make_args(tmpdir) + ['build_clean'] 39 outdir = self.run_analyzer(tmpdir, ['--keep-empty'], make) 40 self.assertTrue(os.path.isdir(outdir)) 41 42 43class RunAnalyzerTest(unittest.TestCase): 44 45 @staticmethod 46 def get_plist_count(directory): 47 return len(glob.glob(os.path.join(directory, 'report-*.plist'))) 48 49 def test_interposition_works(self): 50 with libear.TemporaryDirectory() as tmpdir: 51 make = make_args(tmpdir) + ['build_regular'] 52 outdir = check_call_and_report( 53 ['scan-build', '--plist', '-o', tmpdir, '--override-compiler'], 54 make) 55 56 self.assertTrue(os.path.isdir(outdir)) 57 self.assertEqual(self.get_plist_count(outdir), 5) 58 59 def test_intercept_wrapper_works(self): 60 with libear.TemporaryDirectory() as tmpdir: 61 make = make_args(tmpdir) + ['build_regular'] 62 outdir = check_call_and_report( 63 ['scan-build', '--plist', '-o', tmpdir, '--intercept-first', 64 '--override-compiler'], 65 make) 66 67 self.assertTrue(os.path.isdir(outdir)) 68 self.assertEqual(self.get_plist_count(outdir), 5) 69 70 def test_intercept_library_works(self): 71 with libear.TemporaryDirectory() as tmpdir: 72 make = make_args(tmpdir) + ['build_regular'] 73 outdir = check_call_and_report( 74 ['scan-build', '--plist', '-o', tmpdir, '--intercept-first'], 75 make) 76 77 self.assertTrue(os.path.isdir(outdir)) 78 self.assertEqual(self.get_plist_count(outdir), 5) 79 80 @staticmethod 81 def compile_empty_source_file(target_dir, is_cxx): 82 compiler = '$CXX' if is_cxx else '$CC' 83 src_file_name = 'test.cxx' if is_cxx else 'test.c' 84 src_file = os.path.join(target_dir, src_file_name) 85 obj_file = os.path.join(target_dir, 'test.o') 86 create_empty_file(src_file) 87 command = ' '.join([compiler, '-c', src_file, '-o', obj_file]) 88 return ['sh', '-c', command] 89 90 def test_interposition_cc_works(self): 91 with libear.TemporaryDirectory() as tmpdir: 92 outdir = check_call_and_report( 93 ['scan-build', '--plist', '-o', tmpdir, '--override-compiler'], 94 self.compile_empty_source_file(tmpdir, False)) 95 self.assertEqual(self.get_plist_count(outdir), 1) 96 97 def test_interposition_cxx_works(self): 98 with libear.TemporaryDirectory() as tmpdir: 99 outdir = check_call_and_report( 100 ['scan-build', '--plist', '-o', tmpdir, '--override-compiler'], 101 self.compile_empty_source_file(tmpdir, True)) 102 self.assertEqual(self.get_plist_count(outdir), 1) 103 104 def test_intercept_cc_works(self): 105 with libear.TemporaryDirectory() as tmpdir: 106 outdir = check_call_and_report( 107 ['scan-build', '--plist', '-o', tmpdir, '--override-compiler', 108 '--intercept-first'], 109 self.compile_empty_source_file(tmpdir, False)) 110 self.assertEqual(self.get_plist_count(outdir), 1) 111 112 def test_intercept_cxx_works(self): 113 with libear.TemporaryDirectory() as tmpdir: 114 outdir = check_call_and_report( 115 ['scan-build', '--plist', '-o', tmpdir, '--override-compiler', 116 '--intercept-first'], 117 self.compile_empty_source_file(tmpdir, True)) 118 self.assertEqual(self.get_plist_count(outdir), 1) 119