code.gitea.io/gitea@v1.22.3/modules/storage/storage_test.go (about)

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