diff --git a/tests/integration/api_repo_file_get_test.go b/tests/integration/api_repo_file_get_test.go index 408c630a1c..ab82e5cf15 100644 --- a/tests/integration/api_repo_file_get_test.go +++ b/tests/integration/api_repo_file_get_test.go @@ -17,36 +17,40 @@ import ( ) func TestAPIGetRawFileOrLFS(t *testing.T) { - defer tests.PrepareTestEnv(t)() - - // Test with raw file - req := NewRequest(t, "GET", "/api/v1/repos/user2/repo1/media/README.md") - resp := MakeRequest(t, req, http.StatusOK) - assert.Equal(t, "# repo1\n\nDescription for repo1", resp.Body.String()) - - // Test with LFS onGiteaRun(t, func(t *testing.T, u *url.URL) { - httpContext := NewAPITestContext(t, "user2", "repo-lfs-test", auth_model.AccessTokenScopeWriteRepository) - doAPICreateRepository(httpContext, nil, git.Sha1ObjectFormat, func(t *testing.T, repository api.Repository) { // FIXME: use forEachObjectFormat - u.Path = httpContext.GitPath() - dstPath := t.TempDir() + t.Run("Normal raw file", func(t *testing.T) { + defer tests.PrintCurrentTest(t)() - u.Path = httpContext.GitPath() - u.User = url.UserPassword("user2", userPassword) + req := NewRequest(t, "GET", "/api/v1/repos/user2/repo1/media/README.md") + resp := MakeRequest(t, req, http.StatusOK) + assert.Equal(t, "# repo1\n\nDescription for repo1", resp.Body.String()) + }) - t.Run("Clone", doGitClone(dstPath, u)) + t.Run("LFS raw file", func(t *testing.T) { + defer tests.PrintCurrentTest(t)() - dstPath2 := t.TempDir() + httpContext := NewAPITestContext(t, "user2", "repo-lfs-test", auth_model.AccessTokenScopeWriteRepository) + doAPICreateRepository(httpContext, nil, git.Sha1ObjectFormat, func(t *testing.T, repository api.Repository) { // FIXME: use forEachObjectFormat + u.Path = httpContext.GitPath() + dstPath := t.TempDir() - t.Run("Partial Clone", doPartialGitClone(dstPath2, u)) + u.Path = httpContext.GitPath() + u.User = url.UserPassword("user2", userPassword) - lfs, _ := lfsCommitAndPushTest(t, dstPath) + t.Run("Clone", doGitClone(dstPath, u)) - reqLFS := NewRequest(t, "GET", "/api/v1/repos/user2/repo1/media/"+lfs) - respLFS := MakeRequestNilResponseRecorder(t, reqLFS, http.StatusOK) - assert.Equal(t, littleSize, respLFS.Length) + dstPath2 := t.TempDir() - doAPIDeleteRepository(httpContext) + t.Run("Partial Clone", doPartialGitClone(dstPath2, u)) + + lfs, _ := lfsCommitAndPushTest(t, dstPath) + + reqLFS := NewRequest(t, "GET", "/api/v1/repos/user2/repo1/media/"+lfs) + respLFS := MakeRequestNilResponseRecorder(t, reqLFS, http.StatusOK) + assert.Equal(t, littleSize, respLFS.Length) + + doAPIDeleteRepository(httpContext) + }) }) }) } diff --git a/tests/integration/issue_test.go b/tests/integration/issue_test.go index 120fe6534e..34a6e53a37 100644 --- a/tests/integration/issue_test.go +++ b/tests/integration/issue_test.go @@ -1350,8 +1350,6 @@ body: func TestIssueUnsubscription(t *testing.T) { onGiteaRun(t, func(t *testing.T, u *url.URL) { - defer tests.PrepareTestEnv(t)() - user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}) repo, _, f := tests.CreateDeclarativeRepoWithOptions(t, user, tests.DeclarativeRepoOptions{ AutoInit: optional.Some(false), diff --git a/tests/integration/mirror_push_test.go b/tests/integration/mirror_push_test.go index 1377d1eb1b..fea552e3de 100644 --- a/tests/integration/mirror_push_test.go +++ b/tests/integration/mirror_push_test.go @@ -43,7 +43,6 @@ func TestMirrorPush(t *testing.T) { } func testMirrorPush(t *testing.T, u *url.URL) { - defer tests.PrepareTestEnv(t)() defer test.MockVariableValue(&setting.Migrations.AllowLocalNetworks, true)() require.NoError(t, migrations.Init()) diff --git a/tests/integration/pull_create_test.go b/tests/integration/pull_create_test.go index 43c9bef209..8c9bdcdfc6 100644 --- a/tests/integration/pull_create_test.go +++ b/tests/integration/pull_create_test.go @@ -287,8 +287,6 @@ func testDeleteRepository(t *testing.T, session *TestSession, ownerName, repoNam func TestPullBranchDelete(t *testing.T) { onGiteaRun(t, func(t *testing.T, u *url.URL) { - defer tests.PrepareTestEnv(t)() - session := loginUser(t, "user1") testRepoFork(t, session, "user2", "repo1", "user1", "repo1") testCreateBranch(t, session, "user1", "repo1", "branch/master", "master1", http.StatusSeeOther) diff --git a/tests/integration/pull_update_test.go b/tests/integration/pull_update_test.go index 975b21a239..692699d24f 100644 --- a/tests/integration/pull_update_test.go +++ b/tests/integration/pull_update_test.go @@ -89,7 +89,6 @@ func TestAPIPullUpdateByRebase(t *testing.T) { func TestAPIViewUpdateSettings(t *testing.T) { onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) { - defer tests.PrepareTestEnv(t)() // Create PR to test user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) org26 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 26}) @@ -136,7 +135,6 @@ func TestViewPullUpdateByRebase(t *testing.T) { func testViewPullUpdate(t *testing.T, updateStyle string) { defer test.MockVariableValue(&setting.Repository.PullRequest.DefaultUpdateStyle, updateStyle)() - defer tests.PrepareTestEnv(t)() // Create PR to test user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) org26 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 26}) diff --git a/tests/integration/repo_test.go b/tests/integration/repo_test.go index 7370b63dcd..598a508294 100644 --- a/tests/integration/repo_test.go +++ b/tests/integration/repo_test.go @@ -198,9 +198,9 @@ func TestViewRepoWithSymlinks(t *testing.T) { // TestViewAsRepoAdmin tests PR #2167 func TestViewAsRepoAdmin(t *testing.T) { - for _, user := range []string{"user2", "user4"} { - defer tests.PrepareTestEnv(t)() + defer tests.PrepareTestEnv(t)() + for _, user := range []string{"user2", "user4"} { session := loginUser(t, user) req := NewRequest(t, "GET", "/user2/repo1.git") diff --git a/tests/test_utils.go b/tests/test_utils.go index b53159ae2c..ee786624a2 100644 --- a/tests/test_utils.go +++ b/tests/test_utils.go @@ -13,6 +13,7 @@ import ( "path/filepath" "runtime" "strings" + "sync/atomic" "testing" "time" @@ -312,9 +313,26 @@ func PrepareCleanPackageData(t testing.TB) { require.NoError(t, storage.Clean(storage.Packages)) } +// inTestEnv keeps track if we are current inside a test environment, this is +// used to detect if testing code tries to prepare a test environment more than +// once. +var inTestEnv atomic.Bool + func PrepareTestEnv(t testing.TB, skip ...int) func() { t.Helper() - deferFn := PrintCurrentTest(t, util.OptionalArg(skip)+1) + + if !inTestEnv.CompareAndSwap(false, true) { + t.Fatal("Cannot prepare a test environment if you are already in a test environment. This is a bug in your testing code.") + } + + deferPrintCurrentTest := PrintCurrentTest(t, util.OptionalArg(skip)+1) + deferFn := func() { + deferPrintCurrentTest() + + if !inTestEnv.CompareAndSwap(true, false) { + t.Fatal("Tried to leave test environment, but we are no longer in a test environment. This should not happen.") + } + } cancelProcesses(t, 30*time.Second) t.Cleanup(func() { cancelProcesses(t, 0) }) // cancel remaining processes in a non-blocking way