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 7import libscanbuild.clang as sut 8import unittest 9import os.path 10import sys 11 12 13class ClangGetVersion(unittest.TestCase): 14 def test_get_version_is_not_empty(self): 15 self.assertTrue(sut.get_version('clang')) 16 17 def test_get_version_throws(self): 18 with self.assertRaises(OSError): 19 sut.get_version('notexists') 20 21 22class ClangGetArgumentsTest(unittest.TestCase): 23 def test_get_clang_arguments(self): 24 with libear.TemporaryDirectory() as tmpdir: 25 filename = os.path.join(tmpdir, 'test.c') 26 with open(filename, 'w') as handle: 27 handle.write('') 28 29 result = sut.get_arguments( 30 ['clang', '-c', filename, '-DNDEBUG', '-Dvar="this is it"'], 31 tmpdir) 32 33 self.assertTrue('NDEBUG' in result) 34 self.assertTrue('var="this is it"' in result) 35 36 def test_get_clang_arguments_fails(self): 37 with self.assertRaises(Exception): 38 sut.get_arguments(['clang', '-x', 'c', 'notexist.c'], '.') 39 40 def test_get_clang_arguments_fails_badly(self): 41 with self.assertRaises(OSError): 42 sut.get_arguments(['notexist'], '.') 43 44 45class ClangGetCheckersTest(unittest.TestCase): 46 def test_get_checkers(self): 47 # this test is only to see is not crashing 48 result = sut.get_checkers('clang', []) 49 self.assertTrue(len(result)) 50 # do check result types 51 string_type = unicode if sys.version_info < (3,) else str 52 for key, value in result.items(): 53 self.assertEqual(string_type, type(key)) 54 self.assertEqual(string_type, type(value[0])) 55 self.assertEqual(bool, type(value[1])) 56 57 def test_get_active_checkers(self): 58 # this test is only to see is not crashing 59 result = sut.get_active_checkers('clang', []) 60 self.assertTrue(len(result)) 61 # do check result types 62 for value in result: 63 self.assertEqual(str, type(value)) 64 65 def test_is_active(self): 66 test = sut.is_active(['a', 'b.b', 'c.c.c']) 67 68 self.assertTrue(test('a')) 69 self.assertTrue(test('a.b')) 70 self.assertTrue(test('b.b')) 71 self.assertTrue(test('b.b.c')) 72 self.assertTrue(test('c.c.c.p')) 73 74 self.assertFalse(test('ab')) 75 self.assertFalse(test('ba')) 76 self.assertFalse(test('bb')) 77 self.assertFalse(test('c.c')) 78 self.assertFalse(test('b')) 79 self.assertFalse(test('d')) 80 81 def test_parse_checkers(self): 82 lines = [ 83 'OVERVIEW: Clang Static Analyzer Checkers List', 84 '', 85 'CHECKERS:', 86 ' checker.one Checker One description', 87 ' checker.two', 88 ' Checker Two description'] 89 result = dict(sut.parse_checkers(lines)) 90 self.assertTrue('checker.one' in result) 91 self.assertEqual('Checker One description', result.get('checker.one')) 92 self.assertTrue('checker.two' in result) 93 self.assertEqual('Checker Two description', result.get('checker.two')) 94 95 96class ClangIsCtuCapableTest(unittest.TestCase): 97 def test_ctu_not_found(self): 98 is_ctu = sut.is_ctu_capable('not-found-clang-extdef-mapping') 99 self.assertFalse(is_ctu) 100 101 102class ClangGetTripleArchTest(unittest.TestCase): 103 def test_arch_is_not_empty(self): 104 arch = sut.get_triple_arch(['clang', '-E', '-'], '.') 105 self.assertTrue(len(arch) > 0) 106