gitea源码

storage_test.go 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package storage
  4. import (
  5. "strings"
  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], strings.NewReader(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/1.txt", "b/1.txt", "b/2.txt", "b/3.txt", "b/x 4.txt", "ab/1.txt"},
  32. "a/b/../../a": {"a/1.txt"},
  33. }
  34. for dir, expected := range expectedList {
  35. count := 0
  36. err = l.IterateObjects(dir, func(path string, f Object) error {
  37. defer f.Close()
  38. assert.Contains(t, expected, path)
  39. count++
  40. return nil
  41. })
  42. assert.NoError(t, err)
  43. assert.Len(t, expected, count)
  44. }
  45. }