Skip to content

Commit 2ef16b3

Browse files
committed
[PLUTO-1411] fix all tests
1 parent d84a072 commit 2ef16b3

File tree

6 files changed

+67
-62
lines changed

6 files changed

+67
-62
lines changed

.github/workflows/it-test.yml

+20-38
Original file line numberDiff line numberDiff line change
@@ -7,45 +7,12 @@ on:
77
push:
88

99
jobs:
10-
build:
11-
runs-on: ubuntu-latest
12-
steps:
13-
- name: Checkout code
14-
uses: actions/checkout@v4
15-
16-
- name: Set up Go
17-
uses: actions/setup-go@v5
18-
with:
19-
go-version: '1.21'
20-
cache: true
21-
22-
- name: Build CLI for Linux
23-
run: |
24-
GOOS=linux GOARCH=amd64 go build -o cli-v2-linux ./cli-v2.go
25-
26-
- name: Build CLI for Windows
27-
run: |
28-
GOOS=windows GOARCH=amd64 go build -o cli-v2.exe ./cli-v2.go
29-
30-
- name: Build CLI for macOS
31-
run: |
32-
GOOS=darwin GOARCH=amd64 go build -o cli-v2-macos ./cli-v2.go
33-
34-
- name: Upload CLI binaries
35-
uses: actions/upload-artifact@v4
36-
with:
37-
name: cli-binaries
38-
path: |
39-
cli-v2-linux
40-
cli-v2.exe
41-
cli-v2-macos
42-
4310
test:
44-
needs: build
4511
runs-on: ${{ matrix.os }}
4612
strategy:
4713
matrix:
4814
os: [ubuntu-latest, macos-latest, windows-latest]
15+
fail-fast: false # Don't stop all jobs if one fails
4916
steps:
5017
- name: Checkout code
5118
uses: actions/checkout@v4
@@ -56,9 +23,10 @@ jobs:
5623
go-version: '1.21'
5724
cache: true
5825

59-
- name: Download CLI binaries
60-
uses: actions/download-artifact@v4
26+
- name: Download CLI binaries from go workflow
27+
uses: dawidd6/action-download-artifact@v2
6128
with:
29+
workflow: go.yml
6230
name: cli-binaries
6331
path: .
6432

@@ -78,7 +46,18 @@ jobs:
7846
if: matrix.os != 'windows-latest'
7947
run: chmod +x cli-v2
8048

81-
- name: Run plugin tests
49+
- name: Install dependencies from .codacy/codacy.yaml (Windows)
50+
if: matrix.os == 'windows-latest'
51+
shell: pwsh
52+
run: |
53+
Get-ChildItem
54+
Write-Host "Current directory contents:"
55+
dir
56+
Write-Host "Attempting to run CLI..."
57+
.\cli-v2.exe install
58+
59+
- name: Run tool tests
60+
if: matrix.os != 'windows-latest'
8261
id: run_tests
8362
continue-on-error: true
8463
shell: bash
@@ -95,7 +74,10 @@ jobs:
9574
tool_name=$(basename "$tool_dir")
9675
if [ -d "$tool_dir/test/src" ]; then
9776
echo "Running tests for $tool_name..."
98-
./run-tool-tests.sh "$tool_name"
77+
./run-tool-tests.sh "$tool_name" || {
78+
echo "❌ Test failed for $tool_name"
79+
echo "$tool_name" >> /tmp/failed_tools.txt
80+
}
9981
fi
10082
done
10183

