-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblocklist_test.go
More file actions
256 lines (228 loc) · 7.47 KB
/
Copy pathblocklist_test.go
File metadata and controls
256 lines (228 loc) · 7.47 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
256
package repomap
import (
"context"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestBlocklistConfig_ShouldSkipSymbol_Glob(t *testing.T) {
t.Parallel()
c := &BlocklistConfig{MethodBlocklist: []string{"Test*", "*Mock", "mustJSON"}}
require.NoError(t, c.compile())
cases := []struct {
name string
want bool
}{
{"TestFoo", true},
{"TestBar", true},
{"Regular", false},
{"UserMock", true},
{"MockUser", false},
{"mustJSON", true},
{"mustJson", false},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
assert.Equal(t, tc.want, c.ShouldSkipSymbol(tc.name))
})
}
}
func TestBlocklistConfig_ShouldSkipSymbol_Regex(t *testing.T) {
t.Parallel()
c := &BlocklistConfig{MethodBlocklist: []string{"/^pb_/", "/Marshal$/"}}
require.NoError(t, c.compile())
cases := []struct {
name string
want bool
}{
{"pb_User", true},
{"Pb_User", false},
{"User", false},
{"UnmarshalJSON", false},
{"MarshalJSON", false},
{"fooMarshal", true},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
assert.Equal(t, tc.want, c.ShouldSkipSymbol(tc.name))
})
}
}
func TestBlocklistConfig_ShouldSkipSymbol_NilReceiver(t *testing.T) {
t.Parallel()
var c *BlocklistConfig
assert.False(t, c.ShouldSkipSymbol("anything"))
}
func TestBlocklistConfig_ShouldSkipSymbol_EmptyConfig(t *testing.T) {
t.Parallel()
c := &BlocklistConfig{}
assert.False(t, c.ShouldSkipSymbol("anything"))
}
func TestLoadBlocklistConfig_Missing(t *testing.T) {
t.Parallel()
dir := t.TempDir()
c, err := LoadBlocklistConfig(dir)
require.NoError(t, err)
require.NotNil(t, c)
assert.False(t, c.ShouldSkipSymbol("TestFoo"))
}
func TestLoadBlocklistConfig_Malformed(t *testing.T) {
t.Parallel()
dir := t.TempDir()
path := filepath.Join(dir, ".repomap.yaml")
require.NoError(t, os.WriteFile(path, []byte("method_blocklist: [\n"), 0o644))
_, err := LoadBlocklistConfig(dir)
require.Error(t, err)
assert.Contains(t, err.Error(), ".repomap.yaml")
}
func TestLoadBlocklistConfig_InvalidRegex(t *testing.T) {
t.Parallel()
dir := t.TempDir()
path := filepath.Join(dir, ".repomap.yaml")
require.NoError(t, os.WriteFile(path, []byte("method_blocklist:\n - \"/[/\"\n"), 0o644))
_, err := LoadBlocklistConfig(dir)
require.Error(t, err)
assert.Contains(t, err.Error(), "invalid regex")
}
func TestLoadBlocklistConfig_Valid(t *testing.T) {
t.Parallel()
dir := t.TempDir()
yaml := `method_blocklist:
- "Test*"
- "/^pb_/"
- "mustJSON"
`
path := filepath.Join(dir, ".repomap.yaml")
require.NoError(t, os.WriteFile(path, []byte(yaml), 0o644))
c, err := LoadBlocklistConfig(dir)
require.NoError(t, err)
assert.True(t, c.ShouldSkipSymbol("TestFoo"))
assert.True(t, c.ShouldSkipSymbol("pb_User"))
assert.True(t, c.ShouldSkipSymbol("mustJSON"))
assert.False(t, c.ShouldSkipSymbol("Regular"))
}
// TestBlocklistConfig_EmptyPattern verifies that an empty string in the blocklist
// is silently skipped and does not match any symbol.
func TestBlocklistConfig_EmptyPattern(t *testing.T) {
t.Parallel()
c := &BlocklistConfig{MethodBlocklist: []string{"", " ", "Foo"}}
require.NoError(t, c.compile())
assert.True(t, c.ShouldSkipSymbol("Foo"), "Foo must be blocked")
assert.False(t, c.ShouldSkipSymbol("Bar"), "Bar must not be blocked")
assert.False(t, c.ShouldSkipSymbol(""), "empty name must not be blocked by empty pattern")
assert.False(t, c.ShouldSkipSymbol("anything"), "arbitrary name must not be blocked by empty/whitespace patterns")
}
// TestBlocklistConfig_DotPrefixedSymbols verifies that patterns work correctly when
// symbol names start with dots or slashes (edge cases for path.Match).
func TestBlocklistConfig_DotPrefixedSymbols(t *testing.T) {
t.Parallel()
c := &BlocklistConfig{MethodBlocklist: []string{".*", "/^\\./", "Normal"}}
require.NoError(t, c.compile())
cases := []struct {
name string
want bool
}{
{".hidden", true}, // matches ".*" glob
{".github", true}, // matches ".*" glob
{"Normal", true}, // explicit match
{"Public", false}, // no match
{"notdot", false}, // no leading dot
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
assert.Equal(t, tc.want, c.ShouldSkipSymbol(tc.name))
})
}
}
// TestBlocklistConfig_OverlappingPatterns verifies that a symbol matching multiple
// patterns is blocked (first-match wins; no double-counting needed).
func TestBlocklistConfig_OverlappingPatterns(t *testing.T) {
t.Parallel()
// Both "Test*" and "/^Test/" match "TestFoo" — must still block, not panic.
c := &BlocklistConfig{MethodBlocklist: []string{"Test*", "/^Test/"}}
require.NoError(t, c.compile())
assert.True(t, c.ShouldSkipSymbol("TestFoo"), "symbol matching multiple patterns must be blocked")
assert.False(t, c.ShouldSkipSymbol("Regular"), "non-matching symbol must not be blocked")
}
// TestBlocklistConfig_MixedGlobAndRegex verifies that glob and regex patterns
// coexist correctly — each evaluates independently.
func TestBlocklistConfig_MixedGlobAndRegex(t *testing.T) {
t.Parallel()
c := &BlocklistConfig{MethodBlocklist: []string{"*Mock", "/^gen_/", "mustJSON"}}
require.NoError(t, c.compile())
cases := []struct {
sym string
want bool
}{
{"ServerMock", true}, // glob *Mock
{"gen_user", true}, // regex ^gen_
{"mustJSON", true}, // exact glob
{"realFunc", false}, // no match
{"GenUser", false}, // case-sensitive regex — ^gen_ requires lowercase
{"mockServer", false}, // glob *Mock requires suffix
}
for _, tc := range cases {
t.Run(tc.sym, func(t *testing.T) {
t.Parallel()
assert.Equal(t, tc.want, c.ShouldSkipSymbol(tc.sym))
})
}
}
// TestBlocklistConfig_FilterSymbols_DotfilePattern verifies filterSymbols works
// end-to-end with a pattern that targets dot-prefixed names.
func TestBlocklistConfig_FilterSymbols_DotfilePattern(t *testing.T) {
t.Parallel()
c := &BlocklistConfig{MethodBlocklist: []string{".*"}}
require.NoError(t, c.compile())
fs := &FileSymbols{
Path: ".github/workflows/ci.yml",
Symbols: []Symbol{
{Name: ".hidden", Kind: "function", Exported: true},
{Name: "Visible", Kind: "function", Exported: true},
},
}
c.filterSymbols(fs)
require.Len(t, fs.Symbols, 1)
assert.Equal(t, "Visible", fs.Symbols[0].Name)
}
// TestBlocklistIntegration verifies Build filters symbols matching the blocklist.
// Requires a git repo because ScanFiles returns nil outside one.
func TestBlocklistIntegration(t *testing.T) {
t.Parallel()
if _, err := exec.LookPath("git"); err != nil {
t.Skip("git not available")
}
dir := t.TempDir()
goSrc := `package demo
// Foo is kept.
func Foo() {}
// TestFoo should be filtered out.
func TestFoo() {}
`
require.NoError(t, os.WriteFile(filepath.Join(dir, "demo.go"), []byte(goSrc), 0o644))
require.NoError(t, os.WriteFile(filepath.Join(dir, "go.mod"),
[]byte("module demo\n\ngo 1.26\n"), 0o644))
require.NoError(t, os.WriteFile(filepath.Join(dir, ".repomap.yaml"),
[]byte("method_blocklist:\n - \"Test*\"\n"), 0o644))
runGit := func(args ...string) {
cmd := exec.Command("git", args...)
cmd.Dir = dir
_ = cmd.Run()
}
runGit("init")
runGit("add", ".")
runGit("-c", "user.email=test@test.com", "-c", "user.name=Test", "commit", "-m", "init")
m := New(dir, DefaultConfig())
require.NoError(t, m.Build(context.Background()))
out := m.StringVerbose()
assert.Contains(t, out, "Foo", "Foo must be kept")
assert.False(t, strings.Contains(out, "TestFoo"),
"TestFoo must be filtered; got: %s", out)
}