Template
1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo synced 2024-11-21 17:34:24 +01:00

Merge pull request 'chore: improve preparing tests' (#5948) from gusted/improve-testz into forgejo

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/5948
Reviewed-by: Otto <otto@codeberg.org>
Reviewed-by: Michael Kriese <michael.kriese@gmx.de>
This commit is contained in:
Michael Kriese 2024-11-15 07:35:22 +00:00
commit 296935b0d7
5 changed files with 79 additions and 47 deletions

View file

@ -9,6 +9,7 @@ import (
"encoding/base64"
"fmt"
"net/http"
"net/url"
"testing"
auth_model "code.gitea.io/gitea/models/auth"
@ -206,11 +207,11 @@ func TestAPIListWikiPages(t *testing.T) {
}
func TestAPINewWikiPage(t *testing.T) {
defer tests.PrepareTestEnv(t)()
for _, title := range []string{
"New page",
"&&&&",
} {
defer tests.PrepareTestEnv(t)()
username := "user2"
session := loginUser(t, username)
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository)
@ -386,26 +387,26 @@ func TestAPIListPageRevisions(t *testing.T) {
}
func TestAPIWikiNonMasterBranch(t *testing.T) {
defer tests.PrepareTestEnv(t)()
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
repo, _, f := tests.CreateDeclarativeRepoWithOptions(t, user, tests.DeclarativeRepoOptions{
WikiBranch: optional.Some("main"),
})
defer f()
uris := []string{
"revisions/Home",
"pages",
"page/Home",
}
baseURL := fmt.Sprintf("/api/v1/repos/%s/wiki", repo.FullName())
for _, uri := range uris {
t.Run(uri, func(t *testing.T) {
defer tests.PrintCurrentTest(t)()
req := NewRequestf(t, "GET", "%s/%s", baseURL, uri)
MakeRequest(t, req, http.StatusOK)
onGiteaRun(t, func(t *testing.T, _ *url.URL) {
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
repo, _, f := tests.CreateDeclarativeRepoWithOptions(t, user, tests.DeclarativeRepoOptions{
WikiBranch: optional.Some("main"),
})
}
defer f()
uris := []string{
"revisions/Home",
"pages",
"page/Home",
}
baseURL := fmt.Sprintf("/api/v1/repos/%s/wiki", repo.FullName())
for _, uri := range uris {
t.Run(uri, func(t *testing.T) {
defer tests.PrintCurrentTest(t)()
req := NewRequestf(t, "GET", "%s/%s", baseURL, uri)
MakeRequest(t, req, http.StatusOK)
})
}
})
}

View file

@ -6,9 +6,12 @@ package integration
import (
"net/http"
"testing"
"code.gitea.io/gitea/tests"
)
func TestEasyMDESwitch(t *testing.T) {
defer tests.PrepareTestEnv(t)()
session := loginUser(t, "user2")
testEasyMDESwitch(t, session, "user2/glob/issues/1", false)
testEasyMDESwitch(t, session, "user2/glob/issues/new", false)

View file

@ -13,7 +13,6 @@ import (
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/tests"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
@ -33,8 +32,6 @@ func assertFileEqual(t *testing.T, p string, content []byte) {
func TestRepoCloneWiki(t *testing.T) {
onGiteaRun(t, func(t *testing.T, u *url.URL) {
defer tests.PrepareTestEnv(t)()
dstPath := t.TempDir()
r := fmt.Sprintf("%suser2/repo1.wiki.git", u.String())

View file

@ -12,6 +12,7 @@ import (
"os"
"path"
"path/filepath"
"regexp"
"strconv"
"testing"
"time"
@ -59,6 +60,8 @@ func createSSHUrl(gitPath string, u *url.URL) *url.URL {
return &u2
}
var rootPathRe = regexp.MustCompile("\\[repository\\]\nROOT\\s=\\s.*")
func onGiteaRun[T testing.TB](t T, callback func(T, *url.URL)) {
defer tests.PrepareTestEnv(t, 1)()
s := http.Server{
@ -77,7 +80,13 @@ func onGiteaRun[T testing.TB](t T, callback func(T, *url.URL)) {
require.NoError(t, err)
u.Host = listener.Addr().String()
// Override repository root in config.
conf, err := os.ReadFile(setting.CustomConf)
require.NoError(t, err)
require.NoError(t, os.WriteFile(setting.CustomConf, rootPathRe.ReplaceAll(conf, []byte("[repository]\nROOT = "+setting.RepoRootPath)), os.ModePerm))
defer func() {
require.NoError(t, os.WriteFile(setting.CustomConf, conf, os.ModePerm))
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
s.Shutdown(ctx)
cancel()

View file

@ -48,6 +48,8 @@ func exitf(format string, args ...any) {
os.Exit(1)
}
var preparedDir string
func InitTest(requireGitea bool) {
log.RegisterEventWriter("test", testlogger.NewTestLoggerWriter)
@ -180,6 +182,44 @@ func InitTest(requireGitea bool) {
setting.Database.Path = ":memory:"
}
setting.Repository.Local.LocalCopyPath = os.TempDir()
dir, err := os.MkdirTemp("", "prepared-forgejo")
if err != nil {
log.Fatal("os.MkdirTemp: %v", err)
}
preparedDir = dir
setting.Repository.Local.LocalCopyPath, err = os.MkdirTemp("", "local-upload")
if err != nil {
log.Fatal("os.MkdirTemp: %v", err)
}
if err := unittest.CopyDir(path.Join(filepath.Dir(setting.AppPath), "tests/gitea-repositories-meta"), dir); err != nil {
log.Fatal("os.RemoveAll: %v", err)
}
ownerDirs, err := os.ReadDir(dir)
if err != nil {
log.Fatal("os.ReadDir: %v", err)
}
fmt.Println(ownerDirs)
for _, ownerDir := range ownerDirs {
if !ownerDir.Type().IsDir() {
continue
}
repoDirs, err := os.ReadDir(filepath.Join(dir, ownerDir.Name()))
if err != nil {
log.Fatal("os.ReadDir: %v", err)
}
for _, repoDir := range repoDirs {
_ = os.MkdirAll(filepath.Join(dir, ownerDir.Name(), repoDir.Name(), "objects", "pack"), 0o755)
_ = os.MkdirAll(filepath.Join(dir, ownerDir.Name(), repoDir.Name(), "objects", "info"), 0o755)
_ = os.MkdirAll(filepath.Join(dir, ownerDir.Name(), repoDir.Name(), "refs", "heads"), 0o755)
_ = os.MkdirAll(filepath.Join(dir, ownerDir.Name(), repoDir.Name(), "refs", "tag"), 0o755)
_ = os.MkdirAll(filepath.Join(dir, ownerDir.Name(), repoDir.Name(), "refs", "pull"), 0o755)
}
}
routers.InitWebInstalled(graceful.GetManager().HammerContext())
}
@ -228,28 +268,10 @@ func cancelProcesses(t testing.TB, delay time.Duration) {
}
func PrepareGitRepoDirectory(t testing.TB) {
require.NoError(t, util.RemoveAll(setting.RepoRootPath))
require.NoError(t, unittest.CopyDir(path.Join(filepath.Dir(setting.AppPath), "tests/gitea-repositories-meta"), setting.RepoRootPath))
ownerDirs, err := os.ReadDir(setting.RepoRootPath)
if err != nil {
require.NoError(t, err, "unable to read the new repo root: %v\n", err)
}
for _, ownerDir := range ownerDirs {
if !ownerDir.Type().IsDir() {
continue
}
repoDirs, err := os.ReadDir(filepath.Join(setting.RepoRootPath, ownerDir.Name()))
if err != nil {
require.NoError(t, err, "unable to read the new repo root: %v\n", err)
}
for _, repoDir := range repoDirs {
_ = os.MkdirAll(filepath.Join(setting.RepoRootPath, ownerDir.Name(), repoDir.Name(), "objects", "pack"), 0o755)
_ = os.MkdirAll(filepath.Join(setting.RepoRootPath, ownerDir.Name(), repoDir.Name(), "objects", "info"), 0o755)
_ = os.MkdirAll(filepath.Join(setting.RepoRootPath, ownerDir.Name(), repoDir.Name(), "refs", "heads"), 0o755)
_ = os.MkdirAll(filepath.Join(setting.RepoRootPath, ownerDir.Name(), repoDir.Name(), "refs", "tag"), 0o755)
_ = os.MkdirAll(filepath.Join(setting.RepoRootPath, ownerDir.Name(), repoDir.Name(), "refs", "pull"), 0o755)
}
}
var err error
setting.RepoRootPath, err = os.MkdirTemp(t.TempDir(), "forgejo-repo-rooth")
require.NoError(t, err)
require.NoError(t, unittest.CopyDir(preparedDir, setting.RepoRootPath))
}
func PrepareArtifactsStorage(t testing.TB) {