diff --git a/entrypoint_cpp.sh b/entrypoint_cpp.sh index faa740f..3af337d 100644 --- a/entrypoint_cpp.sh +++ b/entrypoint_cpp.sh @@ -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 @@ -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 diff --git a/entrypoint_python.sh b/entrypoint_python.sh index 292c9af..8062703 100644 --- a/entrypoint_python.sh +++ b/entrypoint_python.sh @@ -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" diff --git a/src/sa_utils.py b/src/sa_utils.py index 25ba8cb..abed0fa 100644 --- a/src/sa_utils.py +++ b/src/sa_utils.py @@ -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. @@ -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): diff --git a/test/test_static_analysis_cpp.py b/test/test_static_analysis_cpp.py index d8974d8..09c8da2 100644 --- a/test/test_static_analysis_cpp.py +++ b/test/test_static_analysis_cpp.py @@ -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"] @@ -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__": diff --git a/test/test_utils.py b/test/test_utils.py index adbb77c..45209c1 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -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()