Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions models/migrations/base/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ func PrepareTestEnv(t *testing.T, skip int, syncModels ...any) (*xorm.Engine, fu
ourSkip := 2
ourSkip += skip
deferFn := testlogger.PrintCurrentTest(t, ourSkip)
assert.NoError(t, os.RemoveAll(setting.RepoRootPath))
assert.NoError(t, unittest.CopyDir(path.Join(filepath.Dir(setting.AppPath), "tests/gitea-repositories-meta"), setting.RepoRootPath))
assert.NoError(t, unittest.SyncDirs(path.Join(filepath.Dir(setting.AppPath), "tests/gitea-repositories-meta"), setting.RepoRootPath))
ownerDirs, err := os.ReadDir(setting.RepoRootPath)
if err != nil {
assert.NoError(t, err, "unable to read the new repo root: %v\n", err)
Expand Down
68 changes: 48 additions & 20 deletions models/unittest/fscopy.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package unittest

import (
"errors"
"io"
"os"
"path"
Expand Down Expand Up @@ -55,44 +54,73 @@ func Copy(src, dest string) error {
return os.Chmod(dest, si.Mode())
}

// CopyDir copy files recursively from source to target directory.
//
// The filter accepts a function that process the path info.
// and should return true for need to filter.
//
// It returns error when error occurs in underlying functions.
func CopyDir(srcPath, destPath string, filters ...func(filePath string) bool) error {
// Check if target directory exists.
if _, err := os.Stat(destPath); !errors.Is(err, os.ErrNotExist) {
return util.NewAlreadyExistErrorf("file or directory already exists: %s", destPath)
// Sync synchronizes the two files. This is skipped if both files
// exist and the size, modtime, and mode match.
func Sync(srcPath, destPath string) error {
dest, err := os.Stat(destPath)
if err != nil {
if os.IsNotExist(err) {
return Copy(srcPath, destPath)
}
return err
}

err := os.MkdirAll(destPath, os.ModePerm)
src, err := os.Stat(srcPath)
if err != nil {
return err
}

// Gather directory info.
infos, err := util.StatDir(srcPath, true)
if src.Size() == dest.Size() &&
src.ModTime() == dest.ModTime() &&
src.Mode() == dest.Mode() {
return nil
}

return Copy(srcPath, destPath)
}

// SyncDirs synchronizes files recursively from source to target directory.
//
// It returns error when error occurs in underlying functions.
func SyncDirs(srcPath, destPath string) error {
err := os.MkdirAll(destPath, os.ModePerm)
if err != nil {
return err
}

var filter func(filePath string) bool
if len(filters) > 0 {
filter = filters[0]
// find and delete all untracked files
infos, err := util.StatDir(destPath, true)
if err != nil {
return err
}

for _, info := range infos {
if filter != nil && filter(info) {
continue
curPath := path.Join(destPath, info)

if _, err := os.Stat(path.Join(srcPath, info)); err != nil {
if os.IsNotExist(err) {
// Delete
if err := os.RemoveAll(curPath); err != nil {
return err
}
} else {
return err
}
}
}

// Gather directory info.
infos, err = util.StatDir(srcPath, true)
if err != nil {
return err
}

for _, info := range infos {
curPath := path.Join(destPath, info)
if strings.HasSuffix(info, "/") {
err = os.MkdirAll(curPath, os.ModePerm)
} else {
err = Copy(path.Join(srcPath, info), curPath)
err = Sync(path.Join(srcPath, info), curPath)
}
if err != nil {
return err
Expand Down
10 changes: 3 additions & 7 deletions models/unittest/testdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,8 @@ func MainTest(m *testing.M, testOpts ...*TestOptions) {
if err = storage.Init(); err != nil {
fatalTestError("storage.Init: %v\n", err)
}
if err = util.RemoveAll(repoRootPath); err != nil {
fatalTestError("util.RemoveAll: %v\n", err)
}
if err = CopyDir(filepath.Join(giteaRoot, "tests", "gitea-repositories-meta"), setting.RepoRootPath); err != nil {
fatalTestError("util.CopyDir: %v\n", err)
if err = SyncDirs(filepath.Join(giteaRoot, "tests", "gitea-repositories-meta"), setting.RepoRootPath); err != nil {
fatalTestError("util.SyncDirs: %v\n", err)
}

if err = git.InitFull(context.Background()); err != nil {
Expand Down Expand Up @@ -255,9 +252,8 @@ func PrepareTestDatabase() error {
// by tests that use the above MainTest(..) function.
func PrepareTestEnv(t testing.TB) {
assert.NoError(t, PrepareTestDatabase())
assert.NoError(t, util.RemoveAll(setting.RepoRootPath))
metaPath := filepath.Join(giteaRoot, "tests", "gitea-repositories-meta")
assert.NoError(t, CopyDir(metaPath, setting.RepoRootPath))
assert.NoError(t, SyncDirs(metaPath, setting.RepoRootPath))
ownerDirs, err := os.ReadDir(setting.RepoRootPath)
assert.NoError(t, err)
for _, ownerDir := range ownerDirs {
Expand Down
12 changes: 7 additions & 5 deletions modules/queue/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (

"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"

"golang.org/x/sync/errgroup"
)

// Manager is a manager for the queues created by "CreateXxxQueue" functions, these queues are called "managed queues".
Expand Down Expand Up @@ -77,14 +79,14 @@ func (m *Manager) ManagedQueues() map[int64]ManagedWorkerPoolQueue {
// FlushAll tries to make all managed queues process all items synchronously, until timeout or the queue is empty.
// It is for testing purpose only. It's not designed to be used in a cluster.
func (m *Manager) FlushAll(ctx context.Context, timeout time.Duration) error {
var finalErr error
g := errgroup.Group{}
qs := m.ManagedQueues()
for _, q := range qs {
if err := q.FlushWithContext(ctx, timeout); err != nil {
finalErr = err // TODO: in Go 1.20: errors.Join
}
g.Go(func() error {
return q.FlushWithContext(ctx, timeout)
})
}
return finalErr
return g.Wait()
}

// CreateSimpleQueue creates a simple queue from global setting config provider by name
Expand Down
2 changes: 1 addition & 1 deletion services/repository/adopt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func TestListUnadoptedRepositories_ListOptions(t *testing.T) {

func TestAdoptRepository(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
assert.NoError(t, unittest.CopyDir(filepath.Join(setting.RepoRootPath, "user2", "repo1.git"), filepath.Join(setting.RepoRootPath, "user2", "test-adopt.git")))
assert.NoError(t, unittest.SyncDirs(filepath.Join(setting.RepoRootPath, "user2", "repo1.git"), filepath.Join(setting.RepoRootPath, "user2", "test-adopt.git")))
user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
_, err := AdoptRepository(db.DefaultContext, user2, user2, CreateRepoOptions{Name: "test-adopt"})
assert.NoError(t, err)
Expand Down
3 changes: 1 addition & 2 deletions tests/integration/migration-test/migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ func initMigrationTest(t *testing.T) func() {
unittest.InitSettings()

assert.True(t, len(setting.RepoRootPath) != 0)
assert.NoError(t, util.RemoveAll(setting.RepoRootPath))
assert.NoError(t, unittest.CopyDir(path.Join(filepath.Dir(setting.AppPath), "tests/gitea-repositories-meta"), setting.RepoRootPath))
assert.NoError(t, unittest.SyncDirs(path.Join(filepath.Dir(setting.AppPath), "tests/gitea-repositories-meta"), setting.RepoRootPath))
ownerDirs, err := os.ReadDir(setting.RepoRootPath)
if err != nil {
assert.NoError(t, err, "unable to read the new repo root: %v\n", err)
Expand Down
4 changes: 1 addition & 3 deletions tests/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,7 @@ func PrepareAttachmentsStorage(t testing.TB) {
}

func PrepareGitRepoDirectory(t testing.TB) {
assert.NoError(t, util.RemoveAll(setting.RepoRootPath))
assert.NoError(t, unittest.CopyDir(path.Join(filepath.Dir(setting.AppPath), "tests/gitea-repositories-meta"), setting.RepoRootPath))

assert.NoError(t, unittest.SyncDirs(path.Join(filepath.Dir(setting.AppPath), "tests/gitea-repositories-meta"), setting.RepoRootPath))
ownerDirs, err := os.ReadDir(setting.RepoRootPath)
if err != nil {
assert.NoError(t, err, "unable to read the new repo root: %v\n", err)
Expand Down
Loading