Filter out gitea-specific variables in tests

There is no particular reason to set gitea variables globally.

Nonetheless, some misguided packagers set GITEA_CUSTOM in /etc/profile
breaking tests.

Fixes: go-gitea/gitea#36042
pull/36070/head
Michal Suchanek 2025-12-05 09:03:10 +07:00
parent c287a8cdb5
commit 9acb9377af
16 changed files with 104 additions and 0 deletions

@ -138,6 +138,9 @@ jobs:
- name: unit-tests
run: make unit-test-coverage test-check
env:
GITEA_ROOT: foo
GITEA_CONF: bar
GITEA_CUSTOM: baz
TAGS: bindata
RACE_ENABLED: true
GITHUB_READ_TOKEN: ${{ secrets.GITHUB_READ_TOKEN }}

@ -10,6 +10,7 @@ import (
"code.gitea.io/gitea/models/unittest"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/test"
"code.gitea.io/gitea/tests/env"
"github.com/stretchr/testify/require"
"xorm.io/xorm"
@ -112,3 +113,8 @@ func BenchmarkFixturesLoader(b *testing.B) {
}
})
}
func TestMain(m *testing.M) {
env.Filter([]string{"GITEA_TEST_"}, []string{"GITEA_"})
m.Run()
}

@ -23,6 +23,7 @@ import (
"code.gitea.io/gitea/modules/tempdir"
"code.gitea.io/gitea/modules/test"
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/tests/env"
"github.com/stretchr/testify/assert"
"xorm.io/xorm"
@ -73,6 +74,7 @@ type TestOptions struct {
// 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, testOptsArg ...*TestOptions) {
env.Filter([]string{"GITEA_TEST_"}, []string{"GITEA_"})
testOpts := util.OptionalArg(testOptsArg, &TestOptions{})
giteaRoot = test.SetupGiteaRoot()
setting.CustomPath = filepath.Join(giteaRoot, "custom")

@ -11,6 +11,7 @@ import (
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/tests/env"
)
func testRun(m *testing.M) error {
@ -33,6 +34,7 @@ func testRun(m *testing.M) error {
}
func TestMain(m *testing.M) {
env.Filter([]string{"GITEA_TEST_"}, []string{"GITEA_"})
if err := testRun(m); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "Test failed: %v", err)
os.Exit(1)

@ -10,6 +10,7 @@ import (
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/tempdir"
"code.gitea.io/gitea/tests/env"
"github.com/hashicorp/go-version"
"github.com/stretchr/testify/assert"
@ -36,6 +37,7 @@ func testRun(m *testing.M) error {
}
func TestMain(m *testing.M) {
env.Filter([]string{"GITEA_TEST_"}, []string{"GITEA_"})
if err := testRun(m); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "Test failed: %v", err)
os.Exit(1)

@ -10,11 +10,13 @@ import (
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/tempdir"
"code.gitea.io/gitea/tests/env"
"github.com/stretchr/testify/assert"
)
func TestMain(m *testing.M) {
env.Filter([]string{"GITEA_TEST_"}, []string{"GITEA_"})
gitHomePath, cleanup, err := tempdir.OsTempDir("gitea-test").MkdirTempRandom("git-home")
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "unable to create temp dir: %v", err)

@ -11,6 +11,7 @@ import (
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/tests/env"
)
func testRun(m *testing.M) error {
@ -33,6 +34,7 @@ func testRun(m *testing.M) error {
}
func TestMain(m *testing.M) {
env.Filter([]string{"GITEA_TEST_"}, []string{"GITEA_"})
if err := testRun(m); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "Test failed: %v", err)
os.Exit(1)

@ -12,9 +12,11 @@ import (
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/tempdir"
"code.gitea.io/gitea/modules/test"
"code.gitea.io/gitea/tests/env"
)
func TestMain(m *testing.M) {
env.Filter([]string{"GITEA_TEST_"}, []string{"GITEA_"})
gitHomePath, cleanup, err := tempdir.OsTempDir("gitea-test").MkdirTempRandom("git-home")
if err != nil {
log.Fatal("Unable to create temp dir: %v", err)

@ -9,9 +9,11 @@ import (
"code.gitea.io/gitea/modules/markup"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/tests/env"
)
func TestMain(m *testing.M) {
env.Filter([]string{"GITEA_TEST_"}, []string{"GITEA_"})
setting.IsInTesting = true
markup.RenderBehaviorForTesting.DisableAdditionalAttributes = true
os.Exit(m.Run())

@ -9,9 +9,11 @@ import (
"code.gitea.io/gitea/modules/markup"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/tests/env"
)
func TestMain(m *testing.M) {
env.Filter([]string{"GITEA_TEST_"}, []string{"GITEA_"})
setting.IsInTesting = true
markup.RenderBehaviorForTesting.DisableAdditionalAttributes = true
os.Exit(m.Run())

@ -11,11 +11,13 @@ import (
"code.gitea.io/gitea/modules/markup"
"code.gitea.io/gitea/modules/markup/orgmode"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/tests/env"
"github.com/stretchr/testify/assert"
)
func TestMain(m *testing.M) {
env.Filter([]string{"GITEA_TEST_"}, []string{"GITEA_"})
setting.AppURL = "http://localhost:3000/"
setting.IsInTesting = true
os.Exit(m.Run())

@ -18,6 +18,7 @@ import (
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/test"
"code.gitea.io/gitea/modules/translation"
"code.gitea.io/gitea/tests/env"
"github.com/stretchr/testify/assert"
)
@ -48,6 +49,7 @@ mail@domain.com
}
func TestMain(m *testing.M) {
env.Filter([]string{"GITEA_TEST_"}, []string{"GITEA_"})
setting.Markdown.RenderOptionsComment.ShortIssuePattern = true
markup.Init(&markup.RenderHelperFuncs{
IsUsernameMentionable: func(ctx context.Context, username string) bool {

@ -11,6 +11,7 @@ import (
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/translation"
"code.gitea.io/gitea/tests/env"
"github.com/stretchr/testify/assert"
)
@ -26,6 +27,7 @@ const (
)
func TestMain(m *testing.M) {
env.Filter([]string{"GITEA_TEST_"}, []string{"GITEA_"})
setting.StaticRootPath = "../../"
setting.Names = []string{"english"}
setting.Langs = []string{"en-US"}

@ -0,0 +1,31 @@
// Copyright 2025 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package env
import (
"os"
"strings"
)
func Filter(include, exclude []string) {
env := os.Environ()
for _, v := range env {
included := false
for _, i := range include {
if strings.HasPrefix(v, i) {
included = true
break
}
}
if !included {
for _, e := range exclude {
if strings.HasPrefix(v, e) {
parts := strings.SplitN(v, "=", 2)
os.Unsetenv(parts[0])
break
}
}
}
}
}

@ -0,0 +1,39 @@
// Copyright 2025 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package env
import (
"os"
"testing"
)
func TestFilter(t *testing.T) {
t.Setenv("GITEA_FOO", "bar")
t.Setenv("FOO", "bar")
Filter([]string{}, []string{"GITEA_"})
if os.Getenv("GITEA_FOO") != "" {
t.FailNow()
}
if os.Getenv("FOO") != "bar" {
t.FailNow()
}
t.Setenv("GITEA_TEST_FOO", "bar")
t.Setenv("GITEA_BAR", "foo")
t.Setenv("GITEA_BAR_BAZ", "foo")
t.Setenv("GITEA_BAZ", "huz")
Filter([]string{"GITEA_TEST_", "GITEA_BAR="}, []string{"GITEA_"})
if os.Getenv("GITEA_BAR") != "foo" {
t.Fail()
}
if os.Getenv("GITEA_TEST_FOO") != "bar" {
t.Fail()
}
if os.Getenv("GITEA_BAZ") != "" {
t.Fail()
}
if os.Getenv("GITEA_BAR_BAZ") != "" {
t.Fail()
}
}

@ -22,6 +22,7 @@ import (
"code.gitea.io/gitea/modules/testlogger"
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/routers"
"code.gitea.io/gitea/tests/env"
"github.com/stretchr/testify/assert"
)
@ -29,6 +30,8 @@ import (
func InitTest(requireGitea bool) {
testlogger.Init()
env.Filter([]string{"GITEA_TEST_", "GITEA_ROOT=", "GITEA_CONF="}, []string{"GITEA_"})
giteaRoot := test.SetupGiteaRoot()
// TODO: Speedup tests that rely on the event source ticker, confirm whether there is any bug or failure.