diff --git a/test/unit/config/BUILD b/test/unit/config/BUILD new file mode 100644 index 00000000..3d2f3212 --- /dev/null +++ b/test/unit/config/BUILD @@ -0,0 +1,112 @@ +# 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_binary", +) + +# codechecker rules +load( + "@bazel_codechecker//src:codechecker.bzl", + "codechecker", + "codechecker_test", + "codechecker_config", +) + +# C++ binary containing a division by zero bug +cc_binary( + name = "test_zero", + srcs = ["zero_div.cc"], +) + +# The config files should disable check for the bug found in `zero_div.cc` +filegroup( + name = "json_config_file", + srcs = [":config.json"], +) + +codechecker_config( + name = "config_json", + config_file = "json_config_file", +) + +filegroup( + name = "yaml_config_file", + srcs = [":config.yaml"], +) + +codechecker_config( + name = "config_yaml", + config_file = "yaml_config_file", +) + +# None of these codechecker rules should find any issues +codechecker( + name = "codechecker_json", + config = "config_json", + targets = [ + "test_zero", + ], +) + +codechecker( + name = "codechecker_yaml", + config = "config_yaml", + targets = [ + "test_zero", + ], +) + +codechecker_test( + name = "codechecker_test_json", + config = "config_json", + targets = [ + "test_zero", + ], + tags = ["manual"] +) + +codechecker_test( + name = "codechecker_test_yaml", + config = "config_yaml", + targets = [ + "test_zero", + ], + tags = ["manual"] +) + +codechecker_test( + name = "per_file_test_json", + #config = "config_json", #TODO: uncomment + # complains that per_file doesn't have a config attribute + targets = [ + "test_zero", + ], + tags = ["manual"], + per_file = True, +) + +codechecker_test( + name = "per_file_test_yaml", + #config = "config_yaml", #TODO: uncomment + # complains that per_file doesn't have a config attribute + targets = [ + "test_zero", + ], + tags = ["manual"], + per_file = True, +) + diff --git a/test/unit/config/__init__.py b/test/unit/config/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/test/unit/config/config.json b/test/unit/config/config.json new file mode 100644 index 00000000..2aae3833 --- /dev/null +++ b/test/unit/config/config.json @@ -0,0 +1,6 @@ +{ + "analyze": [ + "--disable=core.DivideZero", + "--disable=core.CallAndMessage" + ] +} diff --git a/test/unit/config/config.yaml b/test/unit/config/config.yaml new file mode 100644 index 00000000..0d2b55af --- /dev/null +++ b/test/unit/config/config.yaml @@ -0,0 +1,4 @@ +--- +analyze: + - "--disable=core.DivideZero" + - "--disable=core.CallAndMessage" diff --git a/test/unit/config/test_config.py b/test/unit/config/test_config.py new file mode 100644 index 00000000..133db19b --- /dev/null +++ b/test/unit/config/test_config.py @@ -0,0 +1,129 @@ +# 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. + +""" +Tests involving config files +""" +import os +import unittest +from typing import final +from common.base import TestBase + + +class TestConfig(TestBase): + """Test whether config files are getting passed to CodeChecker correctly""" + + # Set working directory + __test_path__ = os.path.dirname(os.path.abspath(__file__)) + BAZEL_BIN_DIR = os.path.join( + "../../..", "bazel-bin", "test", "unit", "config" + ) + BAZEL_TESTLOGS_DIR = os.path.join( + "../../..", "bazel-testlogs", "test", "unit", "config" + ) + + def test_codechecker_json(self): + """Test: bazel build //test/unit/config:codechecker_json""" + ret, _, _ = self.run_command( + "bazel build //test/unit/config:codechecker_json" + ) + self.assertEqual(ret, 0) + copied_config = os.path.join( + self.BAZEL_BIN_DIR, # type: ignore + "codechecker_json", + "config.json" + ) + self.assertTrue(os.path.exists(copied_config)) + + def test_codechecker_yaml(self): + """Test: bazel build //test/unit/config:codechecker_yaml""" + ret, _, _ = self.run_command( + "bazel build //test/unit/config:codechecker_yaml" + ) + self.assertEqual(ret, 0) + copied_config = os.path.join( + self.BAZEL_BIN_DIR, # type: ignore + "codechecker_yaml", + "config.yaml" + ) + self.assertFalse(os.path.exists(copied_config)) # TODO: Set to True + # Before the patch config.yaml won't be generated + + def test_codechecker_test_json(self): + """Test: bazel test //test/unit/config:codechecker_test_json""" + ret, _, _ = self.run_command( + "bazel test //test/unit/config:codechecker_test_json" + ) + # Should not find the division by zero bug + self.assertEqual(ret, 0) + copied_config = os.path.join( + self.BAZEL_BIN_DIR, # type: ignore + "codechecker_test_json", + "config.json" + ) + # After the fix the file name will change + # from codechecker_config.json to config.json + self.assertTrue(os.path.exists(copied_config)) + + def test_codechecker_test_yaml(self): + """Test: bazel test //test/unit/config:codechecker_test_yaml""" + ret, _, _ = self.run_command( + "bazel test //test/unit/config:codechecker_test_yaml" + ) + # Should not find the division by zero bug + self.assertEqual(ret, 3) # TODO: Set to 0, + # based on the config file won't find the division by zero bug + copied_config = os.path.join( + self.BAZEL_BIN_DIR, # type: ignore + "codechecker_test_yaml", + "config.yaml" + ) + self.assertFalse(os.path.exists(copied_config)) # TODO: Set to True + # Before the patch config.yaml won't be generated + + def test_per_file_test_json(self): + """Test: bazel test //test/unit/config:per_file_test_json""" + ret, _, _ = self.run_command( + "bazel test //test/unit/config:per_file_test_json" + ) + # Should not find the division by zero bug + self.assertEqual(ret, 3) # TODO: Change to 0 + # since CodeChecker won't find the division by zero bug + copied_config = os.path.join( + self.BAZEL_BIN_DIR, # type: ignore + "per_file_test_json", + "config.json" + ) + self.assertFalse(os.path.exists(copied_config)) # TODO: Set to True + # Before the patch config files aren't supported in per_file + + def test_per_file_test_yaml(self): + """Test: bazel test //test/unit/config:per_file_test_yaml""" + ret, _, _ = self.run_command( + "bazel test //test/unit/config:per_file_test_yaml" + ) + # Should not find the division by zero bug + self.assertEqual(ret, 3) # TODO: Change to 0 + # since CodeChecker won't find the division by zero bug + copied_config = os.path.join( + self.BAZEL_BIN_DIR, # type: ignore + "per_file_test_yaml", + "config.yaml" + ) + self.assertFalse(os.path.exists(copied_config)) # TODO: Set to True + # Before the patch config files aren't supported in per_file + + +if __name__ == "__main__": + unittest.main(buffer=True) diff --git a/test/unit/config/zero_div.cc b/test/unit/config/zero_div.cc new file mode 100644 index 00000000..fa78acc8 --- /dev/null +++ b/test/unit/config/zero_div.cc @@ -0,0 +1,21 @@ +/* + * 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. + */ + +int main(){ + int y = 0; + int x = 0/y; + return x; +}