11package glob
22
33import (
4+ "errors"
45 "fmt"
56 "sync"
67 "testing"
@@ -11,8 +12,8 @@ import (
1112
1213// Test helpers - these access internal variables for testing purposes
1314
14- // clearGlobCache clears the cached glob patterns for testing.
15- func clearGlobCache () {
15+ // resetGlobCacheForTest clears the cached glob patterns for testing.
16+ func resetGlobCacheForTest () {
1617 globCacheLock .Lock ()
1718 defer globCacheLock .Unlock ()
1819 globCache .Clear ()
@@ -26,8 +27,8 @@ func isPatternCached(pattern string) bool {
2627 return ok
2728}
2829
29- // getCacheLen returns the number of cached patterns.
30- func getCacheLen () int {
30+ // globCacheLen returns the number of cached patterns.
31+ func globCacheLen () int {
3132 globCacheLock .Lock ()
3233 defer globCacheLock .Unlock ()
3334 return globCache .Len ()
@@ -116,7 +117,7 @@ func Test_MatchWithError(t *testing.T) {
116117
117118func Test_GlobCaching (t * testing.T ) {
118119 // Clear cache before test
119- clearGlobCache ()
120+ resetGlobCacheForTest ()
120121
121122 pattern := "test*pattern"
122123 text := "testABCpattern"
@@ -138,33 +139,42 @@ func Test_GlobCaching(t *testing.T) {
138139
139140func Test_GlobCachingConcurrent (t * testing.T ) {
140141 // Clear cache before test
141- clearGlobCache ()
142+ resetGlobCacheForTest ()
142143
143144 pattern := "concurrent*test"
144145 text := "concurrentABCtest"
145146
146147 var wg sync.WaitGroup
147148 numGoroutines := 100
149+ errChan := make (chan error , numGoroutines )
148150
149151 for i := 0 ; i < numGoroutines ; i ++ {
150152 wg .Add (1 )
151153 go func () {
152154 defer wg .Done ()
153155 result := Match (pattern , text )
154- require .True (t , result )
156+ if ! result {
157+ errChan <- errors .New ("expected match to return true" )
158+ }
155159 }()
156160 }
157161
158162 wg .Wait ()
163+ close (errChan )
164+
165+ // Check for any errors from goroutines
166+ for err := range errChan {
167+ t .Error (err )
168+ }
159169
160170 // Verify pattern is cached
161171 require .True (t , isPatternCached (pattern ))
162- require .Equal (t , 1 , getCacheLen (), "should only have one cached entry for the pattern" )
172+ require .Equal (t , 1 , globCacheLen (), "should only have one cached entry for the pattern" )
163173}
164174
165175func Test_GlobCacheLRUEviction (t * testing.T ) {
166176 // Clear cache before test
167- clearGlobCache ()
177+ resetGlobCacheForTest ()
168178
169179 // Fill cache beyond DefaultGlobCacheSize
170180 for i := 0 ; i < DefaultGlobCacheSize + 100 ; i ++ {
@@ -173,12 +183,38 @@ func Test_GlobCacheLRUEviction(t *testing.T) {
173183 }
174184
175185 // Cache size should be limited to DefaultGlobCacheSize
176- require .Equal (t , DefaultGlobCacheSize , getCacheLen (), "cache size should be limited to DefaultGlobCacheSize" )
186+ require .Equal (t , DefaultGlobCacheSize , globCacheLen (), "cache size should be limited to DefaultGlobCacheSize" )
187+
188+ // The oldest patterns should be evicted
189+ oldest := fmt .Sprintf ("pattern-%d-*" , 0 )
190+ require .False (t , isPatternCached (oldest ), "oldest pattern should be evicted" )
177191
178192 // The most recently used patterns should still be cached
179193 require .True (t , isPatternCached (fmt .Sprintf ("pattern-%d-*" , DefaultGlobCacheSize + 99 )), "most recent pattern should be cached" )
180194}
181195
196+ func Test_InvalidGlobNotCached (t * testing.T ) {
197+ // Clear cache before test
198+ resetGlobCacheForTest ()
199+
200+ invalidPattern := "e[[a*"
201+ text := "test"
202+
203+ // Match should return false for invalid pattern
204+ result := Match (invalidPattern , text )
205+ require .False (t , result )
206+
207+ // Invalid patterns should NOT be cached
208+ require .False (t , isPatternCached (invalidPattern ), "invalid pattern should not be cached" )
209+
210+ // Also test with MatchWithError
211+ _ , err := MatchWithError (invalidPattern , text )
212+ require .Error (t , err )
213+
214+ // Still should not be cached after MatchWithError
215+ require .False (t , isPatternCached (invalidPattern ), "invalid pattern should not be cached after MatchWithError" )
216+ }
217+
182218// BenchmarkMatch_WithCache benchmarks Match with caching (cache hit)
183219func BenchmarkMatch_WithCache (b * testing.B ) {
184220 pattern := "proj:*/app-*"
0 commit comments