Skip to content

Commit

Permalink
Add new test for folder prefixed source file skips
Browse files Browse the repository at this point in the history
  • Loading branch information
vodorok committed Mar 14, 2024
1 parent fe9c303 commit 37d4426
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 41 deletions.
13 changes: 13 additions & 0 deletions analyzer/tests/functional/skip/test_files/multidir/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
default: src/a.o src/b.o

src/b.o: src/b.cpp
$(CXX) -c src/b.cpp -o /dev/null

src/a.o: src/a.cpp lib/lib.o
$(CXX) -c src/a.cpp -o /dev/null

lib/lib.o: lib/lib.cpp
$(CXX) -c lib/lib.cpp -o /dev/null

clean:
rm -rf src/*.o lib/*.o
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef LIB_H
#define LIB_H

int myDiv(int x);

#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "../include/lib.h"

int myDiv(int x)
{
return 1 / x;
}
5 changes: 5 additions & 0 deletions analyzer/tests/functional/skip/test_files/multidir/src/a.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include "../include/lib.h"

void foo() {
myDiv(0);
}
3 changes: 3 additions & 0 deletions analyzer/tests/functional/skip/test_files/multidir/src/b.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
int main() {
int i = 1 / 0;
}
110 changes: 70 additions & 40 deletions analyzer/tests/functional/skip/test_skip.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,30 @@ def setup_method(self, method):
self.report_dir = os.path.join(self.test_workspace, "reports")
self.test_dir = os.path.join(os.path.dirname(__file__), 'test_files')

def __analyze_simple(self, build_json, analyzer_extra_options=None):
def __log_and_analyze(self, proj: str, analyzer_extra_options=None):
""" Log and analyze the specified project. """
test_dir = os.path.join(self.test_dir, proj)
build_json = os.path.join(self.test_workspace, "build.json")

clean_cmd = ["make", "clean"]
out = subprocess.check_output(clean_cmd,
cwd=test_dir,
encoding="utf-8", errors="ignore")
print(out)

# Create and run log command.
log_cmd = [self._codechecker_cmd, "log", "-b", "make",
"-o", build_json]
out = subprocess.check_output(log_cmd,
cwd=test_dir,
encoding="utf-8", errors="ignore")
print(out)
# Create and run analyze command.
return self.__analyze(build_json, proj, analyzer_extra_options)

def __analyze(self, build_json, proj: str, analyzer_extra_options=None):
""" Analyze the 'simple' project. """
test_dir = os.path.join(self.test_dir, "simple")
test_dir = os.path.join(self.test_dir, proj)
analyze_cmd = [
self._codechecker_cmd, "analyze", "-c", build_json,
"--analyzers", "clangsa", "-o", self.report_dir]
Expand All @@ -87,27 +108,6 @@ def __analyze_simple(self, build_json, analyzer_extra_options=None):
self.assertEqual(process.returncode, 0)
return out, err

def __log_and_analyze_simple(self, analyzer_extra_options=None):
""" Log and analyze the 'simple' project. """
test_dir = os.path.join(self.test_dir, "simple")
build_json = os.path.join(self.test_workspace, "build.json")

clean_cmd = ["make", "clean"]
out = subprocess.check_output(clean_cmd,
cwd=test_dir,
encoding="utf-8", errors="ignore")
print(out)

# Create and run log command.
log_cmd = [self._codechecker_cmd, "log", "-b", "make",
"-o", build_json]
out = subprocess.check_output(log_cmd,
cwd=test_dir,
encoding="utf-8", errors="ignore")
print(out)
# Create and run analyze command.
return self.__analyze_simple(build_json, analyzer_extra_options)

def __run_parse(self, extra_options=None):
""" Run parse command with the given extra options. """
cmd = [
Expand All @@ -129,7 +129,7 @@ def __run_parse(self, extra_options=None):

def test_skip(self):
"""Analyze a project with a skip file."""
self.__log_and_analyze_simple(["--ignore", "skipfile"])
self.__log_and_analyze("simple", ["--ignore", "skipfile"])

# Check if file is skipped.
report_dir_files = os.listdir(self.report_dir)
Expand Down Expand Up @@ -249,15 +249,18 @@ def test_analyze_skip_everything(self):
skip_file.write('-*')
skip_file.flush()

self.__log_and_analyze_simple([
"--ignore", skip_file.name])
self.__log_and_analyze(
"simple",
["--ignore", skip_file.name])
self.assertFalse(
glob.glob(os.path.join(self.report_dir, '*.plist')))

def test_analyze_header_with_file_option(self):
""" Analyze a header file with the --file option. """
header_file = os.path.join(self.test_dir, "simple", "skip.h")
out, _ = self.__log_and_analyze_simple(["--file", header_file])
out, _ = self.__log_and_analyze(
"simple",
["--file", header_file])
self.assertIn(
f"Get dependent source files for '{header_file}'...", out)
self.assertIn(
Expand Down Expand Up @@ -289,7 +292,7 @@ def test_analyze_header_with_file_option_and_intercept_json(self):
json.dump(build_actions, f)

header_file = os.path.join(self.test_dir, "simple", "skip.h")
out, _ = self.__analyze_simple(build_json, ["--file", header_file])
out, _ = self.__analyze(build_json, "simple", ["--file", header_file])
self.assertIn(
f"Get dependent source files for '{header_file}'...", out)
self.assertIn(
Expand All @@ -312,9 +315,13 @@ def test_analyze_file_option_skip_everything(self):
skip_file.write('-*')
skip_file.flush()

self.__log_and_analyze_simple([
"--ignore", skip_file.name,
"--file", "*/file_to_be_skipped.cpp"])
self.__log_and_analyze(
"simple",
[
"--ignore", skip_file.name,
"--file",
"*/file_to_be_skipped.cpp"
])
self.assertFalse(
glob.glob(os.path.join(self.report_dir, '*.plist')))

