diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2023-02-20 00:12:01 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-02-20 00:12:01 +0800 |
commit | c53ad052d8bd040ecd269d9757ce9885396a04af (patch) | |
tree | 7142da12dc9511445c76d3bf5616f1c220441a84 /cmd | |
parent | 2b02343e218755c36c6a45da8a53c06dfff843a1 (diff) | |
download | gitea-c53ad052d8bd040ecd269d9757ce9885396a04af.tar.gz gitea-c53ad052d8bd040ecd269d9757ce9885396a04af.zip |
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>
Diffstat (limited to 'cmd')
-rw-r--r-- | cmd/cmd.go | 7 | ||||
-rw-r--r-- | cmd/convert.go | 2 | ||||
-rw-r--r-- | cmd/doctor.go | 12 | ||||
-rw-r--r-- | cmd/dump.go | 20 | ||||
-rw-r--r-- | cmd/dump_repo.go | 2 | ||||
-rw-r--r-- | cmd/embedded.go | 3 | ||||
-rw-r--r-- | cmd/mailer.go | 3 | ||||
-rw-r--r-- | cmd/main_test.go | 2 | ||||
-rw-r--r-- | cmd/migrate.go | 2 | ||||
-rw-r--r-- | cmd/migrate_storage.go | 2 | ||||
-rw-r--r-- | cmd/restore_repo.go | 3 | ||||
-rw-r--r-- | cmd/serv.go | 3 | ||||
-rw-r--r-- | cmd/web.go | 3 |
13 files changed, 37 insertions, 27 deletions
diff --git a/cmd/cmd.go b/cmd/cmd.go index 493519e135..18d5db3987 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -57,9 +57,10 @@ func confirm() (bool, error) { } func initDB(ctx context.Context) error { - setting.LoadFromExisting() - setting.InitDBConfig() - setting.NewXORMLogService(false) + setting.InitProviderFromExistingFile() + setting.LoadCommonSettings() + setting.LoadDBSetting() + setting.InitSQLLog(false) if setting.Database.Type == "" { log.Fatal(`Database settings are missing from the configuration file: %q. diff --git a/cmd/convert.go b/cmd/convert.go index b9ed9f1627..30e7d01e11 100644 --- a/cmd/convert.go +++ b/cmd/convert.go @@ -32,7 +32,7 @@ func runConvert(ctx *cli.Context) error { log.Info("AppPath: %s", setting.AppPath) log.Info("AppWorkPath: %s", setting.AppWorkPath) log.Info("Custom path: %s", setting.CustomPath) - log.Info("Log path: %s", setting.LogRootPath) + log.Info("Log path: %s", setting.Log.RootPath) log.Info("Configuration file: %s", setting.CustomConf) if !setting.Database.UseMySQL { diff --git a/cmd/doctor.go b/cmd/doctor.go index ceb6e3fbab..e7baad60c1 100644 --- a/cmd/doctor.go +++ b/cmd/doctor.go @@ -87,14 +87,16 @@ func runRecreateTable(ctx *cli.Context) error { golog.SetPrefix("") golog.SetOutput(log.NewLoggerAsWriter("INFO", log.GetLogger(log.DEFAULT))) - setting.LoadFromExisting() - setting.InitDBConfig() + setting.InitProviderFromExistingFile() + setting.LoadCommonSettings() + setting.LoadDBSetting() - setting.EnableXORMLog = ctx.Bool("debug") + setting.Log.EnableXORMLog = ctx.Bool("debug") setting.Database.LogSQL = ctx.Bool("debug") - setting.Cfg.Section("log").Key("XORM").SetValue(",") + // FIXME: don't use CfgProvider directly + setting.CfgProvider.Section("log").Key("XORM").SetValue(",") - setting.NewXORMLogService(!ctx.Bool("debug")) + setting.InitSQLLog(!ctx.Bool("debug")) stdCtx, cancel := installSignals() defer cancel() diff --git a/cmd/dump.go b/cmd/dump.go index f40ddbac23..c879d2fbee 100644 --- a/cmd/dump.go +++ b/cmd/dump.go @@ -181,20 +181,22 @@ func runDump(ctx *cli.Context) error { } fileName += "." + outType } - setting.LoadFromExisting() + setting.InitProviderFromExistingFile() + setting.LoadCommonSettings() // make sure we are logging to the console no matter what the configuration tells us do to - if _, err := setting.Cfg.Section("log").NewKey("MODE", "console"); err != nil { + // FIXME: don't use CfgProvider directly + if _, err := setting.CfgProvider.Section("log").NewKey("MODE", "console"); err != nil { fatal("Setting logging mode to console failed: %v", err) } - if _, err := setting.Cfg.Section("log.console").NewKey("STDERR", "true"); err != nil { + if _, err := setting.CfgProvider.Section("log.console").NewKey("STDERR", "true"); err != nil { fatal("Setting console logger to stderr failed: %v", err) } if !setting.InstallLock { log.Error("Is '%s' really the right config path?\n", setting.CustomConf) return fmt.Errorf("gitea is not initialized") } - setting.NewServices() // cannot access session settings otherwise + setting.LoadSettings() // cannot access session settings otherwise stdCtx, cancel := installSignals() defer cancel() @@ -322,7 +324,7 @@ func runDump(ctx *cli.Context) error { log.Info("Packing data directory...%s", setting.AppDataPath) var excludes []string - if setting.Cfg.Section("session").Key("PROVIDER").Value() == "file" { + if setting.SessionConfig.OriginalProvider == "file" { var opts session.Options if err = json.Unmarshal([]byte(setting.SessionConfig.ProviderConfig), &opts); err != nil { return err @@ -339,7 +341,7 @@ func runDump(ctx *cli.Context) error { excludes = append(excludes, setting.LFS.Path) excludes = append(excludes, setting.Attachment.Path) excludes = append(excludes, setting.Packages.Path) - excludes = append(excludes, setting.LogRootPath) + excludes = append(excludes, setting.Log.RootPath) excludes = append(excludes, absFileName) if err := addRecursiveExclude(w, "data", setting.AppDataPath, excludes, verbose); err != nil { fatal("Failed to include data directory: %v", err) @@ -378,12 +380,12 @@ func runDump(ctx *cli.Context) error { if ctx.IsSet("skip-log") && ctx.Bool("skip-log") { log.Info("Skip dumping log files") } else { - isExist, err := util.IsExist(setting.LogRootPath) + isExist, err := util.IsExist(setting.Log.RootPath) if err != nil { - log.Error("Unable to check if %s exists. Error: %v", setting.LogRootPath, err) + log.Error("Unable to check if %s exists. Error: %v", setting.Log.RootPath, err) } if isExist { - if err := addRecursiveExclude(w, "log", setting.LogRootPath, []string{absFileName}, verbose); err != nil { + if err := addRecursiveExclude(w, "log", setting.Log.RootPath, []string{absFileName}, verbose); err != nil { fatal("Failed to include log: %v", err) } } diff --git a/cmd/dump_repo.go b/cmd/dump_repo.go index b7b9b3ccc7..0d3970466c 100644 --- a/cmd/dump_repo.go +++ b/cmd/dump_repo.go @@ -94,7 +94,7 @@ func runDumpRepository(ctx *cli.Context) error { log.Info("AppPath: %s", setting.AppPath) log.Info("AppWorkPath: %s", setting.AppWorkPath) log.Info("Custom path: %s", setting.CustomPath) - log.Info("Log path: %s", setting.LogRootPath) + log.Info("Log path: %s", setting.Log.RootPath) log.Info("Configuration file: %s", setting.CustomConf) var ( diff --git a/cmd/embedded.go b/cmd/embedded.go index 118781895e..d87fc0187c 100644 --- a/cmd/embedded.go +++ b/cmd/embedded.go @@ -112,7 +112,8 @@ func initEmbeddedExtractor(c *cli.Context) error { log.DelNamedLogger(log.DEFAULT) // Read configuration file - setting.LoadAllowEmpty() + setting.InitProviderAllowEmpty() + setting.LoadCommonSettings() pats, err := getPatterns(c.Args()) if err != nil { diff --git a/cmd/mailer.go b/cmd/mailer.go index af6613f159..d05fee12bc 100644 --- a/cmd/mailer.go +++ b/cmd/mailer.go @@ -17,7 +17,8 @@ func runSendMail(c *cli.Context) error { ctx, cancel := installSignals() defer cancel() - setting.LoadFromExisting() + setting.InitProviderFromExistingFile() + setting.LoadCommonSettings() if err := argsSet(c, "title"); err != nil { return err diff --git a/cmd/main_test.go b/cmd/main_test.go index 9aacdf7bba..ba323af472 100644 --- a/cmd/main_test.go +++ b/cmd/main_test.go @@ -12,7 +12,7 @@ import ( func init() { setting.SetCustomPathAndConf("", "", "") - setting.LoadForTest() + setting.InitProviderAndLoadCommonSettingsForTest() } func TestMain(m *testing.M) { diff --git a/cmd/migrate.go b/cmd/migrate.go index 2546fca21d..efa791bc65 100644 --- a/cmd/migrate.go +++ b/cmd/migrate.go @@ -33,7 +33,7 @@ func runMigrate(ctx *cli.Context) error { log.Info("AppPath: %s", setting.AppPath) log.Info("AppWorkPath: %s", setting.AppWorkPath) log.Info("Custom path: %s", setting.CustomPath) - log.Info("Log path: %s", setting.LogRootPath) + log.Info("Log path: %s", setting.Log.RootPath) log.Info("Configuration file: %s", setting.CustomConf) if err := db.InitEngineWithMigration(context.Background(), migrations.Migrate); err != nil { diff --git a/cmd/migrate_storage.go b/cmd/migrate_storage.go index 0b8ebe7c8d..dfa7212e5b 100644 --- a/cmd/migrate_storage.go +++ b/cmd/migrate_storage.go @@ -136,7 +136,7 @@ func runMigrateStorage(ctx *cli.Context) error { log.Info("AppPath: %s", setting.AppPath) log.Info("AppWorkPath: %s", setting.AppWorkPath) log.Info("Custom path: %s", setting.CustomPath) - log.Info("Log path: %s", setting.LogRootPath) + log.Info("Log path: %s", setting.Log.RootPath) log.Info("Configuration file: %s", setting.CustomConf) if err := db.InitEngineWithMigration(context.Background(), migrations.Migrate); err != nil { diff --git a/cmd/restore_repo.go b/cmd/restore_repo.go index 23932f821c..c7dff41966 100644 --- a/cmd/restore_repo.go +++ b/cmd/restore_repo.go @@ -54,7 +54,8 @@ func runRestoreRepository(c *cli.Context) error { ctx, cancel := installSignals() defer cancel() - setting.LoadFromExisting() + setting.InitProviderFromExistingFile() + setting.LoadCommonSettings() var units []string if s := c.String("units"); s != "" { units = strings.Split(s, ",") diff --git a/cmd/serv.go b/cmd/serv.go index 346c918b18..145d1b9e93 100644 --- a/cmd/serv.go +++ b/cmd/serv.go @@ -61,7 +61,8 @@ func setup(logPath string, debug bool) { } else { _ = log.NewLogger(1000, "console", "console", `{"level":"fatal","stacktracelevel":"NONE","stderr":true}`) } - setting.LoadFromExisting() + setting.InitProviderFromExistingFile() + setting.LoadCommonSettings() if debug { setting.RunMode = "dev" } diff --git a/cmd/web.go b/cmd/web.go index 49a0335615..8722ddb609 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -158,7 +158,8 @@ func runWeb(ctx *cli.Context) error { log.Info("Global init") // Perform global initialization - setting.LoadFromExisting() + setting.InitProviderFromExistingFile() + setting.LoadCommonSettings() routers.GlobalInitInstalled(graceful.GetManager().HammerContext()) // We check that AppDataPath exists here (it should have been created during installation) |