2019-04-17 18:06:35 +02:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-27 19:20:29 +01:00
|
|
|
// SPDX-License-Identifier: MIT
|
2019-04-17 18:06:35 +02:00
|
|
|
|
2021-11-24 08:56:24 +01:00
|
|
|
package files
|
2019-04-17 18:06:35 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2024-08-26 08:03:48 +02:00
|
|
|
"code.gitea.io/gitea/models/db"
|
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-12 15:36:47 +01:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
Simplify how git repositories are opened (#28937)
## Purpose
This is a refactor toward building an abstraction over managing git
repositories.
Afterwards, it does not matter anymore if they are stored on the local
disk or somewhere remote.
## What this PR changes
We used `git.OpenRepository` everywhere previously.
Now, we should split them into two distinct functions:
Firstly, there are temporary repositories which do not change:
```go
git.OpenRepository(ctx, diskPath)
```
Gitea managed repositories having a record in the database in the
`repository` table are moved into the new package `gitrepo`:
```go
gitrepo.OpenRepository(ctx, repo_model.Repo)
```
Why is `repo_model.Repository` the second parameter instead of file
path?
Because then we can easily adapt our repository storage strategy.
The repositories can be stored locally, however, they could just as well
be stored on a remote server.
## Further changes in other PRs
- A Git Command wrapper on package `gitrepo` could be created. i.e.
`NewCommand(ctx, repo_model.Repository, commands...)`. `git.RunOpts{Dir:
repo.RepoPath()}`, the directory should be empty before invoking this
method and it can be filled in the function only. #28940
- Remove the `RepoPath()`/`WikiPath()` functions to reduce the
possibility of mistakes.
---------
Co-authored-by: delvh <dev.lh@web.de>
2024-01-27 21:09:51 +01:00
|
|
|
"code.gitea.io/gitea/modules/gitrepo"
|
2019-06-29 22:51:10 +02:00
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
2019-04-17 18:06:35 +02:00
|
|
|
|
2023-09-08 06:51:15 +02:00
|
|
|
_ "code.gitea.io/gitea/models/actions"
|
2024-11-01 21:02:52 +01:00
|
|
|
_ "code.gitea.io/gitea/models/forgefed"
|
2023-09-08 06:51:15 +02:00
|
|
|
|
2019-04-17 18:06:35 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
2024-07-30 21:41:10 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-04-17 18:06:35 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
2023-09-28 03:38:53 +02:00
|
|
|
unittest.MainTest(m)
|
2019-04-17 18:06:35 +02:00
|
|
|
}
|
|
|
|
|
2019-06-29 22:51:10 +02:00
|
|
|
func getExpectedReadmeContentsResponse() *api.ContentsResponse {
|
|
|
|
treePath := "README.md"
|
|
|
|
sha := "4b4851ad51df6a7d9f25c979345979eaeb5b349f"
|
|
|
|
encoding := "base64"
|
|
|
|
content := "IyByZXBvMQoKRGVzY3JpcHRpb24gZm9yIHJlcG8x"
|
|
|
|
selfURL := "https://try.gitea.io/api/v1/repos/user2/repo1/contents/" + treePath + "?ref=master"
|
|
|
|
htmlURL := "https://try.gitea.io/user2/repo1/src/branch/master/" + treePath
|
|
|
|
gitURL := "https://try.gitea.io/api/v1/repos/user2/repo1/git/blobs/" + sha
|
|
|
|
downloadURL := "https://try.gitea.io/user2/repo1/raw/branch/master/" + treePath
|
|
|
|
return &api.ContentsResponse{
|
2022-07-30 10:09:04 +02:00
|
|
|
Name: treePath,
|
|
|
|
Path: treePath,
|
|
|
|
SHA: "4b4851ad51df6a7d9f25c979345979eaeb5b349f",
|
|
|
|
LastCommitSHA: "65f1bf27bc3bf70f64657658635e66094edbcb4d",
|
|
|
|
Type: "file",
|
|
|
|
Size: 30,
|
|
|
|
Encoding: &encoding,
|
|
|
|
Content: &content,
|
|
|
|
URL: &selfURL,
|
|
|
|
HTMLURL: &htmlURL,
|
|
|
|
GitURL: &gitURL,
|
|
|
|
DownloadURL: &downloadURL,
|
2019-06-29 22:51:10 +02:00
|
|
|
Links: &api.FileLinksResponse{
|
|
|
|
Self: &selfURL,
|
|
|
|
GitURL: &gitURL,
|
|
|
|
HTMLURL: &htmlURL,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetContents(t *testing.T) {
|
2021-11-12 15:36:47 +01:00
|
|
|
unittest.PrepareTestEnv(t)
|
2024-08-26 08:03:48 +02:00
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
2019-11-13 08:01:19 +01:00
|
|
|
|
2019-04-17 18:06:35 +02:00
|
|
|
treePath := "README.md"
|
2024-08-26 08:03:48 +02:00
|
|
|
ref := repo.DefaultBranch
|
2019-04-17 18:06:35 +02:00
|
|
|
|
2019-06-29 22:51:10 +02:00
|
|
|
expectedContentsResponse := getExpectedReadmeContentsResponse()
|
|
|
|
|
2022-01-20 00:26:57 +01:00
|
|
|
t.Run("Get README.md contents with GetContents(ctx, )", func(t *testing.T) {
|
2024-08-26 08:03:48 +02:00
|
|
|
fileContentResponse, err := GetContents(db.DefaultContext, repo, treePath, ref, false)
|
2019-06-29 22:51:10 +02:00
|
|
|
assert.EqualValues(t, expectedContentsResponse, fileContentResponse)
|
2024-07-30 21:41:10 +02:00
|
|
|
require.NoError(t, err)
|
2019-06-29 22:51:10 +02:00
|
|
|
})
|
|
|
|
|
2022-01-20 00:26:57 +01:00
|
|
|
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) {
|
2024-08-26 08:03:48 +02:00
|
|
|
fileContentResponse, err := GetContents(db.DefaultContext, repo, treePath, "", false)
|
2019-06-29 22:51:10 +02:00
|
|
|
assert.EqualValues(t, expectedContentsResponse, fileContentResponse)
|
2024-07-30 21:41:10 +02:00
|
|
|
require.NoError(t, err)
|
2019-06-29 22:51:10 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetContentsOrListForDir(t *testing.T) {
|
2021-11-12 15:36:47 +01:00
|
|
|
unittest.PrepareTestEnv(t)
|
2024-08-26 08:03:48 +02:00
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
2019-11-13 08:01:19 +01:00
|
|
|
|
2019-06-29 22:51:10 +02:00
|
|
|
treePath := "" // root dir
|
2024-08-26 08:03:48 +02:00
|
|
|
ref := repo.DefaultBranch
|
2019-06-29 22:51:10 +02:00
|
|
|
|
|
|
|
readmeContentsResponse := getExpectedReadmeContentsResponse()
|
|
|
|
// because will be in a list, doesn't have encoding and content
|
|
|
|
readmeContentsResponse.Encoding = nil
|
|
|
|
readmeContentsResponse.Content = nil
|
|
|
|
|
|
|
|
expectedContentsListResponse := []*api.ContentsResponse{
|
|
|
|
readmeContentsResponse,
|
2019-04-17 18:06:35 +02:00
|
|
|
}
|
|
|
|
|
2022-01-20 00:26:57 +01:00
|
|
|
t.Run("Get root dir contents with GetContentsOrList(ctx, )", func(t *testing.T) {
|
2024-08-26 08:03:48 +02:00
|
|
|
fileContentResponse, err := GetContentsOrList(db.DefaultContext, repo, treePath, ref)
|
2019-06-29 22:51:10 +02:00
|
|
|
assert.EqualValues(t, expectedContentsListResponse, fileContentResponse)
|
2024-07-30 21:41:10 +02:00
|
|
|
require.NoError(t, err)
|
2019-06-29 22:51:10 +02:00
|
|
|
})
|
|
|
|
|
2022-01-20 00:26:57 +01:00
|
|
|
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) {
|
2024-08-26 08:03:48 +02:00
|
|
|
fileContentResponse, err := GetContentsOrList(db.DefaultContext, repo, treePath, "")
|
2019-06-29 22:51:10 +02:00
|
|
|
assert.EqualValues(t, expectedContentsListResponse, fileContentResponse)
|
2024-07-30 21:41:10 +02:00
|
|
|
require.NoError(t, err)
|
2019-06-29 22:51:10 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetContentsOrListForFile(t *testing.T) {
|
2021-11-12 15:36:47 +01:00
|
|
|
unittest.PrepareTestEnv(t)
|
2024-08-26 08:03:48 +02:00
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
2019-11-13 08:01:19 +01:00
|
|
|
|
2019-06-29 22:51:10 +02:00
|
|
|
treePath := "README.md"
|
2024-08-26 08:03:48 +02:00
|
|
|
ref := repo.DefaultBranch
|
2019-06-29 22:51:10 +02:00
|
|
|
|
|
|
|
expectedContentsResponse := getExpectedReadmeContentsResponse()
|
|
|
|
|
2022-01-20 00:26:57 +01:00
|
|
|
t.Run("Get README.md contents with GetContentsOrList(ctx, )", func(t *testing.T) {
|
2024-08-26 08:03:48 +02:00
|
|
|
fileContentResponse, err := GetContentsOrList(db.DefaultContext, repo, treePath, ref)
|
2019-06-29 22:51:10 +02:00
|
|
|
assert.EqualValues(t, expectedContentsResponse, fileContentResponse)
|
2024-07-30 21:41:10 +02:00
|
|
|
require.NoError(t, err)
|
2019-04-17 18:06:35 +02:00
|
|
|
})
|
|
|
|
|
2022-01-20 00:26:57 +01:00
|
|
|
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) {
|
2024-08-26 08:03:48 +02:00
|
|
|
fileContentResponse, err := GetContentsOrList(db.DefaultContext, repo, treePath, "")
|
2019-06-29 22:51:10 +02:00
|
|
|
assert.EqualValues(t, expectedContentsResponse, fileContentResponse)
|
2024-07-30 21:41:10 +02:00
|
|
|
require.NoError(t, err)
|
2019-04-17 18:06:35 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-06-29 22:51:10 +02:00
|
|
|
func TestGetContentsErrors(t *testing.T) {
|
2021-11-12 15:36:47 +01:00
|
|
|
unittest.PrepareTestEnv(t)
|
2024-08-26 08:03:48 +02:00
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
|
|
|
|
2019-06-29 22:51:10 +02:00
|
|
|
treePath := "README.md"
|
|
|
|
ref := repo.DefaultBranch
|
|
|
|
|
|
|
|
t.Run("bad treePath", func(t *testing.T) {
|
|
|
|
badTreePath := "bad/tree.md"
|
2024-08-26 08:03:48 +02:00
|
|
|
fileContentResponse, err := GetContents(db.DefaultContext, repo, badTreePath, ref, false)
|
2024-07-30 21:41:10 +02:00
|
|
|
require.EqualError(t, err, "object does not exist [id: , rel_path: bad]")
|
2019-06-29 22:51:10 +02:00
|
|
|
assert.Nil(t, fileContentResponse)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("bad ref", func(t *testing.T) {
|
|
|
|
badRef := "bad_ref"
|
2024-08-26 08:03:48 +02:00
|
|
|
fileContentResponse, err := GetContents(db.DefaultContext, repo, treePath, badRef, false)
|
2024-07-30 21:41:10 +02:00
|
|
|
require.EqualError(t, err, "object does not exist [id: "+badRef+", rel_path: ]")
|
2019-06-29 22:51:10 +02:00
|
|
|
assert.Nil(t, fileContentResponse)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetContentsOrListErrors(t *testing.T) {
|
2021-11-12 15:36:47 +01:00
|
|
|
unittest.PrepareTestEnv(t)
|
2024-08-26 08:03:48 +02:00
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
|
|
|
|
2019-04-17 18:06:35 +02:00
|
|
|
treePath := "README.md"
|
|
|
|
ref := repo.DefaultBranch
|
|
|
|
|
|
|
|
t.Run("bad treePath", func(t *testing.T) {
|
|
|
|
badTreePath := "bad/tree.md"
|
2024-08-26 08:03:48 +02:00
|
|
|
fileContentResponse, err := GetContentsOrList(db.DefaultContext, repo, badTreePath, ref)
|
2024-07-30 21:41:10 +02:00
|
|
|
require.EqualError(t, err, "object does not exist [id: , rel_path: bad]")
|
2019-04-17 18:06:35 +02:00
|
|
|
assert.Nil(t, fileContentResponse)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("bad ref", func(t *testing.T) {
|
|
|
|
badRef := "bad_ref"
|
2024-08-26 08:03:48 +02:00
|
|
|
fileContentResponse, err := GetContentsOrList(db.DefaultContext, repo, treePath, badRef)
|
2024-07-30 21:41:10 +02:00
|
|
|
require.EqualError(t, err, "object does not exist [id: "+badRef+", rel_path: ]")
|
2019-04-17 18:06:35 +02:00
|
|
|
assert.Nil(t, fileContentResponse)
|
|
|
|
})
|
|
|
|
}
|
2019-10-19 17:38:49 +02:00
|
|
|
|
|
|
|
func TestGetContentsOrListOfEmptyRepos(t *testing.T) {
|
2021-11-12 15:36:47 +01:00
|
|
|
unittest.PrepareTestEnv(t)
|
2024-08-26 08:03:48 +02:00
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 52})
|
2019-10-19 17:38:49 +02:00
|
|
|
|
|
|
|
t.Run("empty repo", func(t *testing.T) {
|
2024-08-26 08:03:48 +02:00
|
|
|
contents, err := GetContentsOrList(db.DefaultContext, repo, "", "")
|
2024-07-30 21:41:10 +02:00
|
|
|
require.NoError(t, err)
|
2019-10-19 17:38:49 +02:00
|
|
|
assert.Empty(t, contents)
|
|
|
|
})
|
|
|
|
}
|
2021-11-24 08:56:24 +01:00
|
|
|
|
|
|
|
func TestGetBlobBySHA(t *testing.T) {
|
|
|
|
unittest.PrepareTestEnv(t)
|
2024-08-26 08:03:48 +02:00
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
|
|
|
|
|
|
|
gitRepo, err := gitrepo.OpenRepository(db.DefaultContext, repo)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer gitRepo.Close()
|
2022-04-21 17:17:57 +02:00
|
|
|
|
2024-08-26 08:03:48 +02:00
|
|
|
gbr, err := GetBlobBySHA(db.DefaultContext, repo, gitRepo, "65f1bf27bc3bf70f64657658635e66094edbcb4d")
|
2021-11-24 08:56:24 +01:00
|
|
|
expectedGBR := &api.GitBlobResponse{
|
|
|
|
Content: "dHJlZSAyYTJmMWQ0NjcwNzI4YTJlMTAwNDllMzQ1YmQ3YTI3NjQ2OGJlYWI2CmF1dGhvciB1c2VyMSA8YWRkcmVzczFAZXhhbXBsZS5jb20+IDE0ODk5NTY0NzkgLTA0MDAKY29tbWl0dGVyIEV0aGFuIEtvZW5pZyA8ZXRoYW50a29lbmlnQGdtYWlsLmNvbT4gMTQ4OTk1NjQ3OSAtMDQwMAoKSW5pdGlhbCBjb21taXQK",
|
|
|
|
Encoding: "base64",
|
|
|
|
URL: "https://try.gitea.io/api/v1/repos/user2/repo1/git/blobs/65f1bf27bc3bf70f64657658635e66094edbcb4d",
|
|
|
|
SHA: "65f1bf27bc3bf70f64657658635e66094edbcb4d",
|
|
|
|
Size: 180,
|
|
|
|
}
|
2024-07-30 21:41:10 +02:00
|
|
|
require.NoError(t, err)
|
2021-11-24 08:56:24 +01:00
|
|
|
assert.Equal(t, expectedGBR, gbr)
|
|
|
|
}
|