33# Function to normalize paths in a file
44normalize_paths () {
55 local file=$1
6- local path_prefix
6+ # Get the repository root directory (5 levels up from current test/src directory)
7+ local repo_root=$( cd ../../../../.. && pwd)
78
9+ # Normalize absolute paths to relative ones for consistent testing
810 if [[ " $OSTYPE " == " darwin" * ]]; then
9- path_prefix=" /Users/runner/work/codacy-cli-v2/codacy-cli-v2/"
11+ # Replace absolute paths with relative paths in URI contexts
12+ sed -i ' ' " s#file://${repo_root} /plugins/tools/#file:///plugins/tools/#g" " $file "
13+ sed -i ' ' " s#${repo_root} /plugins/tools/#/plugins/tools/#g" " $file "
14+ # Handle CI runner paths for macOS
15+ sed -i ' ' " s#file:///Users/runner/work/codacy-cli-v2/codacy-cli-v2/plugins/tools/#file:///plugins/tools/#g" " $file "
16+ sed -i ' ' " s#/Users/runner/work/codacy-cli-v2/codacy-cli-v2/plugins/tools/#/plugins/tools/#g" " $file "
1017 else
11- path_prefix=" /home/runner/work/codacy-cli-v2/codacy-cli-v2/"
12- fi
13-
14- if [[ " $OSTYPE " == " darwin" * ]]; then
15- sed -i ' ' " s|file://${path_prefix} |file:///|g" " $file "
16- sed -i ' ' " s|${path_prefix} |/|g" " $file "
17- else
18- sed -i " s|file://${path_prefix} |file:///|g" " $file "
19- sed -i " s|${path_prefix} |/|g" " $file "
18+ # Replace absolute paths with relative paths in URI contexts
19+ sed -i " s#file://${repo_root} /plugins/tools/#file:///plugins/tools/#g" " $file "
20+ sed -i " s#${repo_root} /plugins/tools/#/plugins/tools/#g" " $file "
21+ # Handle CI runner paths for Linux
22+ sed -i " s#file:///home/runner/work/codacy-cli-v2/codacy-cli-v2/plugins/tools/#file:///plugins/tools/#g" " $file "
23+ sed -i " s#/home/runner/work/codacy-cli-v2/codacy-cli-v2/plugins/tools/#/plugins/tools/#g" " $file "
2024 fi
2125}
2226
2327# Function to sort SARIF file
28+
2429sort_sarif () {
2530 local input=$1
2631 local output=$2
5661# Change to the tool's test directory
5762cd " $TOOL_DIR " || exit 1
5863
64+ # Store initial state for cleanup
65+ initial_codacy_config=" "
66+ if [ -f .codacy/codacy.yaml ]; then
67+ # Backup existing config if it exists
68+ cp .codacy/codacy.yaml .codacy/codacy.yaml.backup
69+ initial_codacy_config=" exists"
70+ fi
71+
72+ # Function to cleanup generated files
73+ cleanup_test_files () {
74+ # Remove generated SARIF and sorted files
75+ rm -f actual.sarif actual.sorted.json expected.sorted.json
76+
77+ # Restore or clean up .codacy/codacy.yaml
78+ if [ " $initial_codacy_config " = " exists" ] && [ -f .codacy/codacy.yaml.backup ]; then
79+ # Restore original config
80+ mv .codacy/codacy.yaml.backup .codacy/codacy.yaml
81+ elif [ " $initial_codacy_config " != " exists" ]; then
82+ # Remove generated config and directory if they didn't exist initially
83+ rm -f .codacy/codacy.yaml
84+ if [ -d .codacy ]; then
85+ rmdir .codacy 2> /dev/null || true # Only remove if empty
86+ fi
87+ fi
88+ }
89+
90+ # Set trap to ensure cleanup happens even if script fails
91+ trap cleanup_test_files EXIT
92+
5993# Run analysis
6094" $CLI_PATH " install
6195" $CLI_PATH " analyze --tool " $TOOL_NAME " --format sarif --output actual.sarif
@@ -75,6 +109,8 @@ if ! diff expected.sorted.json actual.sorted.json; then
75109 echo -e " \nActual SARIF output:"
76110 cat actual.sorted.json
77111 echo " $TOOL_NAME " >> /tmp/failed_tools.txt
112+ # Return to original directory before exit
113+ cd ../../../../.. || exit 1
78114 exit 1
79115else
80116 echo " ✅ Tests passed successfully for $TOOL_NAME "
0 commit comments