summaryrefslogtreecommitdiffstats
path: root/models
diff options
context:
space:
mode:
authorzeripath <art27@cantab.net>2021-12-10 19:43:28 +0000
committerGitHub <noreply@github.com>2021-12-10 19:43:28 +0000
commitf550e356d6cfe84b2891776d778531afa92ef902 (patch)
tree2daf491cd4bdc7eeca333b685a335e92a7bf34eb /models
parent3ca5dc7e32b372d14ff80d96f14b8f6a805862f1 (diff)
downloadgitea-f550e356d6cfe84b2891776d778531afa92ef902.tar.gz
gitea-f550e356d6cfe84b2891776d778531afa92ef902.zip
Prevent services/mailer/mailer_test.go tests from deleteing data directory (#17941)
Running `make test-backend` will delete `data/` due to reloading the configuration and resetting the appdatapath. This PR removes this unnecessary config reload but also adds extra code in to the unittest main to prevent its cleanup from deleting the wrong directory. Signed-off-by: Andrew Thornton <art27@cantab.net>
Diffstat (limited to 'models')
-rw-r--r--models/migrations/migrations_test.go11
-rw-r--r--models/unittest/testdb.go12
2 files changed, 17 insertions, 6 deletions
diff --git a/models/migrations/migrations_test.go b/models/migrations/migrations_test.go
index 2effcb99eb..10ba3dde09 100644
--- a/models/migrations/migrations_test.go
+++ b/models/migrations/migrations_test.go
@@ -8,6 +8,7 @@ import (
"context"
"database/sql"
"fmt"
+ "io/ioutil"
"os"
"path"
"path/filepath"
@@ -57,6 +58,14 @@ func TestMain(m *testing.M) {
setting.CustomConf = giteaConf
}
+ tmpDataPath, err := ioutil.TempDir("", "data")
+ if err != nil {
+ fmt.Printf("Unable to create temporary data path %v\n", err)
+ os.Exit(1)
+ }
+
+ setting.AppDataPath = tmpDataPath
+
setting.SetCustomPathAndConf("", "", "")
setting.LoadForTest()
git.CheckLFSVersion()
@@ -68,7 +77,7 @@ func TestMain(m *testing.M) {
if err := removeAllWithRetry(setting.RepoRootPath); err != nil {
fmt.Fprintf(os.Stderr, "os.RemoveAll: %v\n", err)
}
- if err := removeAllWithRetry(setting.AppDataPath); err != nil {
+ if err := removeAllWithRetry(tmpDataPath); err != nil {
fmt.Fprintf(os.Stderr, "os.RemoveAll: %v\n", err)
}
os.Exit(exitStatus)
diff --git a/models/unittest/testdb.go b/models/unittest/testdb.go
index 94c93755e0..8083c607e5 100644
--- a/models/unittest/testdb.go
+++ b/models/unittest/testdb.go
@@ -67,14 +67,16 @@ func MainTest(m *testing.M, pathToGiteaRoot string, fixtureFiles ...string) {
setting.SSH.Port = 3000
setting.SSH.Domain = "try.gitea.io"
setting.Database.UseSQLite3 = true
- setting.RepoRootPath, err = os.MkdirTemp(os.TempDir(), "repos")
+ repoRootPath, err := os.MkdirTemp(os.TempDir(), "repos")
if err != nil {
fatalTestError("TempDir: %v\n", err)
}
- setting.AppDataPath, err = os.MkdirTemp(os.TempDir(), "appdata")
+ setting.RepoRootPath = repoRootPath
+ appDataPath, err := os.MkdirTemp(os.TempDir(), "appdata")
if err != nil {
fatalTestError("TempDir: %v\n", err)
}
+ setting.AppDataPath = appDataPath
setting.AppWorkPath = pathToGiteaRoot
setting.StaticRootPath = pathToGiteaRoot
setting.GravatarSourceURL, err = url.Parse("https://secure.gravatar.com/avatar/")
@@ -95,7 +97,7 @@ func MainTest(m *testing.M, pathToGiteaRoot string, fixtureFiles ...string) {
fatalTestError("storage.Init: %v\n", err)
}
- if err = util.RemoveAll(setting.RepoRootPath); err != nil {
+ if err = util.RemoveAll(repoRootPath); err != nil {
fatalTestError("util.RemoveAll: %v\n", err)
}
if err = util.CopyDir(filepath.Join(pathToGiteaRoot, "integrations", "gitea-repositories-meta"), setting.RepoRootPath); err != nil {
@@ -103,10 +105,10 @@ func MainTest(m *testing.M, pathToGiteaRoot string, fixtureFiles ...string) {
}
exitStatus := m.Run()
- if err = util.RemoveAll(setting.RepoRootPath); err != nil {
+ if err = util.RemoveAll(repoRootPath); err != nil {
fatalTestError("util.RemoveAll: %v\n", err)
}
- if err = util.RemoveAll(setting.AppDataPath); err != nil {
+ if err = util.RemoveAll(appDataPath); err != nil {
fatalTestError("util.RemoveAll: %v\n", err)
}
os.Exit(exitStatus)