diff --git a/test/unit/virtual_include/BUILD b/test/unit/virtual_include/BUILD new file mode 100644 index 00000000..a7395156 --- /dev/null +++ b/test/unit/virtual_include/BUILD @@ -0,0 +1,64 @@ +# Copyright 2023 Ericsson AB +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# cc_binary for simple C++ tests +load( + "@rules_cc//cc:defs.bzl", + "cc_library", +) + +# codechecker rules +load( + "@bazel_codechecker//src:codechecker.bzl", + "codechecker_test", +) + +load( + "@bazel_codechecker//src:code_checker.bzl", + "code_checker_test", +) + +# Test for strip_include_prefix +cc_library( + name = "test_inc", + hdrs = glob(["inc/*.h"]), + strip_include_prefix = "inc", +) + +cc_library( + name = "virtual_include", + srcs = ["source.cc"], + deps = ["test_inc"], +) + +# Simplest virtual include resolution test +codechecker_test( + name = "codechecker_virtual_include", + tags = [ + "manual", + ], + targets = [ + "virtual_include", + ], +) + +code_checker_test( + name = "code_checker_virtual_include", + tags = [ + "manual", + ], + targets = [ + "virtual_include", + ] +) diff --git a/test/unit/virtual_include/__init__.py b/test/unit/virtual_include/__init__.py new file mode 100644 index 00000000..78bab5f1 --- /dev/null +++ b/test/unit/virtual_include/__init__.py @@ -0,0 +1,13 @@ +# Copyright 2023 Ericsson AB +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. diff --git a/test/unit/virtual_include/inc/zeroDiv.h b/test/unit/virtual_include/inc/zeroDiv.h new file mode 100644 index 00000000..36dc565d --- /dev/null +++ b/test/unit/virtual_include/inc/zeroDiv.h @@ -0,0 +1,22 @@ +// Copyright 2023 Ericsson AB +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef ZERODIV_H +#define ZERODIV_H +int warnMe(int i){ + if (i == 0) + return 1/i; +} +#endif //ZERODIV_H + diff --git a/test/unit/virtual_include/source.cc b/test/unit/virtual_include/source.cc new file mode 100644 index 00000000..f8ea427f --- /dev/null +++ b/test/unit/virtual_include/source.cc @@ -0,0 +1,20 @@ +// Copyright 2023 Ericsson AB +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "zeroDiv.h" + +int main(){ + warnMe(0); + return 0; +} diff --git a/test/unit/virtual_include/test_virtual_include.py b/test/unit/virtual_include/test_virtual_include.py new file mode 100644 index 00000000..b6671d37 --- /dev/null +++ b/test/unit/virtual_include/test_virtual_include.py @@ -0,0 +1,113 @@ +# Copyright 2023 Ericsson AB +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +""" +We want CodeChecker to point to the original files in its results, this needs +post processing. +Bazel creates _virtual_includes folder for headers, declared in a cc_library +rule with the include_prefix or strip_include_prefix. When warnings are found +in these headers, their paths in the plist files should get resolved to the +original file path. +This unittest test whether these paths containing `_virtual_include` have been +resolved +""" +import logging +import os +import re +import unittest +import glob +from typing import final +from common.base import TestBase + + +class TestVirtualInclude(TestBase): + """Tests checking virtual include path resolution""" + + # Set working directory + __test_path__ = os.path.dirname(os.path.abspath(__file__)) + BAZEL_BIN_DIR = os.path.join( + "../../..", "bazel-bin", "test", "unit", "virtual_include" + ) + BAZEL_TESTLOGS_DIR = os.path.join( + "../../..", "bazel-testlogs", "test", "unit", "virtual_include" + ) + + @final + @classmethod + def setUpClass(cls): + """Set up before the test suite""" + super().setUpClass() + cls.run_command("bazel clean") + + def contains_in_files(self, regex, folder_path): + result = [] + for file in folder_path: + logging.debug(f"Checking file: {file}") + if self.contains_regex_in_file: + result.append(file) + return result + + def test_bazel_code_checker_plist_path_resolved(self): + """Test: bazel build :code_checker_virtual_include""" + ret, _, _ = self.run_command( + "bazel build //test/unit/virtual_include:code_checker_virtual_include", + ) + self.assertEqual(ret, 0) + plist_files = glob.glob( + os.path.join( + self.BAZEL_BIN_DIR, + "code_checker_virtual_include", + "**", + "*.plist", + ), + recursive=True, + ) + # Test whether the _virtual_include directory was actually created. + self.assertTrue( + os.path.isdir(f"{self.BAZEL_BIN_DIR}/_virtual_includes") + ) + # FIXME: In the postprocessed plists, all _virtual_include paths + # should've been removed. Possible fix is in the github PR #14. + self.assertNotEqual( + self.contains_in_files(r"/_virtual_includes/", plist_files), [] + ) + + def test_bazel_codechecker_plist_path_resolved(self): + """Test: bazel build :codechecker_virtual_include""" + ret, _, _ = self.run_command( + "bazel build //test/unit/virtual_include:codechecker_virtual_include" + ) + self.assertEqual(ret, 0) + plist_files = glob.glob( + os.path.join( + self.BAZEL_BIN_DIR, + "codechecker_virtual_include", + "**", + "*.plist", + ), + recursive=True, + ) + # Test whether the _virtual_include directory was actually created. + self.assertTrue( + os.path.isdir(f"{self.BAZEL_BIN_DIR}/_virtual_includes") + ) + # FIXME: In the postprocessed plists, all _virtual_include paths + # should've been removed. Possible fix is in the github PR #14. + self.assertNotEqual( + self.contains_in_files(r"/_virtual_includes/", plist_files), [] + ) + + +if __name__ == "__main__": + unittest.main(buffer=True)