plugins/tools/dartanalyzer/test/expected.sarif

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
{
99
"physicalLocation": {
1010
"artifactLocation": {
11-
"uri": "/home/runner/work/codacy-cli-v2/codacy-cli-v2/plugins/tools/dartanalyzer/test/src/Test.dart"
11+
"uri": "/plugins/tools/dartanalyzer/test/src/Test.dart"
1212
},
1313
"region": {
1414
"startLine": 2
@@ -26,7 +26,7 @@
2626
{
2727
"physicalLocation": {
2828
"artifactLocation": {
29-
"uri": "/home/runner/work/codacy-cli-v2/codacy-cli-v2/plugins/tools/dartanalyzer/test/src/Test.dart"
29+
"uri": "/plugins/tools/dartanalyzer/test/src/Test.dart"
3030
},
3131
"region": {
3232
"startLine": 28

plugins/tools/eslint/test/expected.sarif

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"artifacts": [
66
{
77
"location": {
8-
"uri": "file:///codacy-cli-v2/plugins/tools/eslint/test/src/Test.js"
8+
"uri": "file:///plugins/tools/eslint/test/src/Test.js"
99
}
1010
}
1111
],
@@ -23,7 +23,7 @@
2323
"physicalLocation": {
2424
"artifactLocation": {
2525
"index": 0,
26-
"uri": "file:///codacy-cli-v2/plugins/tools/eslint/test/src/Test.js"
26+
"uri": "file:///plugins/tools/eslint/test/src/Test.js"
2727
},
2828
"region": {
2929
"startColumn": 1,

plugins/tools/pmd/test/expected.sarif

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
{
6464
"physicalLocation": {
6565
"artifactLocation": {
66-
"uri": "/home/runner/work/codacy-cli-v2/codacy-cli-v2/plugins/tools/pmd/test/src/Test.java"
66+
"uri": "/plugins/tools/pmd/test/src/Test.java"
6767
},
6868
"region": {
6969
"startLine": 2,
@@ -85,7 +85,7 @@
8585
{
8686
"physicalLocation": {
8787
"artifactLocation": {
88-
"uri": "/home/runner/work/codacy-cli-v2/codacy-cli-v2/plugins/tools/pmd/test/src/Test.java"
88+
"uri": "/plugins/tools/pmd/test/src/Test.java"
8989
},
9090
"region": {
9191
"startLine": 5,

plugins/tools/trivy/test/expected.sarif

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
"columnKind": "utf16CodeUnits",
7373
"originalUriBaseIds": {
7474
"ROOTPATH": {
75-
"uri": "file:///codacy-cli-v2/plugins/tools/trivy/test/src/"
75+
"uri": "file:///plugins/tools/trivy/test/src/"
7676
}
7777
}
7878
}

run-tool-tests.sh

+40-17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,32 @@
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
431
if [ -z "$1" ]; then
532
echo "Usage: $0 <tool_name>"
@@ -10,49 +37,45 @@ TOOL_NAME=$1
1037
TOOL_DIR="plugins/tools/$TOOL_NAME/test/src"
1138
TEST_DIR="plugins/tools/$TOOL_NAME/test"
1239
CLI_PATH="$(pwd)/cli-v2"
40+
EXPECTED_SARIF="$(pwd)/$TEST_DIR/expected.sarif"
1341

14-
# Check if tool directory exists
42+
# Validate environment
1543
if [ ! -d "$TOOL_DIR" ]; then
1644
echo "Error: Tool directory $TOOL_DIR does not exist"
1745
exit 1
1846
fi
1947

20-
# Check if CLI binary exists
2148
if [ ! -f "$CLI_PATH" ]; then
2249
echo "Error: CLI binary not found at $CLI_PATH"
2350
exit 1
2451
fi
2552

2653
# 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
3155

3256
# 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
3859

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
4266

43-
# Run diff and capture its exit code
67+
# Compare results
4468
if ! diff expected.sorted.json actual.sorted.json; then
4569
echo "❌ Test output does not match expected output for $TOOL_NAME"
4670
echo -e "\nExpected SARIF output:"
4771
cat expected.sorted.json
4872
echo -e "\nActual SARIF output:"
4973
cat actual.sorted.json
50-
# Write to a file to track failures
5174
echo "$TOOL_NAME" >> /tmp/failed_tools.txt
5275
exit 1
5376
else
5477
echo "✅ Tests passed successfully for $TOOL_NAME"
5578
fi
5679

5780
# Return to original directory
58-
cd ../../../../..
81+
cd ../../../../.. || exit 1

0 commit comments

Comments
 (0)