Skip to content

Commit 2a5c695

Browse files
authored
Merge branch 'main' into fix-project
2 parents 0ee02f2 + 68731c0 commit 2a5c695

File tree

102 files changed

+95
-456
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

102 files changed

+95
-456
lines changed

models/fixtures/repository.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
fork_id: 0
2727
is_template: false
2828
template_id: 0
29-
size: 8478
29+
size: 0
3030
is_fsck_enabled: true
3131
close_issues_via_commit_in_any_branch: false
3232

models/migrations/base/tests.go

+5-26
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"context"
99
"fmt"
1010
"os"
11-
"path"
1211
"path/filepath"
1312
"runtime"
1413
"testing"
@@ -35,27 +34,7 @@ func PrepareTestEnv(t *testing.T, skip int, syncModels ...any) (*xorm.Engine, fu
3534
ourSkip := 2
3635
ourSkip += skip
3736
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))
40-
ownerDirs, err := os.ReadDir(setting.RepoRootPath)
41-
if err != nil {
42-
assert.NoError(t, err, "unable to read the new repo root: %v\n", err)
43-
}
44-
for _, ownerDir := range ownerDirs {
45-
if !ownerDir.Type().IsDir() {
46-
continue
47-
}
48-
repoDirs, err := os.ReadDir(filepath.Join(setting.RepoRootPath, ownerDir.Name()))
49-
if err != nil {
50-
assert.NoError(t, err, "unable to read the new repo root: %v\n", err)
51-
}
52-
for _, repoDir := range repoDirs {
53-
_ = os.MkdirAll(filepath.Join(setting.RepoRootPath, ownerDir.Name(), repoDir.Name(), "objects", "pack"), 0o755)
54-
_ = os.MkdirAll(filepath.Join(setting.RepoRootPath, ownerDir.Name(), repoDir.Name(), "objects", "info"), 0o755)
55-
_ = os.MkdirAll(filepath.Join(setting.RepoRootPath, ownerDir.Name(), repoDir.Name(), "refs", "heads"), 0o755)
56-
_ = os.MkdirAll(filepath.Join(setting.RepoRootPath, ownerDir.Name(), repoDir.Name(), "refs", "tag"), 0o755)
57-
}
58-
}
37+
assert.NoError(t, unittest.SyncDirs(filepath.Join(filepath.Dir(setting.AppPath), "tests/gitea-repositories-meta"), setting.RepoRootPath))
5938

