From 099abf5db47dd5cf797d8898f6aaa06ccdf77c24 Mon Sep 17 00:00:00 2001 From: silverwind Date: Fri, 20 Nov 2026 02:58:39 +0000 Subject: [PATCH] test: reduce fixture setup work in Go tests (#40423) Reduce the work Go tests spend on fixture setup, without dropping any test or covered code. Every fixture repository reset listed and stat'ed both trees, and many packages copied repositories or created a database they never use. - List the fixture repository source tree once per process - Skip the repository copy in DB-only packages and the database in settings-only packages - Drop the unused repository copy and git init from migration tests - Push LFS files last in `TestGitGeneral`, so LFS hooks don't run on every later git command - Link the integration test binary without DWARF Measured locally on macOS, wall and CPU time: | | Before | After | |---|---|---| | Warm fixture repository reset | 127 ms | 63 ms | | 30 affected unit packages | 13.6 s, 72 s CPU | 10.0 s, 51 s CPU | | Migration tests | 29.9 s, 19.1 s CPU | 13.7 s, 8.8 s CPU | | `TestGitGeneral` | 81 s, 69 s CPU | 62 s, 54 s CPU | | 46 API integration tests | 41.7 s | 39.0 s | | Integration test binary relink | 8.7 s, 11.6 s CPU | 3.7 s, 5.7 s CPU | Assisted-by: Claude Code:claude-opus-5-5 --- Makefile | 4 +- cmd/cmdtest/cmd_test.go | 5 +- modelmigration/migrationtest/tests.go | 9 +-- modelmigration/v1_21/main_test.go | 14 ---- models/actions/main_test.go | 1 + models/activities/main_test.go | 2 +- models/asymkey/main_test.go | 1 + models/audit/main_test.go | 2 +- models/auth/main_test.go | 2 +- models/avatars/main_test.go | 2 +- models/db/main_test.go | 2 +- models/db/paginator/main_test.go | 14 ---- models/dbfs/main_test.go | 2 +- models/deploykey/main_test.go | 2 +- models/issues/main_test.go | 2 +- models/organization/main_test.go | 2 +- models/packages/package_test.go | 2 +- models/perm/access/main_test.go | 2 +- models/project/main_test.go | 1 + models/repo/main_test.go | 2 +- models/repostats/main_test.go | 2 +- models/secret/main_test.go | 2 +- models/system/main_test.go | 2 +- models/unittest/fscopy.go | 79 +++++++++++-------- models/unittest/fscopy_test.go | 36 +++++++++ models/unittest/testdb.go | 13 +-- models/user/main_test.go | 2 +- models/webhook/main_test.go | 1 + modules/fileicon/material_test.go | 7 +- modules/git/commit_info_test.go | 9 +++ routers/install/install_test.go | 5 +- routers/web/shared/project/project_test.go | 6 +- routers/web/user/setting/account_test.go | 2 +- routers/web/user/setting/main_test.go | 2 +- .../web/user/setting/security/main_test.go | 14 ---- services/mailer/sender/main_test.go | 6 +- tests/integration/git_general_test.go | 23 ++++-- 37 files changed, 154 insertions(+), 130 deletions(-) delete mode 100644 modelmigration/v1_21/main_test.go delete mode 100644 models/db/paginator/main_test.go create mode 100644 models/unittest/fscopy_test.go delete mode 100644 routers/web/user/setting/security/main_test.go diff --git a/Makefile b/Makefile index 38e51c2fb63..d1ad6d91ada 100644 --- a/Makefile +++ b/Makefile @@ -437,12 +437,12 @@ test-integration: $(EXECUTABLE) @# Use a compiled binary: testlogger forwards gitea logs to t.Log, so `go test -v` @# would flood output per passing test. testcache can't help these tests anyway — @# they mutate the work directory, so cache inputs change between runs. - $(GO) test $(GOTEST_FLAGS) -tags '$(TAGS)' -c gitea.dev/tests/integration -o ./test-integration-$(GITEA_TEST_DATABASE).test + $(GO) test -ldflags=-w $(GOTEST_FLAGS) -tags '$(TAGS)' -c gitea.dev/tests/integration -o ./test-integration-$(GITEA_TEST_DATABASE).test ./tools/test-integration.sh ./test-integration-$(GITEA_TEST_DATABASE).test .PHONY: test-integration-compile test-integration-compile: - $(GO) test $(GOTEST_FLAGS) -tags '$(TAGS)' -c -o /dev/null gitea.dev/tests/integration + $(GO) test -ldflags=-w $(GOTEST_FLAGS) -tags '$(TAGS)' -c -o /dev/null gitea.dev/tests/integration TEST_INTEGRATION_GOALS := $(filter test-integration$(HASH)%,$(MAKECMDGOALS)) .PHONY: $(TEST_INTEGRATION_GOALS) diff --git a/cmd/cmdtest/cmd_test.go b/cmd/cmdtest/cmd_test.go index 557eef2aa1e..6d5597e0513 100644 --- a/cmd/cmdtest/cmd_test.go +++ b/cmd/cmdtest/cmd_test.go @@ -11,12 +11,12 @@ import ( "errors" "fmt" "io" + "os" "path/filepath" "strings" "testing" "gitea.dev/cmd" - "gitea.dev/models/unittest" "gitea.dev/modules/setting" "gitea.dev/modules/test" "gitea.dev/modules/util" @@ -26,7 +26,8 @@ import ( ) func TestMain(m *testing.M) { - unittest.MainTest(m) + setting.SetupGiteaTestEnv() + os.Exit(m.Run()) } func makePathOutput(workPath, customPath, customConf string) string { diff --git a/modelmigration/migrationtest/tests.go b/modelmigration/migrationtest/tests.go index 086faf36d83..bf01f502ed0 100644 --- a/modelmigration/migrationtest/tests.go +++ b/modelmigration/migrationtest/tests.go @@ -11,7 +11,6 @@ import ( "gitea.dev/modelmigration/base" "gitea.dev/models/db" //nolint:depguard // allow to access db in migration tests "gitea.dev/models/unittest" //nolint:depguard // allow to access db in migration tests - "gitea.dev/modules/git" "gitea.dev/modules/setting" "gitea.dev/modules/testlogger" @@ -29,8 +28,6 @@ func PrepareTestEnv(t *testing.T, skip int, syncModels ...any) (base.EngineMigra ourSkip := 2 ourSkip += skip deferFn := testlogger.PrintCurrentTest(t, ourSkip) - giteaRoot := setting.GetGiteaTestSourceRoot() - require.NoError(t, unittest.SyncDirs(filepath.Join(giteaRoot, "tests/gitea-repositories-meta"), setting.RepoRootPath)) cleanup, err := unittest.ResetTestDatabase() if err != nil { @@ -69,7 +66,7 @@ func PrepareTestEnv(t *testing.T, skip int, syncModels ...any) (base.EngineMigra } } - fixturesDir := filepath.Join(giteaRoot, "modelmigration/fixtures", t.Name()) + fixturesDir := filepath.Join(setting.GetGiteaTestSourceRoot(), "modelmigration/fixtures", t.Name()) if _, err := os.Stat(fixturesDir); err == nil { t.Logf("initializing fixtures from: %s", fixturesDir) @@ -110,10 +107,6 @@ func mainTest(m *testing.M) int { return testlogger.MainErrorf("Unable to prepare integration test config: %v", err) } setting.SetupGiteaTestEnv() - - if err = git.InitFull(); err != nil { - return testlogger.MainErrorf("Unable to InitFull: %v", err) - } setting.Database.SlowQueryThreshold = 0 setting.LoadDBSetting() setting.InitLoggersForTest() diff --git a/modelmigration/v1_21/main_test.go b/modelmigration/v1_21/main_test.go deleted file mode 100644 index 7740826921e..00000000000 --- a/modelmigration/v1_21/main_test.go +++ /dev/null @@ -1,14 +0,0 @@ -// Copyright 2023 The Gitea Authors. All rights reserved. -// SPDX-License-Identifier: MIT - -package v1_21 - -import ( - "testing" - - "gitea.dev/modelmigration/migrationtest" -) - -func TestMain(m *testing.M) { - migrationtest.MainTest(m) -} diff --git a/models/actions/main_test.go b/models/actions/main_test.go index c3ae15d769b..5ca9702bf2f 100644 --- a/models/actions/main_test.go +++ b/models/actions/main_test.go @@ -17,5 +17,6 @@ func TestMain(m *testing.M) { "repository.yml", "user.yml", }, + SkipRepoFixtures: true, }) } diff --git a/models/activities/main_test.go b/models/activities/main_test.go index 5b64a455fef..8179140a5ca 100644 --- a/models/activities/main_test.go +++ b/models/activities/main_test.go @@ -13,5 +13,5 @@ import ( ) func TestMain(m *testing.M) { - unittest.MainTest(m) + unittest.MainTest(m, &unittest.TestOptions{SkipRepoFixtures: true}) } diff --git a/models/asymkey/main_test.go b/models/asymkey/main_test.go index 3c64834e274..cb0aae4fb5f 100644 --- a/models/asymkey/main_test.go +++ b/models/asymkey/main_test.go @@ -19,5 +19,6 @@ func TestMain(m *testing.M) { "user.yml", "email_address.yml", }, + SkipRepoFixtures: true, }) } diff --git a/models/audit/main_test.go b/models/audit/main_test.go index e628abeb51d..ac399138b2c 100644 --- a/models/audit/main_test.go +++ b/models/audit/main_test.go @@ -10,5 +10,5 @@ import ( ) func TestMain(m *testing.M) { - unittest.MainTest(m) + unittest.MainTest(m, &unittest.TestOptions{SkipRepoFixtures: true}) } diff --git a/models/auth/main_test.go b/models/auth/main_test.go index 0d9eddbdfe5..6b7c507d01d 100644 --- a/models/auth/main_test.go +++ b/models/auth/main_test.go @@ -16,5 +16,5 @@ import ( ) func TestMain(m *testing.M) { - unittest.MainTest(m) + unittest.MainTest(m, &unittest.TestOptions{SkipRepoFixtures: true}) } diff --git a/models/avatars/main_test.go b/models/avatars/main_test.go index ce48dada28a..dc8ca788d90 100644 --- a/models/avatars/main_test.go +++ b/models/avatars/main_test.go @@ -14,5 +14,5 @@ import ( ) func TestMain(m *testing.M) { - unittest.MainTest(m) + unittest.MainTest(m, &unittest.TestOptions{SkipRepoFixtures: true}) } diff --git a/models/db/main_test.go b/models/db/main_test.go index 81cd92a9f68..60f5e3eeb3f 100644 --- a/models/db/main_test.go +++ b/models/db/main_test.go @@ -13,5 +13,5 @@ import ( ) func TestMain(m *testing.M) { - unittest.MainTest(m) + unittest.MainTest(m, &unittest.TestOptions{SkipRepoFixtures: true}) } diff --git a/models/db/paginator/main_test.go b/models/db/paginator/main_test.go deleted file mode 100644 index 2f5ef392df3..00000000000 --- a/models/db/paginator/main_test.go +++ /dev/null @@ -1,14 +0,0 @@ -// Copyright 2021 The Gitea Authors. All rights reserved. -// SPDX-License-Identifier: MIT - -package paginator - -import ( - "testing" - - "gitea.dev/models/unittest" -) - -func TestMain(m *testing.M) { - unittest.MainTest(m) -} diff --git a/models/dbfs/main_test.go b/models/dbfs/main_test.go index 7c737a1b744..9ddb5c561a8 100644 --- a/models/dbfs/main_test.go +++ b/models/dbfs/main_test.go @@ -10,5 +10,5 @@ import ( ) func TestMain(m *testing.M) { - unittest.MainTest(m) + unittest.MainTest(m, &unittest.TestOptions{SkipRepoFixtures: true}) } diff --git a/models/deploykey/main_test.go b/models/deploykey/main_test.go index f4fe1a94703..26466274eb4 100644 --- a/models/deploykey/main_test.go +++ b/models/deploykey/main_test.go @@ -10,5 +10,5 @@ import ( ) func TestMain(m *testing.M) { - unittest.MainTest(m, &unittest.TestOptions{FixtureFiles: []string{}}) // the tests insert what they assert on + unittest.MainTest(m, &unittest.TestOptions{FixtureFiles: []string{}, SkipRepoFixtures: true}) // the tests insert what they assert on } diff --git a/models/issues/main_test.go b/models/issues/main_test.go index ad43ffbc85c..458264819a7 100644 --- a/models/issues/main_test.go +++ b/models/issues/main_test.go @@ -29,5 +29,5 @@ func TestFixturesAreConsistent(t *testing.T) { } func TestMain(m *testing.M) { - unittest.MainTest(m) + unittest.MainTest(m, &unittest.TestOptions{SkipRepoFixtures: true}) } diff --git a/models/organization/main_test.go b/models/organization/main_test.go index f06678cd03d..56e92385733 100644 --- a/models/organization/main_test.go +++ b/models/organization/main_test.go @@ -17,5 +17,5 @@ import ( ) func TestMain(m *testing.M) { - unittest.MainTest(m) + unittest.MainTest(m, &unittest.TestOptions{SkipRepoFixtures: true}) } diff --git a/models/packages/package_test.go b/models/packages/package_test.go index 46da84e4c05..6efa5f72f7b 100644 --- a/models/packages/package_test.go +++ b/models/packages/package_test.go @@ -18,7 +18,7 @@ import ( ) func TestMain(m *testing.M) { - unittest.MainTest(m) + unittest.MainTest(m, &unittest.TestOptions{SkipRepoFixtures: true}) } func TestHasOwnerPackages(t *testing.T) { diff --git a/models/perm/access/main_test.go b/models/perm/access/main_test.go index e4804124e8a..63161fd9a07 100644 --- a/models/perm/access/main_test.go +++ b/models/perm/access/main_test.go @@ -16,5 +16,5 @@ import ( ) func TestMain(m *testing.M) { - unittest.MainTest(m) + unittest.MainTest(m, &unittest.TestOptions{SkipRepoFixtures: true}) } diff --git a/models/project/main_test.go b/models/project/main_test.go index f52242c98af..a75f6698575 100644 --- a/models/project/main_test.go +++ b/models/project/main_test.go @@ -19,5 +19,6 @@ func TestMain(m *testing.M) { "project_issue.yml", "repository.yml", }, + SkipRepoFixtures: true, }) } diff --git a/models/repo/main_test.go b/models/repo/main_test.go index 277d42343b7..cc114469fa7 100644 --- a/models/repo/main_test.go +++ b/models/repo/main_test.go @@ -17,5 +17,5 @@ import ( ) func TestMain(m *testing.M) { - unittest.MainTest(m) + unittest.MainTest(m, &unittest.TestOptions{SkipRepoFixtures: true}) } diff --git a/models/repostats/main_test.go b/models/repostats/main_test.go index 064d5f4ddf0..23303c0a89f 100644 --- a/models/repostats/main_test.go +++ b/models/repostats/main_test.go @@ -29,5 +29,5 @@ func TestFixturesAreConsistent(t *testing.T) { } func TestMain(m *testing.M) { - unittest.MainTest(m) + unittest.MainTest(m, &unittest.TestOptions{SkipRepoFixtures: true}) } diff --git a/models/secret/main_test.go b/models/secret/main_test.go index bd74c3f5c99..ea9e655b3d9 100644 --- a/models/secret/main_test.go +++ b/models/secret/main_test.go @@ -10,5 +10,5 @@ import ( ) func TestMain(m *testing.M) { - unittest.MainTest(m) + unittest.MainTest(m, &unittest.TestOptions{SkipRepoFixtures: true}) } diff --git a/models/system/main_test.go b/models/system/main_test.go index f52b1e9f045..bcf4b14d3a5 100644 --- a/models/system/main_test.go +++ b/models/system/main_test.go @@ -15,5 +15,5 @@ import ( ) func TestMain(m *testing.M) { - unittest.MainTest(m) + unittest.MainTest(m, &unittest.TestOptions{SkipRepoFixtures: true}) } diff --git a/models/unittest/fscopy.go b/models/unittest/fscopy.go index 7775e75f616..9021ead4b63 100644 --- a/models/unittest/fscopy.go +++ b/models/unittest/fscopy.go @@ -8,37 +8,56 @@ import ( "os" "path/filepath" "strings" + "sync" "gitea.dev/modules/setting" "gitea.dev/modules/util" ) -// SyncFile synchronizes the two files. This is skipped if both files -// exist and the size, modtime, and mode match. -func SyncFile(srcPath, destPath string) error { - dest, err := os.Stat(destPath) +type syncSource struct { + entries []string // util.ListDirRecursively appends a slash to the directory names + infos map[string]os.FileInfo +} + +var ( + syncSourcesMu sync.Mutex + syncSources = map[string]*syncSource{} +) + +func loadSyncSource(srcPath string) (*syncSource, error) { + syncSourcesMu.Lock() + defer syncSourcesMu.Unlock() + if src, ok := syncSources[srcPath]; ok { + return src, nil + } + entries, err := util.ListDirRecursively(srcPath, &util.ListDirOptions{IncludeDir: true}) if err != nil { - if os.IsNotExist(err) { - return util.CopyFile(srcPath, destPath) + return nil, err + } + src := &syncSource{entries: entries, infos: make(map[string]os.FileInfo, len(entries))} + for _, entry := range entries { + if src.infos[entry], err = os.Stat(filepath.Join(srcPath, entry)); err != nil { + return nil, err } + } + syncSources[srcPath] = src + return src, nil +} + +// syncFile copies the source file unless the destination has the same size, modtime and mode +func syncFile(srcInfo os.FileInfo, srcPath, destPath string) error { + dest, err := os.Stat(destPath) + if err != nil && !os.IsNotExist(err) { return err } - - src, err := os.Stat(srcPath) - if err != nil { - return err - } - - if src.Size() == dest.Size() && - src.ModTime().Equal(dest.ModTime()) && - src.Mode() == dest.Mode() { + if err == nil && srcInfo.Size() == dest.Size() && srcInfo.ModTime().Equal(dest.ModTime()) && srcInfo.Mode() == dest.Mode() { return nil } - return util.CopyFile(srcPath, destPath) } // SyncDirs synchronizes files recursively from source to target directory. +// The source directory is only listed once per process, it must not change. // It returns error when error occurs in underlying functions. func SyncDirs(srcPath, destPath string) error { destPath = filepath.Clean(destPath) @@ -54,6 +73,11 @@ func SyncDirs(srcPath, destPath string) error { return errors.New("destination path should not be inside Gitea data directory, otherwise your data for dev mode will be removed") } + src, err := loadSyncSource(srcPath) + if err != nil { + return err + } + err = os.MkdirAll(destPath, os.ModePerm) if err != nil { return err @@ -68,35 +92,20 @@ func SyncDirs(srcPath, destPath string) error { return err } for _, destFile := range destFiles { - destFilePath := filepath.Join(destPath, destFile) - shouldRemove := filepath.Base(destFilePath) == keepFile - if _, err = os.Stat(filepath.Join(srcPath, destFile)); err != nil { - if os.IsNotExist(err) { - shouldRemove = true - } else { - return err - } - } - // if src file does not exist, remove dest file - if shouldRemove { - if err = os.RemoveAll(destFilePath); err != nil { + if _, ok := src.infos[destFile]; !ok || filepath.Base(destFile) == keepFile { + if err = os.RemoveAll(filepath.Join(destPath, destFile)); err != nil { return err } } } // sync src files to dest - srcFiles, err := util.ListDirRecursively(srcPath, &util.ListDirOptions{IncludeDir: true}) - if err != nil { - return err - } - for _, srcFile := range srcFiles { + for _, srcFile := range src.entries { destFilePath := filepath.Join(destPath, srcFile) - // util.ListDirRecursively appends a slash to the directory name if strings.HasSuffix(srcFile, "/") { err = os.MkdirAll(destFilePath, os.ModePerm) } else if filepath.Base(destFilePath) != keepFile { - err = SyncFile(filepath.Join(srcPath, srcFile), destFilePath) + err = syncFile(src.infos[srcFile], filepath.Join(srcPath, srcFile), destFilePath) } if err != nil { return err diff --git a/models/unittest/fscopy_test.go b/models/unittest/fscopy_test.go new file mode 100644 index 00000000000..98883bcad07 --- /dev/null +++ b/models/unittest/fscopy_test.go @@ -0,0 +1,36 @@ +// Copyright 2026 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package unittest_test + +import ( + "os" + "path/filepath" + "testing" + + "gitea.dev/models/unittest" + "gitea.dev/modules/util" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestSyncDirsRestoresDestination(t *testing.T) { + srcPath, destPath := t.TempDir(), t.TempDir() + require.NoError(t, os.MkdirAll(filepath.Join(srcPath, "dir/sub"), 0o755)) + require.NoError(t, os.WriteFile(filepath.Join(srcPath, "dir/file"), []byte("content"), 0o644)) + require.NoError(t, os.WriteFile(filepath.Join(srcPath, "dir/.keep"), nil, 0o644)) + require.NoError(t, unittest.SyncDirs(srcPath, destPath)) + + require.NoError(t, os.WriteFile(filepath.Join(destPath, "dir/file"), []byte("modified content"), 0o644)) + require.NoError(t, os.WriteFile(filepath.Join(destPath, "extra"), nil, 0o644)) + require.NoError(t, os.RemoveAll(filepath.Join(destPath, "dir/sub"))) + require.NoError(t, unittest.SyncDirs(srcPath, destPath)) + + entries, err := util.ListDirRecursively(destPath, &util.ListDirOptions{IncludeDir: true}) + require.NoError(t, err) + assert.Equal(t, []string{"dir/", "dir/file", "dir/sub/"}, entries) + content, err := os.ReadFile(filepath.Join(destPath, "dir/file")) + require.NoError(t, err) + assert.Equal(t, "content", string(content)) +} diff --git a/models/unittest/testdb.go b/models/unittest/testdb.go index c995999aa2b..a3551c77ae4 100644 --- a/models/unittest/testdb.go +++ b/models/unittest/testdb.go @@ -31,9 +31,10 @@ import ( // TestOptions represents test options type TestOptions struct { - FixtureFiles []string - SetUp func() error // SetUp will be executed before all tests in this package - TearDown func() error // TearDown will be executed after all tests in this package + FixtureFiles []string + SkipRepoFixtures bool // for packages that never read the fixture git repositories + SetUp func() error // SetUp will be executed before all tests in this package + TearDown func() error // TearDown will be executed after all tests in this package } // MainTest a reusable TestMain(..) function for unit tests that need to use a @@ -79,8 +80,10 @@ func mainTest(m *testing.M, testOptsArg ...*TestOptions) int { if err = storage.Init(); err != nil { return testlogger.MainErrorf("storage.Init: %v", err) } - if err = SyncDirs(filepath.Join(giteaRoot, "tests", "gitea-repositories-meta"), setting.RepoRootPath); err != nil { - return testlogger.MainErrorf("util.SyncDirs: %v", err) + if !testOpts.SkipRepoFixtures { + if err = SyncDirs(filepath.Join(giteaRoot, "tests", "gitea-repositories-meta"), setting.RepoRootPath); err != nil { + return testlogger.MainErrorf("util.SyncDirs: %v", err) + } } if err = git.InitFull(); err != nil { diff --git a/models/user/main_test.go b/models/user/main_test.go index 1a8cd4af594..5fcbaa32992 100644 --- a/models/user/main_test.go +++ b/models/user/main_test.go @@ -15,5 +15,5 @@ import ( ) func TestMain(m *testing.M) { - unittest.MainTest(m) + unittest.MainTest(m, &unittest.TestOptions{SkipRepoFixtures: true}) } diff --git a/models/webhook/main_test.go b/models/webhook/main_test.go index aa5286bd65f..876e3316d6b 100644 --- a/models/webhook/main_test.go +++ b/models/webhook/main_test.go @@ -15,5 +15,6 @@ func TestMain(m *testing.M) { "webhook.yml", "hook_task.yml", }, + SkipRepoFixtures: true, }) } diff --git a/modules/fileicon/material_test.go b/modules/fileicon/material_test.go index f6e08ba4c1b..2a222449b2e 100644 --- a/modules/fileicon/material_test.go +++ b/modules/fileicon/material_test.go @@ -4,21 +4,22 @@ package fileicon_test import ( + "os" "testing" - "gitea.dev/models/unittest" "gitea.dev/modules/fileicon" "gitea.dev/modules/git" + "gitea.dev/modules/setting" "github.com/stretchr/testify/assert" ) func TestMain(m *testing.M) { - unittest.MainTest(m, &unittest.TestOptions{FixtureFiles: []string{}}) + setting.SetupGiteaTestEnv() + os.Exit(m.Run()) } func TestFindIconName(t *testing.T) { - unittest.PrepareTestEnv(t) p := fileicon.DefaultMaterialIconProvider() assert.Equal(t, "php", p.FindIconName(&fileicon.EntryInfo{BaseName: "foo.php", EntryMode: git.EntryModeBlob})) assert.Equal(t, "php", p.FindIconName(&fileicon.EntryInfo{BaseName: "foo.PHP", EntryMode: git.EntryModeBlob})) diff --git a/modules/git/commit_info_test.go b/modules/git/commit_info_test.go index c3e19935e15..9927052a762 100644 --- a/modules/git/commit_info_test.go +++ b/modules/git/commit_info_test.go @@ -166,6 +166,15 @@ func TestEntries_GetCommitsInfo(t *testing.T) { // since there is no refURL, it means that the submodule info doesn't exist, so it won't have a web link assert.Nil(t, cisf.SubmoduleWebLinkTree(t.Context())) }) + + t.Run("SubsetOfTreePathEntries", func(t *testing.T) { + commit, err := bareRepo1.GetCommit(t.Context(), "37991dec2c8e592043f47155ce4808d4580f9123") + require.NoError(t, err) + revs, err := GetLastCommitForPaths(t.Context(), bareRepo1, commit, "foo", []string{"bar"}) + require.NoError(t, err) + assert.Equal(t, "8006ff9adbf0cb94da7dad9e537e53817f9fa5c0", revs["bar"].ID.String()) + assert.NotContains(t, revs, "link_short") + }) } func TestEntries_GetCommitsInfo_ContextErr(t *testing.T) { diff --git a/routers/install/install_test.go b/routers/install/install_test.go index 534b1156019..53fd76e96c9 100644 --- a/routers/install/install_test.go +++ b/routers/install/install_test.go @@ -6,9 +6,9 @@ package install import ( "net/http" "net/http/httptest" + "os" "testing" - "gitea.dev/models/unittest" "gitea.dev/modules/setting" "gitea.dev/services/contexttest" "gitea.dev/services/forms" @@ -38,7 +38,8 @@ func TestRoutes(t *testing.T) { } func TestMain(m *testing.M) { - unittest.MainTest(m) + setting.SetupGiteaTestEnv() + os.Exit(m.Run()) } func TestFillInstallConfig(t *testing.T) { diff --git a/routers/web/shared/project/project_test.go b/routers/web/shared/project/project_test.go index 5035810c18f..adb40f2019a 100644 --- a/routers/web/shared/project/project_test.go +++ b/routers/web/shared/project/project_test.go @@ -23,14 +23,14 @@ import ( ) func TestMain(m *testing.M) { - unittest.MainTest(m) + unittest.MainTest(m, &unittest.TestOptions{SkipRepoFixtures: true}) } // TestFindColumn covers the scoping rule every board handler depends on: a request only // resolves projects owned by the scope its route assigned, so an ID belonging to anyone // else reads as not found rather than leaking across owners. func TestFindColumn(t *testing.T) { - unittest.PrepareTestEnv(t) + assert.NoError(t, unittest.PrepareTestDatabase()) for _, tc := range []struct { name string projectID string @@ -69,7 +69,7 @@ func TestFindColumn(t *testing.T) { } func TestMoveIssuesNeedsOnlyProjectWrite(t *testing.T) { - unittest.PrepareTestEnv(t) + assert.NoError(t, unittest.PrepareTestDatabase()) require.NoError(t, repo_model.UpdateRepositoryColsNoAutoTime(t.Context(), &repo_model.Repository{ID: 3, IsArchived: true}, "is_archived")) ctx, _ := contexttest.MockContext(t, "POST /org3/-/projects") contexttest.LoadUser(t, ctx, 15) diff --git a/routers/web/user/setting/account_test.go b/routers/web/user/setting/account_test.go index caf358f2c0c..2605fc1e99e 100644 --- a/routers/web/user/setting/account_test.go +++ b/routers/web/user/setting/account_test.go @@ -81,7 +81,7 @@ func TestChangePassword(t *testing.T) { }, } { t.Run(req.OldPassword+"__"+req.NewPassword, func(t *testing.T) { - unittest.PrepareTestEnv(t) + assert.NoError(t, unittest.PrepareTestDatabase()) setting.PasswordComplexity = req.PasswordComplexity ctx, _ := contexttest.MockContext(t, "user/settings/security") contexttest.LoadUser(t, ctx, 2) diff --git a/routers/web/user/setting/main_test.go b/routers/web/user/setting/main_test.go index 5de9b48f3ec..72bef63a2ba 100644 --- a/routers/web/user/setting/main_test.go +++ b/routers/web/user/setting/main_test.go @@ -10,5 +10,5 @@ import ( ) func TestMain(m *testing.M) { - unittest.MainTest(m) + unittest.MainTest(m, &unittest.TestOptions{SkipRepoFixtures: true}) } diff --git a/routers/web/user/setting/security/main_test.go b/routers/web/user/setting/security/main_test.go deleted file mode 100644 index 0fc03c56353..00000000000 --- a/routers/web/user/setting/security/main_test.go +++ /dev/null @@ -1,14 +0,0 @@ -// Copyright 2024 The Gitea Authors. All rights reserved. -// SPDX-License-Identifier: MIT - -package security - -import ( - "testing" - - "gitea.dev/models/unittest" -) - -func TestMain(m *testing.M) { - unittest.MainTest(m) -} diff --git a/services/mailer/sender/main_test.go b/services/mailer/sender/main_test.go index bd3d5f70001..d4ff3b30ae8 100644 --- a/services/mailer/sender/main_test.go +++ b/services/mailer/sender/main_test.go @@ -4,11 +4,13 @@ package sender import ( + "os" "testing" - "gitea.dev/models/unittest" + "gitea.dev/modules/setting" ) func TestMain(m *testing.M) { - unittest.MainTest(m) + setting.SetupGiteaTestEnv() + os.Exit(m.Run()) } diff --git a/tests/integration/git_general_test.go b/tests/integration/git_general_test.go index 93959b6c7ec..518bb62e027 100644 --- a/tests/integration/git_general_test.go +++ b/tests/integration/git_general_test.go @@ -81,16 +81,20 @@ func testGitGeneral(t *testing.T, u *url.URL) { t.Run("Partial Clone", doPartialGitClone(dstPath2, u)) - pushedFilesStandard := standardCommitAndPushTest(t, dstPath, testFileSizeSmall, testFileSizeLarge) - pushedFilesLFS := lfsCommitAndPushTest(t, dstPath, testFileSizeSmall, testFileSizeLarge) - rawTest(t, &httpContext, pushedFilesStandard[0], pushedFilesStandard[1], pushedFilesLFS[0], pushedFilesLFS[1]) - mediaTest(t, &httpContext, pushedFilesStandard[0], pushedFilesStandard[1], pushedFilesLFS[0], pushedFilesLFS[1]) - + // LFS pushes run after the branch tests, a clone with LFS installed runs its hooks on every later git command t.Run("CreateAgitFlowPull", doCreateAgitFlowPull(dstPath, &httpContext, "test/head")) t.Run("CreateProtectedBranch", doCreateProtectedBranch(&httpContext, dstPath)) t.Run("BranchProtectMerge", doBranchProtectPRMerge(&httpContext, dstPath)) t.Run("AutoMerge", doAutoPRMerge(&httpContext, dstPath)) t.Run("CreatePRAndSetManuallyMerged", doCreatePRAndSetManuallyMerged(httpContext, httpContext, dstPath, "master", "test-manually-merge")) + + t.Run("CheckoutMaster", doGitCheckoutBranch(dstPath, "master")) + t.Run("PullMaster", doGitPull(dstPath, "--ff-only", "origin", "master")) + pushedFilesStandard := standardCommitAndPushTest(t, dstPath, testFileSizeSmall, testFileSizeLarge) + pushedFilesLFS := lfsCommitAndPushTest(t, dstPath, testFileSizeSmall, testFileSizeLarge) + rawTest(t, &httpContext, pushedFilesStandard[0], pushedFilesStandard[1], pushedFilesLFS[0], pushedFilesLFS[1]) + mediaTest(t, &httpContext, pushedFilesStandard[0], pushedFilesStandard[1], pushedFilesLFS[0], pushedFilesLFS[1]) + t.Run("MergeFork", func(t *testing.T) { defer tests.PrintCurrentTest(t)() t.Run("CreatePRAndMerge", doMergeFork(httpContext, forkedUserCtx, "master", httpContext.Username+":master")) @@ -128,14 +132,17 @@ func testGitGeneral(t *testing.T, u *url.URL) { t.Run("Clone", doGitClone(dstPath, sshURL)) + t.Run("CreateAgitFlowPull", doCreateAgitFlowPull(dstPath, &sshContext, "test/head2")) + t.Run("CreateProtectedBranch", doCreateProtectedBranch(&sshContext, dstPath)) + t.Run("BranchProtectMerge", doBranchProtectPRMerge(&sshContext, dstPath)) + + t.Run("CheckoutMaster", doGitCheckoutBranch(dstPath, "master")) + t.Run("PullMaster", doGitPull(dstPath, "--ff-only", "origin", "master")) pushedFilesStandard := standardCommitAndPushTest(t, dstPath, testFileSizeSmall, testFileSizeLarge) pushedFilesLFS := lfsCommitAndPushTest(t, dstPath, testFileSizeSmall, testFileSizeLarge) rawTest(t, &sshContext, pushedFilesStandard[0], pushedFilesStandard[1], pushedFilesLFS[0], pushedFilesLFS[1]) mediaTest(t, &sshContext, pushedFilesStandard[0], pushedFilesStandard[1], pushedFilesLFS[0], pushedFilesLFS[1]) - t.Run("CreateAgitFlowPull", doCreateAgitFlowPull(dstPath, &sshContext, "test/head2")) - t.Run("CreateProtectedBranch", doCreateProtectedBranch(&sshContext, dstPath)) - t.Run("BranchProtectMerge", doBranchProtectPRMerge(&sshContext, dstPath)) t.Run("MergeFork", func(t *testing.T) { defer tests.PrintCurrentTest(t)() t.Run("CreatePRAndMerge", doMergeFork(sshContext, forkedUserCtx, "master", sshContext.Username+":master"))