Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 112 additions & 0 deletions test/unit/config/BUILD
Original file line number Diff line number Diff line change
@@ -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,
)

Empty file added test/unit/config/__init__.py
Empty file.
6 changes: 6 additions & 0 deletions test/unit/config/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"analyze": [
"--disable=core.DivideZero",
"--disable=core.CallAndMessage"
]
}
4 changes: 4 additions & 0 deletions test/unit/config/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
analyze:
- "--disable=core.DivideZero"
- "--disable=core.CallAndMessage"
129 changes: 129 additions & 0 deletions test/unit/config/test_config.py
Original file line number Diff line number Diff line change
@@ -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)
21 changes: 21 additions & 0 deletions test/unit/config/zero_div.cc
Original file line number Diff line number Diff line change
@@ -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;
}