You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

storage_test.go 1.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package storage
  4. import (
  5. "bytes"
  6. "testing"
  7. "code.gitea.io/gitea/modules/setting"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. func testStorageIterator(t *testing.T, typStr Type, cfg *setting.Storage) {
  11. l, err := NewStorage(typStr, cfg)
  12. assert.NoError(t, err)
  13. testFiles := [][]string{
  14. {"a/1.txt", "a1"},
  15. {"/a/1.txt", "aa1"}, // same as above, but with leading slash that will be trim
  16. {"ab/1.txt", "ab1"},
  17. {"b/1.txt", "b1"},
  18. {"b/2.txt", "b2"},
  19. {"b/3.txt", "b3"},
  20. {"b/x 4.txt", "bx4"},
  21. }
  22. for _, f := range testFiles {
  23. _, err = l.Save(f[0], bytes.NewBufferString(f[1]), -1)
  24. assert.NoError(t, err)
  25. }
  26. expectedList := map[string][]string{
  27. "a": {"a/1.txt"},
  28. "b": {"b/1.txt", "b/2.txt", "b/3.txt", "b/x 4.txt"},
  29. "": {"a/1.txt", "b/1.txt", "b/2.txt", "b/3.txt", "b/x 4.txt", "ab/1.txt"},
  30. "/": {"a/1.txt", "b/1.txt", "b/2.txt", "b/3.txt", "b/x 4.txt", "ab/1.txt"},
  31. "a/b/../../a": {"a/1.txt"},
  32. }
  33. for dir, expected := range expectedList {
  34. count := 0
  35. err = l.IterateObjects(dir, func(path string, f Object) error {
  36. defer f.Close()
  37. assert.Contains(t, expected, path)
  38. count++
  39. return nil
  40. })
  41. assert.NoError(t, err)
  42. assert.Len(t, expected, count)
  43. }
  44. }