github.com/cobalt77/jfrog-client-go@v0.14.5/artifactory/services/utils/repopathfile_test.go (about) 1 package utils 2 3 import ( 4 "strconv" 5 "testing" 6 ) 7 8 type CreateRepoPathFileTest struct { 9 pattern string 10 recursive bool 11 expected []RepoPathFile 12 } 13 14 var pathFilesDataProvider = []CreateRepoPathFileTest{ 15 {"a", true, 16 []RepoPathFile{{"r", ".", "a"}}}, 17 {"a/*", true, 18 []RepoPathFile{{"r", "a", "*"}, {"r", "a/*", "*"}}}, 19 {"a/a*b", true, 20 []RepoPathFile{{"r", "a", "a*b"}, {"r", "a/a*", "*b"}}}, 21 {"a/a*b*", true, 22 []RepoPathFile{{"r", "a/a*", "*b*"}, {"r", "a/a*", "*b*"}, {"r", "a/a*b*", "*"}}}, 23 {"a/a*b*/a/b", true, 24 []RepoPathFile{{"r", "a/a*b*/a", "b"}}}, 25 {"*/a*/*b*a*", true, 26 []RepoPathFile{{"r", "*/a*", "*b*a*"}, {"r", "*/a*/*", "*b*a*"}, {"r", "*/a*/*b*", "*a*"}, {"r", "*/a*/*b*a*", "*"}}}, 27 {"*", true, 28 []RepoPathFile{{"r", "*", "*"}}}, 29 {"*/*", true, 30 []RepoPathFile{{"r", "*", "*"}, {"r", "*/*", "*"}}}, 31 {"*/a.z", true, 32 []RepoPathFile{{"r", "*", "a.z"}}}, 33 {"a", false, 34 []RepoPathFile{{"r", ".", "a"}}}, 35 {"/*", false, 36 []RepoPathFile{{"r", "", "*"}}}, 37 {"/a*b", false, 38 []RepoPathFile{{"r", "", "a*b"}}}, 39 {"a*b*", true, 40 []RepoPathFile{{"r", "a*", "*b*"}, {"r", "a*b*", "*"}, {"r", ".", "a*b*"}}}, 41 {"*b*", true, 42 []RepoPathFile{{"r", "*b*", "*"}, {"r", "*", "*b*"}}}, 43 } 44 45 var repoPathFilesDataProvider = []CreateRepoPathFileTest{ 46 {"a/*", true, 47 []RepoPathFile{{"a", "*", "*"}}}, 48 {"a/a*b", true, 49 []RepoPathFile{{"a", "a*", "*b"}, {"a", ".", "a*b"}}}, 50 {"a/a*b*", true, 51 []RepoPathFile{{"a", "a*b*", "*"}, {"a", "a*", "*b*"}, {"a", ".", "a*b*"}}}, 52 {"a/a*b*/a/b", true, 53 []RepoPathFile{{"a", "a*b*/a", "b"}}}, 54 {"*a/b*/*c*d*", true, 55 []RepoPathFile{{"*", "*a/b*/*c*d*", "*"}, {"*", "*a/b*/*c*", "*d*"}, {"*", "*a/b*/*", "*c*d*"}, {"*", "*a/b*", "*c*d*"}, 56 {"*a", "b*", "*c*d*"}, {"*a", "b*/*c*", "*d*"}, {"*a", "b*/*", "*c*d*"}, {"*a", "b*/*c*d*", "*"}}}, 57 {"*aa/b*/*c*d*", true, 58 []RepoPathFile{{"*", "*aa/b*/*c*d*", "*"}, {"*", "*aa/b*/*c*", "*d*"}, {"*", "*aa/b*/*", "*c*d*"}, {"*", "*aa/b*", "*c*d*"}, 59 {"*aa", "b*", "*c*d*"}, {"*aa", "b*/*c*", "*d*"}, {"*aa", "b*/*", "*c*d*"}, {"*aa", "b*/*c*d*", "*"}}}, 60 {"*/a*/*b*a*", true, 61 []RepoPathFile{{"*", "a*/*b*a*", "*"}, {"*", "a*", "*b*a*"}, {"*", "a*/*b*", "*a*"}, {"*", "a*/*", "*b*a*"}}}, 62 {"*", true, 63 []RepoPathFile{{"*", "*", "*"}}}, 64 {"*/*", true, 65 []RepoPathFile{{"*", "*", "*"}}}, 66 {"*/a.z", true, 67 []RepoPathFile{{"*", ".", "a.z"}}}, 68 {"a/b", true, 69 []RepoPathFile{{"a", ".", "b"}}}, 70 {"a/b", false, 71 []RepoPathFile{{"a", ".", "b"}}}, 72 {"a//*", false, 73 []RepoPathFile{{"a", "", "*"}}}, 74 {"r//a*b", false, 75 []RepoPathFile{{"r", "", "a*b"}}}, 76 {"a*b", true, 77 []RepoPathFile{{"a*", "*", "*b"}, {"a*b", "*", "*"}}}, 78 {"a*b*", true, 79 []RepoPathFile{{"a*", "*b*", "*"}, {"a*", "*", "*b*"}, {"a*b*", "*", "*"}}}, 80 } 81 82 func TestCreatePathFilePairs(t *testing.T) { 83 for _, sample := range pathFilesDataProvider { 84 t.Run(sample.pattern+"_recursive_"+strconv.FormatBool(sample.recursive), func(t *testing.T) { 85 validateRepoPathFile(createPathFilePairs("r", sample.pattern, sample.recursive), sample.expected, sample.pattern, t) 86 }) 87 } 88 } 89 90 func TestCreateRepoPathFileTriples(t *testing.T) { 91 for _, sample := range repoPathFilesDataProvider { 92 t.Run(sample.pattern+"_recursive_"+strconv.FormatBool(sample.recursive), func(t *testing.T) { 93 validateRepoPathFile(createRepoPathFileTriples(sample.pattern, sample.recursive), sample.expected, sample.pattern, t) 94 }) 95 } 96 } 97 98 func validateRepoPathFile(actual, expected []RepoPathFile, pattern string, t *testing.T) { 99 if len(actual) != len(expected) { 100 t.Errorf("Wrong triple.\nPattern: %v\nExpected: %v\nActual: %v", pattern, expected, actual) 101 } 102 for _, triple := range expected { 103 found := false 104 for _, actualTriple := range actual { 105 if triple.repo == actualTriple.repo && triple.path == actualTriple.path && triple.file == actualTriple.file { 106 found = true 107 } 108 } 109 if found == false { 110 t.Errorf("Wrong triple for pattern: '%s'. Missing %v between %v", pattern, triple, actual) 111 } 112 } 113 }