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