ctx, cancel := installSignals()
defer cancel()
- setting.InitProviderFromExistingFile()
- setting.LoadCommonSettings()
+ setting.Init(&setting.Options{})
scope := c.String("scope")
}
func initDB(ctx context.Context) error {
- setting.InitProviderFromExistingFile()
- setting.LoadCommonSettings()
+ setting.Init(&setting.Options{})
setting.LoadDBSetting()
setting.InitSQLLog(false)
golog.SetPrefix("")
golog.SetOutput(log.NewLoggerAsWriter("INFO", log.GetLogger(log.DEFAULT)))
- setting.InitProviderFromExistingFile()
- setting.LoadCommonSettings()
+ setting.Init(&setting.Options{})
setting.LoadDBSetting()
setting.Log.EnableXORMLog = ctx.Bool("debug")
}
fileName += "." + outType
}
- setting.InitProviderFromExistingFile()
- setting.LoadCommonSettings()
+ setting.Init(&setting.Options{})
// make sure we are logging to the console no matter what the configuration tells us do to
// FIXME: don't use CfgProvider directly
log.DelNamedLogger(log.DEFAULT)
// Read configuration file
- setting.InitProviderAllowEmpty()
- setting.LoadCommonSettings()
+ setting.Init(&setting.Options{
+ AllowEmpty: true,
+ })
patterns, err := compileCollectPatterns(c.Args())
if err != nil {
ctx, cancel := installSignals()
defer cancel()
- setting.InitProviderFromExistingFile()
- setting.LoadCommonSettings()
+ setting.Init(&setting.Options{})
if err := argsSet(c, "title"); err != nil {
return err
"testing"
"code.gitea.io/gitea/models/unittest"
- "code.gitea.io/gitea/modules/setting"
)
-func init() {
- setting.SetCustomPathAndConf("", "", "")
- setting.InitProviderAndLoadCommonSettingsForTest()
-}
-
func TestMain(m *testing.M) {
unittest.MainTest(m, &unittest.TestOptions{
GiteaRootPath: "..",
ctx, cancel := installSignals()
defer cancel()
- setting.InitProviderFromExistingFile()
- setting.LoadCommonSettings()
+ setting.Init(&setting.Options{})
var units []string
if s := c.String("units"); s != "" {
units = strings.Split(s, ",")
} else {
_ = log.NewLogger(1000, "console", "console", `{"level":"fatal","stacktracelevel":"NONE","stderr":true}`)
}
- setting.InitProviderFromExistingFile()
- setting.LoadCommonSettings()
+ setting.Init(&setting.Options{})
if debug {
setting.RunMode = "dev"
}
log.Info("Global init")
// Perform global initialization
- setting.InitProviderFromExistingFile()
- setting.LoadCommonSettings()
+ setting.Init(&setting.Options{})
routers.GlobalInitInstalled(graceful.GetManager().HammerContext())
// We check that AppDataPath exists here (it should have been created during installation)
"testing"
"code.gitea.io/gitea/models/unittest"
- "code.gitea.io/gitea/modules/setting"
)
-func init() {
- setting.SetCustomPathAndConf("", "", "")
- setting.InitProviderAndLoadCommonSettingsForTest()
-}
-
func TestMain(m *testing.M) {
unittest.MainTest(m, &unittest.TestOptions{
GiteaRootPath: filepath.Join("..", ".."),
"testing"
"code.gitea.io/gitea/models/unittest"
- "code.gitea.io/gitea/modules/setting"
)
-func init() {
- setting.SetCustomPathAndConf("", "", "")
- setting.InitProviderAndLoadCommonSettingsForTest()
-}
-
func TestMain(m *testing.M) {
unittest.MainTest(m, &unittest.TestOptions{
GiteaRootPath: filepath.Join("..", ".."),
issues_model "code.gitea.io/gitea/models/issues"
"code.gitea.io/gitea/models/unittest"
- "code.gitea.io/gitea/modules/setting"
_ "code.gitea.io/gitea/models"
_ "code.gitea.io/gitea/models/repo"
"github.com/stretchr/testify/assert"
)
-func init() {
- setting.SetCustomPathAndConf("", "", "")
- setting.InitProviderAndLoadCommonSettingsForTest()
-}
-
func TestFixturesAreConsistent(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
unittest.CheckConsistencyFor(t,
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/unittest"
user_model "code.gitea.io/gitea/models/user"
- "code.gitea.io/gitea/modules/setting"
_ "code.gitea.io/gitea/models/system"
"github.com/stretchr/testify/assert"
)
-func init() {
- setting.SetCustomPathAndConf("", "", "")
- setting.InitProviderAndLoadCommonSettingsForTest()
-}
-
// TestFixturesAreConsistent assert that test fixtures are consistent
func TestFixturesAreConsistent(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
setting.AppDataPath = tmpDataPath
setting.SetCustomPathAndConf("", "", "")
- setting.InitProviderAndLoadCommonSettingsForTest()
+ unittest.InitSettings()
if err = git.InitFull(context.Background()); err != nil {
fmt.Printf("Unable to InitFull: %v\n", err)
os.Exit(1)
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"
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
// 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
}
func initDBDisableConsole(ctx context.Context, disableConsole bool) error {
- setting.InitProviderFromExistingFile()
- setting.LoadCommonSettings()
+ setting.Init(&setting.Options{})
setting.LoadDBSetting()
setting.InitSQLLog(disableConsole)
if err := db.InitEngine(ctx); err != nil {
return err
}
- setting.InitProviderFromExistingFile()
- setting.LoadCommonSettings()
+ setting.Init(&setting.Options{})
configurationFiles := []configurationFile{
{"Configuration File Path", setting.CustomConf, false, true, false},
}
func TestMain(m *testing.M) {
- setting.InitProviderAllowEmpty()
- setting.LoadCommonSettings()
+ setting.Init(&setting.Options{
+ AllowEmpty: true,
+ })
if err := git.InitSimple(context.Background()); err != nil {
log.Fatal("git init failed, err: %v", err)
}
}
func TestMain(m *testing.M) {
- setting.InitProviderAllowEmpty()
- setting.LoadCommonSettings()
+ setting.Init(&setting.Options{
+ AllowEmpty: true,
+ })
if err := git.InitSimple(context.Background()); err != nil {
log.Fatal("git init failed, err: %v", err)
}
}
type iniFileConfigProvider struct {
+ opts *Options
*ini.File
- filepath string // the ini file path
- newFile bool // whether the file has not existed previously
- allowEmpty bool // whether not finding configuration files is allowed (only true for the tests)
+ newFile bool // whether the file has not existed previously
}
// NewEmptyConfigProvider create a new empty config provider
}, 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.
// 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()
newFile := true
- if customConf != "" {
- isFile, err := util.IsFile(customConf)
+ if opts.CustomConf != "" {
+ isFile, err := util.IsFile(opts.CustomConf)
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 err := cfg.Append(customConf); err != nil {
- return nil, fmt.Errorf("failed to load custom conf '%s': %v", customConf, err)
+ if err := cfg.Append(opts.CustomConf); err != nil {
+ return nil, fmt.Errorf("failed to load custom conf '%s': %v", opts.CustomConf, err)
}
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)
}
- if extraConfig != "" {
- if err := cfg.Append([]byte(extraConfig)); err != nil {
+ if opts.ExtraConfig != "" {
+ if err := cfg.Append([]byte(opts.ExtraConfig)); err != nil {
return nil, fmt.Errorf("unable to append more config: %v", err)
}
}
cfg.NameMapper = ini.SnackCase
return &iniFileConfigProvider{
- File: cfg,
- filepath: customConf,
- newFile: newFile,
- allowEmpty: allowEmpty,
+ opts: opts,
+ File: cfg,
+ newFile: newFile,
}, nil
}
// Save save the content into file
func (p *iniFileConfigProvider) Save() error {
- if p.filepath == "" {
- if !p.allowEmpty {
+ if p.opts.CustomConf == "" {
+ if !p.opts.AllowEmpty {
return fmt.Errorf("custom config path must not be empty")
}
return nil
return fmt.Errorf("failed to create '%s': %v", CustomConf, err)
}
}
- if err := p.SaveTo(p.filepath); err != nil {
- return fmt.Errorf("failed to save '%s': %v", p.filepath, err)
+ if err := p.SaveTo(p.opts.CustomConf); err != nil {
+ return fmt.Errorf("failed to save '%s': %v", p.opts.CustomConf, err)
}
// Change permissions to be more restrictive
"strings"
"time"
- "code.gitea.io/gitea/modules/auth/password/hash"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/user"
)
return nil
}
-// InitProviderFromExistingFile initializes config provider from an existing config file (app.ini)
-func InitProviderFromExistingFile() {
- var err error
- CfgProvider, err = newConfigProviderFromFile(CustomConf, false, "")
- if err != nil {
- log.Fatal("InitProviderFromExistingFile: %v", err)
- }
-}
-
-// 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)
+func Init(opts *Options) {
+ if opts.CustomConf == "" {
+ opts.CustomConf = CustomConf
}
-}
-
-// 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"))
+ CfgProvider, err = newConfigProviderFromFile(opts)
if err != nil {
- log.Fatal("InitProviderAndLoadCommonSettingsForTest: %v", err)
+ log.Fatal("Init[%v]: %v", opts, err)
}
- loadCommonSettingsFrom(CfgProvider)
- if err := PrepareAppDataPath(); err != nil {
- log.Fatal("Can not prepare APP_DATA_PATH: %v", err)
+ if !opts.DisableLoadCommonSettings {
+ loadCommonSettingsFrom(CfgProvider)
}
- // 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.
)
func TestMain(m *testing.M) {
- setting.InitProviderAndLoadCommonSettingsForTest()
- setting.LoadQueueSettings()
unittest.MainTest(m, &unittest.TestOptions{
GiteaRootPath: filepath.Join("..", "..", "..", ".."),
- SetUp: webhook_service.Init,
+ SetUp: func() error {
+ setting.LoadQueueSettings()
+ return webhook_service.Init()
+ },
})
}
// PreloadSettings preloads the configuration to check if we need to run install
func PreloadSettings(ctx context.Context) bool {
- setting.InitProviderAllowEmpty()
- setting.LoadCommonSettings()
+ setting.Init(&setting.Options{
+ AllowEmpty: true,
+ })
if !setting.InstallLock {
log.Info("AppPath: %s", setting.AppPath)
log.Info("AppWorkPath: %s", setting.AppWorkPath)
// reloadSettings reloads the existing settings and starts up the database
func reloadSettings(ctx context.Context) {
- setting.InitProviderFromExistingFile()
- setting.LoadCommonSettings()
+ setting.Init(&setting.Options{})
setting.LoadDBSetting()
if setting.InstallLock {
if err := common.InitDBEngine(ctx); err == nil {
PasswordComplexity: pcLU,
},
} {
- unittest.PrepareTestEnv(t)
- ctx := test.MockContext(t, "user/settings/security")
- test.LoadUser(t, ctx, 2)
- test.LoadRepo(t, ctx, 1)
+ t.Run(req.OldPassword+"__"+req.NewPassword, func(t *testing.T) {
+ unittest.PrepareTestEnv(t)
+ setting.PasswordComplexity = req.PasswordComplexity
+ ctx := test.MockContext(t, "user/settings/security")
+ test.LoadUser(t, ctx, 2)
+ test.LoadRepo(t, ctx, 1)
- web.SetForm(ctx, &forms.ChangePasswordForm{
- OldPassword: req.OldPassword,
- Password: req.NewPassword,
- Retype: req.Retype,
- })
- AccountPost(ctx)
+ web.SetForm(ctx, &forms.ChangePasswordForm{
+ OldPassword: req.OldPassword,
+ Password: req.NewPassword,
+ Retype: req.Retype,
+ })
+ AccountPost(ctx)
- assert.Contains(t, ctx.Flash.ErrorMsg, req.Message)
- assert.EqualValues(t, http.StatusSeeOther, ctx.Resp.Status())
+ assert.Contains(t, ctx.Flash.ErrorMsg, req.Message)
+ assert.EqualValues(t, http.StatusSeeOther, ctx.Resp.Status())
+ })
}
}
)
func TestMain(m *testing.M) {
- setting.InitProviderAndLoadCommonSettingsForTest()
- setting.LoadQueueSettings()
-
// for tests, allow only loopback IPs
setting.Webhook.AllowedHostList = hostmatcher.MatchBuiltinLoopback
unittest.MainTest(m, &unittest.TestOptions{
GiteaRootPath: filepath.Join("..", ".."),
- SetUp: Init,
+ SetUp: func() error {
+ setting.LoadQueueSettings()
+ return Init()
+ },
})
}
setting.CustomConf = giteaConf
}
- setting.InitProviderAndLoadCommonSettingsForTest()
+ unittest.InitSettings()
assert.True(t, len(setting.RepoRootPath) != 0)
assert.NoError(t, util.RemoveAll(setting.RepoRootPath))
}
setting.SetCustomPathAndConf("", "", "")
- setting.InitProviderAndLoadCommonSettingsForTest()
+ unittest.InitSettings()
setting.Repository.DefaultBranch = "master" // many test code still assume that default branch is called "master"
_ = util.RemoveAll(repo_module.LocalCopyPath())