Skip to content

Commit 150f47f

Browse files
committed
fix path handling
1 parent 2916af0 commit 150f47f

File tree

2 files changed

+19
-42
lines changed

2 files changed

+19
-42
lines changed

models/migrations/base/tests.go

Lines changed: 5 additions & 6 deletions
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,7 +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, unittest.SyncDirs(path.Join(filepath.Dir(setting.AppPath), "tests/gitea-repositories-meta"), setting.RepoRootPath))
37+
assert.NoError(t, unittest.SyncDirs(filepath.Join(filepath.Dir(setting.AppPath), "tests/gitea-repositories-meta"), setting.RepoRootPath))
3938
ownerDirs, err := os.ReadDir(setting.RepoRootPath)
4039
if err != nil {
4140
assert.NoError(t, err, "unable to read the new repo root: %v\n", err)
@@ -122,20 +121,20 @@ func MainTest(m *testing.M) {
122121
if runtime.GOOS == "windows" {
123122
giteaBinary += ".exe"
124123
}
125-
setting.AppPath = path.Join(giteaRoot, giteaBinary)
124+
setting.AppPath = filepath.Join(giteaRoot, giteaBinary)
126125
if _, err := os.Stat(setting.AppPath); err != nil {
127126
fmt.Printf("Could not find gitea binary at %s\n", setting.AppPath)
128127
os.Exit(1)
129128
}
130129

131130
giteaConf := os.Getenv("GITEA_CONF")
132131
if giteaConf == "" {
133-
giteaConf = path.Join(filepath.Dir(setting.AppPath), "tests/sqlite.ini")
132+
giteaConf = filepath.Join(filepath.Dir(setting.AppPath), "tests/sqlite.ini")
134133
fmt.Printf("Environment variable $GITEA_CONF not set - defaulting to %s\n", giteaConf)
135134
}
136135

137-
if !path.IsAbs(giteaConf) {
138-
setting.CustomConf = path.Join(giteaRoot, giteaConf)
136+
if !filepath.IsAbs(giteaConf) {
137+
setting.CustomConf = filepath.Join(giteaRoot, giteaConf)
139138
} else {
140139
setting.CustomConf = giteaConf
141140
}

models/unittest/fscopy.go

Lines changed: 14 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44
package unittest
55

66
import (
7-
"io"
87
"os"
9-
"path"
8+
"path/filepath"
109
"strings"
1110

1211
"code.gitea.io/gitea/modules/util"
@@ -31,27 +30,7 @@ func Copy(src, dest string) error {
3130
return os.Symlink(target, dest)
3231
}
3332

34-
sr, err := os.Open(src)
35-
if err != nil {
36-
return err
37-
}
38-
defer sr.Close()
39-
40-
dw, err := os.Create(dest)
41-
if err != nil {
42-
return err
43-
}
44-
defer dw.Close()
45-
46-
if _, err = io.Copy(dw, sr); err != nil {
47-
return err
48-
}
49-
50-
// Set back file information.
51-
if err = os.Chtimes(dest, si.ModTime(), si.ModTime()); err != nil {
52-
return err
53-
}
54-
return os.Chmod(dest, si.Mode())
33+
return util.CopyFile(src, dest)
5534
}
5635

5736
// Sync synchronizes the two files. This is skipped if both files
@@ -89,18 +68,17 @@ func SyncDirs(srcPath, destPath string) error {
8968
}
9069

9170
// find and delete all untracked files
92-
infos, err := util.StatDir(destPath, true)
71+
files, err := util.StatDir(destPath, true)
9372
if err != nil {
9473
return err
9574
}
9675

97-
for _, info := range infos {
98-
curPath := path.Join(destPath, info)
99-
100-
if _, err := os.Stat(path.Join(srcPath, info)); err != nil {
76+
for _, file := range files {
77+
destFilePath := filepath.Join(destPath, file)
78+
if _, err = os.Stat(filepath.Join(srcPath, file)); err != nil {
10179
if os.IsNotExist(err) {
102-
// Delete
103-
if err := os.RemoveAll(curPath); err != nil {
80+
// TODO: why delete? it should happen because the file list is just queried above, why not exist?
81+
if err := os.RemoveAll(destFilePath); err != nil {
10482
return err
10583
}
10684
} else {
@@ -110,17 +88,17 @@ func SyncDirs(srcPath, destPath string) error {
11088
}
11189

11290
// Gather directory info.
113-
infos, err = util.StatDir(srcPath, true)
91+
files, err = util.StatDir(srcPath, true)
11492
if err != nil {
11593
return err
11694
}
11795

118-
for _, info := range infos {
119-
curPath := path.Join(destPath, info)
120-
if strings.HasSuffix(info, "/") {
121-
err = os.MkdirAll(curPath, os.ModePerm)
96+
for _, file := range files {
97+
destFilePath := filepath.Join(destPath, file)
98+
if strings.HasSuffix(file, "/") {
99+
err = os.MkdirAll(destFilePath, os.ModePerm)
122100
} else {
123-
err = Sync(path.Join(srcPath, info), curPath)
101+
err = Sync(filepath.Join(srcPath, file), destFilePath)
124102
}
125103
if err != nil {
126104
return err

0 commit comments

Comments
 (0)