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 <[email protected]> Co-authored-by: delvh <[email protected]>
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

Lines changed: 4 additions & 3 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 7 additions & 5 deletions
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

Lines changed: 11 additions & 9 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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 {

0 commit comments

Comments
 (0)