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 from . import make_args
, check_call_and_report
, create_empty_file
15 class OutputDirectoryTest(unittest
.TestCase
):
17 def run_analyzer(outdir
, args
, cmd
):
18 return check_call_and_report(
19 ["scan-build-py", "--intercept-first", "-o", outdir
] + args
, cmd
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
))
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
))
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
))
41 class RunAnalyzerTest(unittest
.TestCase
):
43 def get_plist_count(directory
):
44 return len(glob
.glob(os
.path
.join(directory
, "report-*.plist")))
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
53 self
.assertTrue(os
.path
.isdir(outdir
))
54 self
.assertEqual(self
.get_plist_count(outdir
), 5)
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(
66 "--override-compiler",
71 self
.assertTrue(os
.path
.isdir(outdir
))
72 self
.assertEqual(self
.get_plist_count(outdir
), 5)
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
81 self
.assertTrue(os
.path
.isdir(outdir
))
82 self
.assertEqual(self
.get_plist_count(outdir
), 5)
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
]
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),
100 self
.assertEqual(self
.get_plist_count(outdir
), 1)
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),
108 self
.assertEqual(self
.get_plist_count(outdir
), 1)
110 def test_intercept_cc_works(self
):
111 with libear
.TemporaryDirectory() as tmpdir
:
112 outdir
= check_call_and_report(
118 "--override-compiler",
121 self
.compile_empty_source_file(tmpdir
, False),
123 self
.assertEqual(self
.get_plist_count(outdir
), 1)
125 def test_intercept_cxx_works(self
):
126 with libear
.TemporaryDirectory() as tmpdir
:
127 outdir
= check_call_and_report(
133 "--override-compiler",
136 self
.compile_empty_source_file(tmpdir
, True),
138 self
.assertEqual(self
.get_plist_count(outdir
), 1)