Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
26 changes: 17 additions & 9 deletions entrypoint_cpp.sh
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,26 @@ if [ -z "$INPUT_EXCLUDE_DIR" ]; then
files_to_check=$(python3 /src/get_files_to_check.py -dir="$GITHUB_WORKSPACE" -preselected="$preselected_files" -lang="c++")
debug_print "Running: files_to_check=python3 /src/get_files_to_check.py -dir=\"$GITHUB_WORKSPACE\" -preselected=\"$preselected_files\" -lang=\"c++\")"
else
files_to_check=$(python3 /src/get_files_to_check.py -exclude="$GITHUB_WORKSPACE/$INPUT_EXCLUDE_DIR" -dir="$GITHUB_WORKSPACE" -preselected="$preselected_files" -lang="c++")
debug_print "Running: files_to_check=python3 /src/get_files_to_check.py -exclude=\"$GITHUB_WORKSPACE/$INPUT_EXCLUDE_DIR\" -dir=\"$GITHUB_WORKSPACE\" -preselected=\"$preselected_files\" -lang=\"c++\")"
files_to_check=$(python3 /src/get_files_to_check.py -exclude="$INPUT_EXCLUDE_DIR" -dir="$GITHUB_WORKSPACE" -preselected="$preselected_files" -lang="c++")
debug_print "Running: files_to_check=python3 /src/get_files_to_check.py -exclude=\"$INPUT_EXCLUDE_DIR\" -dir=\"$GITHUB_WORKSPACE\" -preselected=\"$preselected_files\" -lang=\"c++\")"
fi

debug_print "Files to check = $files_to_check"
debug_print "CPPCHECK_ARGS = $CPPCHECK_ARGS"
debug_print "CLANG_TIDY_ARGS = $CLANG_TIDY_ARGS"

