| 1 | package gitattr |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "github.com/git-lfs/gitobj/v2" |
| 7 | "github.com/git-lfs/wildmatch/v2" |
| 8 | "github.com/stretchr/testify/assert" |
| 9 | "github.com/stretchr/testify/require" |
| 10 | ) |
| 11 | |
| 12 | var ( |
| 13 | dat = wildmatch.NewWildmatch("*.dat", |
| 14 | wildmatch.Basename, |
| 15 | wildmatch.SystemCase) |
| 16 | mp = NewMacroProcessor() |
| 17 | example = &Tree{ |
| 18 | mp: mp, |
| 19 | lines: []Line{ |
| 20 | &patternLine{ |
| 21 | pattern: dat, |
| 22 | lineAttrs: lineAttrs{ |
| 23 | attrs: []*Attr{ |
| 24 | { |
| 25 | K: "filter", V: "lfs", |
| 26 | }, |
| 27 | { |
| 28 | K: "diff", V: "lfs", |
| 29 | }, |
| 30 | { |
| 31 | K: "merge", V: "lfs", |
| 32 | }, |
| 33 | { |
| 34 | K: "text", V: "false", |
| 35 | }, |
| 36 | }, |
| 37 | }, |
| 38 | }, |
| 39 | }, |
| 40 | children: map[string]*Tree{ |
| 41 | "subdir": { |
| 42 | mp: mp, |
| 43 | lines: []Line{ |
| 44 | &patternLine{ |
| 45 | pattern: dat, |
| 46 | lineAttrs: lineAttrs{ |
| 47 | attrs: []*Attr{ |
| 48 | { |
| 49 | K: "subdir", V: "yes", |
| 50 | }, |
| 51 | }, |
| 52 | }, |
| 53 | }, |
| 54 | }, |
| 55 | }, |
| 56 | }, |
| 57 | } |
| 58 | ) |
| 59 | |
| 60 | func TestTreeAppliedInRoot(t *testing.T) { |
| 61 | attrs := example.Applied("a.dat") |
| 62 | |
| 63 | assert.Len(t, attrs, 4) |
| 64 | assert.Equal(t, attrs[0], &Attr{K: "filter", V: "lfs"}) |
| 65 | assert.Equal(t, attrs[1], &Attr{K: "diff", V: "lfs"}) |
| 66 | assert.Equal(t, attrs[2], &Attr{K: "merge", V: "lfs"}) |
| 67 | assert.Equal(t, attrs[3], &Attr{K: "text", V: "false"}) |
| 68 | } |
| 69 | |
| 70 | func TestTreeAppliedInSubtreeRelevant(t *testing.T) { |
| 71 | attrs := example.Applied("subdir/a.dat") |
| 72 | |
| 73 | assert.Len(t, attrs, 5) |
| 74 | assert.Equal(t, attrs[0], &Attr{K: "filter", V: "lfs"}) |
| 75 | assert.Equal(t, attrs[1], &Attr{K: "diff", V: "lfs"}) |
| 76 | assert.Equal(t, attrs[2], &Attr{K: "merge", V: "lfs"}) |
| 77 | assert.Equal(t, attrs[3], &Attr{K: "text", V: "false"}) |
| 78 | assert.Equal(t, attrs[4], &Attr{K: "subdir", V: "yes"}) |
| 79 | } |
| 80 | |
| 81 | func TestTreeAppliedInSubtreeIrrelevant(t *testing.T) { |
| 82 | attrs := example.Applied("subdir/a.txt") |
| 83 | |
| 84 | assert.Empty(t, attrs) |
| 85 | } |
| 86 | |
| 87 | func TestTreeAppliedInIrrelevantSubtree(t *testing.T) { |
| 88 | attrs := example.Applied("other/subdir/a.dat") |
| 89 | |
| 90 | assert.Len(t, attrs, 4) |
| 91 | assert.Equal(t, attrs[0], &Attr{K: "filter", V: "lfs"}) |
| 92 | assert.Equal(t, attrs[1], &Attr{K: "diff", V: "lfs"}) |
| 93 | assert.Equal(t, attrs[2], &Attr{K: "merge", V: "lfs"}) |
| 94 | assert.Equal(t, attrs[3], &Attr{K: "text", V: "false"}) |
| 95 | } |
| 96 | |
| 97 | func TestNewDiscoversSimpleTrees(t *testing.T) { |
| 98 | tmp := t.TempDir() |
| 99 | |
| 100 | db, err := gitobj.FromFilesystem(tmp, "") |
| 101 | require.NoError(t, err) |
| 102 | defer db.Close() |
| 103 | |
| 104 | blob, err := db.WriteBlob(gitobj.NewBlobFromBytes([]byte(` |
| 105 | *.dat filter=lfs diff=lfs merge=lfs -text |
| 106 | `))) |
| 107 | require.NoError(t, err) |
| 108 | |
| 109 | tree, err := New(db, &gitobj.Tree{Entries: []*gitobj.TreeEntry{ |
| 110 | { |
| 111 | Name: ".gitattributes", |
| 112 | Oid: blob, |
| 113 | Filemode: 0100644, |
| 114 | }, |
| 115 | }}) |
| 116 | require.NoError(t, err) |
| 117 | |
| 118 | attrs := tree.Applied("foo.dat") |
| 119 | |
| 120 | assert.Len(t, attrs, 4) |
| 121 | assert.Equal(t, attrs[0], &Attr{K: "filter", V: "lfs"}) |
| 122 | assert.Equal(t, attrs[1], &Attr{K: "diff", V: "lfs"}) |
| 123 | assert.Equal(t, attrs[2], &Attr{K: "merge", V: "lfs"}) |
| 124 | assert.Equal(t, attrs[3], &Attr{K: "text", V: "false"}) |
| 125 | } |
| 126 | |
| 127 | func TestNewDiscoversSimpleChildrenTrees(t *testing.T) { |
| 128 | tmp := t.TempDir() |
| 129 | |
| 130 | db, err := gitobj.FromFilesystem(tmp, "") |
| 131 | require.NoError(t, err) |
| 132 | defer db.Close() |
| 133 | |
| 134 | blob, err := db.WriteBlob(gitobj.NewBlobFromBytes([]byte(` |
| 135 | *.dat filter=lfs diff=lfs merge=lfs -text |
| 136 | `))) |
| 137 | require.NoError(t, err) |
| 138 | |
| 139 | child, err := db.WriteTree(&gitobj.Tree{Entries: []*gitobj.TreeEntry{ |
| 140 | { |
| 141 | Name: ".gitattributes", |
| 142 | Oid: blob, |
| 143 | Filemode: 0100644, |
| 144 | }, |
| 145 | }}) |
| 146 | require.NoError(t, err) |
| 147 | |
| 148 | tree, err := New(db, &gitobj.Tree{Entries: []*gitobj.TreeEntry{ |
| 149 | { |
| 150 | Name: "child", |
| 151 | Oid: child, |
| 152 | Filemode: 040000, |
| 153 | }, |
| 154 | }}) |
| 155 | require.NoError(t, err) |
| 156 | assert.Empty(t, tree.lines) |
| 157 | assert.Len(t, tree.children, 1) |
| 158 | |
| 159 | attrs := tree.Applied("child/foo.dat") |
| 160 | |
| 161 | assert.Len(t, attrs, 4) |
| 162 | assert.Equal(t, attrs[0], &Attr{K: "filter", V: "lfs"}) |
| 163 | assert.Equal(t, attrs[1], &Attr{K: "diff", V: "lfs"}) |
| 164 | assert.Equal(t, attrs[2], &Attr{K: "merge", V: "lfs"}) |
| 165 | assert.Equal(t, attrs[3], &Attr{K: "text", V: "false"}) |
| 166 | } |
| 167 | |
| 168 | func TestNewDiscoversIndirectChildrenTrees(t *testing.T) { |
| 169 | tmp := t.TempDir() |
| 170 | |
| 171 | db, err := gitobj.FromFilesystem(tmp, "") |
| 172 | require.NoError(t, err) |
| 173 | defer db.Close() |
| 174 | |
| 175 | blob, err := db.WriteBlob(gitobj.NewBlobFromBytes([]byte(` |
| 176 | *.dat filter=lfs diff=lfs merge=lfs -text |
| 177 | `))) |
| 178 | require.NoError(t, err) |
| 179 | |
| 180 | indirect, err := db.WriteTree(&gitobj.Tree{Entries: []*gitobj.TreeEntry{ |
| 181 | { |
| 182 | Name: ".gitattributes", |
| 183 | Oid: blob, |
| 184 | Filemode: 0100644, |
| 185 | }, |
| 186 | }}) |
| 187 | require.NoError(t, err) |
| 188 | |
| 189 | child, err := db.WriteTree(&gitobj.Tree{Entries: []*gitobj.TreeEntry{ |
| 190 | { |
| 191 | Name: "indirect", |
| 192 | Oid: indirect, |
| 193 | Filemode: 040000, |
| 194 | }, |
| 195 | }}) |
| 196 | require.NoError(t, err) |
| 197 | |
| 198 | tree, err := New(db, &gitobj.Tree{Entries: []*gitobj.TreeEntry{ |
| 199 | { |
| 200 | Name: "child", |
| 201 | Oid: child, |
| 202 | Filemode: 040000, |
| 203 | }, |
| 204 | }}) |
| 205 | require.NoError(t, err) |
| 206 | assert.Empty(t, tree.lines) |
| 207 | assert.Len(t, tree.children, 1) |
| 208 | |
| 209 | attrs := tree.Applied("child/indirect/foo.dat") |
| 210 | |
| 211 | assert.Len(t, attrs, 4) |
| 212 | assert.Equal(t, attrs[0], &Attr{K: "filter", V: "lfs"}) |
| 213 | assert.Equal(t, attrs[1], &Attr{K: "diff", V: "lfs"}) |
| 214 | assert.Equal(t, attrs[2], &Attr{K: "merge", V: "lfs"}) |
| 215 | assert.Equal(t, attrs[3], &Attr{K: "text", V: "false"}) |
| 216 | } |
| 217 | |
| 218 | func TestNewIgnoresChildrenAppropriately(t *testing.T) { |
| 219 | tmp := t.TempDir() |
| 220 | |
| 221 | db, err := gitobj.FromFilesystem(tmp, "") |
| 222 | require.NoError(t, err) |
| 223 | defer db.Close() |
| 224 | |
| 225 | blob, err := db.WriteBlob(gitobj.NewBlobFromBytes([]byte(` |
| 226 | *.dat filter=lfs diff=lfs merge=lfs -text |
| 227 | `))) |
| 228 | require.NoError(t, err) |
| 229 | |
| 230 | child, err := db.WriteTree(&gitobj.Tree{Entries: []*gitobj.TreeEntry{ |
| 231 | { |
| 232 | Name: "README.md", |
| 233 | Oid: []byte("00000000000000000000"), |
| 234 | Filemode: 0100644, |
| 235 | }, |
| 236 | }}) |
| 237 | require.NoError(t, err) |
| 238 | |
| 239 | tree, err := New(db, &gitobj.Tree{Entries: []*gitobj.TreeEntry{ |
| 240 | { |
| 241 | Name: ".gitattributes", |
| 242 | Oid: blob, |
| 243 | Filemode: 0100644, |
| 244 | }, |
| 245 | { |
| 246 | Name: "child", |
| 247 | Oid: child, |
| 248 | Filemode: 040000, |
| 249 | }, |
| 250 | }}) |
| 251 | require.NoError(t, err) |
| 252 | |
| 253 | assert.NotContains(t, tree.children, "child") |
| 254 | } |
| 255 | |
| 256 | func TestNewDiscoversSimpleTreesMacro(t *testing.T) { |
| 257 | tmp := t.TempDir() |
| 258 | |
| 259 | db, err := gitobj.FromFilesystem(tmp, "") |
| 260 | require.NoError(t, err) |
| 261 | defer db.Close() |
| 262 | |
| 263 | blob, err := db.WriteBlob(gitobj.NewBlobFromBytes([]byte(` |
| 264 | [attr]lfs filter=lfs diff=lfs merge=lfs -text |
| 265 | *.dat lfs |
| 266 | `))) |
| 267 | require.NoError(t, err) |
| 268 | |
| 269 | tree, err := New(db, &gitobj.Tree{Entries: []*gitobj.TreeEntry{ |
| 270 | { |
| 271 | Name: ".gitattributes", |
| 272 | Oid: blob, |
| 273 | Filemode: 0100644, |
| 274 | }, |
| 275 | }}) |
| 276 | require.NoError(t, err) |
| 277 | |
| 278 | attrs := tree.Applied("foo.dat") |
| 279 | |
| 280 | assert.Len(t, attrs, 5) |
| 281 | assert.Equal(t, &Attr{K: "filter", V: "lfs"}, attrs[0]) |
| 282 | assert.Equal(t, &Attr{K: "diff", V: "lfs"}, attrs[1]) |
| 283 | assert.Equal(t, &Attr{K: "merge", V: "lfs"}, attrs[2]) |
| 284 | assert.Equal(t, &Attr{K: "text", V: "false"}, attrs[3]) |
| 285 | assert.Equal(t, &Attr{K: "lfs", V: "true"}, attrs[4]) |
| 286 | } |
| 287 | |
| 288 | func TestAppliedProcessInCorrectOrder(t *testing.T) { |
| 289 | tmp := t.TempDir() |
| 290 | |
| 291 | db, err := gitobj.FromFilesystem(tmp, "") |
| 292 | require.NoError(t, err) |
| 293 | defer db.Close() |
| 294 | |
| 295 | blob, err := db.WriteBlob(gitobj.NewBlobFromBytes([]byte(` |
| 296 | [attr]lfs filter=lfs diff=lfs merge=lfs -text |
| 297 | *.dat lfs |
| 298 | `))) |
| 299 | require.NoError(t, err) |
| 300 | |
| 301 | tree, err := New(db, &gitobj.Tree{Entries: []*gitobj.TreeEntry{ |
| 302 | { |
| 303 | Name: ".gitattributes", |
| 304 | Oid: blob, |
| 305 | Filemode: 0100644, |
| 306 | }, |
| 307 | }}) |
| 308 | require.NoError(t, err) |
| 309 | |
| 310 | sysTree := &Tree{ |
| 311 | mp: tree.mp, |
| 312 | lines: []Line{ |
| 313 | &patternLine{ |
| 314 | pattern: wildmatch.NewWildmatch("*.bin"), |
| 315 | lineAttrs: lineAttrs{ |
| 316 | attrs: []*Attr{ |
| 317 | {K: "binary", V: "true"}, |
| 318 | }, |
| 319 | }, |
| 320 | }, |
| 321 | }, |
| 322 | } |
| 323 | |
| 324 | userTree := &Tree{ |
| 325 | mp: tree.mp, |
| 326 | lines: []Line{ |
| 327 | &patternLine{ |
| 328 | pattern: wildmatch.NewWildmatch("*.png"), |
| 329 | lineAttrs: lineAttrs{ |
| 330 | attrs: []*Attr{ |
| 331 | {K: "lfs", V: "true"}, |
| 332 | }, |
| 333 | }, |
| 334 | }, |
| 335 | }, |
| 336 | } |
| 337 | |
| 338 | repoTree := &Tree{ |
| 339 | mp: tree.mp, |
| 340 | lines: []Line{ |
| 341 | &patternLine{ |
| 342 | pattern: wildmatch.NewWildmatch("*.dat"), |
| 343 | lineAttrs: lineAttrs{ |
| 344 | attrs: []*Attr{ |
| 345 | {K: "diff", Unspecified: true}, |
| 346 | }, |
| 347 | }, |
| 348 | }, |
| 349 | }, |
| 350 | } |
| 351 | |
| 352 | tree.systemAttributes = sysTree |
| 353 | tree.userAttributes = userTree |
| 354 | tree.repoAttributes = repoTree |
| 355 | |
| 356 | attrs := tree.Applied("foo.dat") |
| 357 | |
| 358 | assert.Len(t, attrs, 6) |
| 359 | assert.Equal(t, &Attr{K: "filter", V: "lfs"}, attrs[0]) |
| 360 | assert.Equal(t, &Attr{K: "diff", V: "lfs"}, attrs[1]) |
| 361 | assert.Equal(t, &Attr{K: "merge", V: "lfs"}, attrs[2]) |
| 362 | assert.Equal(t, &Attr{K: "text", V: "false"}, attrs[3]) |
| 363 | assert.Equal(t, &Attr{K: "lfs", V: "true"}, attrs[4]) |
| 364 | assert.Equal(t, &Attr{K: "diff", Unspecified: true}, attrs[5]) |
| 365 | |
| 366 | attrs = tree.Applied("foo.bin") |
| 367 | |
| 368 | assert.Len(t, attrs, 4) |
| 369 | assert.Equal(t, &Attr{K: "diff", V: "false"}, attrs[0]) |
| 370 | assert.Equal(t, &Attr{K: "merge", V: "false"}, attrs[1]) |
| 371 | assert.Equal(t, &Attr{K: "text", V: "false"}, attrs[2]) |
| 372 | assert.Equal(t, &Attr{K: "binary", V: "true"}, attrs[3]) |
| 373 | |
| 374 | attrs = tree.Applied("foo.png") |
| 375 | |
| 376 | assert.Len(t, attrs, 5) |
| 377 | assert.Equal(t, &Attr{K: "filter", V: "lfs"}, attrs[0]) |
| 378 | assert.Equal(t, &Attr{K: "diff", V: "lfs"}, attrs[1]) |
| 379 | assert.Equal(t, &Attr{K: "merge", V: "lfs"}, attrs[2]) |
| 380 | assert.Equal(t, &Attr{K: "text", V: "false"}, attrs[3]) |
| 381 | assert.Equal(t, &Attr{K: "lfs", V: "true"}, attrs[4]) |
| 382 | } |