Skip to content

Commit 187c349

Browse files
committed
Synchronize instead of deleting and remaking test Git repos
Reduce overhead of preparing the test Git repos by avoiding unnecessary copying of already existing files.
1 parent f35e2b0 commit 187c349

File tree

5 files changed

+54
-34
lines changed

5 files changed

+54
-34
lines changed

models/migrations/base/tests.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ func PrepareTestEnv(t *testing.T, skip int, syncModels ...any) (*xorm.Engine, fu
3535
ourSkip := 2
3636
ourSkip += skip
3737
deferFn := testlogger.PrintCurrentTest(t, ourSkip)
38-
assert.NoError(t, os.RemoveAll(setting.RepoRootPath))
39-
assert.NoError(t, unittest.CopyDir(path.Join(filepath.Dir(setting.AppPath), "tests/gitea-repositories-meta"), setting.RepoRootPath))
38+
assert.NoError(t, unittest.SyncDirs(path.Join(filepath.Dir(setting.AppPath), "tests/gitea-repositories-meta"), setting.RepoRootPath))
4039
ownerDirs, err := os.ReadDir(setting.RepoRootPath)
4140
if err != nil {
4241
assert.NoError(t, err, "unable to read the new repo root: %v\n", err)

models/unittest/fscopy.go

Lines changed: 48 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package unittest
55

66
import (
7-
"errors"
87
"io"
98
"os"
109
"path"
@@ -55,44 +54,73 @@ func Copy(src, dest string) error {
5554
return os.Chmod(dest, si.Mode())
5655
}
5756

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

70-
err := os.MkdirAll(destPath, os.ModePerm)
68+
src, err := os.Stat(srcPath)
7169
if err != nil {
7270
return err
7371
}
7472

75-
// Gather directory info.
76-
infos, err := util.StatDir(srcPath, true)
73+
if src.Size() == dest.Size() &&
74+
src.ModTime() == dest.ModTime() &&
75+
src.Mode() == dest.Mode() {
76+
return nil
77+
}
78+
79+
return Copy(srcPath, destPath)
80+
}
81+
82+
// SyncDirs synchronizes files recursively from source to target directory.
83+
//
84+
// It returns error when error occurs in underlying functions.
85+
func SyncDirs(srcPath, destPath string) error {
86+
err := os.MkdirAll(destPath, os.ModePerm)
7787
if err != nil {
7888
return err
7989
}
8090

81-
var filter func(filePath string) bool
82-
if len(filters) > 0 {
83-
filter = filters[0]
91+
// find and delete all untracked files
92+
infos, err := util.StatDir(destPath, true)
93+
if err != nil {
94+
return err
8495
}
8596

8697
for _, info := range infos {
87-
if filter != nil && filter(info) {
88-
continue
98+
curPath := path.Join(destPath, info)
99+
100+
if _, err := os.Stat(path.Join(srcPath, info)); err != nil {
101+
if os.IsNotExist(err) {
102+
// Delete
103+
if err := os.RemoveAll(curPath); err != nil {
104+
return err
105+
}
106+
} else {
107+
return err
108+
}
89109
}
110+
}
90111

112+
// Gather directory info.
113+
infos, err = util.StatDir(srcPath, true)
114+
if err != nil {
115+
return err
116+
}
117+
118+
for _, info := range infos {
91119
curPath := path.Join(destPath, info)
92120
if strings.HasSuffix(info, "/") {
93121
err = os.MkdirAll(curPath, os.ModePerm)
94122
} else {
95-
err = Copy(path.Join(srcPath, info), curPath)
123+
err = Sync(path.Join(srcPath, info), curPath)
96124
}
97125
if err != nil {
98126
return err

models/unittest/testdb.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,8 @@ func MainTest(m *testing.M, testOpts ...*TestOptions) {
164164
if err = storage.Init(); err != nil {
165165
fatalTestError("storage.Init: %v\n", err)
166166
}
167-
if err = util.RemoveAll(repoRootPath); err != nil {
168-
fatalTestError("util.RemoveAll: %v\n", err)
169-
}
170-
if err = CopyDir(filepath.Join(giteaRoot, "tests", "gitea-repositories-meta"), setting.RepoRootPath); err != nil {
171-
fatalTestError("util.CopyDir: %v\n", err)
167+
if err = SyncDirs(filepath.Join(giteaRoot, "tests", "gitea-repositories-meta"), setting.RepoRootPath); err != nil {
168+
fatalTestError("util.SyncDirs: %v\n", err)
172169
}
173170

174171
if err = git.InitFull(context.Background()); err != nil {
@@ -255,9 +252,8 @@ func PrepareTestDatabase() error {
255252
// by tests that use the above MainTest(..) function.
256253
func PrepareTestEnv(t testing.TB) {
257254
assert.NoError(t, PrepareTestDatabase())
258-
assert.NoError(t, util.RemoveAll(setting.RepoRootPath))
259255
metaPath := filepath.Join(giteaRoot, "tests", "gitea-repositories-meta")
260-
assert.NoError(t, CopyDir(metaPath, setting.RepoRootPath))
256+
assert.NoError(t, SyncDirs(metaPath, setting.RepoRootPath))
261257
ownerDirs, err := os.ReadDir(setting.RepoRootPath)
262258
assert.NoError(t, err)
263259
for _, ownerDir := range ownerDirs {

tests/integration/migration-test/migration_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,7 @@ func initMigrationTest(t *testing.T) func() {
6464
unittest.InitSettings()
6565

6666
assert.True(t, len(setting.RepoRootPath) != 0)
67-
assert.NoError(t, util.RemoveAll(setting.RepoRootPath))
68-
assert.NoError(t, unittest.CopyDir(path.Join(filepath.Dir(setting.AppPath), "tests/gitea-repositories-meta"), setting.RepoRootPath))
67+
assert.NoError(t, unittest.SyncDirs(path.Join(filepath.Dir(setting.AppPath), "tests/gitea-repositories-meta"), setting.RepoRootPath))
6968
ownerDirs, err := os.ReadDir(setting.RepoRootPath)
7069
if err != nil {
7170
assert.NoError(t, err, "unable to read the new repo root: %v\n", err)

tests/test_utils.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,7 @@ func PrepareAttachmentsStorage(t testing.TB) {
193193
}
194194

195195
func PrepareGitRepoDirectory(t testing.TB) {
196-
assert.NoError(t, util.RemoveAll(setting.RepoRootPath))
197-
assert.NoError(t, unittest.CopyDir(path.Join(filepath.Dir(setting.AppPath), "tests/gitea-repositories-meta"), setting.RepoRootPath))
198-
196+
assert.NoError(t, unittest.SyncDirs(path.Join(filepath.Dir(setting.AppPath), "tests/gitea-repositories-meta"), setting.RepoRootPath))
199197
ownerDirs, err := os.ReadDir(setting.RepoRootPath)
200198
if err != nil {
201199
assert.NoError(t, err, "unable to read the new repo root: %v\n", err)

0 commit comments

Comments
 (0)