-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpmdRunner_test.go
126 lines (106 loc) · 3.68 KB
/
pmdRunner_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package tools
import (
"codacy/cli-v2/config"
"encoding/json"
"log"
"os"
"path/filepath"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
// SarifResult represents a single result in a SARIF report
type SarifResult struct {
RuleID string `json:"ruleId"`
Message struct {
Text string `json:"text"`
} `json:"message"`
Locations []struct {
PhysicalLocation struct {
ArtifactLocation struct {
URI string `json:"uri"`
} `json:"artifactLocation"`
Region struct {
StartLine int `json:"startLine"`
StartColumn int `json:"startColumn"`
EndLine int `json:"endLine"`
EndColumn int `json:"endColumn"`
} `json:"region"`
} `json:"physicalLocation"`
} `json:"locations"`
}
// SarifReport represents the structure of a SARIF report
type SarifReport struct {
Runs []struct {
Results []SarifResult `json:"results"`
} `json:"runs"`
}
func TestRunPmdToFile(t *testing.T) {
homeDirectory, err := os.UserHomeDir()
if err != nil {
log.Fatal(err.Error())
}
currentDirectory, err := os.Getwd()
if err != nil {
log.Fatal(err.Error())
}
// Use the correct path relative to tools directory
testDirectory := filepath.Join(currentDirectory, "testdata", "repositories", "pmd")
repositoryCache := filepath.Join(testDirectory, ".codacy")
globalCache := filepath.Join(homeDirectory, ".cache/codacy")
tempResultFile := filepath.Join(os.TempDir(), "pmd.sarif")
defer os.Remove(tempResultFile)
config := *config.NewConfigType(testDirectory, repositoryCache, globalCache)
// Use absolute paths
repositoryToAnalyze := testDirectory
// Use the standard ruleset file for testing the PMD runner functionality
//rulesetFile := filepath.Join(testDirectory, "ruleset.xml")
// Use the same path as defined in plugin.yaml
pmdBinary := filepath.Join(globalCache, "tools/pmd@6.55.0/pmd-bin-6.55.0/bin/run.sh")
// Run PMD
err = RunPmd(repositoryToAnalyze, pmdBinary, nil, tempResultFile, "sarif", config)
if err != nil {
t.Fatalf("Failed to run pmd: %v", err)
}
// Check if the output file was created
obtainedSarifBytes, err := os.ReadFile(tempResultFile)
if err != nil {
t.Fatalf("Failed to read output file: %v", err)
}
// Normalize paths in the obtained SARIF output
obtainedSarif := string(obtainedSarifBytes)
obtainedSarif = strings.ReplaceAll(obtainedSarif, currentDirectory+"/", "")
// Parse the normalized SARIF output
var sarifReport SarifReport
err = json.Unmarshal([]byte(obtainedSarif), &sarifReport)
if err != nil {
t.Fatalf("Failed to parse SARIF output: %v", err)
}
// Verify we have results
assert.NotEmpty(t, sarifReport.Runs, "SARIF report should have at least one run")
assert.NotEmpty(t, sarifReport.Runs[0].Results, "SARIF report should have at least one result")
// Define expected violations
expectedViolations := map[string]bool{
"UnusedPrivateField": false,
"ShortVariable": false,
"AtLeastOneConstructor": false,
"CommentRequired": false,
}
// Check each result
for _, result := range sarifReport.Runs[0].Results {
// Mark this rule as found
expectedViolations[result.RuleID] = true
// Verify the file path is correct
assert.Contains(t, result.Locations[0].PhysicalLocation.ArtifactLocation.URI, "RulesBreaker.java",
"Violation should be in RulesBreaker.java")
// Verify line numbers are reasonable
assert.Greater(t, result.Locations[0].PhysicalLocation.Region.StartLine, 0,
"Start line should be positive")
assert.Less(t, result.Locations[0].PhysicalLocation.Region.StartLine, 30,
"Start line should be within the file")
}
// Verify all expected violations were found
for ruleID, found := range expectedViolations {
assert.True(t, found, "Expected violation %s was not found", ruleID)
}
}