fork/gdu

git clone https://git.julia.blue/fork/gdu

VXNCXNXfix(ignore): anchor the whole ignore-pattern alternation (#623)5782be9

subvolume
13.0 KiB496 linesraw
1package common_test
2
3import (
4	"os"
5	"path/filepath"
6	"testing"
7
8	log "github.com/sirupsen/logrus"
9
10	"github.com/dundee/gdu/v5/internal/common"
11	"github.com/stretchr/testify/assert"
12)
13
14func init() {
15	log.SetLevel(log.WarnLevel)
16}
17
18func TestCreateIgnorePattern(t *testing.T) {
19	re, err := common.CreateIgnorePattern([]string{"[abc]+"})
20
21	assert.Nil(t, err)
22	assert.True(t, re.MatchString("aa"))
23}
24
25func TestCreateIgnorePatternAnchoredSingleRelative(t *testing.T) {
26	re, err := common.CreateIgnorePattern([]string{"test_dir/abc"})
27
28	assert.Nil(t, err)
29	assert.True(t, re.MatchString("test_dir/abc"))
30	assert.False(t, re.MatchString("test_dir/abcdef"))
31}
32
33func TestCreateIgnorePatternAnchoredMiddleAlternative(t *testing.T) {
34	re, err := common.CreateIgnorePattern([]string{"aaa", `foo\.bak`, "ccc"})
35
36	assert.Nil(t, err)
37	assert.True(t, re.MatchString("foo.bak"))
38	assert.False(t, re.MatchString("xfoo.baky"))
39}
40
41func TestCreateIgnorePatternWithErr(t *testing.T) {
42	re, err := common.CreateIgnorePattern([]string{"[[["})
43
44	assert.NotNil(t, err)
45	assert.Nil(t, re)
46}
47
48func TestEmptyIgnore(t *testing.T) {
49	ui := &common.UI{}
50	shouldBeIgnored := ui.CreateIgnoreFunc()
51
52	assert.False(t, shouldBeIgnored("abc", "/abc"))
53	assert.False(t, shouldBeIgnored("xxx", "/xxx"))
54}
55
56func TestIgnoreByAbsPath(t *testing.T) {
57	ui := &common.UI{}
58	ui.SetIgnoreDirPaths([]string{"/abc"})
59	shouldBeIgnored := ui.CreateIgnoreFunc()
60
61	assert.True(t, shouldBeIgnored("abc", "/abc"))
62	assert.False(t, shouldBeIgnored("xxx", "/xxx"))
63}
64
65func TestIgnoreByPattern(t *testing.T) {
66	ui := &common.UI{}
67	err := ui.SetIgnoreDirPatterns([]string{"/[abc]+"})
68	assert.Nil(t, err)
69	shouldBeIgnored := ui.CreateIgnoreFunc()
70
71	assert.True(t, shouldBeIgnored("aaa", "/aaa"))
72	assert.True(t, shouldBeIgnored("aaa", "/aaabc"))
73	assert.False(t, shouldBeIgnored("xxx", "/xxx"))
74}
75
76func TestIgnoreFromFile(t *testing.T) {
77	file, err := os.OpenFile("ignore", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o600)
78	if err != nil {
79		panic(err)
80	}
81	defer file.Close()
82
83	if _, err := file.WriteString("/aaa\n"); err != nil {
84		panic(err)
85	}
86	if _, err := file.WriteString("/aaabc\n"); err != nil {
87		panic(err)
88	}
89	if _, err := file.WriteString("/[abd]+\n"); err != nil {
90		panic(err)
91	}
92
93	ui := &common.UI{}
94	err = ui.SetIgnoreFromFile("ignore")
95	assert.Nil(t, err)
96	shouldBeIgnored := ui.CreateIgnoreFunc()
97
98	assert.True(t, shouldBeIgnored("aaa", "/aaa"))
99	assert.True(t, shouldBeIgnored("aaabc", "/aaabc"))
100	assert.True(t, shouldBeIgnored("aaabd", "/aaabd"))
101	assert.False(t, shouldBeIgnored("xxx", "/xxx"))
102}
103
104func TestIgnoreFromNotExistingFile(t *testing.T) {
105	ui := &common.UI{}
106	err := ui.SetIgnoreFromFile("xxx")
107	assert.NotNil(t, err)
108}
109
110func TestIgnoreHidden(t *testing.T) {
111	ui := &common.UI{}
112	ui.SetIgnoreHidden(true)
113	shouldBeIgnored := ui.CreateIgnoreFunc()
114
115	assert.True(t, shouldBeIgnored(".git", "/aaa/.git"))
116	assert.True(t, shouldBeIgnored(".bbb", "/aaa/.bbb"))
117	assert.False(t, shouldBeIgnored("xxx", "/xxx"))
118}
119
120func TestIgnoreByAbsPathAndHidden(t *testing.T) {
121	ui := &common.UI{}
122	ui.SetIgnoreDirPaths([]string{"/abc"})
123	ui.SetIgnoreHidden(true)
124	shouldBeIgnored := ui.CreateIgnoreFunc()
125
126	assert.True(t, shouldBeIgnored("abc", "/abc"))
127	assert.True(t, shouldBeIgnored(".git", "/aaa/.git"))
128	assert.True(t, shouldBeIgnored(".bbb", "/aaa/.bbb"))
129	assert.False(t, shouldBeIgnored("xxx", "/xxx"))
130}
131
132func TestIgnoreByAbsPathAndPattern(t *testing.T) {
133	ui := &common.UI{}
134	ui.SetIgnoreDirPaths([]string{"/abc"})
135	err := ui.SetIgnoreDirPatterns([]string{"/[abc]+"})
136	assert.Nil(t, err)
137	shouldBeIgnored := ui.CreateIgnoreFunc()
138
139	assert.True(t, shouldBeIgnored("abc", "/abc"))
140	assert.True(t, shouldBeIgnored("aabc", "/aabc"))
141	assert.True(t, shouldBeIgnored("ccc", "/ccc"))
142	assert.False(t, shouldBeIgnored("xxx", "/xxx"))
143}
144
145func TestIgnoreByPatternAndHidden(t *testing.T) {
146	ui := &common.UI{}
147	err := ui.SetIgnoreDirPatterns([]string{"/[abc]+"})
148	assert.Nil(t, err)
149	ui.SetIgnoreHidden(true)
150	shouldBeIgnored := ui.CreateIgnoreFunc()
151
152	assert.True(t, shouldBeIgnored("abbc", "/abbc"))
153	assert.True(t, shouldBeIgnored(".git", "/aaa/.git"))
154	assert.True(t, shouldBeIgnored(".bbb", "/aaa/.bbb"))
155	assert.False(t, shouldBeIgnored("xxx", "/xxx"))
156}
157
158func TestIgnoreByAll(t *testing.T) {
159	ui := &common.UI{}
160	ui.SetIgnoreDirPaths([]string{"/abc"})
161	err := ui.SetIgnoreDirPatterns([]string{"/[abc]+"})
162	assert.Nil(t, err)
163	ui.SetIgnoreHidden(true)
164	shouldBeIgnored := ui.CreateIgnoreFunc()
165
166	assert.True(t, shouldBeIgnored("abc", "/abc"))
167	assert.True(t, shouldBeIgnored("aabc", "/aabc"))
168	assert.True(t, shouldBeIgnored(".git", "/aaa/.git"))
169	assert.True(t, shouldBeIgnored(".bbb", "/aaa/.bbb"))
170	assert.False(t, shouldBeIgnored("xxx", "/xxx"))
171}
172
173func TestIgnoreByRelativePath(t *testing.T) {
174	ui := &common.UI{}
175	ui.SetIgnoreDirPaths([]string{"test_dir/abc"})
176	shouldBeIgnored := ui.CreateIgnoreFunc()
177
178	assert.True(t, shouldBeIgnored("abc", "test_dir/abc"))
179	absPath, err := filepath.Abs("test_dir/abc")
180	assert.Nil(t, err)
181	assert.True(t, shouldBeIgnored("abc", absPath))
182	assert.False(t, shouldBeIgnored("xxx", "test_dir/xxx"))
183}
184
185func TestIgnoreByRelativePattern(t *testing.T) {
186	ui := &common.UI{}
187	err := ui.SetIgnoreDirPatterns([]string{"test_dir/[abc]+"})
188	assert.Nil(t, err)
189	shouldBeIgnored := ui.CreateIgnoreFunc()
190
191	assert.True(t, shouldBeIgnored("abc", "test_dir/abc"))
192	absPath, err := filepath.Abs("test_dir/abc")
193	assert.Nil(t, err)
194	assert.True(t, shouldBeIgnored("abc", absPath))
195	assert.False(t, shouldBeIgnored("xxx", "test_dir/xxx"))
196}
197
198func TestIgnoreFromFileWithRelativePaths(t *testing.T) {
199	file, err := os.OpenFile("ignore", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o600)
200	if err != nil {
201		panic(err)
202	}
203	defer file.Close()
204	defer os.Remove("ignore")
205
206	if _, err := file.WriteString("test_dir/aaa\n"); err != nil {
207		panic(err)
208	}
209	if _, err := file.WriteString("node_modules/[^/]+\n"); err != nil {
210		panic(err)
211	}
212
213	ui := &common.UI{}
214	err = ui.SetIgnoreFromFile("ignore")
215	assert.Nil(t, err)
216	shouldBeIgnored := ui.CreateIgnoreFunc()
217
218	assert.True(t, shouldBeIgnored("aaa", "test_dir/aaa"))
219	absPath, err := filepath.Abs("test_dir/aaa")
220	assert.Nil(t, err)
221	assert.True(t, shouldBeIgnored("aaa", absPath))
222	assert.False(t, shouldBeIgnored("xxx", "test_dir/xxx"))
223}
224
225func TestShouldFileBeIgnoredByType(t *testing.T) {
226	tests := []struct {
227		name            string
228		ignoreTypes     []string
229		filename        string
230		expectedIgnored bool
231	}{
232		{
233			name:            "no ignore types",
234			ignoreTypes:     []string{},
235			filename:        "test.yaml",
236			expectedIgnored: false,
237		},
238		{
239			name:            "ignore yaml",
240			ignoreTypes:     []string{"yaml"},
241			filename:        "test.yaml",
242			expectedIgnored: true,
243		},
244		{
245			name:            "ignore json",
246			ignoreTypes:     []string{"json"},
247			filename:        "test.json",
248			expectedIgnored: true,
249		},
250		{
251			name:            "ignore multiple types",
252			ignoreTypes:     []string{"yaml", "json"},
253			filename:        "test.yaml",
254			expectedIgnored: true,
255		},
256		{
257			name:            "ignore multiple types - not matched",
258			ignoreTypes:     []string{"yaml", "json"},
259			filename:        "test.txt",
260			expectedIgnored: false,
261		},
262
263		{
264			name:            "ignore with uppercase",
265			ignoreTypes:     []string{"YAML"},
266			filename:        "test.yaml",
267			expectedIgnored: true,
268		},
269		{
270			name:            "ignore file without extension",
271			ignoreTypes:     []string{"yaml"},
272			filename:        "test",
273			expectedIgnored: false,
274		},
275		{
276			name:            "ignore with dot in extension",
277			ignoreTypes:     []string{".yaml"},
278			filename:        "test.yaml",
279			expectedIgnored: true,
280		},
281	}
282
283	for _, tt := range tests {
284		t.Run(tt.name, func(t *testing.T) {
285			ui := &common.UI{}
286			ui.SetIgnoreTypes(tt.ignoreTypes)
287			if len(tt.ignoreTypes) > 0 {
288				assert.True(t, ui.IsFilteringFiles())
289			} else {
290				assert.False(t, ui.IsFilteringFiles())
291			}
292
293			actual := ui.ShouldFileBeIgnoredByType(tt.filename)
294			assert.Equal(t, tt.expectedIgnored, actual)
295		})
296	}
297}
298
299func TestShouldFileBeIncludedByType(t *testing.T) {
300	tests := []struct {
301		name             string
302		includeTypes     []string
303		filename         string
304		expectedIncluded bool
305	}{
306		{
307			name:             "no include types",
308			includeTypes:     []string{},
309			filename:         "test.yaml",
310			expectedIncluded: true,
311		},
312		{
313			name:             "include yaml",
314			includeTypes:     []string{"yaml"},
315			filename:         "test.yaml",
316			expectedIncluded: true,
317		},
318		{
319			name:             "include json",
320			includeTypes:     []string{"json"},
321			filename:         "test.json",
322			expectedIncluded: true,
323		},
324		{
325			name:             "include multiple types",
326			includeTypes:     []string{"yaml", "json"},
327			filename:         "test.yaml",
328			expectedIncluded: true,
329		},
330		{
331			name:             "include multiple types - not matched",
332			includeTypes:     []string{"yaml", "json"},
333			filename:         "test.txt",
334			expectedIncluded: false,
335		},
336
337		{
338			name:             "include with uppercase",
339			includeTypes:     []string{"YAML"},
340			filename:         "test.yaml",
341			expectedIncluded: true,
342		},
343		{
344			name:             "include file without extension",
345			includeTypes:     []string{"yaml"},
346			filename:         "test",
347			expectedIncluded: false,
348		},
349		{
350			name:             "include with dot in extension",
351			includeTypes:     []string{".yaml"},
352			filename:         "test.yaml",
353			expectedIncluded: true,
354		},
355	}
356
357	for _, tt := range tests {
358		t.Run(tt.name, func(t *testing.T) {
359			ui := &common.UI{}
360			ui.SetIncludeTypes(tt.includeTypes)
361			if len(tt.includeTypes) > 0 {
362				assert.True(t, ui.IsFilteringFiles())
363			} else {
364				assert.False(t, ui.IsFilteringFiles())
365			}
366
367			actual := ui.ShouldFileBeIncludedByType(tt.filename)
368			assert.Equal(t, tt.expectedIncluded, actual)
369		})
370	}
371}
372
373func TestCreateFileTypeFilter(t *testing.T) {
374	tests := []struct {
375		name             string
376		includeTypes     []string
377		ignoreTypes      []string
378		filename         string
379		expectedFiltered bool
380	}{
381		{
382			name:             "no filters",
383			includeTypes:     []string{},
384			ignoreTypes:      []string{},
385			filename:         "test.yaml",
386			expectedFiltered: false,
387		},
388		{
389			name:             "include filter - matched",
390			includeTypes:     []string{"yaml"},
391			ignoreTypes:      []string{},
392			filename:         "test.yaml",
393			expectedFiltered: false,
394		},
395		{
396			name:             "include filter - not matched",
397			includeTypes:     []string{"json"},
398			ignoreTypes:      []string{},
399			filename:         "test.yaml",
400			expectedFiltered: true,
401		},
402		{
403			name:             "ignore filter - matched",
404			includeTypes:     []string{},
405			ignoreTypes:      []string{"yaml"},
406			filename:         "test.yaml",
407			expectedFiltered: true,
408		},
409		{
410			name:             "ignore filter - not matched",
411			includeTypes:     []string{},
412			ignoreTypes:      []string{"json"},
413			filename:         "test.yaml",
414			expectedFiltered: false,
415		},
416		{
417			name:             "include filter takes precedence",
418			includeTypes:     []string{"yaml"},
419			ignoreTypes:      []string{"yaml"},
420			filename:         "test.yaml",
421			expectedFiltered: false,
422		},
423	}
424
425	for _, tt := range tests {
426		t.Run(tt.name, func(t *testing.T) {
427			ui := &common.UI{}
428			ui.SetIncludeTypes(tt.includeTypes)
429			ui.SetIgnoreTypes(tt.ignoreTypes)
430
431			filter := ui.CreateFileTypeFilter()
432			var actual bool
433			if filter == nil {
434				// When filter is nil, no filtering is applied, so file should not be filtered
435				actual = false
436			} else {
437				actual = filter(tt.filename)
438			}
439			assert.Equal(t, tt.expectedFiltered, actual)
440		})
441	}
442}
443
444func TestFileTypeFilterWithRealFiles(t *testing.T) {
445	// Create a temporary directory with test files
446	tmpDir := t.TempDir()
447
448	// Create test files
449	testFiles := []struct {
450		name     string
451		content  string
452		expected bool // expected to be included
453	}{
454		{"test.yaml", "key: value", true},
455		{"test.json", "{\"key\": \"value\"}", true},
456		{"test.txt", "plain text", false},
457		{"test.go", "package main", false},
458		{"noextension", "no extension", false},
459	}
460
461	for _, tf := range testFiles {
462		filePath := filepath.Join(tmpDir, tf.name)
463		err := os.WriteFile(filePath, []byte(tf.content), 0644)
464		assert.NoError(t, err)
465	}
466
467	// Test include filter
468	ui := &common.UI{}
469	ui.SetIncludeTypes([]string{"yaml", "json"})
470	filter := ui.CreateFileTypeFilter()
471
472	for _, tf := range testFiles {
473		actual := filter(tf.name)
474		expected := !tf.expected // filter returns true if file should be filtered out
475		assert.Equal(t, expected, actual, "Failed for file: %s", tf.name)
476	}
477
478	// Test ignore filter
479	ui2 := &common.UI{}
480	ui2.SetIgnoreTypes([]string{"txt", "go"})
481	filter2 := ui2.CreateFileTypeFilter()
482
483	for _, tf := range testFiles {
484		actual := filter2(tf.name)
485		// For ignore filter, yaml and json should not be filtered, txt and go should be filtered
486		expected := tf.name == "test.txt" || tf.name == "test.go"
487		assert.Equal(t, expected, actual, "Failed for file: %s", tf.name)
488	}
489}
490
491func TestCreateFileTypeFilterReturnsNilWhenNoFiltering(t *testing.T) {
492	ui := &common.UI{}
493	// No include or ignore types set
494	filter := ui.CreateFileTypeFilter()
495	assert.Nil(t, filter, "CreateFileTypeFilter should return nil when no filtering is configured")
496}