github.com/treeverse/lakefs@v1.24.1-0.20240520134607-95648127bfb0/pkg/actions/lua/path/path_test.go (about) 1 package path_test 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/treeverse/lakefs/pkg/actions/lua/path" 8 ) 9 10 func TestIsHidden(t *testing.T) { 11 tbl := []struct { 12 Input string 13 Expected bool 14 }{ 15 { 16 Input: "foo/bar/baz", 17 Expected: false, 18 }, 19 { 20 Input: "foo/bar/", 21 Expected: false, 22 }, 23 { 24 Input: "", 25 Expected: false, 26 }, 27 { 28 Input: "/", 29 Expected: false, 30 }, 31 { 32 Input: "a/_b/c", 33 Expected: true, 34 }, 35 { 36 Input: "/_b/c", 37 Expected: true, 38 }, 39 { 40 Input: "/b_/c", 41 Expected: false, 42 }, 43 { 44 Input: "b/_c", 45 Expected: true, 46 }, 47 { 48 Input: "b/_c/", 49 Expected: true, 50 }, 51 { 52 Input: "/b/_c/", 53 Expected: true, 54 }, 55 { 56 Input: "/b/_c/a", 57 Expected: true, 58 }, 59 { 60 Input: "/b/_c/_a", 61 Expected: true, 62 }, 63 } 64 65 for _, cas := range tbl { 66 t.Run(cas.Input, func(t *testing.T) { 67 got := path.IsHidden(cas.Input, "/", "_") 68 if got != cas.Expected { 69 t.Errorf("%s: expected hidden = %v but got %v", cas.Input, cas.Expected, got) 70 } 71 }) 72 } 73 } 74 75 func TestJoin(t *testing.T) { 76 tbl := []struct { 77 Input []string 78 Expected string 79 }{ 80 { 81 Input: []string{"foo/bar/baz"}, 82 Expected: "foo/bar/baz", 83 }, 84 85 { 86 Input: []string{"", "bar", "baz"}, 87 Expected: "/bar/baz", 88 }, 89 { 90 Input: []string{"foo/", "bar", "baz"}, 91 Expected: "foo/bar/baz", 92 }, 93 { 94 Input: []string{"foo/", "bar", "/baz"}, 95 Expected: "foo/bar/baz", 96 }, 97 { 98 Input: []string{"a", "b", "c"}, 99 Expected: "a/b/c", 100 }, 101 { 102 Input: []string{"a", "b", "c/"}, 103 Expected: "a/b/c/", 104 }, 105 } 106 107 for i, cas := range tbl { 108 t.Run(fmt.Sprintf("join_%d", i), func(t *testing.T) { 109 got := path.Join(path.SEPARATOR, cas.Input...) 110 if got != cas.Expected { 111 t.Errorf("Expected '%s' got '%s'", cas.Expected, got) 112 } 113 }) 114 } 115 } 116 117 func TestParse(t *testing.T) { 118 tbl := []struct { 119 Input string 120 ExpectedBasename string 121 ExpectedParent string 122 }{ 123 { 124 Input: "foo/bar/baz", 125 ExpectedBasename: "baz", 126 ExpectedParent: "foo/bar/", 127 }, 128 { 129 Input: "bar", 130 ExpectedBasename: "bar", 131 ExpectedParent: "", 132 }, 133 { 134 Input: "bar/", 135 ExpectedBasename: "bar", 136 ExpectedParent: "", 137 }, 138 { 139 Input: "/bar", 140 ExpectedBasename: "bar", 141 ExpectedParent: "/", 142 }, 143 { 144 Input: "foo/bar/", 145 ExpectedParent: "foo/", 146 ExpectedBasename: "bar", 147 }, 148 { 149 Input: "", 150 ExpectedBasename: "", 151 ExpectedParent: "", 152 }, 153 } 154 155 for _, cas := range tbl { 156 t.Run(cas.Input, func(t *testing.T) { 157 got := path.Parse(cas.Input, path.SEPARATOR) 158 baseName := got["base_name"] 159 if baseName != cas.ExpectedBasename { 160 t.Errorf("base_name: expected '%s' got '%s'", cas.ExpectedBasename, baseName) 161 } 162 parent := got["parent"] 163 if parent != cas.ExpectedParent { 164 t.Errorf("parent: expected '%s' got '%s'", cas.ExpectedParent, parent) 165 } 166 }) 167 } 168 }