1
1
#! /bin/bash
2
2
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
+
3
30
# Check if tool name is provided
4
31
if [ -z " $1 " ]; then
5
32
echo " Usage: $0 <tool_name>"
@@ -12,109 +39,43 @@ TEST_DIR="plugins/tools/$TOOL_NAME/test"
12
39
CLI_PATH=" $( pwd) /cli-v2"
13
40
EXPECTED_SARIF=" $( pwd) /$TEST_DIR /expected.sarif"
14
41
15
- # Check if tool directory exists
42
+ # Validate environment
16
43
if [ ! -d " $TOOL_DIR " ]; then
17
44
echo " Error: Tool directory $TOOL_DIR does not exist"
18
45
exit 1
19
46
fi
20
47
21
- # Check if CLI binary exists
22
48
if [ ! -f " $CLI_PATH " ]; then
23
49
echo " Error: CLI binary not found at $CLI_PATH "
24
50
exit 1
25
51
fi
26
52
27
53
# 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
32
55
33
56
# 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
75
59
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
103
66
104
- # Run diff and capture its exit code
67
+ # Compare results
105
68
if ! diff expected.sorted.json actual.sorted.json; then
106
69
echo " ❌ Test output does not match expected output for $TOOL_NAME "
107
70
echo -e " \nExpected SARIF output:"
108
71
cat expected.sorted.json
109
72
echo -e " \nActual SARIF output:"
110
73
cat actual.sorted.json
111
- # Write to a file to track failures
112
74
echo " $TOOL_NAME " >> /tmp/failed_tools.txt
113
75
exit 1
114
76
else
115
77
echo " ✅ Tests passed successfully for $TOOL_NAME "
116
78
fi
117
79
118
80
# 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