mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-01-10 15:42:16 +01:00
Refactor env var related code (#33075)
And add more comments (cherry picked from commit 4f386e2c5e39b860424faf4cbc02c16f641f956e) Conflicts: cmd/main_test.go tests/integration/integration_test.go trivial context conflicts
This commit is contained in:
parent
6208d5f5c2
commit
e507fa30df
5 changed files with 32 additions and 52 deletions
|
@ -6,7 +6,6 @@ package cmd
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
@ -114,37 +113,17 @@ func TestCliCmd(t *testing.T) {
|
||||||
_, _ = fmt.Fprint(ctx.App.Writer, makePathOutput(setting.AppWorkPath, setting.CustomPath, setting.CustomConf))
|
_, _ = fmt.Fprint(ctx.App.Writer, makePathOutput(setting.AppWorkPath, setting.CustomPath, setting.CustomConf))
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
var envBackup []string
|
|
||||||
for _, s := range os.Environ() {
|
|
||||||
if strings.HasPrefix(s, "GITEA_") && strings.Contains(s, "=") {
|
|
||||||
envBackup = append(envBackup, s)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
clearGiteaEnv := func() {
|
|
||||||
for _, s := range os.Environ() {
|
|
||||||
if strings.HasPrefix(s, "GITEA_") {
|
|
||||||
_ = os.Unsetenv(s)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
defer func() {
|
|
||||||
clearGiteaEnv()
|
|
||||||
for _, s := range envBackup {
|
|
||||||
k, v, _ := strings.Cut(s, "=")
|
|
||||||
_ = os.Setenv(k, v)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
for _, c := range cases {
|
for _, c := range cases {
|
||||||
clearGiteaEnv()
|
t.Run(c.cmd, func(t *testing.T) {
|
||||||
for k, v := range c.env {
|
for k, v := range c.env {
|
||||||
_ = os.Setenv(k, v)
|
t.Setenv(k, v)
|
||||||
}
|
}
|
||||||
args := strings.Split(c.cmd, " ") // for test only, "split" is good enough
|
args := strings.Split(c.cmd, " ") // for test only, "split" is good enough
|
||||||
r, err := runTestApp(app, args...)
|
r, err := runTestApp(app, args...)
|
||||||
require.NoError(t, err, c.cmd)
|
require.NoError(t, err, c.cmd)
|
||||||
assert.NotEmpty(t, c.exp, c.cmd)
|
assert.NotEmpty(t, c.exp, c.cmd)
|
||||||
assert.Contains(t, r.Stdout, c.exp, c.cmd)
|
assert.Contains(t, r.Stdout, c.exp, c.cmd)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -60,6 +60,12 @@ func InitSettings() {
|
||||||
|
|
||||||
setting.PasswordHashAlgo, _ = hash.SetDefaultPasswordHashAlgorithm("dummy")
|
setting.PasswordHashAlgo, _ = hash.SetDefaultPasswordHashAlgorithm("dummy")
|
||||||
setting.InitGiteaEnvVars()
|
setting.InitGiteaEnvVars()
|
||||||
|
|
||||||
|
// Avoid loading the git's system config.
|
||||||
|
// On macOS, system config sets the osxkeychain credential helper, which will cause tests to freeze with a dialog.
|
||||||
|
// But we do not set it in production at the moment, because it might be a "breaking" change,
|
||||||
|
// more details are in "modules/git.commonBaseEnvs".
|
||||||
|
_ = os.Setenv("GIT_CONFIG_NOSYSTEM", "true")
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestOptions represents test options
|
// TestOptions represents test options
|
||||||
|
|
|
@ -169,7 +169,21 @@ func EnvironmentToConfig(cfg ConfigProvider, envs []string) (changed bool) {
|
||||||
return changed
|
return changed
|
||||||
}
|
}
|
||||||
|
|
||||||
// InitGiteaEnvVars initilises the environment for gitea
|
// InitGiteaEnvVars initializes the environment variables for gitea
|
||||||
func InitGiteaEnvVars() {
|
func InitGiteaEnvVars() {
|
||||||
_ = os.Unsetenv("XDG_CONFIG_HOME") // unset if set as HOME is managed by gitea
|
// Ideally Gitea should only accept the environment variables which it clearly knows instead of unsetting the ones it doesn't want,
|
||||||
|
// but the ideal behavior would be a breaking change, and it seems not bringing enough benefits to end users,
|
||||||
|
// so at the moment we could still keep "unsetting the unnecessary environments"
|
||||||
|
|
||||||
|
// HOME is managed by Gitea, Gitea's git should use "HOME/.gitconfig".
|
||||||
|
// But git would try "XDG_CONFIG_HOME/git/config" first if "HOME/.gitconfig" does not exist,
|
||||||
|
// then our git.InitFull would still write to "XDG_CONFIG_HOME/git/config" if XDG_CONFIG_HOME is set.
|
||||||
|
_ = os.Unsetenv("XDG_CONFIG_HOME")
|
||||||
|
|
||||||
|
_ = os.Unsetenv("GIT_AUTHOR_NAME")
|
||||||
|
_ = os.Unsetenv("GIT_AUTHOR_EMAIL")
|
||||||
|
_ = os.Unsetenv("GIT_AUTHOR_DATE")
|
||||||
|
_ = os.Unsetenv("GIT_COMMITTER_NAME")
|
||||||
|
_ = os.Unsetenv("GIT_COMMITTER_EMAIL")
|
||||||
|
_ = os.Unsetenv("GIT_COMMITTER_DATE")
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,13 +40,6 @@ func TestMain(m *testing.M) {
|
||||||
initChangedFiles()
|
initChangedFiles()
|
||||||
testE2eWebRoutes = routers.NormalRoutes()
|
testE2eWebRoutes = routers.NormalRoutes()
|
||||||
|
|
||||||
os.Unsetenv("GIT_AUTHOR_NAME")
|
|
||||||
os.Unsetenv("GIT_AUTHOR_EMAIL")
|
|
||||||
os.Unsetenv("GIT_AUTHOR_DATE")
|
|
||||||
os.Unsetenv("GIT_COMMITTER_NAME")
|
|
||||||
os.Unsetenv("GIT_COMMITTER_EMAIL")
|
|
||||||
os.Unsetenv("GIT_COMMITTER_DATE")
|
|
||||||
|
|
||||||
err := unittest.InitFixtures(
|
err := unittest.InitFixtures(
|
||||||
unittest.FixturesOptions{
|
unittest.FixturesOptions{
|
||||||
Dir: filepath.Join(setting.AppWorkPath, "models/fixtures/"),
|
Dir: filepath.Join(setting.AppWorkPath, "models/fixtures/"),
|
||||||
|
|
|
@ -159,18 +159,6 @@ func TestMain(m *testing.M) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
os.Unsetenv("GIT_AUTHOR_NAME")
|
|
||||||
os.Unsetenv("GIT_AUTHOR_EMAIL")
|
|
||||||
os.Unsetenv("GIT_AUTHOR_DATE")
|
|
||||||
os.Unsetenv("GIT_COMMITTER_NAME")
|
|
||||||
os.Unsetenv("GIT_COMMITTER_EMAIL")
|
|
||||||
os.Unsetenv("GIT_COMMITTER_DATE")
|
|
||||||
|
|
||||||
// Avoid loading the default system config. On MacOS, this config
|
|
||||||
// sets the osxkeychain credential helper, which will cause tests
|
|
||||||
// to freeze with a dialog.
|
|
||||||
os.Setenv("GIT_CONFIG_NOSYSTEM", "true")
|
|
||||||
|
|
||||||
err := unittest.InitFixtures(
|
err := unittest.InitFixtures(
|
||||||
unittest.FixturesOptions{
|
unittest.FixturesOptions{
|
||||||
Dir: filepath.Join(filepath.Dir(setting.AppPath), "models/fixtures/"),
|
Dir: filepath.Join(filepath.Dir(setting.AppPath), "models/fixtures/"),
|
||||||
|
|
Loading…
Reference in a new issue