Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
0f1f82e
Add caching test-case
furtib Aug 12, 2025
93b2a10
Add test script to folder
furtib Aug 13, 2025
14fb7d2
Create __init__.py
furtib Aug 13, 2025
d17ae02
Rename to fit with unittest standard
furtib Aug 15, 2025
f1ba2f5
Use common library
furtib Aug 15, 2025
4408c64
Updated to follow common, and to be less than 80 char long
furtib Aug 15, 2025
b06bbc3
Add comments
furtib Aug 15, 2025
7e01520
Update for common lib
furtib Aug 15, 2025
8ec5f4d
Update for working path
furtib Aug 15, 2025
0e01b02
Change to new path fix
furtib Aug 18, 2025
be93405
Format
furtib Aug 18, 2025
c4411be
Update comment
furtib Aug 18, 2025
15f2b42
Add license
furtib Aug 18, 2025
0b4607d
Update to follow common lib
furtib Aug 22, 2025
65bb6aa
Remove comments
furtib Aug 25, 2025
dd014f8
Add setUp and teardown to handle file changes
furtib Aug 25, 2025
fccaec3
Improve regex
furtib Aug 25, 2025
bca3cd7
Replace regex
furtib Aug 25, 2025
a8dc75c
Change cleanup placement
furtib Aug 25, 2025
50f54ad
Update test/unit/caching/test_caching.py
furtib Aug 25, 2025
76cff4f
Use python specific solutions where applicable
furtib Aug 25, 2025
4c5344e
Use only stderr
furtib Aug 25, 2025
8405a5e
Accept suggestion on test/unit/caching/test_caching.py
furtib Aug 25, 2025
b9065ba
Format
furtib Aug 25, 2025
65633a8
Remove unnecesary variable
furtib Aug 26, 2025
42a6ce2
Add test for CTU targets caching
furtib Aug 26, 2025
66387de
Fix name, add comments
furtib Aug 26, 2025
bf0adb1
Add manual tag
furtib Aug 26, 2025
66dbd59
Remove bug from source
furtib Aug 26, 2025
75082b5
Remove unnecesary comments and tags
furtib Aug 26, 2025
413ccc7
Add comment
furtib Aug 26, 2025
de98359
Update test/unit/caching/BUILD
Szelethus Aug 29, 2025
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
64 changes: 64 additions & 0 deletions test/unit/caching/BUILD
Original file line number Diff line number Diff line change
@@ -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_binary",
"cc_library",
)

load(
"@bazel_codechecker//src:code_checker.bzl",
"code_checker_test",
)

# We are not interested in finding bugs, we are only interested in whether the
# analysis re-runs after the files have been modified. As such, these files emit no
# warnings.

cc_library(
name = "linking",
hdrs = ["linking.h"],
)

cc_library(
name = "secondary",
srcs = ["secondary.cc"],
deps = ["linking"],
)

cc_binary(
name = "primary",
srcs = ["primary.cc"],
deps = [
"secondary",
"linking"
],
)

code_checker_test(
name = "code_checker_caching",
targets = [
"primary",
],
)

code_checker_test(
name = "code_checker_caching_ctu",
targets = [
"primary",
],
options = ["--ctu"],
)
1 change: 1 addition & 0 deletions test/unit/caching/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

18 changes: 18 additions & 0 deletions test/unit/caching/linking.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// 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 LINKING_H
#define LINKING_H
int foo();
#endif // LINKING_H
19 changes: 19 additions & 0 deletions test/unit/caching/primary.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// 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 "linking.h"

int main(){
return 1/foo();
}
19 changes: 19 additions & 0 deletions test/unit/caching/secondary.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// 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 "linking.h"

int foo(){
return 1;
}
105 changes: 105 additions & 0 deletions test/unit/caching/test_caching.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
# 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.

"""
Functional test, to check if caching is working correctly
"""
import unittest
import os
import shutil
from typing import final
from common.base import TestBase


class TestCaching(TestBase):
"""Caching tests"""

# Set working directory
__test_path__ = os.path.dirname(os.path.abspath(__file__))
BAZEL_BIN_DIR = os.path.join(
"../../..", "bazel-bin", "test", "unit", "caching"
)
BAZEL_TESTLOGS_DIR = os.path.join(
"../../..", "bazel-testlogs", "test", "unit", "caching"
)

@final
@classmethod
def setUpClass(cls):
"""Clean up before the test suite"""
super().setUpClass()
cls.run_command("bazel clean")

def setUp(self):
"""Before every test: clean Bazel cache"""
super().setUp()
os.mkdir("tmp")
shutil.copy("primary.cc", "tmp")
shutil.copy("secondary.cc", "tmp")
shutil.copy("linking.h", "tmp")
shutil.copy("BUILD", "tmp")

def tearDown(self):
"""Clean up working directory after every test"""
super().tearDown()
try:
shutil.rmtree("tmp")
except FileNotFoundError:
self.fail("Temporary working directory does not exists!")

def test_bazel_test_code_checker_caching(self):
"""
Test whether bazel correctly uses cached analysis
results for unchanged input files.
"""
target = "//test/unit/caching/tmp:code_checker_caching"
ret, _, _ = self.run_command(f"bazel build {target}")
self.assertEqual(ret, 0)
try:
with open("tmp/secondary.cc", "a", encoding="utf-8") as f:
f.write("//test")
except FileNotFoundError:
self.fail(f"File not found!")
ret, _, stderr = self.run_command(f"bazel build {target} --subcommands")
self.assertEqual(ret, 0)
# FIXME: This should be 1; 2 means that both .cpp files were reanalyzed
# despite only one of them being changed.
self.assertEqual(
stderr.count(f"SUBCOMMAND: # {target} [action 'CodeChecker"), 2
)

def test_bazel_test_code_checker_ctu_caching(self):
"""
Test whether bazel correctly reanalyses
the whole project when CTU is enabled
"""
target = "//test/unit/caching/tmp:code_checker_caching_ctu"
ret, _, _ = self.run_command(f"bazel build {target}")
self.assertEqual(ret, 0)
try:
with open("tmp/secondary.cc", "a", encoding="utf-8") as f:
f.write("//test")
except FileNotFoundError:
self.fail(f"File not found!")
ret, _, stderr = self.run_command(f"bazel build {target} --subcommands")
self.assertEqual(ret, 0)
# We expect both files to be reanalyzed, since there is no caching
# implemented for CTU analysis
self.assertEqual(
stderr.count(f"SUBCOMMAND: # {target} [action 'CodeChecker"), 2
)


if __name__ == "__main__":
unittest.main(buffer=True)