Skip to content

Commit dd5e967

Browse files
committed
fix
1 parent 2852708 commit dd5e967

File tree

5 files changed

+32
-52
lines changed

5 files changed

+32
-52
lines changed

cmd/main_test.go

Lines changed: 10 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package cmd
66
import (
77
"fmt"
88
"io"
9-
"os"
109
"path/filepath"
1110
"strings"
1211
"testing"
@@ -113,37 +112,17 @@ func TestCliCmd(t *testing.T) {
113112
_, _ = fmt.Fprint(ctx.App.Writer, makePathOutput(setting.AppWorkPath, setting.CustomPath, setting.CustomConf))
114113
return nil
115114
})
116-
var envBackup []string
117-
for _, s := range os.Environ() {
118-
if strings.HasPrefix(s, "GITEA_") && strings.Contains(s, "=") {
119-
envBackup = append(envBackup, s)
120-
}
121-
}
122-
clearGiteaEnv := func() {
123-
for _, s := range os.Environ() {
124-
if strings.HasPrefix(s, "GITEA_") {
125-
_ = os.Unsetenv(s)
126-
}
127-
}
128-
}
129-
defer func() {
130-
clearGiteaEnv()
131-
for _, s := range envBackup {
132-
k, v, _ := strings.Cut(s, "=")
133-
_ = os.Setenv(k, v)
134-
}
135-
}()
136-
137115
for _, c := range cases {
138-
clearGiteaEnv()
139-
for k, v := range c.env {
140-
_ = os.Setenv(k, v)
141-
}
142-
args := strings.Split(c.cmd, " ") // for test only, "split" is good enough
143-
r, err := runTestApp(app, args...)
144-
assert.NoError(t, err, c.cmd)
145-
assert.NotEmpty(t, c.exp, c.cmd)
146-
assert.Contains(t, r.Stdout, c.exp, c.cmd)
116+
t.Run(c.cmd, func(t *testing.T) {
117+
for k, v := range c.env {
118+
t.Setenv(k, v)
119+
}
120+
args := strings.Split(c.cmd, " ") // for test only, "split" is good enough
121+
r, err := runTestApp(app, args...)
122+
assert.NoError(t, err, c.cmd)
123+
assert.NotEmpty(t, c.exp, c.cmd)
124+
assert.Contains(t, r.Stdout, c.exp, c.cmd)
125+
})
147126
}
148127
}
149128

models/unittest/testdb.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ func InitSettings() {
6060

6161
setting.PasswordHashAlgo, _ = hash.SetDefaultPasswordHashAlgorithm("dummy")
6262
setting.InitGiteaEnvVars()
63+
64+
// Avoid loading the git's system config.
65+
// On macOS, system config sets the osxkeychain credential helper, which will cause tests to freeze with a dialog.
66+
// But we do not set it in production at the moment, because it might be a "breaking" change,
67+
// more details are in "modules/git.commonBaseEnvs".
68+
_ = os.Setenv("GIT_CONFIG_NOSYSTEM", "true")
6369
}
6470

6571
// TestOptions represents test options

modules/setting/config_env.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,21 @@ func EnvironmentToConfig(cfg ConfigProvider, envs []string) (changed bool) {
167167
return changed
168168
}
169169

170-
// InitGiteaEnvVars initilises the environment for gitea
170+
// InitGiteaEnvVars initializes the environment variables for gitea
171171
func InitGiteaEnvVars() {
172-
_ = os.Unsetenv("XDG_CONFIG_HOME") // unset if set as HOME is managed by gitea
172+
// Ideally Gitea should only accept the environment variables which it clearly knows instead of unsetting the ones it doesn't want,
173+
// but the ideal behavior would be a breaking change, and it seems not bringing enough benefits to end users,
174+
// so at the moment we could still keep "unsetting the unnecessary environments"
175+
176+
// HOME is managed by Gitea, Gitea's git should use "HOME/.gitconfig".
177+
// But git would try "XDG_CONFIG_HOME/git/config" first if "HOME/.gitconfig" does not exist,
178+
// then our git.InitFull would still write to "XDG_CONFIG_HOME/git/config" if XDG_CONFIG_HOME is set.
179+
_ = os.Unsetenv("XDG_CONFIG_HOME")
180+
181+
_ = os.Unsetenv("GIT_AUTHOR_NAME")
182+
_ = os.Unsetenv("GIT_AUTHOR_EMAIL")
183+
_ = os.Unsetenv("GIT_AUTHOR_DATE")
184+
_ = os.Unsetenv("GIT_COMMITTER_NAME")
185+
_ = os.Unsetenv("GIT_COMMITTER_EMAIL")
186+
_ = os.Unsetenv("GIT_COMMITTER_DATE")
173187
}

tests/e2e/e2e_test.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,6 @@ func TestMain(m *testing.M) {
4040
tests.InitTest(false)
4141
testE2eWebRoutes = routers.NormalRoutes()
4242

43-
os.Unsetenv("GIT_AUTHOR_NAME")
44-
os.Unsetenv("GIT_AUTHOR_EMAIL")
45-
os.Unsetenv("GIT_AUTHOR_DATE")
46-
os.Unsetenv("GIT_COMMITTER_NAME")
47-
os.Unsetenv("GIT_COMMITTER_EMAIL")
48-
os.Unsetenv("GIT_COMMITTER_DATE")
49-
5043
err := unittest.InitFixtures(
5144
unittest.FixturesOptions{
5245
Dir: filepath.Join(filepath.Dir(setting.AppPath), "models/fixtures/"),

tests/integration/integration_test.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -88,18 +88,6 @@ func TestMain(m *testing.M) {
8888
tests.InitTest(true)
8989
testWebRoutes = routers.NormalRoutes()
9090

91-
os.Unsetenv("GIT_AUTHOR_NAME")
92-
os.Unsetenv("GIT_AUTHOR_EMAIL")
93-
os.Unsetenv("GIT_AUTHOR_DATE")
94-
os.Unsetenv("GIT_COMMITTER_NAME")
95-
os.Unsetenv("GIT_COMMITTER_EMAIL")
96-
os.Unsetenv("GIT_COMMITTER_DATE")
97-
98-
// Avoid loading the default system config. On MacOS, this config
99-
// sets the osxkeychain credential helper, which will cause tests
100-
// to freeze with a dialog.
101-
os.Setenv("GIT_CONFIG_NOSYSTEM", "true")
102-
10391
err := unittest.InitFixtures(
10492
unittest.FixturesOptions{
10593
Dir: filepath.Join(filepath.Dir(setting.AppPath), "models/fixtures/"),

0 commit comments

Comments
 (0)