Skip to content

Commit 237e427

Browse files
authored
Update sarif generation and tests (#124)
1 parent c29ef51 commit 237e427

File tree

14 files changed

+157
-293
lines changed

14 files changed

+157
-293
lines changed

.github/workflows/it-test.yml

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,9 @@ jobs:
6868
if: matrix.os != 'windows-latest'
6969
run: chmod +x cli-v2
7070

71-
- name: Install yq on Windows
72-
if: matrix.os == 'windows-latest'
73-
shell: pwsh
74-
run: |
75-
choco install yq -y
76-
Import-Module $env:ChocolateyInstall\helpers\chocolateyProfile.psm1
77-
refreshenv
78-
7971
- name: Run init tests on Windows
8072
if: matrix.os == 'windows-latest'
73+
id: run_init_tests_windows
8174
shell: pwsh
8275
run: |
8376
$ErrorActionPreference = "Stop"
@@ -102,7 +95,7 @@ jobs:
10295
10396
- name: Run tool tests
10497
if: matrix.os != 'windows-latest'
105-
id: run_tests
98+
id: run_tool_tests_unix
10699
continue-on-error: true
107100
shell: bash
108101
run: |
@@ -136,7 +129,30 @@ jobs:
136129
fi
137130
138131
- name: Check test results
139-
if: failure()
132+
if: always()
133+
shell: bash
140134
run: |
141-
echo "Job failed because some tests failed. Please check the logs above for details."
142-
exit 1
135+
FAILED=false
136+
137+
# Check init tests on Windows
138+
if [ "${{ matrix.os }}" = "windows-latest" ] && [ "${{ steps.run_init_tests_windows.outcome }}" = "failure" ]; then
139+
echo "❌ Init tests failed on Windows"
140+
FAILED=true
141+
fi
142+
143+
# Check init tests on Unix
144+
if [ "${{ matrix.os }}" != "windows-latest" ] && [ "${{ steps.run_init_tests_unix.outcome }}" = "failure" ]; then
145+
echo "❌ Init tests failed on Unix"
146+
FAILED=true
147+
fi
148+
149+
# Check tool tests on Unix
150+
if [ "${{ matrix.os }}" != "windows-latest" ] && [ "${{ steps.run_tool_tests_unix.outcome }}" = "failure" ]; then
151+
echo "❌ Tool tests failed on Unix"
152+
FAILED=true
153+
fi
154+
155+
if [ "$FAILED" = true ]; then
156+
echo "Job failed because some tests failed. Please check the logs above for details."
157+
exit 1
158+
fi

cmd/analyze.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -482,20 +482,23 @@ var analyzeCmd = &cobra.Command{
482482
log.Fatalf("Failed to merge SARIF outputs: %v", err)
483483
}
484484

485+
// Filter rules from the merged SARIF output
486+
sarifData, err := os.ReadFile(tmpOutputFile)
487+
if err != nil {
488+
log.Fatalf("Failed to read merged SARIF output: %v", err)
489+
}
490+
491+
filteredData, err := utils.FilterRulesFromSarif(sarifData)
492+
if err != nil {
493+
log.Fatalf("Failed to filter rules from SARIF: %v", err)
494+
}
495+
485496
if outputFile != "" {
486-
// copy tmpOutputFile to outputFile
487-
content, err := os.ReadFile(tmpOutputFile)
488-
if err != nil {
489-
log.Fatalf("Failed to read merged SARIF output: %v", err)
490-
}
491-
os.WriteFile(outputFile, content, utils.DefaultFilePerms)
497+
// Write filtered SARIF to output file
498+
os.WriteFile(outputFile, filteredData, utils.DefaultFilePerms)
492499
} else {
493-
// println the output file content
494-
content, err := os.ReadFile(tmpOutputFile)
495-
if err != nil {
496-
log.Fatalf("Failed to read merged SARIF output: %v", err)
497-
}
498-
fmt.Println(string(content))
500+
// Print the filtered SARIF output
501+
fmt.Println(string(filteredData))
499502
}
500503
} else {
501504
// Run tools without merging outputs

integration-tests/init-with-token/expected/.codacy/codacy.yaml

Lines changed: 0 additions & 10 deletions
This file was deleted.
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
runtimes:
22
33
4+
45
tools:
56
67
78
89
910
10-
11+

integration-tests/init-without-token/expected/codacy.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ runtimes:
22
33
44
5+
56
tools:
67
78
89
910
1011
1112
12-
13+

integration-tests/run.ps1

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,45 @@ function Normalize-Config {
2020
$ext = [System.IO.Path]::GetExtension($file).TrimStart('.')
2121

2222
switch ($ext) {
23-
{ $_ -in @('yaml', 'yml') } { yq e '.' $file | Sort-Object }
23+
{ $_ -in @('yaml', 'yml') } {
24+
# For YAML files, preserve structure and sort within sections
25+
$content = Get-Content $file
26+
$output = @()
27+
$currentSection = ""
28+
$sectionContent = @()
29+
30+
foreach ($line in $content) {
31+
$line = $line.Trim()
32+
if ($line -match '^(\w+):$') {
33+
# If we have a previous section, sort and add its content
34+
if ($currentSection -and $sectionContent.Count -gt 0) {
35+
$output += $currentSection
36+
$output += ($sectionContent | Sort-Object)
37+
$sectionContent = @()
38+
}
39+
$currentSection = $line
40+
}
41+
elseif ($line -match '^\s*-\s*') {
42+
$sectionContent += $line
43+
}
44+
elseif ($line -match '\S') {
45+
$output += $line
46+
}
47+
}
48+
49+
# Add the last section
50+
if ($currentSection -and $sectionContent.Count -gt 0) {
51+
$output += $currentSection
52+
$output += ($sectionContent | Sort-Object)
53+
}
54+
55+
# Add empty line at the end if the original had one
56+
if ($content[-1] -match '^\s*$') {
57+
$output += ""
58+
}
59+
60+
$output
61+
}
2462
{ $_ -in @('rc', 'conf', 'ini', 'xml') } {
2563
Get-Content $file | ForEach-Object {
2664
if ($_ -match '^[^#].*=.*,') {
@@ -54,14 +92,16 @@ function Compare-Files {
5492
$expectedContent = Normalize-Config $_.FullName
5593
$actualContent = Normalize-Config $actualFile
5694

57-
if (Compare-Object $expectedContent $actualContent) {
95+
# Compare line by line
96+
$diff = Compare-Object $expectedContent $actualContent -PassThru
97+
if ($diff) {
5898
Write-Host "$label/$($_.Name) does not match expected"
5999
Write-Host "=== Expected (normalized) ==="
60100
$expectedContent
61101
Write-Host "=== Actual (normalized) ==="
62102
$actualContent
63103
Write-Host "=== Diff ==="
64-
Compare-Object $expectedContent $actualContent
104+
$diff
65105
Write-Host "==================="
66106
exit 1
67107
}
@@ -70,7 +110,7 @@ function Compare-Files {
70110

71111
# Compare subdirectories
72112
Get-ChildItem -Path $expectedDir -Directory | Where-Object { $_.Name -ne "logs" } | ForEach-Object {
73-
$actualSubDir = if ($_.Name -eq ".codacy") { $actualDir } else { Join-Path $actualDir $_.Name }
113+
$actualSubDir = Join-Path $actualDir $_.Name
74114

75115
if (-not (Test-Path $actualSubDir)) {
76116
Write-Host "❌ Directory $label/$($_.Name) does not exist in actual output"

plugins/tools/codacy-enigma-cli/test/expected.sarif

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
{
66
"tool": {
77
"driver": {
8-
"name": "codacy-enigma-cli"
8+
"name": "codacy-enigma-cli",
9+
"rules": null
910
}
1011
},
1112
"results": [

plugins/tools/dartanalyzer/test/expected.sarif

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@
4242
],
4343
"tool": {
4444
"driver": {
45-
"name": "dartanalyzer"
45+
"name": "dartanalyzer",
46+
"rules": null
4647
}
4748
}
4849
}

plugins/tools/eslint/test/expected.sarif

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
"driver": {
4545
"informationUri": "https://eslint.org",
4646
"name": "ESLint",
47-
"rules": [],
47+
"rules": null,
4848
"version": "8.57.0"
4949
}
5050
}

plugins/tools/lizard/test/expected.sarif

Lines changed: 1 addition & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -8,52 +8,7 @@
88
"name": "Lizard",
99
"version": "1.17.10",
1010
"informationUri": "https://github.com/terryyin/lizard",
11-
"rules": [
12-
{
13-
"id": "Lizard_ccn-medium",
14-
"shortDescription": {
15-
"text": "Check the Cyclomatic Complexity value of a function or logic block. If the threshold is not met, raise a Medium issue. The default threshold is 8."
16-
},
17-
"properties": {
18-
"tags": [
19-
"warning"
20-
]
21-
}
22-
},
23-
{
24-
"id": "Lizard_file-nloc-medium",
25-
"shortDescription": {
26-
"text": "Check the number of lines of code (without comments) in a file. If the threshold is not met, raise a Medium issue. The default threshold is 500."
27-
},
28-
"properties": {
29-
"tags": [
30-
"warning"
31-
]
32-
}
33-
},
34-
{
35-
"id": "Lizard_nloc-medium",
36-
"shortDescription": {
37-
"text": "Check the number of lines of code (without comments) in a function. If the threshold is not met, raise a Medium issue. The default threshold is 50."
38-
},
39-
"properties": {
40-
"tags": [
41-
"warning"
42-
]
43-
}
44-
},
45-
{
46-
"id": "Lizard_parameter-count-medium",
47-
"shortDescription": {
48-
"text": "Check the number of parameters sent to a function. If the threshold is not met, raise a Medium issue. The default threshold is 8."
49-
},
50-
"properties": {
51-
"tags": [
52-
"warning"
53-
]
54-
}
55-
}
56-
]
11+
"rules": null
5712
}
5813
},
5914
"results": [

0 commit comments

Comments
 (0)