Skip to content

Commit c53ad05

Browse files
lunny6543delvh
authored
Refactor the setting to make unit test easier (#22405)
Some bugs caused by less unit tests in fundamental packages. This PR refactor `setting` package so that create a unit test will be easier than before. - All `LoadFromXXX` files has been splited as two functions, one is `InitProviderFromXXX` and `LoadCommonSettings`. The first functions will only include the code to create or new a ini file. The second function will load common settings. - It also renames all functions in setting from `newXXXService` to `loadXXXSetting` or `loadXXXFrom` to make the function name less confusing. - Move `XORMLog` to `SQLLog` because it's a better name for that. Maybe we should finally move these `loadXXXSetting` into the `XXXInit` function? Any idea? --------- Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: delvh <dev.lh@web.de>
1 parent 2b02343 commit c53ad05

Some content is hidden

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

86 files changed

+1692
-1462
lines changed

cmd/cmd.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,10 @@ func confirm() (bool, error) {
5757
}
5858

5959
func initDB(ctx context.Context) error {
60-
setting.LoadFromExisting()
61-
setting.InitDBConfig()
62-
setting.NewXORMLogService(false)
60+
setting.InitProviderFromExistingFile()
61+
setting.LoadCommonSettings()
62+
setting.LoadDBSetting()
63+
setting.InitSQLLog(false)
6364

6465
if setting.Database.Type == "" {
6566
log.Fatal(`Database settings are missing from the configuration file: %q.

cmd/convert.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func runConvert(ctx *cli.Context) error {
3232
log.Info("AppPath: %s", setting.AppPath)
3333
log.Info("AppWorkPath: %s", setting.AppWorkPath)
3434
log.Info("Custom path: %s", setting.CustomPath)
35-
log.Info("Log path: %s", setting.LogRootPath)
35+
log.Info("Log path: %s", setting.Log.RootPath)
3636
log.Info("Configuration file: %s", setting.CustomConf)
3737

3838
if !setting.Database.UseMySQL {

cmd/doctor.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,16 @@ func runRecreateTable(ctx *cli.Context) error {
8787
golog.SetPrefix("")
8888
golog.SetOutput(log.NewLoggerAsWriter("INFO", log.GetLogger(log.DEFAULT)))
8989

90-
setting.LoadFromExisting()
91-
setting.InitDBConfig()
90+
setting.InitProviderFromExistingFile()
91+
setting.LoadCommonSettings()
92+
setting.LoadDBSetting()
9293

93-
setting.EnableXORMLog = ctx.Bool("debug")
94+
setting.Log.EnableXORMLog = ctx.Bool("debug")
9495
setting.Database.LogSQL = ctx.Bool("debug")
95-
setting.Cfg.Section("log").Key("XORM").SetValue(",")
96+
// FIXME: don't use CfgProvider directly
97+
setting.CfgProvider.Section("log").Key("XORM").SetValue(",")
9698

97-
setting.NewXORMLogService(!ctx.Bool("debug"))
99+
setting.InitSQLLog(!ctx.Bool("debug"))
98100
stdCtx, cancel := installSignals()
99101
defer cancel()
100102

cmd/dump.go

+11-9
Original file line numberDiff line numberDiff line change
@@ -181,20 +181,22 @@ func runDump(ctx *cli.Context) error {
181181
}
182182
fileName += "." + outType
183183
}
184-
setting.LoadFromExisting()
184+
setting.InitProviderFromExistingFile()
185+
setting.LoadCommonSettings()
185186

186187
// make sure we are logging to the console no matter what the configuration tells us do to
187-
if _, err := setting.Cfg.Section("log").NewKey("MODE", "console"); err != nil {
188+
// FIXME: don't use CfgProvider directly
189+
if _, err := setting.CfgProvider.Section("log").NewKey("MODE", "console"); err != nil {
188190
fatal("Setting logging mode to console failed: %v", err)
189191
}
190-
if _, err := setting.Cfg.Section("log.console").NewKey("STDERR", "true"); err != nil {
192+
if _, err := setting.CfgProvider.Section("log.console").NewKey("STDERR", "true"); err != nil {
191193
fatal("Setting console logger to stderr failed: %v", err)
192194
}
193195
if !setting.InstallLock {
194196
log.Error("Is '%s' really the right config path?\n", setting.CustomConf)
195197
return fmt.Errorf("gitea is not initialized")
196198
}
197-
setting.NewServices() // cannot access session settings otherwise
199+
setting.LoadSettings() // cannot access session settings otherwise
198200

199201
stdCtx, cancel := installSignals()
200202
defer cancel()
@@ -322,7 +324,7 @@ func runDump(ctx *cli.Context) error {
322324
log.Info("Packing data directory...%s", setting.AppDataPath)
323325

324326
var excludes []string
325-
if setting.Cfg.Section("session").Key("PROVIDER").Value() == "file" {
327+
if setting.SessionConfig.OriginalProvider == "file" {
326328
var opts session.Options
327329
if err = json.Unmarshal([]byte(setting.SessionConfig.ProviderConfig), &opts); err != nil {
328330
return err
@@ -339,7 +341,7 @@ func runDump(ctx *cli.Context) error {
339341
excludes = append(excludes, setting.LFS.Path)
340342
excludes = append(excludes, setting.Attachment.Path)
341343
excludes = append(excludes, setting.Packages.Path)
342-
excludes = append(excludes, setting.LogRootPath)
344+
excludes = append(excludes, setting.Log.RootPath)
343345
excludes = append(excludes, absFileName)
344346
if err := addRecursiveExclude(w, "data", setting.AppDataPath, excludes, verbose); err != nil {
345347
fatal("Failed to include data directory: %v", err)
@@ -378,12 +380,12 @@ func runDump(ctx *cli.Context) error {
378380
if ctx.IsSet("skip-log") && ctx.Bool("skip-log") {
379381
log.Info("Skip dumping log files")
380382
} else {
381-
isExist, err := util.IsExist(setting.LogRootPath)
383+
isExist, err := util.IsExist(setting.Log.RootPath)
382384
if err != nil {
383-
log.Error("Unable to check if %s exists. Error: %v", setting.LogRootPath, err)
385+
log.Error("Unable to check if %s exists. Error: %v", setting.Log.RootPath, err)
384386
}
385387
if isExist {
386-
if err := addRecursiveExclude(w, "log", setting.LogRootPath, []string{absFileName}, verbose); err != nil {
388+
if err := addRecursiveExclude(w, "log", setting.Log.RootPath, []string{absFileName}, verbose); err != nil {
387389
fatal("Failed to include log: %v", err)
388390
}
389391
}

cmd/dump_repo.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func runDumpRepository(ctx *cli.Context) error {
9494
log.Info("AppPath: %s", setting.AppPath)
9595
log.Info("AppWorkPath: %s", setting.AppWorkPath)
9696
log.Info("Custom path: %s", setting.CustomPath)
97-
log.Info("Log path: %s", setting.LogRootPath)
97+
log.Info("Log path: %s", setting.Log.RootPath)
9898
log.Info("Configuration file: %s", setting.CustomConf)
9999

100100
var (

cmd/embedded.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ func initEmbeddedExtractor(c *cli.Context) error {
112112
log.DelNamedLogger(log.DEFAULT)
113113

114114
// Read configuration file
115-
setting.LoadAllowEmpty()
115+
setting.InitProviderAllowEmpty()
116+
setting.LoadCommonSettings()
116117

117118
pats, err := getPatterns(c.Args())
118119
if err != nil {

cmd/mailer.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ func runSendMail(c *cli.Context) error {
1717
ctx, cancel := installSignals()
1818
defer cancel()
1919

20-
setting.LoadFromExisting()
20+
setting.InitProviderFromExistingFile()
21+
setting.LoadCommonSettings()
2122

2223
if err := argsSet(c, "title"); err != nil {
2324
return err

cmd/main_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212

1313
func init() {
1414
setting.SetCustomPathAndConf("", "", "")
15-
setting.LoadForTest()
15+
setting.InitProviderAndLoadCommonSettingsForTest()
1616
}
1717

1818
func TestMain(m *testing.M) {

cmd/migrate.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func runMigrate(ctx *cli.Context) error {
3333
log.Info("AppPath: %s", setting.AppPath)
3434
log.Info("AppWorkPath: %s", setting.AppWorkPath)
3535
log.Info("Custom path: %s", setting.CustomPath)
36-
log.Info("Log path: %s", setting.LogRootPath)
36+
log.Info("Log path: %s", setting.Log.RootPath)
3737
log.Info("Configuration file: %s", setting.CustomConf)
3838

3939
if err := db.InitEngineWithMigration(context.Background(), migrations.Migrate); err != nil {

cmd/migrate_storage.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func runMigrateStorage(ctx *cli.Context) error {
136136
log.Info("AppPath: %s", setting.AppPath)
137137
log.Info("AppWorkPath: %s", setting.AppWorkPath)
138138
log.Info("Custom path: %s", setting.CustomPath)
139-
log.Info("Log path: %s", setting.LogRootPath)
139+
log.Info("Log path: %s", setting.Log.RootPath)
140140
log.Info("Configuration file: %s", setting.CustomConf)
141141

142142
if err := db.InitEngineWithMigration(context.Background(), migrations.Migrate); err != nil {

cmd/restore_repo.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ func runRestoreRepository(c *cli.Context) error {
5454
ctx, cancel := installSignals()
5555
defer cancel()
5656

57-
setting.LoadFromExisting()
57+
setting.InitProviderFromExistingFile()
58+
setting.LoadCommonSettings()
5859
var units []string
5960
if s := c.String("units"); s != "" {
6061
units = strings.Split(s, ",")

cmd/serv.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ func setup(logPath string, debug bool) {
6161
} else {
6262
_ = log.NewLogger(1000, "console", "console", `{"level":"fatal","stacktracelevel":"NONE","stderr":true}`)
6363
}
64-
setting.LoadFromExisting()
64+
setting.InitProviderFromExistingFile()
65+
setting.LoadCommonSettings()
6566
if debug {
6667
setting.RunMode = "dev"
6768
}

cmd/web.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,8 @@ func runWeb(ctx *cli.Context) error {
158158

159159
log.Info("Global init")
160160
// Perform global initialization
161-
setting.LoadFromExisting()
161+
setting.InitProviderFromExistingFile()
162+
setting.LoadCommonSettings()
162163
routers.GlobalInitInstalled(graceful.GetManager().HammerContext())
163164

164165
// We check that AppDataPath exists here (it should have been created during installation)

contrib/pr/checkout.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ func runPR() {
4949
log.Fatal(err)
5050
}
5151
setting.SetCustomPathAndConf("", "", "")
52-
setting.LoadAllowEmpty()
52+
setting.InitProviderAllowEmpty()
53+
setting.LoadCommonSettings()
5354

5455
setting.RepoRootPath, err = os.MkdirTemp(os.TempDir(), "repos")
5556
if err != nil {
@@ -82,7 +83,7 @@ func runPR() {
8283
setting.Database.Path = ":memory:"
8384
setting.Database.Timeout = 500
8485
*/
85-
dbCfg := setting.Cfg.Section("database")
86+
dbCfg := setting.CfgProvider.Section("database")
8687
dbCfg.NewKey("DB_TYPE", "sqlite3")
8788
dbCfg.NewKey("PATH", ":memory:")
8889

models/asymkey/main_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313

1414
func init() {
1515
setting.SetCustomPathAndConf("", "", "")
16-
setting.LoadForTest()
16+
setting.InitProviderAndLoadCommonSettingsForTest()
1717
}
1818

1919
func TestMain(m *testing.M) {

models/dbfs/main_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313

1414
func init() {
1515
setting.SetCustomPathAndConf("", "", "")
16-
setting.LoadForTest()
16+
setting.InitProviderAndLoadCommonSettingsForTest()
1717
}
1818

1919
func TestMain(m *testing.M) {

models/issues/main_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020

2121
func init() {
2222
setting.SetCustomPathAndConf("", "", "")
23-
setting.LoadForTest()
23+
setting.InitProviderAndLoadCommonSettingsForTest()
2424
}
2525

2626
func TestFixturesAreConsistent(t *testing.T) {

models/main_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020

2121
func init() {
2222
setting.SetCustomPathAndConf("", "", "")
23-
setting.LoadForTest()
23+
setting.InitProviderAndLoadCommonSettingsForTest()
2424
}
2525

2626
// TestFixturesAreConsistent assert that test fixtures are consistent

models/migrations/base/tests.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,13 @@ func MainTest(m *testing.M) {
149149
setting.AppDataPath = tmpDataPath
150150

151151
setting.SetCustomPathAndConf("", "", "")
152-
setting.LoadForTest()
152+
setting.InitProviderAndLoadCommonSettingsForTest()
153153
if err = git.InitFull(context.Background()); err != nil {
154154
fmt.Printf("Unable to InitFull: %v\n", err)
155155
os.Exit(1)
156156
}
157-
setting.InitDBConfig()
158-
setting.NewLogServices(true)
157+
setting.LoadDBSetting()
158+
setting.InitLogs(true)
159159

160160
exitStatus := m.Run()
161161

modules/context/access_log.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ var signedUserNameStringPointerKey interface{} = "signedUserNameStringPointerKey
2727
// AccessLogger returns a middleware to log access logger
2828
func AccessLogger() func(http.Handler) http.Handler {
2929
logger := log.GetLogger("access")
30-
logTemplate, _ := template.New("log").Parse(setting.AccessLogTemplate)
30+
logTemplate, _ := template.New("log").Parse(setting.Log.AccessLogTemplate)
3131
return func(next http.Handler) http.Handler {
3232
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
3333
start := time.Now()

modules/doctor/doctor.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ func (w *wrappedLevelLogger) Log(skip int, level log.Level, format string, v ...
4444
}
4545

4646
func initDBDisableConsole(ctx context.Context, disableConsole bool) error {
47-
setting.LoadFromExisting()
48-
setting.InitDBConfig()
49-
50-
setting.NewXORMLogService(disableConsole)
47+
setting.InitProviderFromExistingFile()
48+
setting.LoadCommonSettings()
49+
setting.LoadDBSetting()
50+
setting.InitSQLLog(disableConsole)
5151
if err := db.InitEngine(ctx); err != nil {
5252
return fmt.Errorf("db.InitEngine: %w", err)
5353
}
@@ -71,7 +71,7 @@ func RunChecks(ctx context.Context, logger log.Logger, autofix bool, checks []*C
7171
for i, check := range checks {
7272
if !dbIsInit && !check.SkipDatabaseInitialization {
7373
// Only open database after the most basic configuration check
74-
setting.EnableXORMLog = false
74+
setting.Log.EnableXORMLog = false
7575
if err := initDBDisableConsole(ctx, true); err != nil {
7676
logger.Error("Error whilst initializing the database: %v", err)
7777
logger.Error("Check if you are using the right config file. You can use a --config directive to specify one.")

modules/doctor/paths.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,16 @@ func checkConfigurationFiles(ctx context.Context, logger log.Logger, autofix boo
6767
return err
6868
}
6969

70-
setting.LoadFromExisting()
70+
setting.InitProviderFromExistingFile()
71+
setting.LoadCommonSettings()
7172

7273
configurationFiles := []configurationFile{
7374
{"Configuration File Path", setting.CustomConf, false, true, false},
7475
{"Repository Root Path", setting.RepoRootPath, true, true, true},
7576
{"Data Root Path", setting.AppDataPath, true, true, true},
7677
{"Custom File Root Path", setting.CustomPath, true, false, false},
7778
{"Work directory", setting.AppWorkPath, true, true, false},
78-
{"Log Root Path", setting.LogRootPath, true, true, true},
79+
{"Log Root Path", setting.Log.RootPath, true, true, true},
7980
}
8081

8182
if options.IsDynamic() {

modules/highlight/highlight.go

+2-6
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,8 @@ var (
4141
// NewContext loads custom highlight map from local config
4242
func NewContext() {
4343
once.Do(func() {
44-
if setting.Cfg != nil {
45-
keys := setting.Cfg.Section("highlight.mapping").Keys()
46-
for i := range keys {
47-
highlightMapping[keys[i].Name()] = keys[i].Value()
48-
}
49-
}
44+
highlightMapping = setting.GetHighlightMapping()
45+
5046
// The size 512 is simply a conservative rule of thumb
5147
c, err := lru.New2Q(512)
5248
if err != nil {

modules/indexer/issues/indexer_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ func TestMain(m *testing.M) {
2727

2828
func TestBleveSearchIssues(t *testing.T) {
2929
assert.NoError(t, unittest.PrepareTestDatabase())
30-
setting.Cfg = ini.Empty()
30+
setting.CfgProvider = ini.Empty()
3131

3232
tmpIndexerDir := t.TempDir()
3333

34-
setting.Cfg.Section("queue.issue_indexer").Key("DATADIR").MustString(path.Join(tmpIndexerDir, "issues.queue"))
34+
setting.CfgProvider.Section("queue.issue_indexer").Key("DATADIR").MustString(path.Join(tmpIndexerDir, "issues.queue"))
3535

3636
oldIssuePath := setting.Indexer.IssuePath
3737
setting.Indexer.IssuePath = path.Join(tmpIndexerDir, "issues.queue")
@@ -40,7 +40,7 @@ func TestBleveSearchIssues(t *testing.T) {
4040
}()
4141

4242
setting.Indexer.IssueType = "bleve"
43-
setting.NewQueueService()
43+
setting.LoadQueueSettings()
4444
InitIssueIndexer(true)
4545
defer func() {
4646
indexer := holder.get()

modules/indexer/stats/indexer_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ func TestMain(m *testing.M) {
2929

3030
func TestRepoStatsIndex(t *testing.T) {
3131
assert.NoError(t, unittest.PrepareTestDatabase())
32-
setting.Cfg = ini.Empty()
32+
setting.CfgProvider = ini.Empty()
3333

34-
setting.NewQueueService()
34+
setting.LoadQueueSettings()
3535

3636
err := Init()
3737
assert.NoError(t, err)

modules/markup/html_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ var localMetas = map[string]string{
2828
}
2929

3030
func TestMain(m *testing.M) {
31-
setting.LoadAllowEmpty()
31+
setting.InitProviderAllowEmpty()
32+
setting.LoadCommonSettings()
3233
if err := git.InitSimple(context.Background()); err != nil {
3334
log.Fatal("git init failed, err: %v", err)
3435
}

modules/markup/markdown/markdown_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ var localMetas = map[string]string{
3333
}
3434

3535
func TestMain(m *testing.M) {
36-
setting.LoadAllowEmpty()
36+
setting.InitProviderAllowEmpty()
37+
setting.LoadCommonSettings()
3738
if err := git.InitSimple(context.Background()); err != nil {
3839
log.Fatal("git init failed, err: %v", err)
3940
}

0 commit comments

Comments
 (0)