-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpmdConfigCreator_test.go
181 lines (156 loc) · 5.21 KB
/
pmdConfigCreator_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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package tools
import (
"encoding/xml"
"os"
"path/filepath"
"strings"
"testing"
"codacy/cli-v2/domain"
"github.com/stretchr/testify/assert"
)
// PMDRuleset represents the structure of a PMD ruleset XML
type PMDRuleset struct {
XMLName xml.Name `xml:"ruleset"`
Name string `xml:"name,attr"`
Xmlns string `xml:"xmlns,attr"`
XmlnsXsi string `xml:"xmlns:xsi,attr"`
SchemaLoc string `xml:"xsi:schemaLocation,attr"`
Description string `xml:"description,omitempty"`
Rules []PMDRule `xml:"rule"`
}
type PMDRule struct {
Ref string `xml:"ref,attr"`
Properties *PMDProperties `xml:"properties,omitempty"`
}
type PMDProperties struct {
Properties []PMDProperty `xml:"property"`
}
type PMDProperty struct {
Name string `xml:"name,attr"`
Value string `xml:"value,attr"`
}
func TestCreatePmdConfig(t *testing.T) {
// Setup test configuration with patterns
config := []domain.PatternConfiguration{
{
PatternDefinition: domain.PatternDefinition{
Id: "java/codestyle/AtLeastOneConstructor",
},
Parameters: []domain.ParameterConfiguration{
{
Name: "enabled",
Value: "true",
},
},
},
{
PatternDefinition: domain.PatternDefinition{
Id: "java/design/UnusedPrivateField",
},
Parameters: []domain.ParameterConfiguration{
{
Name: "enabled",
Value: "true",
},
},
},
{
PatternDefinition: domain.PatternDefinition{
Id: "java/design/LoosePackageCoupling",
},
Parameters: []domain.ParameterConfiguration{
{
Name: "enabled",
Value: "true",
},
{
Name: "packages",
Value: "java.util,java.io",
},
},
},
}
// Generate PMD config
generatedConfig := CreatePmdConfig(config)
// Read expected ruleset
expectedRulesetPath := filepath.Join("testdata", "repositories", "pmd", "expected-ruleset.xml")
expectedRulesetBytes, err := os.ReadFile(expectedRulesetPath)
if err != nil {
t.Fatalf("Failed to read expected ruleset: %v", err)
}
// Normalize both configurations by removing whitespace differences
normalizeXML := func(xml string) string {
// Remove all whitespace between tags
xml = strings.ReplaceAll(xml, ">\n", ">")
xml = strings.ReplaceAll(xml, ">\t", ">")
xml = strings.ReplaceAll(xml, ">\r", ">")
xml = strings.ReplaceAll(xml, "\n<", "<")
xml = strings.ReplaceAll(xml, "\t<", "<")
xml = strings.ReplaceAll(xml, "\r<", "<")
// Remove multiple spaces
xml = strings.Join(strings.Fields(xml), " ")
return xml
}
normalizedGenerated := normalizeXML(generatedConfig)
normalizedExpected := normalizeXML(string(expectedRulesetBytes))
// Compare the normalized configurations
assert.Equal(t, normalizedExpected, normalizedGenerated, "Generated PMD config should match expected ruleset")
}
func TestCreatePmdConfigWithDisabledRules(t *testing.T) {
config := []domain.PatternConfiguration{
{
PatternDefinition: domain.PatternDefinition{
Id: "java/codestyle/AtLeastOneConstructor",
},
Parameters: []domain.ParameterConfiguration{
{
Name: "enabled",
Value: "false",
},
},
},
}
obtainedConfig := CreatePmdConfig(config)
var ruleset PMDRuleset
err := xml.Unmarshal([]byte(obtainedConfig), &ruleset)
if err != nil {
t.Fatalf("Failed to parse generated XML: %v", err)
}
for _, rule := range ruleset.Rules {
assert.NotContains(t, rule.Ref, "AtLeastOneConstructor", "Disabled rule should not appear in config")
}
}
func TestCreatePmdConfigEmpty(t *testing.T) {
cwd, err := os.Getwd()
if err != nil {
t.Fatalf("Failed to get current directory: %v", err)
}
tmpDir := t.TempDir()
err = os.MkdirAll(filepath.Join(tmpDir, "plugins", "tools", "pmd"), 0755)
if err != nil {
t.Fatalf("Failed to create test directory structure: %v", err)
}
defaultRuleset := `<?xml version="1.0" encoding="UTF-8"?>
<ruleset name="Default PMD Ruleset"
xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 http://pmd.sourceforge.net/ruleset_2_0_0.xsd">
<!-- Default rules here -->
</ruleset>`
err = os.WriteFile(filepath.Join(tmpDir, "plugins", "tools", "pmd", "default-ruleset.xml"), []byte(defaultRuleset), 0644)
if err != nil {
t.Fatalf("Failed to write test ruleset: %v", err)
}
err = os.Chdir(tmpDir)
if err != nil {
t.Fatalf("Failed to change directory: %v", err)
}
defer os.Chdir(cwd)
config := []domain.PatternConfiguration{}
obtainedConfig := CreatePmdConfig(config)
assert.Contains(t, obtainedConfig, `name="Default PMD Ruleset"`, "XML should contain the correct ruleset name")
assert.Contains(t, obtainedConfig, `xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"`, "XML should contain the correct xmlns")
assert.Contains(t, obtainedConfig, `xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 http://pmd.sourceforge.net/ruleset_2_0_0.xsd"`, "XML should contain the correct schema location")
assert.Contains(t, obtainedConfig, `<rule ref="category/java/bestpractices.xml/AvoidReassigningParameters"/>`, "XML should contain expected rules")
assert.Contains(t, obtainedConfig, `<rule ref="category/java/codestyle.xml/ClassNamingConventions">`, "XML should contain rules with properties")
}