Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
23 changes: 10 additions & 13 deletions web/client/codechecker_client/cli/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@
from codechecker_report_converter import twodim
from codechecker_report_converter.report import Report, report_file, \
reports as reports_helper, statistics as report_statistics
from codechecker_report_converter.report.hash import HashType, \
get_report_path_hash
from codechecker_report_converter.report.hash import get_report_path_hash
from codechecker_report_converter.report.parser.base import AnalyzerInfo

try:
Expand Down Expand Up @@ -421,17 +420,15 @@ def get_reports(
""" Get reports from the given analyzer result file. """
reports = report_file.get_reports(
analyzer_result_file_path, checker_labels)

# CppCheck generates a '0' value for the report hash. In case all of the
# reports in a result file contain only a hash with '0' value, overwrite
# the hash values in the report files with a context free hash value.
if all(r.report_hash == '0' for r in reports):
report_file.replace_report_hash(
analyzer_result_file_path, HashType.CONTEXT_FREE)

reports = report_file.get_reports(
analyzer_result_file_path, checker_labels)

# If CppCheck is ran natively (without CodeChecker), it generates a '0'
# value for the report hash by default. We used to correct this during the
# store operation, but we no longer accept plists that are not compliant
# with out plist specification in docs/tools/plist.md.
if reports and all(r.report_hash == '0' for r in reports):
LOG.error("All bug hashes are 0 in the report file, which is no "
"longer supported. Please re-analyze the project using "
"CodeChecker!")
sys.exit(1)
return reports


Expand Down
56 changes: 46 additions & 10 deletions web/tests/functional/cppcheck/test_cppcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ def setup_class(self):

def teardown_class(self):
"""Clean up after the test."""

# TODO: If environment variable is set keep the workspace
# and print out the path.
global TEST_WORKSPACE
Expand All @@ -78,14 +77,13 @@ def setup_method(self, _):

# Get the test workspace used to cppcheck tests.
self._test_workspace = os.environ['TEST_WORKSPACE']

test_class = self.__class__.__name__
print('Running ' + test_class + ' tests in ' + self._test_workspace)

self._test_cfg = env.import_test_cfg(self._test_workspace)

def test_cppcheck_report_storage(self):
""" In the stored report not the default zero hash should be used. """
""" Test storing a Codechecker generated, CppCheck analyzed plist. """

test_dir = os.path.dirname(os.path.realpath(__file__))

Expand All @@ -96,11 +94,17 @@ def test_cppcheck_report_storage(self):
# Copy report files to a temporary directory not to modify the
# files in the repository.
# Report files will be overwritten during the tests.
# Ignore the plist file generated by normal CppCheck run
# as it contains 0 Hashes which are not accepted,
temp_workspace = os.path.join(codechecker_cfg['workspace'],
'test_proj')
shutil.copytree(report_dir, temp_workspace)
'test_proj_code')

shutil.copytree(report_dir, temp_workspace,
ignore=shutil.ignore_patterns
('divide_zero_cppcheck.plist'))

report_file = os.path.join(temp_workspace, 'divide_zero.plist')
report_file = os.path.join(temp_workspace,
'divide_zero_codechecker.plist')
Comment thread
Szelethus marked this conversation as resolved.
Outdated
# Convert file paths to absolute in the report.
plist_test.prefix_file_path(report_file, temp_workspace)

Expand All @@ -111,7 +115,7 @@ def test_cppcheck_report_storage(self):
temp_workspace]

out = subprocess.check_output(
store_cmd, encoding="utf-8", errors="ignore")
store_cmd, encoding="utf-8")
print(out)
query_cmd = [env.codechecker_cmd(), 'cmd', 'results', run_name,
# Use the 'Default' product.
Expand All @@ -121,11 +125,43 @@ def test_cppcheck_report_storage(self):
query_cmd, encoding="utf-8", errors="ignore")
print(out)
reports = json.loads(out)
self.assertEqual(len(reports), 5)
self.assertEqual(len(reports), 2)
for report in reports:
# The stored hash should not be "0".
self.assertNotEqual(report["bugHash"], "0")
# The stored checker name should not be the fake(d) default that
# was created because no 'metadata.json' (and thus no checker
# list) exists for this "project".
self.assertNotEqual(report["checkerId"], "__FAKE__")

def test_cppcheck_0_hash(self):
""" Test storing a CppCheck generated plist that contains 0 as Hash"""

test_dir = os.path.dirname(os.path.realpath(__file__))

report_dir = os.path.join(test_dir, 'test_proj')

codechecker_cfg = self._test_cfg['codechecker_cfg']

# Copy report files to a temporary directory not to modify the
# files in the repository.
# Report files will be overwritten during the tests.
temp_workspace = os.path.join(codechecker_cfg['workspace'],
'test_proj_cpp')

shutil.copytree(report_dir, temp_workspace)

report_file = os.path.join(temp_workspace,
'divide_zero_cppcheck.plist')
# Convert file paths to absolute in the report.
plist_test.prefix_file_path(report_file, temp_workspace)

run_name = 'cppcheck'
store_cmd = [env.codechecker_cmd(), 'store', '--name', run_name,
# Use the 'Default' product.
'--url', env.parts_to_url(codechecker_cfg),
temp_workspace]
# As the plist file contains 0 as Hash the expected
# behaviour is to terminate with error code 1.
result = subprocess.run(
store_cmd, encoding="utf-8",
errors="ignore", check=False)
self.assertEqual(result.returncode, 1)
Comment thread
Szelethus marked this conversation as resolved.
4 changes: 2 additions & 2 deletions web/tests/functional/cppcheck/test_proj/divide_zero.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ void test1(int z) {
int x = 1 / z; // warn
}

void test2{
void test2(){
int x = 1;
div(x)
div(x);
}
Loading
Loading