|
| 1 | +package modelfile |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | +) |
| 10 | + |
| 11 | +func TestNewPathFilter(t *testing.T) { |
| 12 | + testcases := []struct { |
| 13 | + name string |
| 14 | + input []string |
| 15 | + expected []string |
| 16 | + expectError bool |
| 17 | + errorMsg string |
| 18 | + }{ |
| 19 | + { |
| 20 | + name: "normal patterns", |
| 21 | + input: []string{"*.log", "checkpoint*/"}, |
| 22 | + expected: []string{"*.log", "checkpoint*"}, |
| 23 | + }, |
| 24 | + { |
| 25 | + name: "invalid pattern", |
| 26 | + input: []string{"*.log", "[invalid"}, |
| 27 | + expectError: true, |
| 28 | + errorMsg: `invalid exclude pattern: "[invalid"`, |
| 29 | + }, |
| 30 | + } |
| 31 | + |
| 32 | + for _, tc := range testcases { |
| 33 | + t.Run(tc.name, func(t *testing.T) { |
| 34 | + filter, err := NewPathFilter(tc.input...) |
| 35 | + |
| 36 | + if tc.expectError { |
| 37 | + require.Error(t, err, "Expected an error for input: %q", tc.input) |
| 38 | + assert.Contains(t, err.Error(), tc.errorMsg) |
| 39 | + assert.Nil(t, filter) |
| 40 | + return |
| 41 | + } |
| 42 | + |
| 43 | + require.NoError(t, err, "Did not expect an error for input: %q", tc.input) |
| 44 | + require.NotNil(t, filter) |
| 45 | + assert.Equal(t, tc.expected, filter.patterns) |
| 46 | + }) |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +func TestPathFilter_Matches(t *testing.T) { |
| 51 | + testcases := []struct { |
| 52 | + filterName string |
| 53 | + patterns []string |
| 54 | + tests []struct { |
| 55 | + desc string |
| 56 | + path string |
| 57 | + expected bool |
| 58 | + } |
| 59 | + }{ |
| 60 | + { |
| 61 | + filterName: "Empty_Filter", |
| 62 | + patterns: []string{}, |
| 63 | + tests: []struct { |
| 64 | + desc string |
| 65 | + path string |
| 66 | + expected bool |
| 67 | + }{ |
| 68 | + {"any file", "any/path/file.txt", false}, |
| 69 | + {"root file", "main.go", false}, |
| 70 | + {"empty path", "", false}, |
| 71 | + }, |
| 72 | + }, |
| 73 | + { |
| 74 | + filterName: "Single_Asterisk_Filter", |
| 75 | + patterns: []string{"*.log"}, |
| 76 | + tests: []struct { |
| 77 | + desc string |
| 78 | + path string |
| 79 | + expected bool |
| 80 | + }{ |
| 81 | + {"matches a simple log file", "debug.log", true}, |
| 82 | + {"matches a hidden log file", ".config.log", true}, |
| 83 | + {"does not match if not at end", "debug.log.old", false}, |
| 84 | + {"does not match different extension", "main.go", false}, |
| 85 | + {"does not cross path separator", "logs/debug.log", false}, |
| 86 | + }, |
| 87 | + }, |
| 88 | + { |
| 89 | + filterName: "Directory_Wildcard_Filter", |
| 90 | + patterns: []string{"build/*"}, |
| 91 | + tests: []struct { |
| 92 | + desc string |
| 93 | + path string |
| 94 | + expected bool |
| 95 | + }{ |
| 96 | + {"matches file directly inside", "build/app", true}, |
| 97 | + {"matches hidden file inside", "build/.config", true}, |
| 98 | + {"does not match the directory itself", "build", false}, |
| 99 | + {"does not match nested files", "build/assets/icon.png", false}, |
| 100 | + }, |
| 101 | + }, |
| 102 | + { |
| 103 | + // Since filepath.Match does not support **, the behavior is the same as Directory_Wildcard_Filter |
| 104 | + filterName: "Directory_Double_Asterisk_Filter", |
| 105 | + patterns: []string{"build/**"}, |
| 106 | + tests: []struct { |
| 107 | + desc string |
| 108 | + path string |
| 109 | + expected bool |
| 110 | + }{ |
| 111 | + {"matches file directly inside", "build/app", true}, |
| 112 | + {"matches hidden file inside", "build/.config", true}, |
| 113 | + {"does not match the directory itself", "build", false}, |
| 114 | + {"does not match nested files", "build/assets/icon.png", false}, |
| 115 | + }, |
| 116 | + }, |
| 117 | + { |
| 118 | + filterName: "Directory_Filter", |
| 119 | + patterns: []string{"checkpoint/"}, |
| 120 | + tests: []struct { |
| 121 | + desc string |
| 122 | + path string |
| 123 | + expected bool |
| 124 | + }{ |
| 125 | + {"matches the directory itself", "checkpoint", true}, |
| 126 | + {"match file inside", "checkpoint/file.py", false}, |
| 127 | + }, |
| 128 | + }, |
| 129 | + { |
| 130 | + filterName: "Complex_Filter_With_Multiple_Patterns", |
| 131 | + patterns: []string{"*.tmp", ".git*", "build/"}, |
| 132 | + tests: []struct { |
| 133 | + desc string |
| 134 | + path string |
| 135 | + expected bool |
| 136 | + }{ |
| 137 | + {"matches a .tmp file", "temp.tmp", true}, |
| 138 | + {"matches .git directory", ".git", true}, |
| 139 | + {"matches .gitignore file", ".gitignore", true}, |
| 140 | + {"matches build directory exactly", "build", true}, |
| 141 | + {"does not cross separator", "data/cache.tmp", false}, |
| 142 | + {"does not match file inside build/", "build/app.js", false}, |
| 143 | + {"does not match src file", "src/main.go", false}, |
| 144 | + }, |
| 145 | + }, |
| 146 | + } |
| 147 | + |
| 148 | + for _, tc := range testcases { |
| 149 | + t.Run(tc.filterName, func(t *testing.T) { |
| 150 | + filter, err := NewPathFilter(tc.patterns...) |
| 151 | + require.NoError(t, err, "Filter creation with patterns %q failed", tc.patterns) |
| 152 | + require.NotNil(t, filter) |
| 153 | + |
| 154 | + for _, test := range tc.tests { |
| 155 | + t.Run(test.desc, func(t *testing.T) { |
| 156 | + result := filter.Match(test.path) |
| 157 | + assert.Equal(t, test.expected, result, fmt.Sprintf("Path: %q", test.path)) |
| 158 | + }) |
| 159 | + } |
| 160 | + }) |
| 161 | + } |
| 162 | +} |
0 commit comments