6039
if err := deleteDB(); err != nil {
6140
t.Errorf("unable to reset database: %v", err)
@@ -123,20 +102,20 @@ func MainTest(m *testing.M) {
123102
if runtime.GOOS == "windows" {
124103
giteaBinary += ".exe"
125104
}
126-
setting.AppPath = path.Join(giteaRoot, giteaBinary)
105+
setting.AppPath = filepath.Join(giteaRoot, giteaBinary)
127106
if _, err := os.Stat(setting.AppPath); err != nil {
128107
fmt.Printf("Could not find gitea binary at %s\n", setting.AppPath)
129108
os.Exit(1)
130109
}
131110

132111
giteaConf := os.Getenv("GITEA_CONF")
133112
if giteaConf == "" {
134-
giteaConf = path.Join(filepath.Dir(setting.AppPath), "tests/sqlite.ini")
113+
giteaConf = filepath.Join(filepath.Dir(setting.AppPath), "tests/sqlite.ini")
135114
fmt.Printf("Environment variable $GITEA_CONF not set - defaulting to %s\n", giteaConf)
136115
}
137116

138-
if !path.IsAbs(giteaConf) {
139-
setting.CustomConf = path.Join(giteaRoot, giteaConf)
117+
if !filepath.IsAbs(giteaConf) {
118+
setting.CustomConf = filepath.Join(giteaRoot, giteaConf)
140119
} else {
141120
setting.CustomConf = giteaConf
142121
}

models/unittest/fscopy.go

+44-40
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@
44
package unittest
55

66
import (
7-
"errors"
8-
"io"
97
"os"
10-
"path"
8+
"path/filepath"
119
"strings"
1210

1311
"code.gitea.io/gitea/modules/util"
@@ -32,67 +30,73 @@ func Copy(src, dest string) error {
3230
return os.Symlink(target, dest)
3331
}
3432

35-
sr, err := os.Open(src)
33+
return util.CopyFile(src, dest)
34+
}
35+
36+
// Sync synchronizes the two files. This is skipped if both files
37+
// exist and the size, modtime, and mode match.
38+
func Sync(srcPath, destPath string) error {
39+
dest, err := os.Stat(destPath)
3640
if err != nil {
41+
if os.IsNotExist(err) {
42+
return Copy(srcPath, destPath)
43+
}
3744
return err
3845
}
39-
defer sr.Close()
4046

41-
dw, err := os.Create(dest)
47+
src, err := os.Stat(srcPath)
4248
if err != nil {
4349
return err
4450
}
45-
defer dw.Close()
4651

47-
if _, err = io.Copy(dw, sr); err != nil {
48-
return err
52+
if src.Size() == dest.Size() &&
53+
src.ModTime() == dest.ModTime() &&
54+
src.Mode() == dest.Mode() {
55+
return nil
4956
}
5057

51-
// Set back file information.
52-
if err = os.Chtimes(dest, si.ModTime(), si.ModTime()); err != nil {
53-
return err
54-
}
55-
return os.Chmod(dest, si.Mode())
58+
return Copy(srcPath, destPath)
5659
}
5760

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-
//
61+
// SyncDirs synchronizes files recursively from source to target directory.
6362
// 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)
68-
}
69-
63+
func SyncDirs(srcPath, destPath string) error {
7064
err := os.MkdirAll(destPath, os.ModePerm)
7165
if err != nil {
7266
return err
7367
}
7468

75-
// Gather directory info.
76-
infos, err := util.StatDir(srcPath, true)
69+
// find and delete all untracked files
70+
destFiles, err := util.StatDir(destPath, true)
7771
if err != nil {
7872
return err
7973
}
80-
81-
var filter func(filePath string) bool
82-
if len(filters) > 0 {
83-
filter = filters[0]
84-
}
85-
86-
for _, info := range infos {
87-
if filter != nil && filter(info) {
88-
continue
74+
for _, destFile := range destFiles {
75+
destFilePath := filepath.Join(destPath, destFile)
76+
if _, err = os.Stat(filepath.Join(srcPath, destFile)); err != nil {
77+
if os.IsNotExist(err) {
78+
// if src file does not exist, remove dest file
79+
if err = os.RemoveAll(destFilePath); err != nil {
80+
return err
81+
}
82+
} else {
83+
return err
84+
}
8985
}
86+
}
9087

91-
curPath := path.Join(destPath, info)
92-
if strings.HasSuffix(info, "/") {
93-
err = os.MkdirAll(curPath, os.ModePerm)
88+
// sync src files to dest
89+
srcFiles, err := util.StatDir(srcPath, true)
90+
if err != nil {
91+
return err
92+
}
93+
for _, srcFile := range srcFiles {
94+
destFilePath := filepath.Join(destPath, srcFile)
95+
// util.StatDir appends a slash to the directory name
96+
if strings.HasSuffix(srcFile, "/") {
97+
err = os.MkdirAll(destFilePath, os.ModePerm)
9498
} else {
95-
err = Copy(path.Join(srcPath, info), curPath)
99+
err = Sync(filepath.Join(srcPath, srcFile), destFilePath)
96100
}
97101
if err != nil {
98102
return err

models/unittest/testdb.go

+3-42
Original file line numberDiff line numberDiff line change
@@ -164,35 +164,13 @@ 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 {
175172
fatalTestError("git.Init: %v\n", err)
176173
}
177-
ownerDirs, err := os.ReadDir(setting.RepoRootPath)
178-
if err != nil {
179-
fatalTestError("unable to read the new repo root: %v\n", err)
180-
}
181-
for _, ownerDir := range ownerDirs {
182-
if !ownerDir.Type().IsDir() {
183-
continue
184-
}
185-
repoDirs, err := os.ReadDir(filepath.Join(setting.RepoRootPath, ownerDir.Name()))
186-
if err != nil {
187-
fatalTestError("unable to read the new repo root: %v\n", err)
188-
}
189-
for _, repoDir := range repoDirs {
190-
_ = os.MkdirAll(filepath.Join(setting.RepoRootPath, ownerDir.Name(), repoDir.Name(), "objects", "pack"), 0o755)
191-
_ = os.MkdirAll(filepath.Join(setting.RepoRootPath, ownerDir.Name(), repoDir.Name(), "objects", "info"), 0o755)
192-
_ = os.MkdirAll(filepath.Join(setting.RepoRootPath, ownerDir.Name(), repoDir.Name(), "refs", "heads"), 0o755)
193-
_ = os.MkdirAll(filepath.Join(setting.RepoRootPath, ownerDir.Name(), repoDir.Name(), "refs", "tag"), 0o755)
194-
}
195-
}
196174

197175
if len(testOpts) > 0 && testOpts[0].SetUp != nil {
198176
if err := testOpts[0].SetUp(); err != nil {
@@ -255,24 +233,7 @@ func PrepareTestDatabase() error {
255233
// by tests that use the above MainTest(..) function.
256234
func PrepareTestEnv(t testing.TB) {
257235
assert.NoError(t, PrepareTestDatabase())
258-
assert.NoError(t, util.RemoveAll(setting.RepoRootPath))
259236
metaPath := filepath.Join(giteaRoot, "tests", "gitea-repositories-meta")
260-
assert.NoError(t, CopyDir(metaPath, setting.RepoRootPath))
261-
ownerDirs, err := os.ReadDir(setting.RepoRootPath)
262-
assert.NoError(t, err)
263-
for _, ownerDir := range ownerDirs {
264-
if !ownerDir.Type().IsDir() {
265-
continue
266-
}
267-
repoDirs, err := os.ReadDir(filepath.Join(setting.RepoRootPath, ownerDir.Name()))
268-
assert.NoError(t, err)
269-
for _, repoDir := range repoDirs {
270-
_ = os.MkdirAll(filepath.Join(setting.RepoRootPath, ownerDir.Name(), repoDir.Name(), "objects", "pack"), 0o755)
271-
_ = os.MkdirAll(filepath.Join(setting.RepoRootPath, ownerDir.Name(), repoDir.Name(), "objects", "info"), 0o755)
272-
_ = os.MkdirAll(filepath.Join(setting.RepoRootPath, ownerDir.Name(), repoDir.Name(), "refs", "heads"), 0o755)
273-
_ = os.MkdirAll(filepath.Join(setting.RepoRootPath, ownerDir.Name(), repoDir.Name(), "refs", "tag"), 0o755)
274-
}
275-
}
276-
237+
assert.NoError(t, SyncDirs(metaPath, setting.RepoRootPath))
277238
base.SetupGiteaRoot() // Makes sure GITEA_ROOT is set
278239
}

modules/git/tests/repos/language_stats_repo/description

-1
This file was deleted.

modules/git/tests/repos/language_stats_repo/info/exclude

-6
This file was deleted.

modules/git/tests/repos/repo1_bare/description

-1
This file was deleted.

modules/git/tests/repos/repo1_bare/info/exclude

-6
This file was deleted.

modules/git/tests/repos/repo1_bare_sha256/description

-1
This file was deleted.

modules/git/tests/repos/repo1_bare_sha256/info/exclude

-6
This file was deleted.

modules/git/tests/repos/repo2_empty/description

-1
This file was deleted.

modules/git/tests/repos/repo2_empty/info/exclude

-6
This file was deleted.

modules/git/tests/repos/repo3_notes/description

-1
This file was deleted.

modules/git/tests/repos/repo5_pulls/description

-1
This file was deleted.

modules/git/tests/repos/repo5_pulls/info/exclude

-6
This file was deleted.

modules/git/tests/repos/repo5_pulls_sha256/description

-1
This file was deleted.

modules/git/tests/repos/repo6_blame_sha256/description

-1
This file was deleted.

modules/git/tests/repos/repo6_blame_sha256/info/exclude

-6
This file was deleted.

modules/git/tests/repos/repo6_merge_sha256/description

-1
This file was deleted.

modules/git/tests/repos/repo6_merge_sha256/info/exclude

-6
This file was deleted.

modules/indexer/code/indexer_test.go

+4-7
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
_ "code.gitea.io/gitea/models/activities"
2222

2323
"github.com/stretchr/testify/assert"
24+
"github.com/stretchr/testify/require"
2425

2526
_ "github.com/mattn/go-sqlite3"
2627
)
@@ -284,15 +285,11 @@ func TestBleveIndexAndSearch(t *testing.T) {
284285
dir := t.TempDir()
285286

286287
idx := bleve.NewIndexer(dir)
287-
_, err := idx.Init(context.Background())
288-
if err != nil {
289-
if idx != nil {
290-
idx.Close()
291-
}
292-
assert.FailNow(t, "Unable to create bleve indexer Error: %v", err)
293-
}
294288
defer idx.Close()
295289

290+
_, err := idx.Init(context.Background())
291+
require.NoError(t, err)
292+
296293
testIndexer("beleve", t, idx)
297294
}
298295

modules/queue/manager.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package queue
55

66
import (
77
"context"
8+
"errors"
89
"sync"
910
"time"
1011

@@ -32,6 +33,7 @@ type ManagedWorkerPoolQueue interface {
3233

3334
// FlushWithContext tries to make the handler process all items in the queue synchronously.
3435
// It is for testing purpose only. It's not designed to be used in a cluster.
36+
// Negative timeout means discarding all items in the queue.
3537
FlushWithContext(ctx context.Context, timeout time.Duration) error
3638

3739
// RemoveAllItems removes all items in the base queue (on-the-fly items are not affected)
@@ -76,15 +78,16 @@ func (m *Manager) ManagedQueues() map[int64]ManagedWorkerPoolQueue {
7678

7779
// FlushAll tries to make all managed queues process all items synchronously, until timeout or the queue is empty.
7880
// It is for testing purpose only. It's not designed to be used in a cluster.
81+
// Negative timeout means discarding all items in the queue.
7982
func (m *Manager) FlushAll(ctx context.Context, timeout time.Duration) error {
80-
var finalErr error
83+
var finalErrors []error
8184
qs := m.ManagedQueues()
8285
for _, q := range qs {
8386
if err := q.FlushWithContext(ctx, timeout); err != nil {
84-
finalErr = err // TODO: in Go 1.20: errors.Join
87+
finalErrors = append(finalErrors, err)
8588
}
8689
}
87-
return finalErr
90+
return errors.Join(finalErrors...)
8891
}
8992

9093
// CreateSimpleQueue creates a simple queue from global setting config provider by name

0 commit comments

Comments
 (0)