From 81c0bf4e41fc81c8664024b4baf05ba83d59832e Mon Sep 17 00:00:00 2001 From: John Pennycook Date: Thu, 7 Mar 2024 08:14:38 -0800 Subject: [PATCH] Test for warning with empty compilation databases If a compilation database specifies files relative to a build directory (or otherwise points to paths that do not exist) then the resulting platform definition will be empty. We should ensure that we issue a warning in this case. Signed-off-by: John Pennycook --- tests/build-dir/test_build_dir.py | 33 ++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/tests/build-dir/test_build_dir.py b/tests/build-dir/test_build_dir.py index 663fe25..1020234 100644 --- a/tests/build-dir/test_build_dir.py +++ b/tests/build-dir/test_build_dir.py @@ -18,7 +18,7 @@ class TestBuildDirectories(unittest.TestCase): def setUp(self): self.rootdir = str(Path(__file__).parent) - logging.getLogger("codebasin").disabled = True + logging.getLogger("codebasin").disabled = False def test_absolute_paths(self): """ @@ -74,6 +74,37 @@ def test_absolute_paths(self): setmap = mapper.walk(state) self.assertDictEqual(setmap, expected_setmap, "Mismatch in setmap") + def test_empty_platform(self): + """ + Check that we warn if all files from a platform are excluded. + This may be a sign that the compilation database has incorrect paths. + """ + + source = str(Path(__file__).parent.joinpath("foo.cpp")) + + # CBI only understands how to load compilation databases from file. + # For now, create temporary files every time we test. + build = str(Path(__file__).parent.joinpath("build/")) + tmp = tempfile.NamedTemporaryFile() + obj = [ + { + "command": f"/usr/bin/c++ -o foo.cpp.o -c {source}", + "directory": f"{build}", + "file": "foo.cpp", + }, + ] + with open(tmp.name, "w") as f: + json.dump(obj, f) + + with self.assertLogs("codebasin", level="WARNING") as log: + config.load_database(tmp.name, self.rootdir) + + found_expected_warning = False + for msg in log.output: + if msg.find("No files found in compilation database"): + found_expected_warning = True + self.assertTrue(found_expected_warning) + if __name__ == "__main__": unittest.main()