Skip to content

Commit f6d949b

Browse files
committed
fix tests
1 parent d173d4e commit f6d949b

File tree

6 files changed

+46
-77
lines changed

6 files changed

+46
-77
lines changed

routers/api/v1/api.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1389,15 +1389,15 @@ func Routes() *web.Router {
13891389
m.Post("/diffpatch", reqRepoWriter(unit.TypeCode), reqToken(), bind(api.ApplyDiffPatchFileOptions{}), mustNotBeArchived, repo.ApplyDiffPatch)
13901390
m.Group("/contents", func() {
13911391
m.Get("", repo.GetContentsList)
1392-
m.Post("", reqToken(), bind(api.ChangeFilesOptions{}), reqRepoBranchWriter, mustNotBeArchived, repo.ChangeFiles)
13931392
m.Get("/*", repo.GetContents)
1393+
m.Post("", reqToken(), bind(api.ChangeFilesOptions{}), reqRepoBranchWriter, mustNotBeArchived, repo.ChangeFiles)
13941394
m.Group("/*", func() {
13951395
m.Post("", bind(api.CreateFileOptions{}), reqRepoBranchWriter, mustNotBeArchived, repo.CreateFile)
13961396
m.Put("", bind(api.UpdateFileOptions{}), reqRepoBranchWriter, mustNotBeArchived, repo.UpdateFile)
13971397
m.Delete("", bind(api.DeleteFileOptions{}), reqRepoBranchWriter, mustNotBeArchived, repo.DeleteFile)
13981398
}, reqToken())
1399-
}, reqRepoReader(unit.TypeCode))
1400-
m.Post("/files", context.ReferencesGitRepo(), context.RepoRefForAPI, bind(api.GetFilesOptions{}), reqRepoReader(unit.TypeCode), repo.GetFiles)
1399+
}, reqRepoReader(unit.TypeCode), context.ReferencesGitRepo())
1400+
m.Post("/files", reqRepoReader(unit.TypeCode), context.ReferencesGitRepo(), context.RepoRefForAPI, bind(api.GetFilesOptions{}), repo.GetFiles)
14011401
m.Get("/signing-key.gpg", misc.SigningKey)
14021402
m.Group("/topics", func() {
14031403
m.Combo("").Get(repo.ListTopics).

routers/api/v1/repo/file.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -898,7 +898,7 @@ func DeleteFile(ctx *context.APIContext) {
898898

899899
func resolveRefCommit(ctx *context.APIContext, ref string) *utils.RefCommit {
900900
ref = util.IfZero(ref, ctx.Repo.Repository.DefaultBranch)
901-
refCommit, err := utils.ResolveRefCommit(ctx, ref)
901+
refCommit, err := utils.ResolveRefCommit(ctx, ctx.Repo.Repository, ref)
902902
if errors.Is(err, util.ErrNotExist) {
903903
ctx.APIErrorNotFound(err)
904904
} else if err != nil {

routers/api/v1/utils/git.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ package utils
66
import (
77
"errors"
88

9+
repo_model "code.gitea.io/gitea/models/repo"
910
"code.gitea.io/gitea/modules/git"
1011
"code.gitea.io/gitea/modules/gitrepo"
12+
"code.gitea.io/gitea/modules/reqctx"
1113
"code.gitea.io/gitea/services/context"
1214
)
1315

@@ -19,19 +21,23 @@ type RefCommit struct {
1921
}
2022

2123
// ResolveRefCommit resolve ref to a commit if exist
22-
func ResolveRefCommit(ctx *context.APIContext, inputRef string) (_ *RefCommit, err error) {
24+
func ResolveRefCommit(ctx reqctx.RequestContext, repo *repo_model.Repository, inputRef string) (_ *RefCommit, err error) {
25+
gitRepo, err := gitrepo.RepositoryFromRequestContextOrOpen(ctx, repo)
26+
if err != nil {
27+
return nil, err
28+
}
2329
refCommit := RefCommit{InputRef: inputRef}
24-
if gitrepo.IsBranchExist(ctx, ctx.Repo.Repository, inputRef) {
30+
if gitrepo.IsBranchExist(ctx, repo, inputRef) {
2531
refCommit.Ref = git.RefNameFromBranch(inputRef)
26-
} else if gitrepo.IsTagExist(ctx, ctx.Repo.Repository, inputRef) {
32+
} else if gitrepo.IsTagExist(ctx, repo, inputRef) {
2733
refCommit.Ref = git.RefNameFromTag(inputRef)
28-
} else if len(inputRef) == ctx.Repo.GetObjectFormat().FullLength() {
34+
} else if len(inputRef) == git.ObjectFormatFromName(repo.ObjectFormatName).FullLength() {
2935
refCommit.Ref = git.RefNameFromCommit(inputRef)
3036
}
3137
if refCommit.Ref == "" {
3238
return nil, git.ErrNotExist{ID: inputRef}
3339
}
34-
if refCommit.Commit, err = ctx.Repo.GitRepo.GetCommit(refCommit.Ref.String()); err != nil {
40+
if refCommit.Commit, err = gitRepo.GetCommit(refCommit.Ref.String()); err != nil {
3541
return nil, err
3642
}
3743
refCommit.CommitID = refCommit.Commit.ID.String()

services/contexttest/context_tests.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,19 @@ func LoadUser(t *testing.T, ctx gocontext.Context, userID int64) {
170170

171171
// LoadGitRepo load a git repo into a test context. Requires that ctx.Repo has
172172
// already been populated.
173-
func LoadGitRepo(t *testing.T, ctx *context.Context) {
174-
assert.NoError(t, ctx.Repo.Repository.LoadOwner(ctx))
173+
func LoadGitRepo(t *testing.T, ctx gocontext.Context) {
174+
var repo *context.Repository
175+
switch ctx := any(ctx).(type) {
176+
case *context.Context:
177+
repo = ctx.Repo
178+
case *context.APIContext:
179+
repo = ctx.Repo
180+
default:
181+
assert.FailNow(t, "context is not *context.Context or *context.APIContext")
182+
}
183+
assert.NoError(t, repo.Repository.LoadOwner(ctx))
175184
var err error
176-
ctx.Repo.GitRepo, err = gitrepo.OpenRepository(ctx, ctx.Repo.Repository)
185+
repo.GitRepo, err = gitrepo.OpenRepository(ctx, repo.Repository)
177186
assert.NoError(t, err)
178187
}
179188

services/repository/files/content_test.go

Lines changed: 17 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ import (
1010
"code.gitea.io/gitea/models/unittest"
1111
"code.gitea.io/gitea/modules/gitrepo"
1212
api "code.gitea.io/gitea/modules/structs"
13+
"code.gitea.io/gitea/routers/api/v1/utils"
1314
"code.gitea.io/gitea/services/contexttest"
1415

1516
_ "code.gitea.io/gitea/models/actions"
1617

1718
"github.com/stretchr/testify/assert"
19+
"github.com/stretchr/testify/require"
1820
)
1921

2022
func TestMain(m *testing.M) {
@@ -64,18 +66,13 @@ func TestGetContents(t *testing.T) {
6466
defer ctx.Repo.GitRepo.Close()
6567

6668
treePath := "README.md"
67-
ref := ctx.Repo.Repository.DefaultBranch
69+
refCommit, err := utils.ResolveRefCommit(ctx, ctx.Repo.Repository, ctx.Repo.Repository.DefaultBranch)
70+
require.NoError(t, err)
6871

6972
expectedContentsResponse := getExpectedReadmeContentsResponse()
7073

7174
t.Run("Get README.md contents with GetContents(ctx, )", func(t *testing.T) {
72-
fileContentResponse, err := GetContents(ctx, ctx.Repo.Repository, treePath, ref, false)
73-
assert.Equal(t, expectedContentsResponse, fileContentResponse)
74-
assert.NoError(t, err)
75-
})
76-
77-
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) {
78-
fileContentResponse, err := GetContents(ctx, ctx.Repo.Repository, treePath, "", false)
75+
fileContentResponse, err := GetContents(ctx, ctx.Repo.Repository, refCommit, treePath, false)
7976
assert.Equal(t, expectedContentsResponse, fileContentResponse)
8077
assert.NoError(t, err)
8178
})
@@ -92,7 +89,8 @@ func TestGetContentsOrListForDir(t *testing.T) {
9289
defer ctx.Repo.GitRepo.Close()
9390

9491
treePath := "" // root dir
95-
ref := ctx.Repo.Repository.DefaultBranch
92+
refCommit, err := utils.ResolveRefCommit(ctx, ctx.Repo.Repository, ctx.Repo.Repository.DefaultBranch)
93+
require.NoError(t, err)
9694

9795
readmeContentsResponse := getExpectedReadmeContentsResponse()
9896
// because will be in a list, doesn't have encoding and content
@@ -104,13 +102,7 @@ func TestGetContentsOrListForDir(t *testing.T) {
104102
}
105103

106104
t.Run("Get root dir contents with GetContentsOrList(ctx, )", func(t *testing.T) {
107-
fileContentResponse, err := GetContentsOrList(ctx, ctx.Repo.Repository, treePath, ref)
108-
assert.EqualValues(t, expectedContentsListResponse, fileContentResponse)
109-
assert.NoError(t, err)
110-
})
111-
112-
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) {
113-
fileContentResponse, err := GetContentsOrList(ctx, ctx.Repo.Repository, treePath, "")
105+
fileContentResponse, err := GetContentsOrList(ctx, ctx.Repo.Repository, refCommit, treePath)
114106
assert.EqualValues(t, expectedContentsListResponse, fileContentResponse)
115107
assert.NoError(t, err)
116108
})
@@ -127,18 +119,13 @@ func TestGetContentsOrListForFile(t *testing.T) {
127119
defer ctx.Repo.GitRepo.Close()
128120

129121
treePath := "README.md"
130-
ref := ctx.Repo.Repository.DefaultBranch
122+
refCommit, err := utils.ResolveRefCommit(ctx, ctx.Repo.Repository, ctx.Repo.Repository.DefaultBranch)
123+
require.NoError(t, err)
131124

132125
expectedContentsResponse := getExpectedReadmeContentsResponse()
133126

134127
t.Run("Get README.md contents with GetContentsOrList(ctx, )", func(t *testing.T) {
135-
fileContentResponse, err := GetContentsOrList(ctx, ctx.Repo.Repository, treePath, ref)
136-
assert.EqualValues(t, expectedContentsResponse, fileContentResponse)
137-
assert.NoError(t, err)
138-
})
139-
140-
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) {
141-
fileContentResponse, err := GetContentsOrList(ctx, ctx.Repo.Repository, treePath, "")
128+
fileContentResponse, err := GetContentsOrList(ctx, ctx.Repo.Repository, refCommit, treePath)
142129
assert.EqualValues(t, expectedContentsResponse, fileContentResponse)
143130
assert.NoError(t, err)
144131
})
@@ -155,24 +142,16 @@ func TestGetContentsErrors(t *testing.T) {
155142
defer ctx.Repo.GitRepo.Close()
156143

157144
repo := ctx.Repo.Repository
158-
treePath := "README.md"
159-
ref := repo.DefaultBranch
145+
refCommit, err := utils.ResolveRefCommit(ctx, ctx.Repo.Repository, ctx.Repo.Repository.DefaultBranch)
146+
require.NoError(t, err)
160147

161148
t.Run("bad treePath", func(t *testing.T) {
162149
badTreePath := "bad/tree.md"
163-
fileContentResponse, err := GetContents(ctx, repo, badTreePath, ref, false)
150+
fileContentResponse, err := GetContents(ctx, repo, refCommit, badTreePath, false)
164151
assert.Error(t, err)
165152
assert.EqualError(t, err, "object does not exist [id: , rel_path: bad]")
166153
assert.Nil(t, fileContentResponse)
167154
})
168-
169-
t.Run("bad ref", func(t *testing.T) {
170-
badRef := "bad_ref"
171-
fileContentResponse, err := GetContents(ctx, repo, treePath, badRef, false)
172-
assert.Error(t, err)
173-
assert.EqualError(t, err, "object does not exist [id: "+badRef+", rel_path: ]")
174-
assert.Nil(t, fileContentResponse)
175-
})
176155
}
177156

178157
func TestGetContentsOrListErrors(t *testing.T) {
@@ -186,42 +165,16 @@ func TestGetContentsOrListErrors(t *testing.T) {
186165
defer ctx.Repo.GitRepo.Close()
187166

188167
repo := ctx.Repo.Repository
189-
treePath := "README.md"
190-
ref := repo.DefaultBranch
168+
refCommit, err := utils.ResolveRefCommit(ctx, ctx.Repo.Repository, ctx.Repo.Repository.DefaultBranch)
169+
require.NoError(t, err)
191170

192171
t.Run("bad treePath", func(t *testing.T) {
193172
badTreePath := "bad/tree.md"
194-
fileContentResponse, err := GetContentsOrList(ctx, repo, badTreePath, ref)
173+
fileContentResponse, err := GetContentsOrList(ctx, repo, refCommit, badTreePath)
195174
assert.Error(t, err)
196175
assert.EqualError(t, err, "object does not exist [id: , rel_path: bad]")
197176
assert.Nil(t, fileContentResponse)
198177
})
199-
200-
t.Run("bad ref", func(t *testing.T) {
201-
badRef := "bad_ref"
202-
fileContentResponse, err := GetContentsOrList(ctx, repo, treePath, badRef)
203-
assert.Error(t, err)
204-
assert.EqualError(t, err, "object does not exist [id: "+badRef+", rel_path: ]")
205-
assert.Nil(t, fileContentResponse)
206-
})
207-
}
208-
209-
func TestGetContentsOrListOfEmptyRepos(t *testing.T) {
210-
unittest.PrepareTestEnv(t)
211-
ctx, _ := contexttest.MockContext(t, "user30/empty")
212-
ctx.SetPathParam("id", "52")
213-
contexttest.LoadRepo(t, ctx, 52)
214-
contexttest.LoadUser(t, ctx, 30)
215-
contexttest.LoadGitRepo(t, ctx)
216-
defer ctx.Repo.GitRepo.Close()
217-
218-
repo := ctx.Repo.Repository
219-
220-
t.Run("empty repo", func(t *testing.T) {
221-
contents, err := GetContentsOrList(ctx, repo, "", "")
222-
assert.NoError(t, err)
223-
assert.Empty(t, contents)
224-
})
225178
}
226179

227180
func TestGetBlobBySHA(t *testing.T) {

services/repository/files/update.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,8 @@ func ChangeRepoFiles(ctx context.Context, repo *repo_model.Repository, doer *use
298298
}
299299

300300
// FIXME: this call seems not right, why it needs to read the file content again
301-
filesResponse, err := GetFilesResponseFromCommit(ctx, repo, utils.NewRefCommit(git.RefNameFromCommit(commit.ID.String()), commit), treePaths)
301+
// FIXME: why it uses the NewBranch as "ref", it should use the commit ID because the response is only for this commit
302+
filesResponse, err := GetFilesResponseFromCommit(ctx, repo, utils.NewRefCommit(git.RefNameFromBranch(opts.NewBranch), commit), treePaths)
302303
if err != nil {
303304
return nil, err
304305
}

0 commit comments

Comments
 (0)