11#! /bin/bash
22
3+ # Function to normalize paths in a file
4+ normalize_paths () {
5+ local file=$1
6+ local path_prefix
7+
8+ if [[ " $OSTYPE " == " darwin" * ]]; then
9+ path_prefix=" /Users/runner/work/codacy-cli-v2/codacy-cli-v2/"
10+ 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 "
20+ fi
21+ }
22+
23+ # Function to sort SARIF file
24+ sort_sarif () {
25+ local input=$1
26+ local output=$2
27+ jq --sort-keys ' if .runs[0].tool.driver.rules == null then . else .runs[0].tool.driver.rules |= sort_by(.id) end' " $input " > " $output "
28+ }
29+
330# Check if tool name is provided
431if [ -z " $1 " ]; then
532 echo " Usage: $0 <tool_name>"
@@ -12,109 +39,43 @@ TEST_DIR="plugins/tools/$TOOL_NAME/test"
1239CLI_PATH=" $( pwd) /cli-v2"
1340EXPECTED_SARIF=" $( pwd) /$TEST_DIR /expected.sarif"
1441
15- # Check if tool directory exists
42+ # Validate environment
1643if [ ! -d " $TOOL_DIR " ]; then
1744 echo " Error: Tool directory $TOOL_DIR does not exist"
1845 exit 1
1946fi
2047
21- # Check if CLI binary exists
2248if [ ! -f " $CLI_PATH " ]; then
2349 echo " Error: CLI binary not found at $CLI_PATH "
2450 exit 1
2551fi
2652
2753# Change to the tool's test directory
28- cd " $TOOL_DIR "
29-
30- # Install the tool
31- " $CLI_PATH " install
54+ cd " $TOOL_DIR " || exit 1
3255
3356# Run analysis
34- " $CLI_PATH " analyze --tool $TOOL_NAME --format sarif --output actual.sarif
35-
36- # Convert absolute paths to relative paths in the output
37- if [[ " $OSTYPE " == " darwin" * ]]; then
38- # macOS
39- sed -i ' ' ' s|file:///Users/runner/work/codacy-cli-v2/codacy-cli-v2/|file:///|g' actual.sarif
40- sed -i ' ' ' s|/Users/runner/work/codacy-cli-v2/codacy-cli-v2/|/|g' actual.sarif
41- elif [[ " $OSTYPE " == " msys" * ]] || [[ " $OSTYPE " == " cygwin" * ]]; then
42- # Windows (Git Bash or Cygwin)
43- sed -i ' s|file:///D:/a/codacy-cli-v2/codacy-cli-v2/|file:///|g' actual.sarif
44- sed -i ' s|file:///C:/Users/runner/work/codacy-cli-v2/codacy-cli-v2/|file:///|g' actual.sarif
45- sed -i ' s|/D:/a/codacy-cli-v2/codacy-cli-v2/|/|g' actual.sarif
46- sed -i ' s|/C:/Users/runner/work/codacy-cli-v2/codacy-cli-v2/|/|g' actual.sarif
47- # Convert Windows paths to forward slashes
48- sed -i ' s|\\|/|g' actual.sarif
49-
50- # Windows-specific: Ensure the SARIF file is properly terminated
51- # Remove any trailing commas and ensure proper JSON structure
52- sed -i ' s/,\s*$//' actual.sarif
53- # Ensure the file ends with proper JSON closing brackets
54- if ! grep -q ' ^}$' actual.sarif; then
55- echo " }" >> actual.sarif
56- fi
57- else
58- # Linux
59- sed -i ' s|file:///home/runner/work/codacy-cli-v2/codacy-cli-v2/|file:///|g' actual.sarif
60- sed -i ' s|/home/runner/work/codacy-cli-v2/codacy-cli-v2/|/|g' actual.sarif
61- fi
62-
63- # Sort all fields in both files, handling null rules array
64- if [[ " $OSTYPE " == " msys" * ]] || [[ " $OSTYPE " == " cygwin" * ]]; then
65- # Windows-specific: Validate JSON before processing
66- if ! jq ' .' actual.sarif > /dev/null 2>&1 ; then
67- echo " Error: Invalid JSON in actual.sarif"
68- cat actual.sarif
69- exit 1
70- fi
71- fi
72-
73- jq --sort-keys ' if .runs[0].tool.driver.rules == null then . else .runs[0].tool.driver.rules |= sort_by(.id) end' " $EXPECTED_SARIF " > expected.sorted.json
74- jq --sort-keys ' if .runs[0].tool.driver.rules == null then . else .runs[0].tool.driver.rules |= sort_by(.id) end' actual.sarif > actual.sorted.json
57+ " $CLI_PATH " install
58+ " $CLI_PATH " analyze --tool " $TOOL_NAME " --format sarif --output actual.sarif
7559
76- # Normalize paths in both files
77- if [[ " $OSTYPE " == " darwin" * ]]; then
78- # macOS
79- sed -i ' ' ' s|file:///Users/runner/work/codacy-cli-v2/codacy-cli-v2/|file:///|g' expected.sorted.json
80- sed -i ' ' ' s|/Users/runner/work/codacy-cli-v2/codacy-cli-v2/|/|g' expected.sorted.json
81- sed -i ' ' ' s|file:///Users/runner/work/codacy-cli-v2/codacy-cli-v2/|file:///|g' actual.sorted.json
82- sed -i ' ' ' s|/Users/runner/work/codacy-cli-v2/codacy-cli-v2/|/|g' actual.sorted.json
83- elif [[ " $OSTYPE " == " msys" * ]] || [[ " $OSTYPE " == " cygwin" * ]]; then
84- # Windows (Git Bash or Cygwin)
85- sed -i ' s|file:///D:/a/codacy-cli-v2/codacy-cli-v2/|file:///|g' expected.sorted.json
86- sed -i ' s|file:///C:/Users/runner/work/codacy-cli-v2/codacy-cli-v2/|file:///|g' expected.sorted.json
87- sed -i ' s|/D:/a/codacy-cli-v2/codacy-cli-v2/|/|g' expected.sorted.json
88- sed -i ' s|/C:/Users/runner/work/codacy-cli-v2/codacy-cli-v2/|/|g' expected.sorted.json
89- sed -i ' s|file:///D:/a/codacy-cli-v2/codacy-cli-v2/|file:///|g' actual.sorted.json
90- sed -i ' s|file:///C:/Users/runner/work/codacy-cli-v2/codacy-cli-v2/|file:///|g' actual.sorted.json
91- sed -i ' s|/D:/a/codacy-cli-v2/codacy-cli-v2/|/|g' actual.sorted.json
92- sed -i ' s|/C:/Users/runner/work/codacy-cli-v2/codacy-cli-v2/|/|g' actual.sorted.json
93- # Convert Windows paths to forward slashes
94- sed -i ' s|\\|/|g' expected.sorted.json
95- sed -i ' s|\\|/|g' actual.sorted.json
96- else
97- # Linux
98- sed -i ' s|file:///home/runner/work/codacy-cli-v2/codacy-cli-v2/|file:///|g' expected.sorted.json
99- sed -i ' s|/home/runner/work/codacy-cli-v2/codacy-cli-v2/|/|g' expected.sorted.json
100- sed -i ' s|file:///home/runner/work/codacy-cli-v2/codacy-cli-v2/|file:///|g' actual.sorted.json
101- sed -i ' s|/home/runner/work/codacy-cli-v2/codacy-cli-v2/|/|g' actual.sorted.json
102- fi
60+ # Process SARIF files
61+ normalize_paths actual.sarif
62+ sort_sarif " $EXPECTED_SARIF " expected.sorted.json
63+ sort_sarif actual.sarif actual.sorted.json
64+ normalize_paths expected.sorted.json
65+ normalize_paths actual.sorted.json
10366
104- # Run diff and capture its exit code
67+ # Compare results
10568if ! diff expected.sorted.json actual.sorted.json; then
10669 echo " ❌ Test output does not match expected output for $TOOL_NAME "
10770 echo -e " \nExpected SARIF output:"
10871 cat expected.sorted.json
10972 echo -e " \nActual SARIF output:"
11073 cat actual.sorted.json
111- # Write to a file to track failures
11274 echo " $TOOL_NAME " >> /tmp/failed_tools.txt
11375 exit 1
11476else
11577 echo " ✅ Tests passed successfully for $TOOL_NAME "
11678fi
11779
11880# Return to original directory
119- echo " 📂 Returning to original directory..."
120- cd ../../../../.. || { echo " Failed to return to original directory" ; exit 1; }
81+ cd ../../../../.. || exit 1
0 commit comments