1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo.git synced 2024-11-21 08:31:27 -05:00

[TESTS] Fix usage of LoadRepoCommit

It loads the Commit with a temporary open GitRepo. This is incorrect,
the GitRepo should be open as long as the Commit can be used. This
mainly removes the usage of this function as it's not needed.
This commit is contained in:
Gusted 2024-08-26 08:03:48 +02:00
parent 24bbf051c3
commit b44dcf553c
No known key found for this signature in database
GPG key ID: FD821B732837125F
9 changed files with 84 additions and 190 deletions

View file

@ -19,9 +19,11 @@ func TestTestHook(t *testing.T) {
ctx, _ := contexttest.MockAPIContext(t, "user2/repo1/wiki/_pages") ctx, _ := contexttest.MockAPIContext(t, "user2/repo1/wiki/_pages")
ctx.SetParams(":id", "1") ctx.SetParams(":id", "1")
contexttest.LoadRepo(t, ctx, 1)
contexttest.LoadRepoCommit(t, ctx)
contexttest.LoadUser(t, ctx, 2) contexttest.LoadUser(t, ctx, 2)
contexttest.LoadRepo(t, ctx, 1)
contexttest.LoadGitRepo(t, ctx)
defer ctx.Repo.GitRepo.Close()
contexttest.LoadRepoCommit(t, ctx)
TestHook(ctx) TestHook(ctx)
assert.EqualValues(t, http.StatusNoContent, ctx.Resp.Status()) assert.EqualValues(t, http.StatusNoContent, ctx.Resp.Status())

View file

@ -6,6 +6,7 @@ package repo
import ( import (
"testing" "testing"
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/models/unittest"
"code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/gitrepo"
@ -45,7 +46,6 @@ func TestGetUniquePatchBranchName(t *testing.T) {
ctx, _ := contexttest.MockContext(t, "user2/repo1") ctx, _ := contexttest.MockContext(t, "user2/repo1")
ctx.SetParams(":id", "1") ctx.SetParams(":id", "1")
contexttest.LoadRepo(t, ctx, 1) contexttest.LoadRepo(t, ctx, 1)
contexttest.LoadRepoCommit(t, ctx)
contexttest.LoadUser(t, ctx, 2) contexttest.LoadUser(t, ctx, 2)
contexttest.LoadGitRepo(t, ctx) contexttest.LoadGitRepo(t, ctx)
defer ctx.Repo.GitRepo.Close() defer ctx.Repo.GitRepo.Close()
@ -57,15 +57,7 @@ func TestGetUniquePatchBranchName(t *testing.T) {
func TestGetClosestParentWithFiles(t *testing.T) { func TestGetClosestParentWithFiles(t *testing.T) {
unittest.PrepareTestEnv(t) unittest.PrepareTestEnv(t)
ctx, _ := contexttest.MockContext(t, "user2/repo1") repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
ctx.SetParams(":id", "1")
contexttest.LoadRepo(t, ctx, 1)
contexttest.LoadRepoCommit(t, ctx)
contexttest.LoadUser(t, ctx, 2)
contexttest.LoadGitRepo(t, ctx)
defer ctx.Repo.GitRepo.Close()
repo := ctx.Repo.Repository
branch := repo.DefaultBranch branch := repo.DefaultBranch
gitRepo, _ := gitrepo.OpenRepository(git.DefaultContext, repo) gitRepo, _ := gitrepo.OpenRepository(git.DefaultContext, repo)
defer gitRepo.Close() defer gitRepo.Close()

View file

@ -136,14 +136,15 @@ func LoadRepoCommit(t *testing.T, ctx gocontext.Context) {
assert.FailNow(t, "context is not *context.Context or *context.APIContext") assert.FailNow(t, "context is not *context.Context or *context.APIContext")
} }
gitRepo, err := gitrepo.OpenRepository(ctx, repo.Repository) if repo.GitRepo == nil {
require.NoError(t, err) assert.FailNow(t, "must call LoadGitRepo")
defer gitRepo.Close() }
branch, err := gitRepo.GetHEADBranch()
branch, err := repo.GitRepo.GetHEADBranch()
require.NoError(t, err) require.NoError(t, err)
assert.NotNil(t, branch) assert.NotNil(t, branch)
if branch != nil { if branch != nil {
repo.Commit, err = gitRepo.GetBranchCommit(branch.Name) repo.Commit, err = repo.GitRepo.GetBranchCommit(branch.Name)
require.NoError(t, err) require.NoError(t, err)
} }
} }
@ -176,10 +177,20 @@ func LoadOrganization(t *testing.T, ctx gocontext.Context, orgID int64) {
// LoadGitRepo load a git repo into a test context. Requires that ctx.Repo has // LoadGitRepo load a git repo into a test context. Requires that ctx.Repo has
// already been populated. // already been populated.
func LoadGitRepo(t *testing.T, ctx *context.Context) { func LoadGitRepo(t *testing.T, ctx gocontext.Context) {
require.NoError(t, ctx.Repo.Repository.LoadOwner(ctx)) var repo *context.Repository
switch ctx := ctx.(type) {
case *context.Context:
repo = ctx.Repo
case *context.APIContext:
repo = ctx.Repo
default:
assert.FailNow(t, "context is not *context.Context or *context.APIContext")
}
require.NoError(t, repo.Repository.LoadOwner(ctx))
var err error var err error
ctx.Repo.GitRepo, err = gitrepo.OpenRepository(ctx, ctx.Repo.Repository) repo.GitRepo, err = gitrepo.OpenRepository(ctx, repo.Repository)
require.NoError(t, err) require.NoError(t, err)
} }

View file

@ -6,10 +6,11 @@ package files
import ( import (
"testing" "testing"
"code.gitea.io/gitea/models/db"
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/models/unittest"
"code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/gitrepo"
api "code.gitea.io/gitea/modules/structs" api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/services/contexttest"
_ "code.gitea.io/gitea/models/actions" _ "code.gitea.io/gitea/models/actions"
@ -53,27 +54,21 @@ func getExpectedReadmeContentsResponse() *api.ContentsResponse {
func TestGetContents(t *testing.T) { func TestGetContents(t *testing.T) {
unittest.PrepareTestEnv(t) unittest.PrepareTestEnv(t)
ctx, _ := contexttest.MockContext(t, "user2/repo1") repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
ctx.SetParams(":id", "1")
contexttest.LoadRepo(t, ctx, 1)
contexttest.LoadRepoCommit(t, ctx)
contexttest.LoadUser(t, ctx, 2)
contexttest.LoadGitRepo(t, ctx)
defer ctx.Repo.GitRepo.Close()
treePath := "README.md" treePath := "README.md"
ref := ctx.Repo.Repository.DefaultBranch ref := repo.DefaultBranch
expectedContentsResponse := getExpectedReadmeContentsResponse() expectedContentsResponse := getExpectedReadmeContentsResponse()
t.Run("Get README.md contents with GetContents(ctx, )", func(t *testing.T) { t.Run("Get README.md contents with GetContents(ctx, )", func(t *testing.T) {
fileContentResponse, err := GetContents(ctx, ctx.Repo.Repository, treePath, ref, false) fileContentResponse, err := GetContents(db.DefaultContext, repo, treePath, ref, false)
assert.EqualValues(t, expectedContentsResponse, fileContentResponse) assert.EqualValues(t, expectedContentsResponse, fileContentResponse)
require.NoError(t, err) require.NoError(t, err)
}) })
t.Run("Get README.md contents with ref as empty string (should then use the repo's default branch) with GetContents(ctx, )", func(t *testing.T) { t.Run("Get README.md contents with ref as empty string (should then use the repo's default branch) with GetContents(ctx, )", func(t *testing.T) {
fileContentResponse, err := GetContents(ctx, ctx.Repo.Repository, treePath, "", false) fileContentResponse, err := GetContents(db.DefaultContext, repo, treePath, "", false)
assert.EqualValues(t, expectedContentsResponse, fileContentResponse) assert.EqualValues(t, expectedContentsResponse, fileContentResponse)
require.NoError(t, err) require.NoError(t, err)
}) })
@ -81,16 +76,10 @@ func TestGetContents(t *testing.T) {
func TestGetContentsOrListForDir(t *testing.T) { func TestGetContentsOrListForDir(t *testing.T) {
unittest.PrepareTestEnv(t) unittest.PrepareTestEnv(t)
ctx, _ := contexttest.MockContext(t, "user2/repo1") repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
ctx.SetParams(":id", "1")
contexttest.LoadRepo(t, ctx, 1)
contexttest.LoadRepoCommit(t, ctx)
contexttest.LoadUser(t, ctx, 2)
contexttest.LoadGitRepo(t, ctx)
defer ctx.Repo.GitRepo.Close()
treePath := "" // root dir treePath := "" // root dir
ref := ctx.Repo.Repository.DefaultBranch ref := repo.DefaultBranch
readmeContentsResponse := getExpectedReadmeContentsResponse() readmeContentsResponse := getExpectedReadmeContentsResponse()
// because will be in a list, doesn't have encoding and content // because will be in a list, doesn't have encoding and content
@ -102,13 +91,13 @@ func TestGetContentsOrListForDir(t *testing.T) {
} }
t.Run("Get root dir contents with GetContentsOrList(ctx, )", func(t *testing.T) { t.Run("Get root dir contents with GetContentsOrList(ctx, )", func(t *testing.T) {
fileContentResponse, err := GetContentsOrList(ctx, ctx.Repo.Repository, treePath, ref) fileContentResponse, err := GetContentsOrList(db.DefaultContext, repo, treePath, ref)
assert.EqualValues(t, expectedContentsListResponse, fileContentResponse) assert.EqualValues(t, expectedContentsListResponse, fileContentResponse)
require.NoError(t, err) require.NoError(t, err)
}) })
t.Run("Get root dir contents with ref as empty string (should then use the repo's default branch) with GetContentsOrList(ctx, )", func(t *testing.T) { t.Run("Get root dir contents with ref as empty string (should then use the repo's default branch) with GetContentsOrList(ctx, )", func(t *testing.T) {
fileContentResponse, err := GetContentsOrList(ctx, ctx.Repo.Repository, treePath, "") fileContentResponse, err := GetContentsOrList(db.DefaultContext, repo, treePath, "")
assert.EqualValues(t, expectedContentsListResponse, fileContentResponse) assert.EqualValues(t, expectedContentsListResponse, fileContentResponse)
require.NoError(t, err) require.NoError(t, err)
}) })
@ -116,27 +105,21 @@ func TestGetContentsOrListForDir(t *testing.T) {
func TestGetContentsOrListForFile(t *testing.T) { func TestGetContentsOrListForFile(t *testing.T) {
unittest.PrepareTestEnv(t) unittest.PrepareTestEnv(t)
ctx, _ := contexttest.MockContext(t, "user2/repo1") repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
ctx.SetParams(":id", "1")
contexttest.LoadRepo(t, ctx, 1)
contexttest.LoadRepoCommit(t, ctx)
contexttest.LoadUser(t, ctx, 2)
contexttest.LoadGitRepo(t, ctx)
defer ctx.Repo.GitRepo.Close()
treePath := "README.md" treePath := "README.md"
ref := ctx.Repo.Repository.DefaultBranch ref := repo.DefaultBranch
expectedContentsResponse := getExpectedReadmeContentsResponse() expectedContentsResponse := getExpectedReadmeContentsResponse()
t.Run("Get README.md contents with GetContentsOrList(ctx, )", func(t *testing.T) { t.Run("Get README.md contents with GetContentsOrList(ctx, )", func(t *testing.T) {
fileContentResponse, err := GetContentsOrList(ctx, ctx.Repo.Repository, treePath, ref) fileContentResponse, err := GetContentsOrList(db.DefaultContext, repo, treePath, ref)
assert.EqualValues(t, expectedContentsResponse, fileContentResponse) assert.EqualValues(t, expectedContentsResponse, fileContentResponse)
require.NoError(t, err) require.NoError(t, err)
}) })
t.Run("Get README.md contents with ref as empty string (should then use the repo's default branch) with GetContentsOrList(ctx, )", func(t *testing.T) { t.Run("Get README.md contents with ref as empty string (should then use the repo's default branch) with GetContentsOrList(ctx, )", func(t *testing.T) {
fileContentResponse, err := GetContentsOrList(ctx, ctx.Repo.Repository, treePath, "") fileContentResponse, err := GetContentsOrList(db.DefaultContext, repo, treePath, "")
assert.EqualValues(t, expectedContentsResponse, fileContentResponse) assert.EqualValues(t, expectedContentsResponse, fileContentResponse)
require.NoError(t, err) require.NoError(t, err)
}) })
@ -144,28 +127,21 @@ func TestGetContentsOrListForFile(t *testing.T) {
func TestGetContentsErrors(t *testing.T) { func TestGetContentsErrors(t *testing.T) {
unittest.PrepareTestEnv(t) unittest.PrepareTestEnv(t)
ctx, _ := contexttest.MockContext(t, "user2/repo1") repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
ctx.SetParams(":id", "1")
contexttest.LoadRepo(t, ctx, 1)
contexttest.LoadRepoCommit(t, ctx)
contexttest.LoadUser(t, ctx, 2)
contexttest.LoadGitRepo(t, ctx)
defer ctx.Repo.GitRepo.Close()
repo := ctx.Repo.Repository
treePath := "README.md" treePath := "README.md"
ref := repo.DefaultBranch ref := repo.DefaultBranch
t.Run("bad treePath", func(t *testing.T) { t.Run("bad treePath", func(t *testing.T) {
badTreePath := "bad/tree.md" badTreePath := "bad/tree.md"
fileContentResponse, err := GetContents(ctx, repo, badTreePath, ref, false) fileContentResponse, err := GetContents(db.DefaultContext, repo, badTreePath, ref, false)
require.EqualError(t, err, "object does not exist [id: , rel_path: bad]") require.EqualError(t, err, "object does not exist [id: , rel_path: bad]")
assert.Nil(t, fileContentResponse) assert.Nil(t, fileContentResponse)
}) })
t.Run("bad ref", func(t *testing.T) { t.Run("bad ref", func(t *testing.T) {
badRef := "bad_ref" badRef := "bad_ref"
fileContentResponse, err := GetContents(ctx, repo, treePath, badRef, false) fileContentResponse, err := GetContents(db.DefaultContext, repo, treePath, badRef, false)
require.EqualError(t, err, "object does not exist [id: "+badRef+", rel_path: ]") require.EqualError(t, err, "object does not exist [id: "+badRef+", rel_path: ]")
assert.Nil(t, fileContentResponse) assert.Nil(t, fileContentResponse)
}) })
@ -173,28 +149,21 @@ func TestGetContentsErrors(t *testing.T) {
func TestGetContentsOrListErrors(t *testing.T) { func TestGetContentsOrListErrors(t *testing.T) {
unittest.PrepareTestEnv(t) unittest.PrepareTestEnv(t)
ctx, _ := contexttest.MockContext(t, "user2/repo1") repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
ctx.SetParams(":id", "1")
contexttest.LoadRepo(t, ctx, 1)
contexttest.LoadRepoCommit(t, ctx)
contexttest.LoadUser(t, ctx, 2)
contexttest.LoadGitRepo(t, ctx)
defer ctx.Repo.GitRepo.Close()
repo := ctx.Repo.Repository
treePath := "README.md" treePath := "README.md"
ref := repo.DefaultBranch ref := repo.DefaultBranch
t.Run("bad treePath", func(t *testing.T) { t.Run("bad treePath", func(t *testing.T) {
badTreePath := "bad/tree.md" badTreePath := "bad/tree.md"
fileContentResponse, err := GetContentsOrList(ctx, repo, badTreePath, ref) fileContentResponse, err := GetContentsOrList(db.DefaultContext, repo, badTreePath, ref)
require.EqualError(t, err, "object does not exist [id: , rel_path: bad]") require.EqualError(t, err, "object does not exist [id: , rel_path: bad]")
assert.Nil(t, fileContentResponse) assert.Nil(t, fileContentResponse)
}) })
t.Run("bad ref", func(t *testing.T) { t.Run("bad ref", func(t *testing.T) {
badRef := "bad_ref" badRef := "bad_ref"
fileContentResponse, err := GetContentsOrList(ctx, repo, treePath, badRef) fileContentResponse, err := GetContentsOrList(db.DefaultContext, repo, treePath, badRef)
require.EqualError(t, err, "object does not exist [id: "+badRef+", rel_path: ]") require.EqualError(t, err, "object does not exist [id: "+badRef+", rel_path: ]")
assert.Nil(t, fileContentResponse) assert.Nil(t, fileContentResponse)
}) })
@ -202,17 +171,10 @@ func TestGetContentsOrListErrors(t *testing.T) {
func TestGetContentsOrListOfEmptyRepos(t *testing.T) { func TestGetContentsOrListOfEmptyRepos(t *testing.T) {
unittest.PrepareTestEnv(t) unittest.PrepareTestEnv(t)
ctx, _ := contexttest.MockContext(t, "user30/empty") repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 52})
ctx.SetParams(":id", "52")
contexttest.LoadRepo(t, ctx, 52)
contexttest.LoadUser(t, ctx, 30)
contexttest.LoadGitRepo(t, ctx)
defer ctx.Repo.GitRepo.Close()
repo := ctx.Repo.Repository
t.Run("empty repo", func(t *testing.T) { t.Run("empty repo", func(t *testing.T) {
contents, err := GetContentsOrList(ctx, repo, "", "") contents, err := GetContentsOrList(db.DefaultContext, repo, "", "")
require.NoError(t, err) require.NoError(t, err)
assert.Empty(t, contents) assert.Empty(t, contents)
}) })
@ -220,23 +182,13 @@ func TestGetContentsOrListOfEmptyRepos(t *testing.T) {
func TestGetBlobBySHA(t *testing.T) { func TestGetBlobBySHA(t *testing.T) {
unittest.PrepareTestEnv(t) unittest.PrepareTestEnv(t)
ctx, _ := contexttest.MockContext(t, "user2/repo1") repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
contexttest.LoadRepo(t, ctx, 1)
contexttest.LoadRepoCommit(t, ctx)
contexttest.LoadUser(t, ctx, 2)
contexttest.LoadGitRepo(t, ctx)
defer ctx.Repo.GitRepo.Close()
sha := "65f1bf27bc3bf70f64657658635e66094edbcb4d" gitRepo, err := gitrepo.OpenRepository(db.DefaultContext, repo)
ctx.SetParams(":id", "1") require.NoError(t, err)
ctx.SetParams(":sha", sha) defer gitRepo.Close()
gitRepo, err := gitrepo.OpenRepository(ctx, ctx.Repo.Repository) gbr, err := GetBlobBySHA(db.DefaultContext, repo, gitRepo, "65f1bf27bc3bf70f64657658635e66094edbcb4d")
if err != nil {
t.Fail()
}
gbr, err := GetBlobBySHA(ctx, ctx.Repo.Repository, gitRepo, ctx.Params(":sha"))
expectedGBR := &api.GitBlobResponse{ expectedGBR := &api.GitBlobResponse{
Content: "dHJlZSAyYTJmMWQ0NjcwNzI4YTJlMTAwNDllMzQ1YmQ3YTI3NjQ2OGJlYWI2CmF1dGhvciB1c2VyMSA8YWRkcmVzczFAZXhhbXBsZS5jb20+IDE0ODk5NTY0NzkgLTA0MDAKY29tbWl0dGVyIEV0aGFuIEtvZW5pZyA8ZXRoYW50a29lbmlnQGdtYWlsLmNvbT4gMTQ4OTk1NjQ3OSAtMDQwMAoKSW5pdGlhbCBjb21taXQK", Content: "dHJlZSAyYTJmMWQ0NjcwNzI4YTJlMTAwNDllMzQ1YmQ3YTI3NjQ2OGJlYWI2CmF1dGhvciB1c2VyMSA8YWRkcmVzczFAZXhhbXBsZS5jb20+IDE0ODk5NTY0NzkgLTA0MDAKY29tbWl0dGVyIEV0aGFuIEtvZW5pZyA8ZXRoYW50a29lbmlnQGdtYWlsLmNvbT4gMTQ4OTk1NjQ3OSAtMDQwMAoKSW5pdGlhbCBjb21taXQK",
Encoding: "base64", Encoding: "base64",

View file

@ -6,6 +6,7 @@ package files
import ( import (
"testing" "testing"
"code.gitea.io/gitea/models/db"
repo_model "code.gitea.io/gitea/models/repo" repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/models/unittest"
"code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/json"
@ -21,7 +22,6 @@ func TestGetDiffPreview(t *testing.T) {
ctx, _ := contexttest.MockContext(t, "user2/repo1") ctx, _ := contexttest.MockContext(t, "user2/repo1")
ctx.SetParams(":id", "1") ctx.SetParams(":id", "1")
contexttest.LoadRepo(t, ctx, 1) contexttest.LoadRepo(t, ctx, 1)
contexttest.LoadRepoCommit(t, ctx)
contexttest.LoadUser(t, ctx, 2) contexttest.LoadUser(t, ctx, 2)
contexttest.LoadGitRepo(t, ctx) contexttest.LoadGitRepo(t, ctx)
defer ctx.Repo.GitRepo.Close() defer ctx.Repo.GitRepo.Close()
@ -140,33 +140,26 @@ func TestGetDiffPreview(t *testing.T) {
func TestGetDiffPreviewErrors(t *testing.T) { func TestGetDiffPreviewErrors(t *testing.T) {
unittest.PrepareTestEnv(t) unittest.PrepareTestEnv(t)
ctx, _ := contexttest.MockContext(t, "user2/repo1") repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
ctx.SetParams(":id", "1") branch := repo.DefaultBranch
contexttest.LoadRepo(t, ctx, 1)
contexttest.LoadRepoCommit(t, ctx)
contexttest.LoadUser(t, ctx, 2)
contexttest.LoadGitRepo(t, ctx)
defer ctx.Repo.GitRepo.Close()
branch := ctx.Repo.Repository.DefaultBranch
treePath := "README.md" treePath := "README.md"
content := "# repo1\n\nDescription for repo1\nthis is a new line" content := "# repo1\n\nDescription for repo1\nthis is a new line"
t.Run("empty repo", func(t *testing.T) { t.Run("empty repo", func(t *testing.T) {
diff, err := GetDiffPreview(ctx, &repo_model.Repository{}, branch, treePath, content) diff, err := GetDiffPreview(db.DefaultContext, &repo_model.Repository{}, branch, treePath, content)
assert.Nil(t, diff) assert.Nil(t, diff)
assert.EqualError(t, err, "repository does not exist [id: 0, uid: 0, owner_name: , name: ]") assert.EqualError(t, err, "repository does not exist [id: 0, uid: 0, owner_name: , name: ]")
}) })
t.Run("bad branch", func(t *testing.T) { t.Run("bad branch", func(t *testing.T) {
badBranch := "bad_branch" badBranch := "bad_branch"
diff, err := GetDiffPreview(ctx, ctx.Repo.Repository, badBranch, treePath, content) diff, err := GetDiffPreview(db.DefaultContext, repo, badBranch, treePath, content)
assert.Nil(t, diff) assert.Nil(t, diff)
assert.EqualError(t, err, "branch does not exist [name: "+badBranch+"]") assert.EqualError(t, err, "branch does not exist [name: "+badBranch+"]")
}) })
t.Run("empty treePath", func(t *testing.T) { t.Run("empty treePath", func(t *testing.T) {
diff, err := GetDiffPreview(ctx, ctx.Repo.Repository, branch, "", content) diff, err := GetDiffPreview(db.DefaultContext, repo, branch, "", content)
assert.Nil(t, diff) assert.Nil(t, diff)
assert.EqualError(t, err, "path is invalid [path: ]") assert.EqualError(t, err, "path is invalid [path: ]")
}) })

View file

@ -6,11 +6,12 @@ package files
import ( import (
"testing" "testing"
"code.gitea.io/gitea/models/db"
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/models/unittest"
"code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/gitrepo"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs" api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/services/contexttest"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
@ -99,23 +100,16 @@ func getExpectedFileResponse() *api.FileResponse {
func TestGetFileResponseFromCommit(t *testing.T) { func TestGetFileResponseFromCommit(t *testing.T) {
unittest.PrepareTestEnv(t) unittest.PrepareTestEnv(t)
ctx, _ := contexttest.MockContext(t, "user2/repo1")
ctx.SetParams(":id", "1")
contexttest.LoadRepo(t, ctx, 1)
contexttest.LoadRepoCommit(t, ctx)
contexttest.LoadUser(t, ctx, 2)
contexttest.LoadGitRepo(t, ctx)
defer ctx.Repo.GitRepo.Close()
repo := ctx.Repo.Repository repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
branch := repo.DefaultBranch branch := repo.DefaultBranch
treePath := "README.md" treePath := "README.md"
gitRepo, _ := gitrepo.OpenRepository(ctx, repo) gitRepo, _ := gitrepo.OpenRepository(db.DefaultContext, repo)
defer gitRepo.Close() defer gitRepo.Close()
commit, _ := gitRepo.GetBranchCommit(branch) commit, _ := gitRepo.GetBranchCommit(branch)
expectedFileResponse := getExpectedFileResponse() expectedFileResponse := getExpectedFileResponse()
fileResponse, err := GetFileResponseFromCommit(ctx, repo, commit, branch, treePath) fileResponse, err := GetFileResponseFromCommit(db.DefaultContext, repo, commit, branch, treePath)
require.NoError(t, err) require.NoError(t, err)
assert.EqualValues(t, expectedFileResponse, fileResponse) assert.EqualValues(t, expectedFileResponse, fileResponse)
} }

View file

@ -18,7 +18,6 @@ func TestGetTreeBySHA(t *testing.T) {
unittest.PrepareTestEnv(t) unittest.PrepareTestEnv(t)
ctx, _ := contexttest.MockContext(t, "user2/repo1") ctx, _ := contexttest.MockContext(t, "user2/repo1")
contexttest.LoadRepo(t, ctx, 1) contexttest.LoadRepo(t, ctx, 1)
contexttest.LoadRepoCommit(t, ctx)
contexttest.LoadUser(t, ctx, 2) contexttest.LoadUser(t, ctx, 2)
contexttest.LoadGitRepo(t, ctx) contexttest.LoadGitRepo(t, ctx)
defer ctx.Repo.GitRepo.Close() defer ctx.Repo.GitRepo.Close()

View file

@ -52,8 +52,13 @@ func createRepoAndGetContext(t *testing.T, files []string, deleteMdReadme bool)
ctx, _ := contexttest.MockContext(t, "user1/readmetest") ctx, _ := contexttest.MockContext(t, "user1/readmetest")
ctx.SetParams(":id", fmt.Sprint(repo.ID)) ctx.SetParams(":id", fmt.Sprint(repo.ID))
contexttest.LoadRepo(t, ctx, repo.ID) contexttest.LoadRepo(t, ctx, repo.ID)
contexttest.LoadGitRepo(t, ctx)
contexttest.LoadRepoCommit(t, ctx) contexttest.LoadRepoCommit(t, ctx)
return ctx, f
return ctx, func() {
f()
ctx.Repo.GitRepo.Close()
}
} }
func TestRepoView_FindReadme(t *testing.T) { func TestRepoView_FindReadme(t *testing.T) {

View file

@ -12,11 +12,11 @@ import (
repo_model "code.gitea.io/gitea/models/repo" repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/models/unittest"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/gitrepo"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs" api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/services/contexttest"
files_service "code.gitea.io/gitea/services/repository/files" files_service "code.gitea.io/gitea/services/repository/files"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -247,16 +247,8 @@ func getExpectedFileResponseForRepofilesUpdate(commitID, filename, lastCommitSHA
func TestChangeRepoFilesForCreate(t *testing.T) { func TestChangeRepoFilesForCreate(t *testing.T) {
// setup // setup
onGiteaRun(t, func(t *testing.T, u *url.URL) { onGiteaRun(t, func(t *testing.T, u *url.URL) {
ctx, _ := contexttest.MockContext(t, "user2/repo1") doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
ctx.SetParams(":id", "1") repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
contexttest.LoadRepo(t, ctx, 1)
contexttest.LoadRepoCommit(t, ctx)
contexttest.LoadUser(t, ctx, 2)
contexttest.LoadGitRepo(t, ctx)
defer ctx.Repo.GitRepo.Close()
repo := ctx.Repo.Repository
doer := ctx.Doer
opts := getCreateRepoFilesOptions(repo) opts := getCreateRepoFilesOptions(repo)
// test // test
@ -284,16 +276,8 @@ func TestChangeRepoFilesForCreate(t *testing.T) {
func TestChangeRepoFilesForUpdate(t *testing.T) { func TestChangeRepoFilesForUpdate(t *testing.T) {
// setup // setup
onGiteaRun(t, func(t *testing.T, u *url.URL) { onGiteaRun(t, func(t *testing.T, u *url.URL) {
ctx, _ := contexttest.MockContext(t, "user2/repo1") doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
ctx.SetParams(":id", "1") repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
contexttest.LoadRepo(t, ctx, 1)
contexttest.LoadRepoCommit(t, ctx)
contexttest.LoadUser(t, ctx, 2)
contexttest.LoadGitRepo(t, ctx)
defer ctx.Repo.GitRepo.Close()
repo := ctx.Repo.Repository
doer := ctx.Doer
opts := getUpdateRepoFilesOptions(repo) opts := getUpdateRepoFilesOptions(repo)
// test // test
@ -318,16 +302,8 @@ func TestChangeRepoFilesForUpdate(t *testing.T) {
func TestChangeRepoFilesForUpdateWithFileMove(t *testing.T) { func TestChangeRepoFilesForUpdateWithFileMove(t *testing.T) {
// setup // setup
onGiteaRun(t, func(t *testing.T, u *url.URL) { onGiteaRun(t, func(t *testing.T, u *url.URL) {
ctx, _ := contexttest.MockContext(t, "user2/repo1") doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
ctx.SetParams(":id", "1") repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
contexttest.LoadRepo(t, ctx, 1)
contexttest.LoadRepoCommit(t, ctx)
contexttest.LoadUser(t, ctx, 2)
contexttest.LoadGitRepo(t, ctx)
defer ctx.Repo.GitRepo.Close()
repo := ctx.Repo.Repository
doer := ctx.Doer
opts := getUpdateRepoFilesOptions(repo) opts := getUpdateRepoFilesOptions(repo)
opts.Files[0].FromTreePath = "README.md" opts.Files[0].FromTreePath = "README.md"
opts.Files[0].TreePath = "README_new.md" // new file name, README_new.md opts.Files[0].TreePath = "README_new.md" // new file name, README_new.md
@ -369,16 +345,8 @@ func TestChangeRepoFilesForUpdateWithFileMove(t *testing.T) {
func TestChangeRepoFilesWithoutBranchNames(t *testing.T) { func TestChangeRepoFilesWithoutBranchNames(t *testing.T) {
// setup // setup
onGiteaRun(t, func(t *testing.T, u *url.URL) { onGiteaRun(t, func(t *testing.T, u *url.URL) {
ctx, _ := contexttest.MockContext(t, "user2/repo1") doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
ctx.SetParams(":id", "1") repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
contexttest.LoadRepo(t, ctx, 1)
contexttest.LoadRepoCommit(t, ctx)
contexttest.LoadUser(t, ctx, 2)
contexttest.LoadGitRepo(t, ctx)
defer ctx.Repo.GitRepo.Close()
repo := ctx.Repo.Repository
doer := ctx.Doer
opts := getUpdateRepoFilesOptions(repo) opts := getUpdateRepoFilesOptions(repo)
opts.OldBranch = "" opts.OldBranch = ""
opts.NewBranch = "" opts.NewBranch = ""
@ -405,15 +373,8 @@ func TestChangeRepoFilesForDelete(t *testing.T) {
func testDeleteRepoFiles(t *testing.T, u *url.URL) { func testDeleteRepoFiles(t *testing.T, u *url.URL) {
// setup // setup
unittest.PrepareTestEnv(t) unittest.PrepareTestEnv(t)
ctx, _ := contexttest.MockContext(t, "user2/repo1") doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
ctx.SetParams(":id", "1") repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
contexttest.LoadRepo(t, ctx, 1)
contexttest.LoadRepoCommit(t, ctx)
contexttest.LoadUser(t, ctx, 2)
contexttest.LoadGitRepo(t, ctx)
defer ctx.Repo.GitRepo.Close()
repo := ctx.Repo.Repository
doer := ctx.Doer
opts := getDeleteRepoFilesOptions(repo) opts := getDeleteRepoFilesOptions(repo)
t.Run("Delete README.md file", func(t *testing.T) { t.Run("Delete README.md file", func(t *testing.T) {
@ -444,16 +405,9 @@ func TestChangeRepoFilesForDeleteWithoutBranchNames(t *testing.T) {
func testDeleteRepoFilesWithoutBranchNames(t *testing.T, u *url.URL) { func testDeleteRepoFilesWithoutBranchNames(t *testing.T, u *url.URL) {
// setup // setup
unittest.PrepareTestEnv(t) unittest.PrepareTestEnv(t)
ctx, _ := contexttest.MockContext(t, "user2/repo1") doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
ctx.SetParams(":id", "1") repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
contexttest.LoadRepo(t, ctx, 1)
contexttest.LoadRepoCommit(t, ctx)
contexttest.LoadUser(t, ctx, 2)
contexttest.LoadGitRepo(t, ctx)
defer ctx.Repo.GitRepo.Close()
repo := ctx.Repo.Repository
doer := ctx.Doer
opts := getDeleteRepoFilesOptions(repo) opts := getDeleteRepoFilesOptions(repo)
opts.OldBranch = "" opts.OldBranch = ""
opts.NewBranch = "" opts.NewBranch = ""
@ -474,16 +428,8 @@ func testDeleteRepoFilesWithoutBranchNames(t *testing.T, u *url.URL) {
func TestChangeRepoFilesErrors(t *testing.T) { func TestChangeRepoFilesErrors(t *testing.T) {
// setup // setup
onGiteaRun(t, func(t *testing.T, u *url.URL) { onGiteaRun(t, func(t *testing.T, u *url.URL) {
ctx, _ := contexttest.MockContext(t, "user2/repo1") doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
ctx.SetParams(":id", "1") repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
contexttest.LoadRepo(t, ctx, 1)
contexttest.LoadRepoCommit(t, ctx)
contexttest.LoadUser(t, ctx, 2)
contexttest.LoadGitRepo(t, ctx)
defer ctx.Repo.GitRepo.Close()
repo := ctx.Repo.Repository
doer := ctx.Doer
t.Run("bad branch", func(t *testing.T) { t.Run("bad branch", func(t *testing.T) {
opts := getUpdateRepoFilesOptions(repo) opts := getUpdateRepoFilesOptions(repo)