Expand All @@ -336,9 +343,13 @@ def test_analyze_file_option(self):
]))
skip_file.flush()

self.__log_and_analyze_simple([
"--ignore", skip_file.name,
"--file", "*/skip_header.cpp"])
self.__log_and_analyze(
"simple",
[
"--ignore", skip_file.name,
"--file",
"*/skip_header.cpp"
])
print(glob.glob(
os.path.join(self.report_dir, '*.plist')))
self.assertFalse(
Expand All @@ -353,9 +364,13 @@ def test_analyze_file_option(self):
]))
skip_file.flush()

self.__log_and_analyze_simple([
"--ignore", skip_file.name,
"--file", "*/skip_header.cpp"])
self.__log_and_analyze(
"simple",
[
"--ignore", skip_file.name,
"--file",
"*/skip_header.cpp"
])
print(glob.glob(
os.path.join(self.report_dir, '*.plist')))
self.assertFalse(
Expand All @@ -366,8 +381,7 @@ def test_analyze_only_file_option(self):
"""
Test analyze command --file option without a skip file.
"""
self.__log_and_analyze_simple([
"--file", "*/skip_header.cpp"])
self.__log_and_analyze("simple", ["--file", "*/skip_header.cpp"])
self.assertFalse(
any('skip_header.cpp' not in f for f in glob.glob(
os.path.join(self.report_dir, '*.plist'))))
Expand All @@ -376,7 +390,7 @@ def test_parse_file_option(self):
""" Test parse command --file option. """
skipfile = os.path.join(self.test_dir, "simple", "skipfile")

self.__log_and_analyze_simple()
self.__log_and_analyze("simple")

# Only reports from the given files are returned.
out, _, returncode = self.__run_parse(
Expand All @@ -402,3 +416,19 @@ def test_parse_file_option(self):
self.assertTrue(all(
r['file']['original_path'].endswith('/skip_header.cpp')
for r in data['reports']))

def test_analyze_file_option_with_path_prefixes(self):
""" Analyze a header file with the --file option. """
out, _ = self.__log_and_analyze(
proj="multidir",
analyzer_extra_options=["--file", "*/src/b.cpp"])

# Only src/b.cpp should be analyzed.
self.assertIn("analyzed b.cpp successfully.", out)
self.assertNotIn("analyzed a.cpp successfully.", out)
self.assertNotIn("analyzed lib.cpp successfully.", out)
out, _, _ = self.__run_parse()
# Only reports from b.cpp should be present in the report folder.
self.assertIn("b.cpp", out)
self.assertNotIn("a.cpp", out)
self.assertNotIn("lib.cpp", out)
1 change: 0 additions & 1 deletion codechecker_common/skiplist_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ def should_skip(self, source):
Check if the given source should be skipped.
Should the analyzer skip the given source file?
"""

if not self.__skip:
return False

Expand Down

0 comments on commit 37d4426

Please sign in to comment.