Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Filter gitea-specific variables when running tests#36070

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
hramrach wants to merge1 commit intogo-gitea:main
base:main
Choose a base branch
Loading
fromhramrach:main
Open
Show file tree
Hide file tree
Changes fromall 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
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -146,6 +146,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 deletionsmodels/unittest/fixtures_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand Up@@ -112,3 +113,8 @@ func BenchmarkFixturesLoader(b *testing.B) {
}
})
}

func TestMain(m *testing.M) {
env.Filter([]string{"GITEA_TEST_", "GITEA_UNIT_TESTS_"}, []string{"GITEA_"})
m.Run()
}
2 changes: 2 additions & 0 deletionsmodels/unittest/testdb.go
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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.
funcMainTest(m*testing.M,testOptsArg...*TestOptions) {
env.Filter([]string{"GITEA_TEST_","GITEA_UNIT_TESTS_"}, []string{"GITEA_"})
testOpts:=util.OptionalArg(testOptsArg,&TestOptions{})
giteaRoot=test.SetupGiteaRoot()
setting.CustomPath=filepath.Join(giteaRoot,"custom")
Expand Down
2 changes: 2 additions & 0 deletionsmodules/git/attribute/main_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff 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_", "GITEA_UNIT_TESTS_"}, []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 deletionsmodules/git/git_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff 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_", "GITEA_UNIT_TESTS_"}, []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 deletionsmodules/git/gitcmd/command_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff 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_", "GITEA_UNIT_TESTS_"}, []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 deletionsmodules/git/languagestats/main_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff 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"
)

functestRun(m*testing.M)error {
Expand All@@ -33,6 +34,7 @@ func testRun(m *testing.M) error {
}

funcTestMain(m*testing.M) {
env.Filter([]string{"GITEA_TEST_","GITEA_UNIT_TESTS_"}, []string{"GITEA_"})
iferr:=testRun(m);err!=nil {
_,_=fmt.Fprintf(os.Stderr,"Test failed: %v",err)
os.Exit(1)
Expand Down
2 changes: 2 additions & 0 deletionsmodules/gitrepo/main_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff 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_", "GITEA_UNIT_TESTS_"}, []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 deletionsmodules/markup/main_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff 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"
)

funcTestMain(m*testing.M) {
env.Filter([]string{"GITEA_TEST_","GITEA_UNIT_TESTS_"}, []string{"GITEA_"})
setting.IsInTesting=true
markup.RenderBehaviorForTesting.DisableAdditionalAttributes=true
os.Exit(m.Run())
Expand Down
2 changes: 2 additions & 0 deletionsmodules/markup/markdown/main_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff 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"
)

funcTestMain(m*testing.M) {
env.Filter([]string{"GITEA_TEST_","GITEA_UNIT_TESTS_"}, []string{"GITEA_"})
setting.IsInTesting=true
markup.RenderBehaviorForTesting.DisableAdditionalAttributes=true
os.Exit(m.Run())
Expand Down
2 changes: 2 additions & 0 deletionsmodules/markup/orgmode/orgmode_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff 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_", "GITEA_UNIT_TESTS_"}, []string{"GITEA_"})
setting.AppURL = "http://localhost:3000/"
setting.IsInTesting = true
os.Exit(m.Run())
Expand Down
2 changes: 2 additions & 0 deletionsmodules/templates/util_render_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand Up@@ -48,6 +49,7 @@ mail@domain.com
}

funcTestMain(m*testing.M) {
env.Filter([]string{"GITEA_TEST_","GITEA_UNIT_TESTS_"}, []string{"GITEA_"})
setting.Markdown.RenderOptionsComment.ShortIssuePattern=true
markup.Init(&markup.RenderHelperFuncs{
IsUsernameMentionable:func(ctx context.Context,usernamestring)bool {
Expand Down
2 changes: 2 additions & 0 deletionsmodules/timeutil/since_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff 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_", "GITEA_UNIT_TESTS_"}, []string{"GITEA_"})
setting.StaticRootPath = "../../"
setting.Names = []string{"english"}
setting.Langs = []string{"en-US"}
Expand Down
31 changes: 31 additions & 0 deletionstests/env/filter.go
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionstests/env/filter_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff 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()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

entire codebase uses testify package, so let's stick to that.

}
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 deletionstests/test_utils.go
View file
Open in desktop
Original file line numberDiff line numberDiff 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_UNIT_TESTS_", "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

[8]ページ先頭

©2009-2025 Movatter.jp