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
7 import libscanbuild
.clang
as sut
13 class ClangGetVersion(unittest
.TestCase
):
14 def test_get_version_is_not_empty(self
):
15 self
.assertTrue(sut
.get_version("clang"))
17 def test_get_version_throws(self
):
18 with self
.assertRaises(OSError):
19 sut
.get_version("notexists")
22 class 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
:
29 result
= sut
.get_arguments(
30 ["clang", "-c", filename
, "-DNDEBUG", '-Dvar="this is it"'], tmpdir
33 self
.assertTrue("NDEBUG" in result
)
34 self
.assertTrue('var="this is it"' in result
)
36 def test_get_clang_arguments_fails(self
):
37 with self
.assertRaises(Exception):
38 sut
.get_arguments(["clang", "-x", "c", "notexist.c"], ".")
40 def test_get_clang_arguments_fails_badly(self
):
41 with self
.assertRaises(OSError):
42 sut
.get_arguments(["notexist"], ".")
45 class 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]))
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
63 self
.assertEqual(str, type(value
))
65 def test_is_active(self
):
66 test
= sut
.is_active(["a", "b.b", "c.c.c"])
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"))
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"))
81 def test_parse_checkers(self
):
83 "OVERVIEW: Clang Static Analyzer Checkers List",
86 " checker.one Checker One description",
88 " Checker Two description",
90 result
= dict(sut
.parse_checkers(lines
))
91 self
.assertTrue("checker.one" in result
)
92 self
.assertEqual("Checker One description", result
.get("checker.one"))
93 self
.assertTrue("checker.two" in result
)
94 self
.assertEqual("Checker Two description", result
.get("checker.two"))
97 class ClangIsCtuCapableTest(unittest
.TestCase
):
98 def test_ctu_not_found(self
):
99 is_ctu
= sut
.is_ctu_capable("not-found-clang-extdef-mapping")
100 self
.assertFalse(is_ctu
)
103 class ClangGetTripleArchTest(unittest
.TestCase
):
104 def test_arch_is_not_empty(self
):
105 arch
= sut
.get_triple_arch(["clang", "-E", "-"], ".")
106 self
.assertTrue(len(arch
) > 0)