aboutsummaryrefslogtreecommitdiffstats
path: root/models/unittest
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2023-05-04 11:55:35 +0800
committerGitHub <noreply@github.com>2023-05-04 11:55:35 +0800
commit377a0a20f01a62f15a1504a3bba6cf6cc0c98bea (patch)
treee86a1818f23be1605a4cd707fadc7dcdce546d18 /models/unittest
parenta2fe68e50ba819daed9b0e28166a749d18c58750 (diff)
downloadgitea-377a0a20f01a62f15a1504a3bba6cf6cc0c98bea.tar.gz
gitea-377a0a20f01a62f15a1504a3bba6cf6cc0c98bea.zip
Merge setting.InitXXX into one function with options (#24389)
This PR will merge 3 Init functions on setting packages as 1 and introduce an options struct.
Diffstat (limited to 'models/unittest')
-rw-r--r--models/unittest/testdb.go22
1 files changed, 22 insertions, 0 deletions
diff --git a/models/unittest/testdb.go b/models/unittest/testdb.go
index cff1489a7c..a5b126350d 100644
--- a/models/unittest/testdb.go
+++ b/models/unittest/testdb.go
@@ -6,12 +6,15 @@ package unittest
import (
"context"
"fmt"
+ "log"
"os"
"path/filepath"
+ "strings"
"testing"
"code.gitea.io/gitea/models/db"
system_model "code.gitea.io/gitea/models/system"
+ "code.gitea.io/gitea/modules/auth/password/hash"
"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/setting"
@@ -39,6 +42,22 @@ func fatalTestError(fmtStr string, args ...interface{}) {
os.Exit(1)
}
+// InitSettings initializes config provider and load common setttings for tests
+func InitSettings(extraConfigs ...string) {
+ setting.Init(&setting.Options{
+ AllowEmpty: true,
+ ExtraConfig: strings.Join(extraConfigs, "\n"),
+ })
+
+ if err := setting.PrepareAppDataPath(); err != nil {
+ log.Fatalf("Can not prepare APP_DATA_PATH: %v", err)
+ }
+ // register the dummy hash algorithm function used in the test fixtures
+ _ = hash.Register("dummy", hash.NewDummyHasher)
+
+ setting.PasswordHashAlgo, _ = hash.SetDefaultPasswordHashAlgorithm("dummy")
+}
+
// TestOptions represents test options
type TestOptions struct {
GiteaRootPath string
@@ -50,6 +69,9 @@ type TestOptions struct {
// MainTest a reusable TestMain(..) function for unit tests that need to use a
// test database. Creates the test database, and sets necessary settings.
func MainTest(m *testing.M, testOpts *TestOptions) {
+ setting.SetCustomPathAndConf("", "", "")
+ InitSettings()
+
var err error
giteaRoot = testOpts.GiteaRootPath