|
| 1 | +package unixfsnode_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/ipfs/go-unixfsnode" |
| 9 | + "github.com/ipld/go-ipld-prime" |
| 10 | + "github.com/ipld/go-ipld-prime/codec/dagjson" |
| 11 | + "github.com/ipld/go-ipld-prime/traversal/selector/builder" |
| 12 | + selectorparse "github.com/ipld/go-ipld-prime/traversal/selector/parse" |
| 13 | + "github.com/stretchr/testify/require" |
| 14 | +) |
| 15 | + |
| 16 | +// Selectors are tested against JSON expected forms; this doesn't necessarily |
| 17 | +// validate that they work as advertised. It's just a sanity check that the |
| 18 | +// selectors are being built as expected. |
| 19 | + |
| 20 | +var exploreAllJson = mustDagJson(selectorparse.CommonSelector_ExploreAllRecursively) |
| 21 | + |
| 22 | +// explore interpret-as (~), next (>), match (.), interpreted as unixfs-preload |
| 23 | +var matchUnixfsPreloadJson = `{"~":{">":{".":{}},"as":"unixfs-preload"}}` |
| 24 | + |
| 25 | +// match interpret-as (~), next (>), match (.), interpreted as unixfs |
| 26 | +var matchUnixfsJson = `{"~":{">":{".":{}},"as":"unixfs"}}` |
| 27 | + |
| 28 | +func TestUnixFSPathSelector(t *testing.T) { |
| 29 | + testCases := []struct { |
| 30 | + name string |
| 31 | + path string |
| 32 | + expextedSelector string |
| 33 | + }{ |
| 34 | + { |
| 35 | + name: "empty path", |
| 36 | + path: "", |
| 37 | + expextedSelector: matchUnixfsJson, |
| 38 | + }, |
| 39 | + { |
| 40 | + name: "single field", |
| 41 | + path: "/foo", |
| 42 | + expextedSelector: jsonFields(matchUnixfsJson, "foo"), |
| 43 | + }, |
| 44 | + { |
| 45 | + name: "multiple fields", |
| 46 | + path: "/foo/bar", |
| 47 | + expextedSelector: jsonFields(matchUnixfsJson, "foo", "bar"), |
| 48 | + }, |
| 49 | + { |
| 50 | + name: "leading slash optional", |
| 51 | + path: "foo/bar", |
| 52 | + expextedSelector: jsonFields(matchUnixfsJson, "foo", "bar"), |
| 53 | + }, |
| 54 | + { |
| 55 | + name: "trailing slash optional", |
| 56 | + path: "/foo/bar/", |
| 57 | + expextedSelector: jsonFields(matchUnixfsJson, "foo", "bar"), |
| 58 | + }, |
| 59 | + { |
| 60 | + // a go-ipld-prime specific thing, not clearly specified by path spec (?) |
| 61 | + name: ".. is a field named ..", |
| 62 | + path: "/foo/../bar/", |
| 63 | + expextedSelector: jsonFields(matchUnixfsJson, "foo", "..", "bar"), |
| 64 | + }, |
| 65 | + { |
| 66 | + // a go-ipld-prime specific thing, not clearly specified by path spec |
| 67 | + name: "redundant slashes ignored", |
| 68 | + path: "foo///bar", |
| 69 | + expextedSelector: jsonFields(matchUnixfsJson, "foo", "bar"), |
| 70 | + }, |
| 71 | + } |
| 72 | + |
| 73 | + for _, tc := range testCases { |
| 74 | + t.Run(tc.name, func(t *testing.T) { |
| 75 | + sel := unixfsnode.UnixFSPathSelector(tc.path) |
| 76 | + require.Equal(t, tc.expextedSelector, mustDagJson(sel)) |
| 77 | + }) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +func TestUnixFSPathSelectorBuilder(t *testing.T) { |
| 82 | + testCases := []struct { |
| 83 | + name string |
| 84 | + path string |
| 85 | + target builder.SelectorSpec |
| 86 | + matchPath bool |
| 87 | + expextedSelector string |
| 88 | + }{ |
| 89 | + { |
| 90 | + name: "empty path", |
| 91 | + path: "", |
| 92 | + target: unixfsnode.ExploreAllRecursivelySelector, |
| 93 | + expextedSelector: exploreAllJson, |
| 94 | + }, |
| 95 | + { |
| 96 | + name: "empty path shallow", |
| 97 | + path: "", |
| 98 | + target: unixfsnode.MatchUnixFSPreloadSelector, |
| 99 | + expextedSelector: matchUnixfsPreloadJson, |
| 100 | + }, |
| 101 | + { |
| 102 | + name: "single field", |
| 103 | + path: "/foo", |
| 104 | + expextedSelector: jsonFields(exploreAllJson, "foo"), |
| 105 | + target: unixfsnode.ExploreAllRecursivelySelector, |
| 106 | + }, |
| 107 | + { |
| 108 | + name: "single field, match path", |
| 109 | + path: "/foo", |
| 110 | + expextedSelector: jsonFieldsMatchPoint(exploreAllJson, "foo"), |
| 111 | + target: unixfsnode.ExploreAllRecursivelySelector, |
| 112 | + matchPath: true, |
| 113 | + }, |
| 114 | + { |
| 115 | + name: "single field shallow", |
| 116 | + path: "/foo", |
| 117 | + expextedSelector: jsonFields(matchUnixfsPreloadJson, "foo"), |
| 118 | + target: unixfsnode.MatchUnixFSPreloadSelector, |
| 119 | + }, |
| 120 | + { |
| 121 | + name: "multiple fields", |
| 122 | + path: "/foo/bar", |
| 123 | + expextedSelector: jsonFields(exploreAllJson, "foo", "bar"), |
| 124 | + target: unixfsnode.ExploreAllRecursivelySelector, |
| 125 | + }, |
| 126 | + { |
| 127 | + name: "multiple fields, match path", |
| 128 | + path: "/foo/bar", |
| 129 | + expextedSelector: jsonFieldsMatchPoint(exploreAllJson, "foo", "bar"), |
| 130 | + target: unixfsnode.ExploreAllRecursivelySelector, |
| 131 | + matchPath: true, |
| 132 | + }, |
| 133 | + { |
| 134 | + name: "multiple fields shallow", |
| 135 | + path: "/foo/bar", |
| 136 | + expextedSelector: jsonFields(matchUnixfsPreloadJson, "foo", "bar"), |
| 137 | + target: unixfsnode.MatchUnixFSPreloadSelector, |
| 138 | + }, |
| 139 | + { |
| 140 | + name: "leading slash optional", |
| 141 | + path: "foo/bar", |
| 142 | + expextedSelector: jsonFields(exploreAllJson, "foo", "bar"), |
| 143 | + target: unixfsnode.ExploreAllRecursivelySelector, |
| 144 | + }, |
| 145 | + { |
| 146 | + name: "trailing slash optional", |
| 147 | + path: "/foo/bar/", |
| 148 | + expextedSelector: jsonFields(exploreAllJson, "foo", "bar"), |
| 149 | + target: unixfsnode.ExploreAllRecursivelySelector, |
| 150 | + }, |
| 151 | + // a go-ipld-prime specific thing, not clearly specified by path spec (?) |
| 152 | + { |
| 153 | + name: ".. is a field named ..", |
| 154 | + path: "/foo/../bar/", |
| 155 | + expextedSelector: jsonFields(exploreAllJson, "foo", "..", "bar"), |
| 156 | + target: unixfsnode.ExploreAllRecursivelySelector, |
| 157 | + }, |
| 158 | + { |
| 159 | + // a go-ipld-prime specific thing, not clearly specified by path spec |
| 160 | + name: "redundant slashes ignored", |
| 161 | + path: "foo///bar", |
| 162 | + expextedSelector: jsonFields(exploreAllJson, "foo", "bar"), |
| 163 | + target: unixfsnode.ExploreAllRecursivelySelector, |
| 164 | + }, |
| 165 | + } |
| 166 | + |
| 167 | + for _, tc := range testCases { |
| 168 | + t.Run(tc.name, func(t *testing.T) { |
| 169 | + sel := unixfsnode.UnixFSPathSelectorBuilder(tc.path, tc.target, tc.matchPath) |
| 170 | + require.Equal(t, tc.expextedSelector, mustDagJson(sel)) |
| 171 | + }) |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +func jsonFields(target string, fields ...string) string { |
| 176 | + var sb strings.Builder |
| 177 | + for _, n := range fields { |
| 178 | + // explore interpret-as (~) next (>), explore field (f) + specific field (f>), with field name |
| 179 | + sb.WriteString(fmt.Sprintf(`{"~":{">":{"f":{"f>":{"%s":`, n)) |
| 180 | + } |
| 181 | + sb.WriteString(target) |
| 182 | + sb.WriteString(strings.Repeat(`}}},"as":"unixfs"}}`, len(fields))) |
| 183 | + return sb.String() |
| 184 | +} |
| 185 | + |
| 186 | +func jsonFieldsMatchPoint(target string, fields ...string) string { |
| 187 | + var sb strings.Builder |
| 188 | + for _, n := range fields { |
| 189 | + // union (|) of match (.) and explore interpret-as (~) next (>), explore field (f) + specific field (f>), with field name |
| 190 | + sb.WriteString(fmt.Sprintf(`{"|":[{".":{}},{"~":{">":{"f":{"f>":{"%s":`, n)) |
| 191 | + } |
| 192 | + sb.WriteString(target) |
| 193 | + sb.WriteString(strings.Repeat(`}}},"as":"unixfs"}}]}`, len(fields))) |
| 194 | + return sb.String() |
| 195 | +} |
| 196 | + |
| 197 | +func mustDagJson(n ipld.Node) string { |
| 198 | + byts, err := ipld.Encode(n, dagjson.Encode) |
| 199 | + if err != nil { |
| 200 | + panic(err) |
| 201 | + } |
| 202 | + return string(byts) |
| 203 | +} |
0 commit comments