Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/pull-db-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
6 changes: 6 additions & 0 deletions models/unittest/fixtures_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -112,3 +113,8 @@ func BenchmarkFixturesLoader(b *testing.B) {
}
})
}

func TestMain(m *testing.M) {
env.Filter([]string{"GITEA_TEST_"}, []string{"GITEA_"})
m.Run()
}
2 changes: 2 additions & 0 deletions models/unittest/testdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
Expand Down
2 changes: 2 additions & 0 deletions modules/git/attribute/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions modules/git/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions modules/git/gitcmd/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions modules/git/languagestats/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions modules/gitrepo/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions modules/markup/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
2 changes: 2 additions & 0 deletions modules/markup/markdown/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
2 changes: 2 additions & 0 deletions modules/markup/orgmode/orgmode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
2 changes: 2 additions & 0 deletions modules/templates/util_render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions modules/timeutil/since_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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"}
Expand Down
31 changes: 31 additions & 0 deletions tests/env/filter.go
Original file line number Diff line number Diff line change
@@ -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
}
}
}
}
}
39 changes: 39 additions & 0 deletions tests/env/filter_test.go
Original file line number Diff line number Diff line change
@@ -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()
}
}
3 changes: 3 additions & 0 deletions tests/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,16 @@ 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"
)

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.
Expand Down