Skip to content

Commit

Permalink
[server][fix] trim path prefix at store
Browse files Browse the repository at this point in the history
The trim path prefix feature was broken during a previous
refactoring this change fixes it and adds an additional functional
test to detect if the feature is broken.

Smaller refactoring was done to be more explicit
which filepath is used where.
  • Loading branch information
Gyorgy Orban committed Nov 6, 2020
1 parent 0a94cb2 commit a95a6d6
Show file tree
Hide file tree
Showing 8 changed files with 733 additions and 13 deletions.
2 changes: 1 addition & 1 deletion codechecker_common/tests/unit/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

CURRENT_DIR = $(shell pwd)
# Root of the repository.
REPO_ROOT = $(CURRENT_DIR)/../../../
REPO_ROOT ?= REPO_ROOT=$(CURRENT_DIR)/../../../

# Nose test runner configuration options.
NOSECFG = --config .noserc
Expand Down
27 changes: 15 additions & 12 deletions web/server/codechecker_server/api/report_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -2509,7 +2509,7 @@ def __store_source_files(self, source_root, filename_to_hash,
# have the content. Let's check if we already have a file
# record in the database or we need to add one.

LOG.debug(file_name + ' not found or already stored.')
LOG.debug('%s not found or already stored.', trimmed_file_path)
with DBSession(self.__Session) as session:
fid = store_handler.addFileRecord(session,
trimmed_file_path,
Expand All @@ -2518,12 +2518,12 @@ def __store_source_files(self, source_root, filename_to_hash,
LOG.error("File ID for %s is not found in the DB with "
"content hash %s. Missing from ZIP?",
source_file_name, file_hash)
file_path_to_id[file_name] = fid
file_path_to_id[trimmed_file_path] = fid
LOG.debug("%d fileid found", fid)
continue

with DBSession(self.__Session) as session:
file_path_to_id[file_name] = \
file_path_to_id[trimmed_file_path] = \
store_handler.addFileContent(session,
trimmed_file_path,
source_file_name,
Expand Down Expand Up @@ -2609,15 +2609,17 @@ def get_analyzer_name(report):
except Exception as ex:
LOG.error('Parsing the plist failed: %s', str(ex))
continue

trimmed_files = {}
file_ids = {}
if reports:
missing_ids_for_files = []

for file_name in files.values():
for k, v in files.items():
trimmed_files[k] = \
util.trim_path_prefixes(v, trim_path_prefixes)

for file_name in trimmed_files.values():

file_name = util.trim_path_prefixes(file_name,
trim_path_prefixes)
file_id = file_path_to_id.get(file_name, -1)
if file_id == -1:
missing_ids_for_files.append(file_name)
Expand All @@ -2627,22 +2629,22 @@ def get_analyzer_name(report):

if missing_ids_for_files:
LOG.error("Failed to get file path id for '%s'!",
file_name)
' '.join(missing_ids_for_files))
continue

# Store report.
for report in reports:
checker_name = report.main['check_name']
all_report_checkers.add(checker_name)

source_file = util.trim_path_prefixes(
report.main['location']['file'], trim_path_prefixes)
report.trim_path_prefixes(trim_path_prefixes)
source_file = report.file_path

if skip_handler.should_skip(source_file):
continue
bug_paths, bug_events, bug_extended_data = \
store_handler.collect_paths_events(report, file_ids,
files)
trimmed_files)
report_path_hash = get_report_path_hash(report)
if report_path_hash in already_added:
LOG.debug('Not storing report. Already added')
Expand Down Expand Up @@ -2683,7 +2685,8 @@ def get_analyzer_name(report):
already_added.add(report_path_hash)

last_report_event = report.bug_path[-1]
file_name = files[last_report_event['location']['file']]
file_name = \
trimmed_files[last_report_event['location']['file']]
source_file_name = os.path.realpath(
os.path.join(source_root, file_name.strip("/")))

Expand Down
68 changes: 68 additions & 0 deletions web/tests/functional/store/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# -------------------------------------------------------------------------
#
# Part of the CodeChecker project, under the Apache License v2.0 with
# LLVM Exceptions. See LICENSE for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#
# -------------------------------------------------------------------------

"""Setup for the package tests."""


import multiprocessing
import os
import shutil

from libtest import codechecker
from libtest import env

# Test workspace initialized at setup for report storage tests.
TEST_WORKSPACE = None


def setup_package():
"""Setup the environment for the tests then start the server."""

global TEST_WORKSPACE
TEST_WORKSPACE = env.get_workspace('store_test')

os.environ['TEST_WORKSPACE'] = TEST_WORKSPACE

# Configuration options.
codechecker_cfg = {
'suppress_file': None,
'skip_list_file': None,
'check_env': env.test_env(TEST_WORKSPACE),
'workspace': TEST_WORKSPACE,
'checkers': [],
'reportdir': os.path.join(TEST_WORKSPACE, 'reports'),
'test_project': 'store_test'
}

# Start or connect to the running CodeChecker server and get connection
# details.
print("This test uses a CodeChecker server... connecting...")
server_access = codechecker.start_or_get_server()
server_access['viewer_product'] = 'store_test'
codechecker.add_test_package_product(server_access, TEST_WORKSPACE)

# Extend the checker configuration with the server access.
codechecker_cfg.update(server_access)

# Export the test configuration to the workspace.
env.export_test_cfg(TEST_WORKSPACE, {'codechecker_cfg': codechecker_cfg})


def teardown_package():
"""Clean up after the test."""

# TODO: If environment variable is set keep the workspace
# and print out the path.
global TEST_WORKSPACE

check_env = env.import_test_cfg(TEST_WORKSPACE)[
'codechecker_cfg']['check_env']
codechecker.remove_test_package_product(TEST_WORKSPACE, check_env)

print("Removing: " + TEST_WORKSPACE)
shutil.rmtree(TEST_WORKSPACE)
15 changes: 15 additions & 0 deletions web/tests/functional/store/test_proj/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Makefile to build and analyze the test files required
# for the store update tests.

build:
$(CXX) divide_zero.cpp -o divide_zero

clean:
rm -rf divide_zero

# Using relative path to the source files
# it is easier to prefix them with the temporary
# directory during test preparation.
analyze:
clang --analyze -I. divide_zero.cpp --analyzer-output plist-multi-file \
-o divide_zero.plist
19 changes: 19 additions & 0 deletions web/tests/functional/store/test_proj/divide_zero.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// -------------------------------------------------------------------------
// Part of the CodeChecker project, under the Apache License v2.0 with
// LLVM Exceptions. See LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// -------------------------------------------------------------------------

// core.DivideZero (C, C++, ObjC)
// Check for division by zero.
#include "lib.h"

void test1(int z) {
if (z == 0)
int x = 1 / z; // warn
}

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

0 comments on commit a95a6d6

Please sign in to comment.