Skip to content

Commit 2e2520d

Browse files
authored
Create Test case for config files (#103)
Why: We want to support config files; we should test for them. What: - Test whether every rule that takes a config file as an argument acts on the content of said config file Addresses: #30 Test for #102
1 parent 8157d8f commit 2e2520d

6 files changed

Lines changed: 272 additions & 0 deletions

File tree

test/unit/config/BUILD

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
# Copyright 2023 Ericsson AB
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# cc_binary for simple C++ tests
16+
load(
17+
"@rules_cc//cc:defs.bzl",
18+
"cc_binary",
19+
)
20+
21+
# codechecker rules
22+
load(
23+
"@bazel_codechecker//src:codechecker.bzl",
24+
"codechecker",
25+
"codechecker_test",
26+
"codechecker_config",
27+
)
28+
29+
# C++ binary containing a division by zero bug
30+
cc_binary(
31+
name = "test_zero",
32+
srcs = ["zero_div.cc"],
33+
)
34+
35+
# The config files should disable check for the bug found in `zero_div.cc`
36+
filegroup(
37+
name = "json_config_file",
38+
srcs = [":config.json"],
39+
)
40+
41+
codechecker_config(
42+
name = "config_json",
43+
config_file = "json_config_file",
44+
)
45+
46+
filegroup(
47+
name = "yaml_config_file",
48+
srcs = [":config.yaml"],
49+
)
50+
51+
codechecker_config(
52+
name = "config_yaml",
53+
config_file = "yaml_config_file",
54+
)
55+
56+
# None of these codechecker rules should find any issues
57+
codechecker(
58+
name = "codechecker_json",
59+
config = "config_json",
60+
targets = [
61+
"test_zero",
62+
],
63+
)
64+
65+
codechecker(
66+
name = "codechecker_yaml",
67+
config = "config_yaml",
68+
targets = [
69+
"test_zero",
70+
],
71+
)
72+
73+
codechecker_test(
74+
name = "codechecker_test_json",
75+
config = "config_json",
76+
targets = [
77+
"test_zero",
78+
],
79+
tags = ["manual"]
80+
)
81+
82+
codechecker_test(
83+
name = "codechecker_test_yaml",
84+
config = "config_yaml",
85+
targets = [
86+
"test_zero",
87+
],
88+
tags = ["manual"]
89+
)
90+
91+
codechecker_test(
92+
name = "per_file_test_json",
93+
#config = "config_json", #TODO: uncomment
94+
# complains that per_file doesn't have a config attribute
95+
targets = [
96+
"test_zero",
97+
],
98+
tags = ["manual"],
99+
per_file = True,
100+
)
101+
102+
codechecker_test(
103+
name = "per_file_test_yaml",
104+
#config = "config_yaml", #TODO: uncomment
105+
# complains that per_file doesn't have a config attribute
106+
targets = [
107+
"test_zero",
108+
],
109+
tags = ["manual"],
110+
per_file = True,
111+
)
112+

test/unit/config/__init__.py

Whitespace-only changes.

test/unit/config/config.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"analyze": [
3+
"--disable=core.DivideZero",
4+
"--disable=core.CallAndMessage"
5+
]
6+
}

test/unit/config/config.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
analyze:
3+
- "--disable=core.DivideZero"
4+
- "--disable=core.CallAndMessage"

