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>"
@@ -10,49 +37,45 @@ TOOL_NAME=$1
10
37
TOOL_DIR=" plugins/tools/$TOOL_NAME /test/src"
11
38
TEST_DIR=" plugins/tools/$TOOL_NAME /test"
12
39
CLI_PATH=" $( pwd) /cli-v2"
40
+ EXPECTED_SARIF=" $( pwd) /$TEST_DIR /expected.sarif"
13
41
14
- # Check if tool directory exists
42
+ # Validate environment
15
43
if [ ! -d " $TOOL_DIR " ]; then
16
44
echo " Error: Tool directory $TOOL_DIR does not exist"
17
45
exit 1
18
46
fi
19
47
20
- # Check if CLI binary exists
21
48
if [ ! -f " $CLI_PATH " ]; then
22
49
echo " Error: CLI binary not found at $CLI_PATH "
23
50
exit 1
24
51
fi
25
52
26
53
# Change to the tool's test directory
27
- cd " $TOOL_DIR "
28
-
29
- # Install the tool
30
- " $CLI_PATH " install
54
+ cd " $TOOL_DIR " || exit 1
31
55
32
56
# Run analysis
33
- " $CLI_PATH " analyze --tool $TOOL_NAME --format sarif --output actual.sarif
34
-
35
- # Convert absolute paths to relative paths in the output
36
- # Handle both path formats: with and without extra codacy-cli-v2
37
- sed -i ' s|file:///home/runner/work/codacy-cli-v2/|file:///|g' actual.sarif
57
+ " $CLI_PATH " install
58
+ " $CLI_PATH " analyze --tool " $TOOL_NAME " --format sarif --output actual.sarif
38
59
39
- # Sort all fields in both files, handling null rules array
40
- jq --sort-keys ' if .runs[0].tool.driver.rules == null then . else .runs[0].tool.driver.rules |= sort_by(.id) end' " $TEST_DIR /expected.sarif" > expected.sorted.json
41
- 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
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
42
66
43
- # Run diff and capture its exit code
67
+ # Compare results
44
68
if ! diff expected.sorted.json actual.sorted.json; then
45
69
echo " ❌ Test output does not match expected output for $TOOL_NAME "
46
70
echo -e " \nExpected SARIF output:"
47
71
cat expected.sorted.json
48
72
echo -e " \nActual SARIF output:"
49
73
cat actual.sorted.json
50
- # Write to a file to track failures
51
74
echo " $TOOL_NAME " >> /tmp/failed_tools.txt
52
75
exit 1
53
76
else
54
77
echo " ✅ Tests passed successfully for $TOOL_NAME "
55
78
fi
56
79
57
80
# Return to original directory
58
- cd ../../../../..
81
+ cd ../../../../.. || exit 1
0 commit comments