mirror of
				https://github.com/go-gitea/gitea.git
				synced 2025-10-29 18:44:46 +01:00 
			
		
		
		
	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.
This commit is contained in:
		
							parent
							
								
									a2fe68e50b
								
							
						
					
					
						commit
						377a0a20f0
					
				| @ -42,8 +42,7 @@ func runGenerateActionsRunnerToken(c *cli.Context) error { | |||||||
| 	ctx, cancel := installSignals() | 	ctx, cancel := installSignals() | ||||||
| 	defer cancel() | 	defer cancel() | ||||||
| 
 | 
 | ||||||
| 	setting.InitProviderFromExistingFile() | 	setting.Init(&setting.Options{}) | ||||||
| 	setting.LoadCommonSettings() |  | ||||||
| 
 | 
 | ||||||
| 	scope := c.String("scope") | 	scope := c.String("scope") | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -57,8 +57,7 @@ func confirm() (bool, error) { | |||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func initDB(ctx context.Context) error { | func initDB(ctx context.Context) error { | ||||||
| 	setting.InitProviderFromExistingFile() | 	setting.Init(&setting.Options{}) | ||||||
| 	setting.LoadCommonSettings() |  | ||||||
| 	setting.LoadDBSetting() | 	setting.LoadDBSetting() | ||||||
| 	setting.InitSQLLog(false) | 	setting.InitSQLLog(false) | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -87,8 +87,7 @@ func runRecreateTable(ctx *cli.Context) error { | |||||||
| 	golog.SetPrefix("") | 	golog.SetPrefix("") | ||||||
| 	golog.SetOutput(log.NewLoggerAsWriter("INFO", log.GetLogger(log.DEFAULT))) | 	golog.SetOutput(log.NewLoggerAsWriter("INFO", log.GetLogger(log.DEFAULT))) | ||||||
| 
 | 
 | ||||||
| 	setting.InitProviderFromExistingFile() | 	setting.Init(&setting.Options{}) | ||||||
| 	setting.LoadCommonSettings() |  | ||||||
| 	setting.LoadDBSetting() | 	setting.LoadDBSetting() | ||||||
| 
 | 
 | ||||||
| 	setting.Log.EnableXORMLog = ctx.Bool("debug") | 	setting.Log.EnableXORMLog = ctx.Bool("debug") | ||||||
|  | |||||||
| @ -185,8 +185,7 @@ func runDump(ctx *cli.Context) error { | |||||||
| 		} | 		} | ||||||
| 		fileName += "." + outType | 		fileName += "." + outType | ||||||
| 	} | 	} | ||||||
| 	setting.InitProviderFromExistingFile() | 	setting.Init(&setting.Options{}) | ||||||
| 	setting.LoadCommonSettings() |  | ||||||
| 
 | 
 | ||||||
| 	// make sure we are logging to the console no matter what the configuration tells us do to | 	// make sure we are logging to the console no matter what the configuration tells us do to | ||||||
| 	// FIXME: don't use CfgProvider directly | 	// FIXME: don't use CfgProvider directly | ||||||
|  | |||||||
| @ -106,8 +106,9 @@ func initEmbeddedExtractor(c *cli.Context) error { | |||||||
| 	log.DelNamedLogger(log.DEFAULT) | 	log.DelNamedLogger(log.DEFAULT) | ||||||
| 
 | 
 | ||||||
| 	// Read configuration file | 	// Read configuration file | ||||||
| 	setting.InitProviderAllowEmpty() | 	setting.Init(&setting.Options{ | ||||||
| 	setting.LoadCommonSettings() | 		AllowEmpty: true, | ||||||
|  | 	}) | ||||||
| 
 | 
 | ||||||
| 	patterns, err := compileCollectPatterns(c.Args()) | 	patterns, err := compileCollectPatterns(c.Args()) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
|  | |||||||
| @ -16,8 +16,7 @@ func runSendMail(c *cli.Context) error { | |||||||
| 	ctx, cancel := installSignals() | 	ctx, cancel := installSignals() | ||||||
| 	defer cancel() | 	defer cancel() | ||||||
| 
 | 
 | ||||||
| 	setting.InitProviderFromExistingFile() | 	setting.Init(&setting.Options{}) | ||||||
| 	setting.LoadCommonSettings() |  | ||||||
| 
 | 
 | ||||||
| 	if err := argsSet(c, "title"); err != nil { | 	if err := argsSet(c, "title"); err != nil { | ||||||
| 		return err | 		return err | ||||||
|  | |||||||
| @ -7,14 +7,8 @@ import ( | |||||||
| 	"testing" | 	"testing" | ||||||
| 
 | 
 | ||||||
| 	"code.gitea.io/gitea/models/unittest" | 	"code.gitea.io/gitea/models/unittest" | ||||||
| 	"code.gitea.io/gitea/modules/setting" |  | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| func init() { |  | ||||||
| 	setting.SetCustomPathAndConf("", "", "") |  | ||||||
| 	setting.InitProviderAndLoadCommonSettingsForTest() |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| func TestMain(m *testing.M) { | func TestMain(m *testing.M) { | ||||||
| 	unittest.MainTest(m, &unittest.TestOptions{ | 	unittest.MainTest(m, &unittest.TestOptions{ | ||||||
| 		GiteaRootPath: "..", | 		GiteaRootPath: "..", | ||||||
|  | |||||||
| @ -51,8 +51,7 @@ func runRestoreRepository(c *cli.Context) error { | |||||||
| 	ctx, cancel := installSignals() | 	ctx, cancel := installSignals() | ||||||
| 	defer cancel() | 	defer cancel() | ||||||
| 
 | 
 | ||||||
| 	setting.InitProviderFromExistingFile() | 	setting.Init(&setting.Options{}) | ||||||
| 	setting.LoadCommonSettings() |  | ||||||
| 	var units []string | 	var units []string | ||||||
| 	if s := c.String("units"); s != "" { | 	if s := c.String("units"); s != "" { | ||||||
| 		units = strings.Split(s, ",") | 		units = strings.Split(s, ",") | ||||||
|  | |||||||
| @ -62,8 +62,7 @@ func setup(ctx context.Context, debug bool) { | |||||||
| 	} else { | 	} else { | ||||||
| 		_ = log.NewLogger(1000, "console", "console", `{"level":"fatal","stacktracelevel":"NONE","stderr":true}`) | 		_ = log.NewLogger(1000, "console", "console", `{"level":"fatal","stacktracelevel":"NONE","stderr":true}`) | ||||||
| 	} | 	} | ||||||
| 	setting.InitProviderFromExistingFile() | 	setting.Init(&setting.Options{}) | ||||||
| 	setting.LoadCommonSettings() |  | ||||||
| 	if debug { | 	if debug { | ||||||
| 		setting.RunMode = "dev" | 		setting.RunMode = "dev" | ||||||
| 	} | 	} | ||||||
|  | |||||||
| @ -177,8 +177,7 @@ func runWeb(ctx *cli.Context) error { | |||||||
| 
 | 
 | ||||||
| 	log.Info("Global init") | 	log.Info("Global init") | ||||||
| 	// Perform global initialization | 	// Perform global initialization | ||||||
| 	setting.InitProviderFromExistingFile() | 	setting.Init(&setting.Options{}) | ||||||
| 	setting.LoadCommonSettings() |  | ||||||
| 	routers.GlobalInitInstalled(graceful.GetManager().HammerContext()) | 	routers.GlobalInitInstalled(graceful.GetManager().HammerContext()) | ||||||
| 
 | 
 | ||||||
| 	// We check that AppDataPath exists here (it should have been created during installation) | 	// We check that AppDataPath exists here (it should have been created during installation) | ||||||
|  | |||||||
| @ -8,14 +8,8 @@ import ( | |||||||
| 	"testing" | 	"testing" | ||||||
| 
 | 
 | ||||||
| 	"code.gitea.io/gitea/models/unittest" | 	"code.gitea.io/gitea/models/unittest" | ||||||
| 	"code.gitea.io/gitea/modules/setting" |  | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| func init() { |  | ||||||
| 	setting.SetCustomPathAndConf("", "", "") |  | ||||||
| 	setting.InitProviderAndLoadCommonSettingsForTest() |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| func TestMain(m *testing.M) { | func TestMain(m *testing.M) { | ||||||
| 	unittest.MainTest(m, &unittest.TestOptions{ | 	unittest.MainTest(m, &unittest.TestOptions{ | ||||||
| 		GiteaRootPath: filepath.Join("..", ".."), | 		GiteaRootPath: filepath.Join("..", ".."), | ||||||
|  | |||||||
| @ -8,14 +8,8 @@ import ( | |||||||
| 	"testing" | 	"testing" | ||||||
| 
 | 
 | ||||||
| 	"code.gitea.io/gitea/models/unittest" | 	"code.gitea.io/gitea/models/unittest" | ||||||
| 	"code.gitea.io/gitea/modules/setting" |  | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| func init() { |  | ||||||
| 	setting.SetCustomPathAndConf("", "", "") |  | ||||||
| 	setting.InitProviderAndLoadCommonSettingsForTest() |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| func TestMain(m *testing.M) { | func TestMain(m *testing.M) { | ||||||
| 	unittest.MainTest(m, &unittest.TestOptions{ | 	unittest.MainTest(m, &unittest.TestOptions{ | ||||||
| 		GiteaRootPath: filepath.Join("..", ".."), | 		GiteaRootPath: filepath.Join("..", ".."), | ||||||
|  | |||||||
| @ -9,7 +9,6 @@ import ( | |||||||
| 
 | 
 | ||||||
| 	issues_model "code.gitea.io/gitea/models/issues" | 	issues_model "code.gitea.io/gitea/models/issues" | ||||||
| 	"code.gitea.io/gitea/models/unittest" | 	"code.gitea.io/gitea/models/unittest" | ||||||
| 	"code.gitea.io/gitea/modules/setting" |  | ||||||
| 
 | 
 | ||||||
| 	_ "code.gitea.io/gitea/models" | 	_ "code.gitea.io/gitea/models" | ||||||
| 	_ "code.gitea.io/gitea/models/repo" | 	_ "code.gitea.io/gitea/models/repo" | ||||||
| @ -18,11 +17,6 @@ import ( | |||||||
| 	"github.com/stretchr/testify/assert" | 	"github.com/stretchr/testify/assert" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| func init() { |  | ||||||
| 	setting.SetCustomPathAndConf("", "", "") |  | ||||||
| 	setting.InitProviderAndLoadCommonSettingsForTest() |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| func TestFixturesAreConsistent(t *testing.T) { | func TestFixturesAreConsistent(t *testing.T) { | ||||||
| 	assert.NoError(t, unittest.PrepareTestDatabase()) | 	assert.NoError(t, unittest.PrepareTestDatabase()) | ||||||
| 	unittest.CheckConsistencyFor(t, | 	unittest.CheckConsistencyFor(t, | ||||||
|  | |||||||
| @ -11,18 +11,12 @@ import ( | |||||||
| 	repo_model "code.gitea.io/gitea/models/repo" | 	repo_model "code.gitea.io/gitea/models/repo" | ||||||
| 	"code.gitea.io/gitea/models/unittest" | 	"code.gitea.io/gitea/models/unittest" | ||||||
| 	user_model "code.gitea.io/gitea/models/user" | 	user_model "code.gitea.io/gitea/models/user" | ||||||
| 	"code.gitea.io/gitea/modules/setting" |  | ||||||
| 
 | 
 | ||||||
| 	_ "code.gitea.io/gitea/models/system" | 	_ "code.gitea.io/gitea/models/system" | ||||||
| 
 | 
 | ||||||
| 	"github.com/stretchr/testify/assert" | 	"github.com/stretchr/testify/assert" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| func init() { |  | ||||||
| 	setting.SetCustomPathAndConf("", "", "") |  | ||||||
| 	setting.InitProviderAndLoadCommonSettingsForTest() |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| // TestFixturesAreConsistent assert that test fixtures are consistent | // TestFixturesAreConsistent assert that test fixtures are consistent | ||||||
| func TestFixturesAreConsistent(t *testing.T) { | func TestFixturesAreConsistent(t *testing.T) { | ||||||
| 	assert.NoError(t, unittest.PrepareTestDatabase()) | 	assert.NoError(t, unittest.PrepareTestDatabase()) | ||||||
|  | |||||||
| @ -150,7 +150,7 @@ func MainTest(m *testing.M) { | |||||||
| 	setting.AppDataPath = tmpDataPath | 	setting.AppDataPath = tmpDataPath | ||||||
| 
 | 
 | ||||||
| 	setting.SetCustomPathAndConf("", "", "") | 	setting.SetCustomPathAndConf("", "", "") | ||||||
| 	setting.InitProviderAndLoadCommonSettingsForTest() | 	unittest.InitSettings() | ||||||
| 	if err = git.InitFull(context.Background()); err != nil { | 	if err = git.InitFull(context.Background()); err != nil { | ||||||
| 		fmt.Printf("Unable to InitFull: %v\n", err) | 		fmt.Printf("Unable to InitFull: %v\n", err) | ||||||
| 		os.Exit(1) | 		os.Exit(1) | ||||||
|  | |||||||
| @ -6,12 +6,15 @@ package unittest | |||||||
| import ( | import ( | ||||||
| 	"context" | 	"context" | ||||||
| 	"fmt" | 	"fmt" | ||||||
|  | 	"log" | ||||||
| 	"os" | 	"os" | ||||||
| 	"path/filepath" | 	"path/filepath" | ||||||
|  | 	"strings" | ||||||
| 	"testing" | 	"testing" | ||||||
| 
 | 
 | ||||||
| 	"code.gitea.io/gitea/models/db" | 	"code.gitea.io/gitea/models/db" | ||||||
| 	system_model "code.gitea.io/gitea/models/system" | 	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/base" | ||||||
| 	"code.gitea.io/gitea/modules/git" | 	"code.gitea.io/gitea/modules/git" | ||||||
| 	"code.gitea.io/gitea/modules/setting" | 	"code.gitea.io/gitea/modules/setting" | ||||||
| @ -39,6 +42,22 @@ func fatalTestError(fmtStr string, args ...interface{}) { | |||||||
| 	os.Exit(1) | 	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 | // TestOptions represents test options | ||||||
| type TestOptions struct { | type TestOptions struct { | ||||||
| 	GiteaRootPath string | 	GiteaRootPath string | ||||||
| @ -50,6 +69,9 @@ type TestOptions struct { | |||||||
| // MainTest a reusable TestMain(..) function for unit tests that need to use a | // MainTest a reusable TestMain(..) function for unit tests that need to use a | ||||||
| // test database. Creates the test database, and sets necessary settings. | // test database. Creates the test database, and sets necessary settings. | ||||||
| func MainTest(m *testing.M, testOpts *TestOptions) { | func MainTest(m *testing.M, testOpts *TestOptions) { | ||||||
|  | 	setting.SetCustomPathAndConf("", "", "") | ||||||
|  | 	InitSettings() | ||||||
|  | 
 | ||||||
| 	var err error | 	var err error | ||||||
| 
 | 
 | ||||||
| 	giteaRoot = testOpts.GiteaRootPath | 	giteaRoot = testOpts.GiteaRootPath | ||||||
|  | |||||||
| @ -44,8 +44,7 @@ func (w *wrappedLevelLogger) Log(skip int, level log.Level, format string, v ... | |||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func initDBDisableConsole(ctx context.Context, disableConsole bool) error { | func initDBDisableConsole(ctx context.Context, disableConsole bool) error { | ||||||
| 	setting.InitProviderFromExistingFile() | 	setting.Init(&setting.Options{}) | ||||||
| 	setting.LoadCommonSettings() |  | ||||||
| 	setting.LoadDBSetting() | 	setting.LoadDBSetting() | ||||||
| 	setting.InitSQLLog(disableConsole) | 	setting.InitSQLLog(disableConsole) | ||||||
| 	if err := db.InitEngine(ctx); err != nil { | 	if err := db.InitEngine(ctx); err != nil { | ||||||
|  | |||||||
| @ -66,8 +66,7 @@ func checkConfigurationFiles(ctx context.Context, logger log.Logger, autofix boo | |||||||
| 		return err | 		return err | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	setting.InitProviderFromExistingFile() | 	setting.Init(&setting.Options{}) | ||||||
| 	setting.LoadCommonSettings() |  | ||||||
| 
 | 
 | ||||||
| 	configurationFiles := []configurationFile{ | 	configurationFiles := []configurationFile{ | ||||||
| 		{"Configuration File Path", setting.CustomConf, false, true, false}, | 		{"Configuration File Path", setting.CustomConf, false, true, false}, | ||||||
|  | |||||||
| @ -28,8 +28,9 @@ var localMetas = map[string]string{ | |||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func TestMain(m *testing.M) { | func TestMain(m *testing.M) { | ||||||
| 	setting.InitProviderAllowEmpty() | 	setting.Init(&setting.Options{ | ||||||
| 	setting.LoadCommonSettings() | 		AllowEmpty: true, | ||||||
|  | 	}) | ||||||
| 	if err := git.InitSimple(context.Background()); err != nil { | 	if err := git.InitSimple(context.Background()); err != nil { | ||||||
| 		log.Fatal("git init failed, err: %v", err) | 		log.Fatal("git init failed, err: %v", err) | ||||||
| 	} | 	} | ||||||
|  | |||||||
| @ -33,8 +33,9 @@ var localMetas = map[string]string{ | |||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func TestMain(m *testing.M) { | func TestMain(m *testing.M) { | ||||||
| 	setting.InitProviderAllowEmpty() | 	setting.Init(&setting.Options{ | ||||||
| 	setting.LoadCommonSettings() | 		AllowEmpty: true, | ||||||
|  | 	}) | ||||||
| 	if err := git.InitSimple(context.Background()); err != nil { | 	if err := git.InitSimple(context.Background()); err != nil { | ||||||
| 		log.Fatal("git init failed, err: %v", err) | 		log.Fatal("git init failed, err: %v", err) | ||||||
| 	} | 	} | ||||||
|  | |||||||
| @ -35,10 +35,9 @@ type ConfigProvider interface { | |||||||
| } | } | ||||||
| 
 | 
 | ||||||
| type iniFileConfigProvider struct { | type iniFileConfigProvider struct { | ||||||
|  | 	opts *Options | ||||||
| 	*ini.File | 	*ini.File | ||||||
| 	filepath   string // the ini file path | 	newFile bool // whether the file has not existed previously | ||||||
| 	newFile    bool   // whether the file has not existed previously |  | ||||||
| 	allowEmpty bool   // whether not finding configuration files is allowed (only true for the tests) |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // NewEmptyConfigProvider create a new empty config provider | // NewEmptyConfigProvider create a new empty config provider | ||||||
| @ -66,41 +65,47 @@ func newConfigProviderFromData(configContent string) (ConfigProvider, error) { | |||||||
| 	}, nil | 	}, nil | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | type Options struct { | ||||||
|  | 	CustomConf                string // the ini file path | ||||||
|  | 	AllowEmpty                bool   // whether not finding configuration files is allowed (only true for the tests) | ||||||
|  | 	ExtraConfig               string | ||||||
|  | 	DisableLoadCommonSettings bool | ||||||
|  | } | ||||||
|  | 
 | ||||||
| // newConfigProviderFromFile load configuration from file. | // newConfigProviderFromFile load configuration from file. | ||||||
| // NOTE: do not print any log except error. | // NOTE: do not print any log except error. | ||||||
| func newConfigProviderFromFile(customConf string, allowEmpty bool, extraConfig string) (*iniFileConfigProvider, error) { | func newConfigProviderFromFile(opts *Options) (*iniFileConfigProvider, error) { | ||||||
| 	cfg := ini.Empty() | 	cfg := ini.Empty() | ||||||
| 	newFile := true | 	newFile := true | ||||||
| 
 | 
 | ||||||
| 	if customConf != "" { | 	if opts.CustomConf != "" { | ||||||
| 		isFile, err := util.IsFile(customConf) | 		isFile, err := util.IsFile(opts.CustomConf) | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			return nil, fmt.Errorf("unable to check if %s is a file. Error: %v", customConf, err) | 			return nil, fmt.Errorf("unable to check if %s is a file. Error: %v", opts.CustomConf, err) | ||||||
| 		} | 		} | ||||||
| 		if isFile { | 		if isFile { | ||||||
| 			if err := cfg.Append(customConf); err != nil { | 			if err := cfg.Append(opts.CustomConf); err != nil { | ||||||
| 				return nil, fmt.Errorf("failed to load custom conf '%s': %v", customConf, err) | 				return nil, fmt.Errorf("failed to load custom conf '%s': %v", opts.CustomConf, err) | ||||||
| 			} | 			} | ||||||
| 			newFile = false | 			newFile = false | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	if newFile && !allowEmpty { | 	if newFile && !opts.AllowEmpty { | ||||||
| 		return nil, fmt.Errorf("unable to find configuration file: %q, please ensure you are running in the correct environment or set the correct configuration file with -c", CustomConf) | 		return nil, fmt.Errorf("unable to find configuration file: %q, please ensure you are running in the correct environment or set the correct configuration file with -c", CustomConf) | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	if extraConfig != "" { | 	if opts.ExtraConfig != "" { | ||||||
| 		if err := cfg.Append([]byte(extraConfig)); err != nil { | 		if err := cfg.Append([]byte(opts.ExtraConfig)); err != nil { | ||||||
| 			return nil, fmt.Errorf("unable to append more config: %v", err) | 			return nil, fmt.Errorf("unable to append more config: %v", err) | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	cfg.NameMapper = ini.SnackCase | 	cfg.NameMapper = ini.SnackCase | ||||||
| 	return &iniFileConfigProvider{ | 	return &iniFileConfigProvider{ | ||||||
| 		File:       cfg, | 		opts:    opts, | ||||||
| 		filepath:   customConf, | 		File:    cfg, | ||||||
| 		newFile:    newFile, | 		newFile: newFile, | ||||||
| 		allowEmpty: allowEmpty, |  | ||||||
| 	}, nil | 	}, nil | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| @ -123,8 +128,8 @@ func (p *iniFileConfigProvider) DeleteSection(name string) error { | |||||||
| 
 | 
 | ||||||
| // Save save the content into file | // Save save the content into file | ||||||
| func (p *iniFileConfigProvider) Save() error { | func (p *iniFileConfigProvider) Save() error { | ||||||
| 	if p.filepath == "" { | 	if p.opts.CustomConf == "" { | ||||||
| 		if !p.allowEmpty { | 		if !p.opts.AllowEmpty { | ||||||
| 			return fmt.Errorf("custom config path must not be empty") | 			return fmt.Errorf("custom config path must not be empty") | ||||||
| 		} | 		} | ||||||
| 		return nil | 		return nil | ||||||
| @ -135,8 +140,8 @@ func (p *iniFileConfigProvider) Save() error { | |||||||
| 			return fmt.Errorf("failed to create '%s': %v", CustomConf, err) | 			return fmt.Errorf("failed to create '%s': %v", CustomConf, err) | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 	if err := p.SaveTo(p.filepath); err != nil { | 	if err := p.SaveTo(p.opts.CustomConf); err != nil { | ||||||
| 		return fmt.Errorf("failed to save '%s': %v", p.filepath, err) | 		return fmt.Errorf("failed to save '%s': %v", p.opts.CustomConf, err) | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	// Change permissions to be more restrictive | 	// Change permissions to be more restrictive | ||||||
|  | |||||||
| @ -15,7 +15,6 @@ import ( | |||||||
| 	"strings" | 	"strings" | ||||||
| 	"time" | 	"time" | ||||||
| 
 | 
 | ||||||
| 	"code.gitea.io/gitea/modules/auth/password/hash" |  | ||||||
| 	"code.gitea.io/gitea/modules/log" | 	"code.gitea.io/gitea/modules/log" | ||||||
| 	"code.gitea.io/gitea/modules/user" | 	"code.gitea.io/gitea/modules/user" | ||||||
| ) | ) | ||||||
| @ -203,46 +202,20 @@ func PrepareAppDataPath() error { | |||||||
| 	return nil | 	return nil | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // InitProviderFromExistingFile initializes config provider from an existing config file (app.ini) | func Init(opts *Options) { | ||||||
| func InitProviderFromExistingFile() { | 	if opts.CustomConf == "" { | ||||||
|  | 		opts.CustomConf = CustomConf | ||||||
|  | 	} | ||||||
| 	var err error | 	var err error | ||||||
| 	CfgProvider, err = newConfigProviderFromFile(CustomConf, false, "") | 	CfgProvider, err = newConfigProviderFromFile(opts) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		log.Fatal("InitProviderFromExistingFile: %v", err) | 		log.Fatal("Init[%v]: %v", opts, err) | ||||||
| 	} | 	} | ||||||
| } | 	if !opts.DisableLoadCommonSettings { | ||||||
| 
 | 		loadCommonSettingsFrom(CfgProvider) | ||||||
| // InitProviderAllowEmpty initializes config provider from file, it's also fine that if the config file (app.ini) doesn't exist |  | ||||||
| func InitProviderAllowEmpty() { |  | ||||||
| 	var err error |  | ||||||
| 	CfgProvider, err = newConfigProviderFromFile(CustomConf, true, "") |  | ||||||
| 	if err != nil { |  | ||||||
| 		log.Fatal("InitProviderAllowEmpty: %v", err) |  | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // InitProviderAndLoadCommonSettingsForTest initializes config provider and load common setttings for tests |  | ||||||
| func InitProviderAndLoadCommonSettingsForTest(extraConfigs ...string) { |  | ||||||
| 	var err error |  | ||||||
| 	CfgProvider, err = newConfigProviderFromFile(CustomConf, true, strings.Join(extraConfigs, "\n")) |  | ||||||
| 	if err != nil { |  | ||||||
| 		log.Fatal("InitProviderAndLoadCommonSettingsForTest: %v", err) |  | ||||||
| 	} |  | ||||||
| 	loadCommonSettingsFrom(CfgProvider) |  | ||||||
| 	if err := PrepareAppDataPath(); err != nil { |  | ||||||
| 		log.Fatal("Can not prepare APP_DATA_PATH: %v", err) |  | ||||||
| 	} |  | ||||||
| 	// register the dummy hash algorithm function used in the test fixtures |  | ||||||
| 	_ = hash.Register("dummy", hash.NewDummyHasher) |  | ||||||
| 
 |  | ||||||
| 	PasswordHashAlgo, _ = hash.SetDefaultPasswordHashAlgorithm("dummy") |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| // LoadCommonSettings loads common configurations from a configuration provider. |  | ||||||
| func LoadCommonSettings() { |  | ||||||
| 	loadCommonSettingsFrom(CfgProvider) |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| // loadCommonSettingsFrom loads common configurations from a configuration provider. | // loadCommonSettingsFrom loads common configurations from a configuration provider. | ||||||
| func loadCommonSettingsFrom(cfg ConfigProvider) { | func loadCommonSettingsFrom(cfg ConfigProvider) { | ||||||
| 	// WARNNING: don't change the sequence except you know what you are doing. | 	// WARNNING: don't change the sequence except you know what you are doing. | ||||||
|  | |||||||
| @ -13,10 +13,11 @@ import ( | |||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| func TestMain(m *testing.M) { | func TestMain(m *testing.M) { | ||||||
| 	setting.InitProviderAndLoadCommonSettingsForTest() |  | ||||||
| 	setting.LoadQueueSettings() |  | ||||||
| 	unittest.MainTest(m, &unittest.TestOptions{ | 	unittest.MainTest(m, &unittest.TestOptions{ | ||||||
| 		GiteaRootPath: filepath.Join("..", "..", "..", ".."), | 		GiteaRootPath: filepath.Join("..", "..", "..", ".."), | ||||||
| 		SetUp:         webhook_service.Init, | 		SetUp: func() error { | ||||||
|  | 			setting.LoadQueueSettings() | ||||||
|  | 			return webhook_service.Init() | ||||||
|  | 		}, | ||||||
| 	}) | 	}) | ||||||
| } | } | ||||||
|  | |||||||
| @ -15,8 +15,9 @@ import ( | |||||||
| 
 | 
 | ||||||
| // PreloadSettings preloads the configuration to check if we need to run install | // PreloadSettings preloads the configuration to check if we need to run install | ||||||
| func PreloadSettings(ctx context.Context) bool { | func PreloadSettings(ctx context.Context) bool { | ||||||
| 	setting.InitProviderAllowEmpty() | 	setting.Init(&setting.Options{ | ||||||
| 	setting.LoadCommonSettings() | 		AllowEmpty: true, | ||||||
|  | 	}) | ||||||
| 	if !setting.InstallLock { | 	if !setting.InstallLock { | ||||||
| 		log.Info("AppPath: %s", setting.AppPath) | 		log.Info("AppPath: %s", setting.AppPath) | ||||||
| 		log.Info("AppWorkPath: %s", setting.AppWorkPath) | 		log.Info("AppWorkPath: %s", setting.AppWorkPath) | ||||||
| @ -38,8 +39,7 @@ func PreloadSettings(ctx context.Context) bool { | |||||||
| 
 | 
 | ||||||
| // reloadSettings reloads the existing settings and starts up the database | // reloadSettings reloads the existing settings and starts up the database | ||||||
| func reloadSettings(ctx context.Context) { | func reloadSettings(ctx context.Context) { | ||||||
| 	setting.InitProviderFromExistingFile() | 	setting.Init(&setting.Options{}) | ||||||
| 	setting.LoadCommonSettings() |  | ||||||
| 	setting.LoadDBSetting() | 	setting.LoadDBSetting() | ||||||
| 	if setting.InstallLock { | 	if setting.InstallLock { | ||||||
| 		if err := common.InitDBEngine(ctx); err == nil { | 		if err := common.InitDBEngine(ctx); err == nil { | ||||||
|  | |||||||
| @ -80,19 +80,22 @@ func TestChangePassword(t *testing.T) { | |||||||
| 			PasswordComplexity: pcLU, | 			PasswordComplexity: pcLU, | ||||||
| 		}, | 		}, | ||||||
| 	} { | 	} { | ||||||
| 		unittest.PrepareTestEnv(t) | 		t.Run(req.OldPassword+"__"+req.NewPassword, func(t *testing.T) { | ||||||
| 		ctx := test.MockContext(t, "user/settings/security") | 			unittest.PrepareTestEnv(t) | ||||||
| 		test.LoadUser(t, ctx, 2) | 			setting.PasswordComplexity = req.PasswordComplexity | ||||||
| 		test.LoadRepo(t, ctx, 1) | 			ctx := test.MockContext(t, "user/settings/security") | ||||||
|  | 			test.LoadUser(t, ctx, 2) | ||||||
|  | 			test.LoadRepo(t, ctx, 1) | ||||||
| 
 | 
 | ||||||
| 		web.SetForm(ctx, &forms.ChangePasswordForm{ | 			web.SetForm(ctx, &forms.ChangePasswordForm{ | ||||||
| 			OldPassword: req.OldPassword, | 				OldPassword: req.OldPassword, | ||||||
| 			Password:    req.NewPassword, | 				Password:    req.NewPassword, | ||||||
| 			Retype:      req.Retype, | 				Retype:      req.Retype, | ||||||
|  | 			}) | ||||||
|  | 			AccountPost(ctx) | ||||||
|  | 
 | ||||||
|  | 			assert.Contains(t, ctx.Flash.ErrorMsg, req.Message) | ||||||
|  | 			assert.EqualValues(t, http.StatusSeeOther, ctx.Resp.Status()) | ||||||
| 		}) | 		}) | ||||||
| 		AccountPost(ctx) |  | ||||||
| 
 |  | ||||||
| 		assert.Contains(t, ctx.Flash.ErrorMsg, req.Message) |  | ||||||
| 		assert.EqualValues(t, http.StatusSeeOther, ctx.Resp.Status()) |  | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  | |||||||
| @ -15,13 +15,13 @@ import ( | |||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| func TestMain(m *testing.M) { | func TestMain(m *testing.M) { | ||||||
| 	setting.InitProviderAndLoadCommonSettingsForTest() |  | ||||||
| 	setting.LoadQueueSettings() |  | ||||||
| 
 |  | ||||||
| 	// for tests, allow only loopback IPs | 	// for tests, allow only loopback IPs | ||||||
| 	setting.Webhook.AllowedHostList = hostmatcher.MatchBuiltinLoopback | 	setting.Webhook.AllowedHostList = hostmatcher.MatchBuiltinLoopback | ||||||
| 	unittest.MainTest(m, &unittest.TestOptions{ | 	unittest.MainTest(m, &unittest.TestOptions{ | ||||||
| 		GiteaRootPath: filepath.Join("..", ".."), | 		GiteaRootPath: filepath.Join("..", ".."), | ||||||
| 		SetUp:         Init, | 		SetUp: func() error { | ||||||
|  | 			setting.LoadQueueSettings() | ||||||
|  | 			return Init() | ||||||
|  | 		}, | ||||||
| 	}) | 	}) | ||||||
| } | } | ||||||
|  | |||||||
| @ -57,7 +57,7 @@ func initMigrationTest(t *testing.T) func() { | |||||||
| 		setting.CustomConf = giteaConf | 		setting.CustomConf = giteaConf | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	setting.InitProviderAndLoadCommonSettingsForTest() | 	unittest.InitSettings() | ||||||
| 
 | 
 | ||||||
| 	assert.True(t, len(setting.RepoRootPath) != 0) | 	assert.True(t, len(setting.RepoRootPath) != 0) | ||||||
| 	assert.NoError(t, util.RemoveAll(setting.RepoRootPath)) | 	assert.NoError(t, util.RemoveAll(setting.RepoRootPath)) | ||||||
|  | |||||||
| @ -74,7 +74,7 @@ func InitTest(requireGitea bool) { | |||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	setting.SetCustomPathAndConf("", "", "") | 	setting.SetCustomPathAndConf("", "", "") | ||||||
| 	setting.InitProviderAndLoadCommonSettingsForTest() | 	unittest.InitSettings() | ||||||
| 	setting.Repository.DefaultBranch = "master" // many test code still assume that default branch is called "master" | 	setting.Repository.DefaultBranch = "master" // many test code still assume that default branch is called "master" | ||||||
| 	_ = util.RemoveAll(repo_module.LocalCopyPath()) | 	_ = util.RemoveAll(repo_module.LocalCopyPath()) | ||||||
| 
 | 
 | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user