github.com/mondo192/jfrog-client-go@v1.0.0/utils/antUtils_test.go (about)

     1  package utils
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"regexp"
     7  	"strings"
     8  	"testing"
     9  )
    10  
    11  var separator = string(os.PathSeparator)
    12  
    13  var paths = getFileSystemsPathsForTestingAntPattern(separator)
    14  
    15  var testAntPathToRegExpDataProvider = []struct {
    16  	description           string
    17  	antPattern            string
    18  	paths                 []string
    19  	expectedMatchingPaths []string
    20  }{
    21  	{"check '?' in file's name", filepath.Join("dev", "a", "b?.txt"), paths, []string{filepath.Join("dev", "a", "bb.txt"), filepath.Join("dev", "a", "bc.txt")}},
    22  	{"check '?' in directory's name", filepath.Join("dev", "a?", "b.txt"), paths, []string{filepath.Join("dev", "aa", "b.txt")}},
    23  	{"check '*' in file's name", filepath.Join("dev", "a", "b*.txt"), paths, []string{filepath.Join("dev", "a", "b.txt"), filepath.Join("dev", "a", "bb.txt"), filepath.Join("dev", "a", "bc.txt")}},
    24  	{"check '*' in directory's name", filepath.Join("dev", "*", "b.txt"), paths, []string{filepath.Join("dev", "a", "b.txt"), filepath.Join("dev", "aa", "b.txt")}},
    25  	{"check '*' in directory's name", filepath.Join("dev", "*", "a", "b.txt"), paths, nil},
    26  	{"check '**' in directory path", filepath.Join("**", "b.txt"), paths, []string{filepath.Join("dev", "a", "b.txt"), filepath.Join("dev", "aa", "b.txt"), filepath.Join("dev", "a1", "a2", "a3", "b.txt"), filepath.Join("dev", "a1", "a2", "b.txt"), filepath.Join("test", "a", "b.txt"), filepath.Join("test", "aa", "b.txt")}},
    27  	{"check '**' in the beginning and the end of path", filepath.Join("**", "a2", "**"), paths, []string{filepath.Join("dev", "a1", "a2", "a3", "b.txt"), filepath.Join("dev", "a1", "a2", "b.txt"), filepath.Join("dev", "a1", "a2", "a3", "bc.txt"), filepath.Join("dev", "a1", "a2", "bc.txt"), "a2"}},
    28  	{"check '**' in the beginning and the end of path", filepath.Join("**a2**"), paths, []string{filepath.Join("dev", "a1", "a2", "a3", "b.txt"), filepath.Join("dev", "a1", "a2", "b.txt"), filepath.Join("dev", "a1", "a2", "a3", "bc.txt"), filepath.Join("dev", "a1", "a2", "bc.txt"), "a2"}},
    29  	{"check double '**'", filepath.Join("**", "a2", "**", "**"), paths, []string{filepath.Join("dev", "a1", "a2", "a3", "b.txt"), filepath.Join("dev", "a1", "a2", "b.txt"), filepath.Join("dev", "a1", "a2", "a3", "bc.txt"), filepath.Join("dev", "a1", "a2", "bc.txt")}},
    30  	{"check '**' in the beginning and the end of file", filepath.Join("**", "b.zip", "**"), paths, []string{filepath.Join("dev", "aa", "b.zip"), filepath.Join("test", "aa", "b.zip"), filepath.Join("b.zip"), filepath.Join("test2", "b.zip")}},
    31  	{"combine '**' and '*'", filepath.Join("**", "a2", "*"), paths, []string{filepath.Join("dev", "a1", "a2", "b.txt"), filepath.Join("dev", "a1", "a2", "bc.txt")}},
    32  	{"combine '**' and '*'", filepath.Join("**", "a2", "*", "**"), paths, []string{filepath.Join("dev", "a1", "a2", "a3", "b.txt"), filepath.Join("dev", "a1", "a2", "b.txt"), filepath.Join("dev", "a1", "a2", "a3", "bc.txt"), filepath.Join("dev", "a1", "a2", "bc.txt")}},
    33  	{"combine all signs", filepath.Join("**", "b?.*"), paths, []string{filepath.Join("dev", "a", "bb.txt"), filepath.Join("dev", "a", "bc.txt"), filepath.Join("dev", "aa", "bb.txt"), filepath.Join("dev", "aa", "bc.txt"), filepath.Join("dev", "aa", "bc.zip"), filepath.Join("dev", "a1", "a2", "a3", "bc.txt"), filepath.Join("dev", "a1", "a2", "bc.txt"), filepath.Join("test", "a", "bb.txt"), filepath.Join("test", "a", "bc.txt"), filepath.Join("test", "aa", "bb.txt"), filepath.Join("test", "aa", "bc.txt"), filepath.Join("test", "aa", "bc.zip")}},
    34  	{"'**' all files", filepath.Join("**"), paths, paths},
    35  	{"test2/**/b/**", filepath.Join("test2", "**", "b", "**"), paths, []string{filepath.Join("test2", "a", "b", "c.zip")}},
    36  	{"*/b.zip", filepath.Join("*", "b.zip"), paths, []string{filepath.Join("test2", "b.zip")}},
    37  	{"**/dev/**/a3/*c*", filepath.Join("dev", "**", "a3", "*c*"), paths, []string{filepath.Join("dev", "a1", "a2", "a3", "bc.txt")}},
    38  	{"**/dev/**/a3/**", filepath.Join("dev", "**", "a3", "**"), paths, []string{filepath.Join("dev", "a1", "a2", "a3", "bc.txt"), filepath.Join("dev", "a1", "a2", "a3", "b.txt")}},
    39  	{"exclude 'temp/foo5/a'", filepath.Join("**", "foo", "**"), paths, []string{filepath.Join("tmp", "foo", "a"), filepath.Join("tmp", "foo")}},
    40  	{"include dirs", filepath.Join("tmp", "*", "**"), paths, []string{"tmp" + separator, filepath.Join("tmp", "foo", "a"), filepath.Join("tmp", "foo5", "a"), filepath.Join("tmp", "foo"), filepath.Join("tmp", "foo5")}},
    41  	{"include dirs", filepath.Join("tmp", "**"), paths, []string{"tmp" + separator, filepath.Join("tmp", "foo", "a"), filepath.Join("tmp", "foo5", "a"), filepath.Join("tmp", "foo"), filepath.Join("tmp", "foo5")}},
    42  	{"double and single wildcard", filepath.Join("**", "tmp*", "**"), paths, []string{"tmp" + separator, filepath.Join("tmp", "foo", "a"), filepath.Join("tmp", "foo5", "a"), filepath.Join("tmp", "foo"), filepath.Join("tmp", "foo5"), filepath.Join("Wrapper", "tmp", "boo"), filepath.Join("Wrapper", "tmp12", "boo")}},
    43  	{"exclude only sub dir", filepath.Join("**", "loo", "**", "bar", "**"), paths, []string{filepath.Join("kmp", "loo", "bar"), filepath.Join("kmp", "loo", "bar", "b"), filepath.Join("kmp", "loo", "bar", "a")}},
    44  	{"**/", "**" + separator, paths, paths},
    45  	{"xxx/x*", filepath.Join("tmp", "f*"), paths, []string{filepath.Join("tmp", "foo"), filepath.Join("tmp", "foo5")}},
    46  	{"xxx/x*x", filepath.Join("tmp", "f*5"), paths, []string{filepath.Join("tmp", "foo5")}},
    47  	{"xxx/x*", filepath.Join("dev", "a1", "a2", "b*"), paths, []string{filepath.Join("dev", "a1", "a2", "b.txt"), filepath.Join("dev", "a1", "a2", "bc.txt")}},
    48  	{"xxx/*x*", filepath.Join("dev", "a1", "a2", "*c*"), paths, []string{filepath.Join("dev", "a1", "a2", "bc.txt")}},
    49  	{"*", filepath.Join("*"), paths, []string{"b.zip", "a2"}},
    50  	{"*", filepath.Join("*"), []string{"a", "a" + separator, filepath.Join("a", "b")}, []string{"a"}},
    51  }
    52  
    53  // In each case, we take an array of paths, simulating a filesystem hierarchy, and an ANT pattern expression and
    54  // check if the conversion to regular expression worked.
    55  func TestAntPathToRegExp(t *testing.T) {
    56  	for _, test := range testAntPathToRegExpDataProvider {
    57  		t.Run(test.description, func(t *testing.T) {
    58  			regExpStr := AntToRegex(cleanPath(test.antPattern))
    59  			var matches []string
    60  			for _, checkedPath := range test.paths {
    61  				match, _ := regexp.MatchString(regExpStr, checkedPath)
    62  				if match {
    63  					matches = append(matches, checkedPath)
    64  				}
    65  			}
    66  			if !equalSlicesIgnoreOrder(matches, test.expectedMatchingPaths) {
    67  				t.Error("Unmatched! : ant pattern `" + test.antPattern + "` matches paths:\n[" + strings.Join(test.expectedMatchingPaths, ",") + "]\nbut got:\n[" + strings.Join(matches, ",") + "]")
    68  			}
    69  		})
    70  	}
    71  }
    72  
    73  var testAntToRegexProvider = []struct {
    74  	ant           string
    75  	expectedRegex string
    76  }{
    77  	{"a.zip", "^a\\.zip$"},
    78  	{"ab", "^ab$"},
    79  	{"**", "^(.*)$"},
    80  	{"**/", "^(.*/)*(.*)$"},
    81  	{"**/*", "^(.*/)*([^/]*)$"},
    82  	{"/**", "^(/.*)*$"},
    83  	{"*/**", "^([^/]*)(/.*)*$"},
    84  	{"/**/ab", "^/(.*/)*ab$"},
    85  	{"/**/ab*", "^/(.*/)*ab([^/]*)$"},
    86  	{"/**/ab/", "^/(.*/)*ab(/.*)*$"},
    87  	{"/**/ab/*", "^/(.*/)*ab/([^/]*)$"},
    88  	{"/**/ab*/", "^/(.*/)*ab([^/]*)(/.*)*$"},
    89  	{"ab/**/", "^ab/(.*/)*(.*)$"},
    90  	{"*ab/**/", "^([^/]*)ab/(.*/)*(.*)$"},
    91  	{"/ab/**/", "^/ab/(.*/)*(.*)$"},
    92  	{"/ab*/**/", "^/ab([^/]*)/(.*/)*(.*)$"},
    93  	{"/**/ab/**/", "^/(.*/)*ab/(.*/)*(.*)$"},
    94  	{"/**/a*b/**/", "^/(.*/)*a([^/]*)b/(.*/)*(.*)$"},
    95  	{"/**/ab/**/cd/**/ef/", "^/(.*/)*ab/(.*/)*cd/(.*/)*ef(/.*)*$"},
    96  }
    97  
    98  func TestAntToRegex(t *testing.T) {
    99  	for _, test := range testAntToRegexProvider {
   100  		t.Run("'"+test.ant+"'", func(t *testing.T) {
   101  			regExpStr := AntToRegex(cleanPath(strings.ReplaceAll(test.ant, "/", separator)))
   102  			expectedRegExpStr := strings.ReplaceAll(test.expectedRegex, "/", getFileSeparatorForAntToRegex())
   103  			if regExpStr != expectedRegExpStr {
   104  				t.Error("Unmatched! : ant pattern `" + test.ant + "` translated to:\n" + regExpStr + "\nbut expect it to be:\n" + expectedRegExpStr + "")
   105  			}
   106  		})
   107  	}
   108  }
   109  
   110  func getFileSystemsPathsForTestingAntPattern(separator string) []string {
   111  	return []string{
   112  		filepath.Join("dev", "a", "b.txt"),
   113  		filepath.Join("dev", "a", "bb.txt"),
   114  		filepath.Join("dev", "a", "bc.txt"),
   115  		filepath.Join("dev", "aa", "b.txt"),
   116  		filepath.Join("dev", "aa", "bb.txt"),
   117  		filepath.Join("dev", "aa", "bc.txt"),
   118  		filepath.Join("dev", "aa", "b.zip"),
   119  		filepath.Join("dev", "aa", "bc.zip"),
   120  		filepath.Join("dev", "a1", "a2", "a3", "b.txt"),
   121  		filepath.Join("dev", "a1", "a2", "b.txt"),
   122  		filepath.Join("dev", "a1", "a2", "a3", "bc.txt"),
   123  		filepath.Join("dev", "a1", "a2", "bc.txt"),
   124  		"a2",
   125  		filepath.Join("test", "a", "b.txt"),
   126  		filepath.Join("test", "a", "bb.txt"),
   127  		filepath.Join("test", "a", "bc.txt"),
   128  		filepath.Join("test", "aa", "b.txt"),
   129  		filepath.Join("test", "aa", "bb.txt"),
   130  		filepath.Join("test", "aa", "bc.txt"),
   131  		filepath.Join("test", "aa", "b.zip"),
   132  		filepath.Join("test", "aa", "bc.zip"),
   133  
   134  		filepath.Join("test2", "a", "b", "c.zip"),
   135  		filepath.Join("test2", "a", "bb", "c.zip"),
   136  		filepath.Join("test2", "b.zip"),
   137  		filepath.Join("b.zip"),
   138  		"tmp" + separator,
   139  		filepath.Join("tmp", "foo"),
   140  		filepath.Join("Wrapper", "tmp", "boo"),
   141  		filepath.Join("Wrapper", "tmp12", "boo"),
   142  		filepath.Join("tmp", "foo", "a"),
   143  		filepath.Join("tmp", "foo5"),
   144  		filepath.Join("tmp", "foo5", "a"),
   145  
   146  		filepath.Join("kmp", "loo"),
   147  		filepath.Join("kmp", "loo", "bar", "a"),
   148  		filepath.Join("kmp", "loo", "bar", "b"),
   149  		filepath.Join("kmp", "loo", "bar"),
   150  		filepath.Join("kmp", "loo", "lar"),
   151  		filepath.Join("kmp", "loo", "lar", "a"),
   152  		filepath.Join("kmp", "loo", "lar"),
   153  		filepath.Join("kmp", "loo", "kar", "a"),
   154  		filepath.Join("kmp", "loo", "kar"),
   155  	}
   156  }