-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfix_test.go
More file actions
255 lines (204 loc) · 6.44 KB
/
fix_test.go
File metadata and controls
255 lines (204 loc) · 6.44 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package lint_test
import (
"os"
"path/filepath"
"strings"
"testing"
lint "github.com/channel-io/cht-go-lint"
_ "github.com/channel-io/cht-go-lint/fixers"
_ "github.com/channel-io/cht-go-lint/rules"
)
func TestFixDeclarationOrder(t *testing.T) {
dir := t.TempDir()
writeGoFile(t, dir, "go.mod", "module example.com/test\n\ngo 1.22\n")
writeGoFile(t, dir, "bad.go", `package test
func Hello() {}
const Name = "test"
var X = 1
type MyStruct struct{}
`)
cfg := &lint.Config{
Root: dir,
ModulePath: "example.com/test",
Rules: map[string]lint.RuleConfig{
"structure/declaration-order": {Severity: lint.Error},
},
}
// First check without fix — should have violations
report := lint.Check(cfg)
if report.ErrorCount() == 0 {
t.Fatal("expected violations before fix")
}
// Fix and re-check
report = lint.CheckWithFix(cfg, true, false)
if report.ErrorCount() != 0 {
t.Errorf("expected 0 violations after fix, got %d:\n%s", report.ErrorCount(), report.String())
}
fixResults := report.FixResults()
if len(fixResults) != 1 {
t.Errorf("expected 1 fix result, got %d", len(fixResults))
}
// Verify file content is reordered
content, _ := os.ReadFile(filepath.Join(dir, "bad.go"))
src := string(content)
constIdx := strings.Index(src, "const Name")
varIdx := strings.Index(src, "var X")
structIdx := strings.Index(src, "type MyStruct")
funcIdx := strings.Index(src, "func Hello")
if constIdx > varIdx || varIdx > structIdx || structIdx > funcIdx {
t.Errorf("declarations not in expected order:\n%s", src)
}
}
func TestFixDryRun(t *testing.T) {
dir := t.TempDir()
writeGoFile(t, dir, "go.mod", "module example.com/test\n\ngo 1.22\n")
writeGoFile(t, dir, "bad.go", `package test
func Hello() {}
const Name = "test"
`)
original, _ := os.ReadFile(filepath.Join(dir, "bad.go"))
cfg := &lint.Config{
Root: dir,
ModulePath: "example.com/test",
Rules: map[string]lint.RuleConfig{
"structure/declaration-order": {Severity: lint.Error},
},
}
report := lint.CheckWithFix(cfg, true, true)
// Should report fix results
if len(report.FixResults()) == 0 {
t.Error("expected fix results in dry-run")
}
// File should NOT be modified
after, _ := os.ReadFile(filepath.Join(dir, "bad.go"))
if string(after) != string(original) {
t.Error("dry-run should not modify files")
}
// Should still report violations (file wasn't fixed)
if report.ErrorCount() == 0 {
t.Error("expected violations in dry-run (file not modified)")
}
}
func TestFixPreservesComments(t *testing.T) {
dir := t.TempDir()
writeGoFile(t, dir, "go.mod", "module example.com/test\n\ngo 1.22\n")
writeGoFile(t, dir, "commented.go", `package test
// Hello says hello.
func Hello() {}
// Name is the name.
const Name = "test"
`)
cfg := &lint.Config{
Root: dir,
ModulePath: "example.com/test",
Rules: map[string]lint.RuleConfig{
"structure/declaration-order": {Severity: lint.Error},
},
}
report := lint.CheckWithFix(cfg, true, false)
if report.ErrorCount() != 0 {
t.Errorf("expected 0 violations after fix, got %d:\n%s", report.ErrorCount(), report.String())
}
content, _ := os.ReadFile(filepath.Join(dir, "commented.go"))
src := string(content)
// Comment should be associated with const, not orphaned
if !strings.Contains(src, "// Name is the name.\nconst Name") {
t.Errorf("comment not preserved with const declaration:\n%s", src)
}
if !strings.Contains(src, "// Hello says hello.\nfunc Hello") {
t.Errorf("comment not preserved with func declaration:\n%s", src)
}
}
func TestFixNoChangeOnCorrectOrder(t *testing.T) {
dir := t.TempDir()
writeGoFile(t, dir, "go.mod", "module example.com/test\n\ngo 1.22\n")
writeGoFile(t, dir, "good.go", `package test
const Name = "test"
var X = 1
type MyStruct struct{}
func Hello() {}
`)
cfg := &lint.Config{
Root: dir,
ModulePath: "example.com/test",
Rules: map[string]lint.RuleConfig{
"structure/declaration-order": {Severity: lint.Error},
},
}
report := lint.CheckWithFix(cfg, true, false)
if len(report.FixResults()) != 0 {
t.Errorf("expected 0 fix results for correct file, got %d", len(report.FixResults()))
}
if report.ErrorCount() != 0 {
t.Errorf("expected 0 violations for correct file, got %d", report.ErrorCount())
}
}
func TestFixImportsStayAtTop(t *testing.T) {
dir := t.TempDir()
writeGoFile(t, dir, "go.mod", "module example.com/test\n\ngo 1.22\n")
writeGoFile(t, dir, "imports.go", `package test
import "fmt"
func Hello() { fmt.Println("hi") }
const Name = "test"
`)
cfg := &lint.Config{
Root: dir,
ModulePath: "example.com/test",
Rules: map[string]lint.RuleConfig{
"structure/declaration-order": {Severity: lint.Error},
},
}
report := lint.CheckWithFix(cfg, true, false)
if report.ErrorCount() != 0 {
t.Errorf("expected 0 violations after fix, got %d:\n%s", report.ErrorCount(), report.String())
}
content, _ := os.ReadFile(filepath.Join(dir, "imports.go"))
src := string(content)
importIdx := strings.Index(src, "import")
constIdx := strings.Index(src, "const Name")
funcIdx := strings.Index(src, "func Hello")
if importIdx > constIdx || importIdx > funcIdx {
t.Errorf("import should stay at top:\n%s", src)
}
if constIdx > funcIdx {
t.Errorf("const should appear before func:\n%s", src)
}
}
func TestFixWithLayerOverrides(t *testing.T) {
dir := t.TempDir()
writeGoFile(t, dir, "go.mod", "module example.com/test\n\ngo 1.22\n")
// File where func comes before struct — violation in default order but valid in overridden order
writeGoFile(t, dir, "internal/model/entity.go", `package model
func NewUser() *User { return &User{} }
type User struct{}
`)
cfg := &lint.Config{
Root: dir,
ModulePath: "example.com/test",
Layers: []lint.LayerConfig{
{Name: "model", MayImport: []string{}},
},
Location: &lint.LocationConfig{
Strategy: "flat-pkg",
Options: map[string]any{"roots": []any{"internal"}},
},
Rules: map[string]lint.RuleConfig{
"structure/declaration-order": {
Severity: lint.Error,
Options: map[string]any{
"layer_overrides": map[string]any{
"model": []any{"func", "struct"},
},
},
},
},
}
report := lint.CheckWithFix(cfg, true, false)
// With layer override [func, struct], the file is already correct
if len(report.FixResults()) != 0 {
t.Errorf("expected 0 fix results (file matches layer override order), got %d", len(report.FixResults()))
}
if report.ErrorCount() != 0 {
t.Errorf("expected 0 violations, got %d:\n%s", report.ErrorCount(), report.String())
}
}