cppcheck_exclude_args=""
if [ -n "$INPUT_EXCLUDE_DIR" ]; then
read -r -a exclude_dirs <<< "$INPUT_EXCLUDE_DIR"
for exclude_dir in "${exclude_dirs[@]}"; do
exclude_path="$exclude_dir"
if [[ "$exclude_path" != /* ]]; then
exclude_path="$GITHUB_WORKSPACE/$exclude_path"
fi
cppcheck_exclude_args="$cppcheck_exclude_args -i$exclude_path"
done
fi

num_proc=$(nproc)

if [ -z "$files_to_check" ]; then
Expand All @@ -71,16 +83,12 @@ else
fi

for file in $files_to_check; do
exclude_arg=""
if [ -n "$INPUT_EXCLUDE_DIR" ]; then
exclude_arg="-i$GITHUB_WORKSPACE/$INPUT_EXCLUDE_DIR"
fi

# Replace '/' with '_'
file_name=$(echo "$file" | tr '/' '_')

debug_print "Running cppcheck --project=$compile_commands_path $CPPCHECK_ARGS --file-filter=$file --output-file=cppcheck_$file_name.txt $exclude_arg"
eval cppcheck --project="$compile_commands_path" "$CPPCHECK_ARGS" --file-filter="$file" --output-file="cppcheck_$file_name.txt" "$exclude_arg" || true
debug_print "Running cppcheck --project=$compile_commands_path $CPPCHECK_ARGS --file-filter=$file --output-file=cppcheck_$file_name.txt $cppcheck_exclude_args"
cppcheck_cmd="cppcheck --project=\"$compile_commands_path\" $CPPCHECK_ARGS --file-filter=\"$file\" --output-file=\"cppcheck_$file_name.txt\"$cppcheck_exclude_args"
eval "$cppcheck_cmd" || true
done

cat cppcheck_*.txt > cppcheck.txt
Expand Down
4 changes: 2 additions & 2 deletions entrypoint_python.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ if [ -z "$INPUT_EXCLUDE_DIR" ]; then
debug_print "Running: files_to_check=\$(python3 /src/get_files_to_check.py -dir=\"$GITHUB_WORKSPACE\" -include=\"$INPUT_PYTHON_DIRS\" -preselected=\"$preselected_files\" -lang=\"python\")"
files_to_check=$(python3 /src/get_files_to_check.py -dir="$GITHUB_WORKSPACE" -include="$INPUT_PYTHON_DIRS" -preselected="$preselected_files" -lang="python")
else
debug_print "Running: files_to_check=\$(python3 /src/get_files_to_check.py -exclude=\"$GITHUB_WORKSPACE/$INPUT_EXCLUDE_DIR\" -dir=\"$GITHUB_WORKSPACE\" -include=\"$INPUT_PYTHON_DIRS\" -preselected=\"$preselected_files\" -lang=\"python\")"
files_to_check=$(python3 /src/get_files_to_check.py -exclude="$GITHUB_WORKSPACE/$INPUT_EXCLUDE_DIR" -dir="$GITHUB_WORKSPACE" -include="$INPUT_PYTHON_DIRS" -preselected="$preselected_files" -lang="python")
debug_print "Running: files_to_check=\$(python3 /src/get_files_to_check.py -exclude=\"$INPUT_EXCLUDE_DIR\" -dir=\"$GITHUB_WORKSPACE\" -include=\"$INPUT_PYTHON_DIRS\" -preselected=\"$preselected_files\" -lang=\"python\")"
files_to_check=$(python3 /src/get_files_to_check.py -exclude="$INPUT_EXCLUDE_DIR" -dir="$GITHUB_WORKSPACE" -include="$INPUT_PYTHON_DIRS" -preselected="$preselected_files" -lang="python")
fi

debug_print "Files to check = $files_to_check"
Expand Down
17 changes: 11 additions & 6 deletions src/sa_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,12 @@ def check_for_char_limit(incoming_line):
return (CURRENT_COMMENT_LENGTH + len(incoming_line)) <= COMMENT_MAX_SIZE


def _normalize_excluded_dir(path_in):
if os.path.isabs(path_in):
return os.path.realpath(path_in)
return os.path.realpath(f"{WORK_DIR}/{path_in}")


def is_excluded_dir(line):
"""
Determines if a given line is from a directory that should be excluded from processing.
Expand All @@ -219,17 +225,16 @@ def is_excluded_dir(line):
bool: True if the line is from a directory that should be excluded, False otherwise.
"""

# In future this could be multiple different directories
exclude_dir = os.getenv("INPUT_EXCLUDE_DIR")
if not exclude_dir:
return False

excluded_dir = f"{WORK_DIR}/{exclude_dir}"
debug_print(
f"{line} and {excluded_dir} with result {line.startswith(excluded_dir)}"
)
excluded_dirs = [_normalize_excluded_dir(path) for path in exclude_dir.split()]
result = any(line.startswith(excluded_dir) for excluded_dir in excluded_dirs)

debug_print(f"{line} and {excluded_dirs} with result {result}")

return line.startswith(excluded_dir)
return result


def get_file_line_end(file_in, file_line_start_in):
Expand Down
2 changes: 2 additions & 0 deletions test/test_static_analysis_cpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ def test_get_files_to_check(self):
"",
"c++",
)
self.assertEqual(to_list_and_sort(result), expected)

# Preselected files present
expected = [f"{pwd}/utils/dummy_project/DummyFile.cpp"]
Expand All @@ -219,6 +220,7 @@ def test_get_files_to_check(self):
f"{pwd}/utils/dummy_project/DummyFile.cpp {pwd}/utils/dummy_project/exclude_dir_1/ExcludedFile1.hpp",
"c++",
)
self.assertEqual(to_list_and_sort(result), expected)


if __name__ == "__main__":
Expand Down
28 changes: 28 additions & 0 deletions test/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,34 @@ def test_get_lines_changed_from_patch(self):
lines = sa_utils.get_lines_changed_from_patch(patch)
self.assertEqual(lines, [(48, 56), (1, 1)])

def test_is_excluded_dir_with_multiple_directories(self):
previous_exclude_dir = os.environ.get("INPUT_EXCLUDE_DIR")
previous_work_dir = sa_utils.WORK_DIR

try:
os.environ["INPUT_EXCLUDE_DIR"] = "exclude_dir_1 exclude_dir_2"
sa_utils.WORK_DIR = "/github/workspace"

self.assertTrue(
sa_utils.is_excluded_dir(
"/github/workspace/exclude_dir_1/file.cpp:8:1: warning"
)
)
self.assertTrue(
sa_utils.is_excluded_dir(
"/github/workspace/exclude_dir_2/file.cpp:3:1: warning"
)
)
self.assertFalse(
sa_utils.is_excluded_dir("/github/workspace/src/file.cpp:4:1: warning")
)
finally:
if previous_exclude_dir is None:
os.environ.pop("INPUT_EXCLUDE_DIR", None)
else:
os.environ["INPUT_EXCLUDE_DIR"] = previous_exclude_dir
sa_utils.WORK_DIR = previous_work_dir


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