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