test/unit/config/test_config.py

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
# Copyright 2023 Ericsson AB
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""
16+
Tests involving config files
17+
"""
18+
import os
19+
import unittest
20+
from typing import final
21+
from common.base import TestBase
22+
23+
24+
class TestConfig(TestBase):
25+
"""Test whether config files are getting passed to CodeChecker correctly"""
26+
27+
# Set working directory
28+
__test_path__ = os.path.dirname(os.path.abspath(__file__))
29+
BAZEL_BIN_DIR = os.path.join(
30+
"../../..", "bazel-bin", "test", "unit", "config"
31+
)
32+
BAZEL_TESTLOGS_DIR = os.path.join(
33+
"../../..", "bazel-testlogs", "test", "unit", "config"
34+
)
35+
36+
def test_codechecker_json(self):
37+
"""Test: bazel build //test/unit/config:codechecker_json"""
38+
ret, _, _ = self.run_command(
39+
"bazel build //test/unit/config:codechecker_json"
40+
)
41+
self.assertEqual(ret, 0)
42+
copied_config = os.path.join(
43+
self.BAZEL_BIN_DIR, # type: ignore
44+
"codechecker_json",
45+
"config.json"
46+
)
47+
self.assertTrue(os.path.exists(copied_config))
48+
49+
def test_codechecker_yaml(self):
50+
"""Test: bazel build //test/unit/config:codechecker_yaml"""
51+
ret, _, _ = self.run_command(
52+
"bazel build //test/unit/config:codechecker_yaml"
53+
)
54+
self.assertEqual(ret, 0)
55+
copied_config = os.path.join(
56+
self.BAZEL_BIN_DIR, # type: ignore
57+
"codechecker_yaml",
58+
"config.yaml"
59+
)
60+
self.assertFalse(os.path.exists(copied_config)) # TODO: Set to True
61+
# Before the patch config.yaml won't be generated
62+
63+
def test_codechecker_test_json(self):
64+
"""Test: bazel test //test/unit/config:codechecker_test_json"""
65+
ret, _, _ = self.run_command(
66+
"bazel test //test/unit/config:codechecker_test_json"
67+
)
68+
# Should not find the division by zero bug
69+
self.assertEqual(ret, 0)
70+
copied_config = os.path.join(
71+
self.BAZEL_BIN_DIR, # type: ignore
72+
"codechecker_test_json",
73+
"config.json"
74+
)
75+
# After the fix the file name will change
76+
# from codechecker_config.json to config.json
77+
self.assertTrue(os.path.exists(copied_config))
78+
79+
def test_codechecker_test_yaml(self):
80+
"""Test: bazel test //test/unit/config:codechecker_test_yaml"""
81+
ret, _, _ = self.run_command(
82+
"bazel test //test/unit/config:codechecker_test_yaml"
83+
)
84+
# Should not find the division by zero bug
85+
self.assertEqual(ret, 3) # TODO: Set to 0,
86+
# based on the config file won't find the division by zero bug
87+
copied_config = os.path.join(
88+
self.BAZEL_BIN_DIR, # type: ignore
89+
"codechecker_test_yaml",
90+
"config.yaml"
91+
)
92+
self.assertFalse(os.path.exists(copied_config)) # TODO: Set to True
93+
# Before the patch config.yaml won't be generated
94+
95+
def test_per_file_test_json(self):
96+
"""Test: bazel test //test/unit/config:per_file_test_json"""
97+
ret, _, _ = self.run_command(
98+
"bazel test //test/unit/config:per_file_test_json"
99+
)
100+
# Should not find the division by zero bug
101+
self.assertEqual(ret, 3) # TODO: Change to 0
102+
# since CodeChecker won't find the division by zero bug
103+
copied_config = os.path.join(
104+
self.BAZEL_BIN_DIR, # type: ignore
105+
"per_file_test_json",
106+
"config.json"
107+
)
108+
self.assertFalse(os.path.exists(copied_config)) # TODO: Set to True
109+
# Before the patch config files aren't supported in per_file
110+
111+
def test_per_file_test_yaml(self):
112+
"""Test: bazel test //test/unit/config:per_file_test_yaml"""
113+
ret, _, _ = self.run_command(
114+
"bazel test //test/unit/config:per_file_test_yaml"
115+
)
116+
# Should not find the division by zero bug
117+
self.assertEqual(ret, 3) # TODO: Change to 0
118+
# since CodeChecker won't find the division by zero bug
119+
copied_config = os.path.join(
120+
self.BAZEL_BIN_DIR, # type: ignore
121+
"per_file_test_yaml",
122+
"config.yaml"
123+
)
124+
self.assertFalse(os.path.exists(copied_config)) # TODO: Set to True
125+
# Before the patch config files aren't supported in per_file
126+
127+
128+
if __name__ == "__main__":
129+
unittest.main(buffer=True)

test/unit/config/zero_div.cc

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright 2023 Ericsson AB
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
int main(){
18+
int y = 0;
19+
int x = 0/y;
20+
return x;
21+
}

0 commit comments

Comments
 (0)