github.com/argoproj/argo-cd/v3@v3.2.1/util/glob/glob_test.go (about) 1 package glob 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/require" 7 ) 8 9 func Test_Match(t *testing.T) { 10 tests := []struct { 11 name string 12 input string 13 pattern string 14 result bool 15 }{ 16 {"Exact match", "hello", "hello", true}, 17 {"Non-match exact", "hello", "hell", false}, 18 {"Long glob match", "hello", "hell*", true}, 19 {"Short glob match", "hello", "h*", true}, 20 {"Glob non-match", "hello", "e*", false}, 21 {"Invalid pattern", "e[[a*", "e[[a*", false}, 22 } 23 24 for _, tt := range tests { 25 t.Run(tt.name, func(t *testing.T) { 26 res := Match(tt.pattern, tt.input) 27 require.Equal(t, tt.result, res) 28 }) 29 } 30 } 31 32 func Test_MatchList(t *testing.T) { 33 tests := []struct { 34 name string 35 input string 36 list []string 37 patternMatch string 38 result bool 39 }{ 40 {"Exact name in list", "test", []string{"test"}, EXACT, true}, 41 {"Exact name not in list", "test", []string{"other"}, EXACT, false}, 42 {"Exact name not in list, multiple elements", "test", []string{"some", "other"}, EXACT, false}, 43 {"Exact name not in list, list empty", "test", []string{}, EXACT, false}, 44 {"Exact name not in list, empty element", "test", []string{""}, EXACT, false}, 45 {"Glob name in list, but exact wanted", "test", []string{"*"}, EXACT, false}, 46 {"Glob name in list with simple wildcard", "test", []string{"*"}, GLOB, true}, 47 {"Glob name in list without wildcard", "test", []string{"test"}, GLOB, true}, 48 {"Glob name in list, multiple elements", "test", []string{"other*", "te*"}, GLOB, true}, 49 {"match everything but specified word: fail", "disallowed", []string{"/^((?!disallowed).)*$/"}, REGEXP, false}, 50 {"match everything but specified word: pass", "allowed", []string{"/^((?!disallowed).)*$/"}, REGEXP, true}, 51 } 52 53 for _, tt := range tests { 54 t.Run(tt.name, func(t *testing.T) { 55 res := MatchStringInList(tt.list, tt.input, tt.patternMatch) 56 require.Equal(t, tt.result, res) 57 }) 58 } 59 } 60 61 func Test_MatchWithError(t *testing.T) { 62 tests := []struct { 63 name string 64 input string 65 pattern string 66 result bool 67 expectedErr string 68 }{ 69 {"Exact match", "hello", "hello", true, ""}, 70 {"Non-match exact", "hello", "hell", false, ""}, 71 {"Long glob match", "hello", "hell*", true, ""}, 72 {"Short glob match", "hello", "h*", true, ""}, 73 {"Glob non-match", "hello", "e*", false, ""}, 74 {"Invalid pattern", "e[[a*", "e[[a*", false, "unexpected end of input"}, 75 } 76 77 for _, tt := range tests { 78 t.Run(tt.name, func(t *testing.T) { 79 res, err := MatchWithError(tt.pattern, tt.input) 80 require.Equal(t, tt.result, res) 81 if tt.expectedErr == "" { 82 require.NoError(t, err) 83 } else { 84 require.ErrorContains(t, err, tt.expectedErr) 85 } 86 }) 87 } 88 }