github.com/noqcks/syft@v0.0.0-20230920222752-a9e2c4e288e5/internal/file/glob_match_test.go (about)

     1  package file
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  )
     7  
     8  func TestGlobMatch(t *testing.T) {
     9  	var tests = []struct {
    10  		pattern string
    11  		data    string
    12  		ok      bool
    13  	}{
    14  		{"", "", true},
    15  		{"x", "", false},
    16  		{"", "x", false},
    17  		{"abc", "abc", true},
    18  		{"*", "abc", true},
    19  		{"*c", "abc", true},
    20  		{"*b", "abc", false},
    21  		{"a*", "abc", true},
    22  		{"b*", "abc", false},
    23  		{"a*", "a", true},
    24  		{"*a", "a", true},
    25  		{"a*b*c*d*e*", "axbxcxdxe", true},
    26  		{"a*b*c*d*e*", "axbxcxdxexxx", true},
    27  		{"a*b?c*x", "abxbbxdbxebxczzx", true},
    28  		{"a*b?c*x", "abxbbxdbxebxczzy", false},
    29  		{"a*a*a*a*b", strings.Repeat("a", 100), false},
    30  		{"*x", "xxx", true},
    31  		{"/home/place/**", "/home/place/a/thing", true},
    32  	}
    33  
    34  	for _, test := range tests {
    35  		if GlobMatch(test.pattern, test.data) != test.ok {
    36  			t.Errorf("failed glob='%s' data='%s'", test.pattern, test.data)
    37  		}
    38  	}
    39  }