mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-05-31 11:52:10 +00:00
[TESTS] Lift out CreateDeclarativeRepo()
There are a number of tests which require creating a repository on the fly, and they each do it their own way. To reduce code duplication, lift out this common pattern into a helper called `CreateDeclarativeRepo()`, which lets us create a repository, set up enabled and disabled repo units, and even add, delete, or update files. Also convert a number of users of this pattern to the new helper - those users that I introduced, and are in code introduced by Forgejo in the first place. Signed-off-by: Gergely Nagy <forgejo@gergo.csillger.hu> (cherry picked from commit342b7bae3c
) (cherry picked from commit2ece8764e9
)
This commit is contained in:
parent
403bb4bf48
commit
48d752c630
5 changed files with 284 additions and 406 deletions
|
@ -10,228 +10,181 @@ import (
|
|||
"net/url"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
actions_model "code.gitea.io/gitea/models/actions"
|
||||
"code.gitea.io/gitea/models/db"
|
||||
repo_model "code.gitea.io/gitea/models/repo"
|
||||
unit_model "code.gitea.io/gitea/models/unit"
|
||||
"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/test"
|
||||
repo_service "code.gitea.io/gitea/services/repository"
|
||||
files_service "code.gitea.io/gitea/services/repository/files"
|
||||
"code.gitea.io/gitea/tests"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func assertBadge(t *testing.T, resp *httptest.ResponseRecorder, badge string) {
|
||||
assert.Equal(t, fmt.Sprintf("https://img.shields.io/badge/%s", badge), test.RedirectURL(resp))
|
||||
}
|
||||
|
||||
func createMinimalRepo(t *testing.T) func() {
|
||||
user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
||||
|
||||
// Create a new repository
|
||||
repo, err := repo_service.CreateRepository(db.DefaultContext, user2, user2, repo_service.CreateRepoOptions{
|
||||
Name: "minimal",
|
||||
Description: "minimal repo for badge testing",
|
||||
AutoInit: true,
|
||||
Gitignores: "Go",
|
||||
License: "MIT",
|
||||
Readme: "Default",
|
||||
DefaultBranch: "main",
|
||||
IsPrivate: false,
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
assert.NotEmpty(t, repo)
|
||||
|
||||
// Enable Actions, and disable Issues, PRs and Releases
|
||||
err = repo_service.UpdateRepositoryUnits(db.DefaultContext, repo, []repo_model.RepoUnit{{
|
||||
RepoID: repo.ID,
|
||||
Type: unit_model.TypeActions,
|
||||
}}, []unit_model.Type{unit_model.TypeIssues, unit_model.TypePullRequests, unit_model.TypeReleases})
|
||||
assert.NoError(t, err)
|
||||
|
||||
return func() {
|
||||
repo_service.DeleteRepository(db.DefaultContext, user2, repo, false)
|
||||
}
|
||||
}
|
||||
|
||||
func addWorkflow(t *testing.T) {
|
||||
user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
||||
repo, err := repo_model.GetRepositoryByOwnerAndName(db.DefaultContext, "user2", "minimal")
|
||||
assert.NoError(t, err)
|
||||
|
||||
// Add a workflow file to the repo
|
||||
addWorkflowToBaseResp, err := files_service.ChangeRepoFiles(git.DefaultContext, repo, user2, &files_service.ChangeRepoFilesOptions{
|
||||
Files: []*files_service.ChangeRepoFile{
|
||||
{
|
||||
Operation: "create",
|
||||
TreePath: ".gitea/workflows/pr.yml",
|
||||
ContentReader: strings.NewReader("name: test\non:\n push:\njobs:\n test:\n runs-on: ubuntu-latest\n steps:\n - run: echo helloworld\n"),
|
||||
},
|
||||
},
|
||||
Message: "add workflow",
|
||||
OldBranch: "main",
|
||||
NewBranch: "main",
|
||||
Author: &files_service.IdentityOptions{
|
||||
Name: user2.Name,
|
||||
Email: user2.Email,
|
||||
},
|
||||
Committer: &files_service.IdentityOptions{
|
||||
Name: user2.Name,
|
||||
Email: user2.Email,
|
||||
},
|
||||
Dates: &files_service.CommitDateOptions{
|
||||
Author: time.Now(),
|
||||
Committer: time.Now(),
|
||||
},
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
assert.NotEmpty(t, addWorkflowToBaseResp)
|
||||
|
||||
assert.Equal(t, 1, unittest.GetCount(t, &actions_model.ActionRun{RepoID: repo.ID}))
|
||||
}
|
||||
|
||||
func TestWorkflowBadges(t *testing.T) {
|
||||
onGiteaRun(t, func(t *testing.T, u *url.URL) {
|
||||
defer tests.PrintCurrentTest(t)()
|
||||
defer createMinimalRepo(t)()
|
||||
|
||||
addWorkflow(t)
|
||||
|
||||
// Actions disabled
|
||||
req := NewRequest(t, "GET", "/user2/repo1/badges/workflows/test.yaml/badge.svg")
|
||||
resp := MakeRequest(t, req, http.StatusSeeOther)
|
||||
assertBadge(t, resp, "test.yaml-Not%20found-crimson")
|
||||
|
||||
req = NewRequest(t, "GET", "/user2/repo1/badges/workflows/test.yaml/badge.svg?branch=no-such-branch")
|
||||
resp = MakeRequest(t, req, http.StatusSeeOther)
|
||||
assertBadge(t, resp, "test.yaml-Not%20found-crimson")
|
||||
|
||||
// Actions enabled
|
||||
req = NewRequest(t, "GET", "/user2/minimal/badges/workflows/pr.yml/badge.svg")
|
||||
resp = MakeRequest(t, req, http.StatusSeeOther)
|
||||
assertBadge(t, resp, "pr.yml-waiting-lightgrey")
|
||||
|
||||
req = NewRequest(t, "GET", "/user2/minimal/badges/workflows/pr.yml/badge.svg?branch=main")
|
||||
resp = MakeRequest(t, req, http.StatusSeeOther)
|
||||
assertBadge(t, resp, "pr.yml-waiting-lightgrey")
|
||||
|
||||
req = NewRequest(t, "GET", "/user2/minimal/badges/workflows/pr.yml/badge.svg?branch=no-such-branch")
|
||||
resp = MakeRequest(t, req, http.StatusSeeOther)
|
||||
assertBadge(t, resp, "pr.yml-Not%20found-crimson")
|
||||
|
||||
req = NewRequest(t, "GET", "/user2/minimal/badges/workflows/pr.yml/badge.svg?event=cron")
|
||||
resp = MakeRequest(t, req, http.StatusSeeOther)
|
||||
assertBadge(t, resp, "pr.yml-Not%20found-crimson")
|
||||
|
||||
// GitHub compatibility
|
||||
req = NewRequest(t, "GET", "/user2/minimal/actions/workflows/pr.yml/badge.svg")
|
||||
resp = MakeRequest(t, req, http.StatusSeeOther)
|
||||
assertBadge(t, resp, "pr.yml-waiting-lightgrey")
|
||||
|
||||
req = NewRequest(t, "GET", "/user2/minimal/actions/workflows/pr.yml/badge.svg?branch=main")
|
||||
resp = MakeRequest(t, req, http.StatusSeeOther)
|
||||
assertBadge(t, resp, "pr.yml-waiting-lightgrey")
|
||||
|
||||
req = NewRequest(t, "GET", "/user2/minimal/actions/workflows/pr.yml/badge.svg?branch=no-such-branch")
|
||||
resp = MakeRequest(t, req, http.StatusSeeOther)
|
||||
assertBadge(t, resp, "pr.yml-Not%20found-crimson")
|
||||
|
||||
req = NewRequest(t, "GET", "/user2/minimal/actions/workflows/pr.yml/badge.svg?event=cron")
|
||||
resp = MakeRequest(t, req, http.StatusSeeOther)
|
||||
assertBadge(t, resp, "pr.yml-Not%20found-crimson")
|
||||
})
|
||||
}
|
||||
|
||||
func TestBadges(t *testing.T) {
|
||||
defer tests.PrepareTestEnv(t)()
|
||||
onGiteaRun(t, func(t *testing.T, u *url.URL) {
|
||||
prep := func(t *testing.T) (*repo_model.Repository, func()) {
|
||||
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
||||
|
||||
t.Run("Stars", func(t *testing.T) {
|
||||
defer tests.PrintCurrentTest(t)()
|
||||
repo, _, f := CreateDeclarativeRepo(t, owner, "",
|
||||
[]unit_model.Type{unit_model.TypeActions},
|
||||
[]unit_model.Type{unit_model.TypeIssues, unit_model.TypePullRequests, unit_model.TypeReleases},
|
||||
[]*files_service.ChangeRepoFile{
|
||||
{
|
||||
Operation: "create",
|
||||
TreePath: ".gitea/workflows/pr.yml",
|
||||
ContentReader: strings.NewReader("name: test\non:\n push:\njobs:\n test:\n runs-on: ubuntu-latest\n steps:\n - run: echo helloworld\n"),
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
req := NewRequest(t, "GET", "/user2/repo1/badges/stars.svg")
|
||||
resp := MakeRequest(t, req, http.StatusSeeOther)
|
||||
assert.Equal(t, 1, unittest.GetCount(t, &actions_model.ActionRun{RepoID: repo.ID}))
|
||||
|
||||
assertBadge(t, resp, "stars-0-blue")
|
||||
})
|
||||
return repo, f
|
||||
}
|
||||
|
||||
t.Run("Issues", func(t *testing.T) {
|
||||
defer tests.PrintCurrentTest(t)()
|
||||
defer createMinimalRepo(t)()
|
||||
assertBadge := func(t *testing.T, resp *httptest.ResponseRecorder, badge string) {
|
||||
t.Helper()
|
||||
|
||||
// Issues enabled
|
||||
req := NewRequest(t, "GET", "/user2/repo1/badges/issues.svg")
|
||||
resp := MakeRequest(t, req, http.StatusSeeOther)
|
||||
assertBadge(t, resp, "issues-2-blue")
|
||||
assert.Equal(t, fmt.Sprintf("https://img.shields.io/badge/%s", badge), test.RedirectURL(resp))
|
||||
}
|
||||
|
||||
req = NewRequest(t, "GET", "/user2/repo1/badges/issues/open.svg")
|
||||
resp = MakeRequest(t, req, http.StatusSeeOther)
|
||||
assertBadge(t, resp, "issues-1%20open-blue")
|
||||
t.Run("Workflows", func(t *testing.T) {
|
||||
defer tests.PrintCurrentTest(t)()
|
||||
repo, f := prep(t)
|
||||
defer f()
|
||||
|
||||
req = NewRequest(t, "GET", "/user2/repo1/badges/issues/closed.svg")
|
||||
resp = MakeRequest(t, req, http.StatusSeeOther)
|
||||
assertBadge(t, resp, "issues-1%20closed-blue")
|
||||
// Actions disabled
|
||||
req := NewRequest(t, "GET", "/user2/repo1/badges/workflows/test.yaml/badge.svg")
|
||||
resp := MakeRequest(t, req, http.StatusSeeOther)
|
||||
assertBadge(t, resp, "test.yaml-Not%20found-crimson")
|
||||
|
||||
// Issues disabled
|
||||
req = NewRequest(t, "GET", "/user2/minimal/badges/issues.svg")
|
||||
resp = MakeRequest(t, req, http.StatusSeeOther)
|
||||
assertBadge(t, resp, "issues-Not%20found-crimson")
|
||||
req = NewRequest(t, "GET", "/user2/repo1/badges/workflows/test.yaml/badge.svg?branch=no-such-branch")
|
||||
resp = MakeRequest(t, req, http.StatusSeeOther)
|
||||
assertBadge(t, resp, "test.yaml-Not%20found-crimson")
|
||||
|
||||
req = NewRequest(t, "GET", "/user2/minimal/badges/issues/open.svg")
|
||||
resp = MakeRequest(t, req, http.StatusSeeOther)
|
||||
assertBadge(t, resp, "issues-Not%20found-crimson")
|
||||
// Actions enabled
|
||||
req = NewRequestf(t, "GET", "/user2/%s/badges/workflows/pr.yml/badge.svg", repo.Name)
|
||||
resp = MakeRequest(t, req, http.StatusSeeOther)
|
||||
assertBadge(t, resp, "pr.yml-waiting-lightgrey")
|
||||
|
||||
req = NewRequest(t, "GET", "/user2/minimal/badges/issues/closed.svg")
|
||||
resp = MakeRequest(t, req, http.StatusSeeOther)
|
||||
assertBadge(t, resp, "issues-Not%20found-crimson")
|
||||
})
|
||||
req = NewRequestf(t, "GET", "/user2/%s/badges/workflows/pr.yml/badge.svg?branch=main", repo.Name)
|
||||
resp = MakeRequest(t, req, http.StatusSeeOther)
|
||||
assertBadge(t, resp, "pr.yml-waiting-lightgrey")
|
||||
|
||||
t.Run("Pulls", func(t *testing.T) {
|
||||
defer tests.PrintCurrentTest(t)()
|
||||
defer createMinimalRepo(t)()
|
||||
req = NewRequestf(t, "GET", "/user2/%s/badges/workflows/pr.yml/badge.svg?branch=no-such-branch", repo.Name)
|
||||
resp = MakeRequest(t, req, http.StatusSeeOther)
|
||||
assertBadge(t, resp, "pr.yml-Not%20found-crimson")
|
||||
|
||||
// Pull requests enabled
|
||||
req := NewRequest(t, "GET", "/user2/repo1/badges/pulls.svg")
|
||||
resp := MakeRequest(t, req, http.StatusSeeOther)
|
||||
assertBadge(t, resp, "pulls-3-blue")
|
||||
req = NewRequestf(t, "GET", "/user2/%s/badges/workflows/pr.yml/badge.svg?event=cron", repo.Name)
|
||||
resp = MakeRequest(t, req, http.StatusSeeOther)
|
||||
assertBadge(t, resp, "pr.yml-Not%20found-crimson")
|
||||
|
||||
req = NewRequest(t, "GET", "/user2/repo1/badges/pulls/open.svg")
|
||||
resp = MakeRequest(t, req, http.StatusSeeOther)
|
||||
assertBadge(t, resp, "pulls-3%20open-blue")
|
||||
// GitHub compatibility
|
||||
req = NewRequestf(t, "GET", "/user2/%s/actions/workflows/pr.yml/badge.svg", repo.Name)
|
||||
resp = MakeRequest(t, req, http.StatusSeeOther)
|
||||
assertBadge(t, resp, "pr.yml-waiting-lightgrey")
|
||||
|
||||
req = NewRequest(t, "GET", "/user2/repo1/badges/pulls/closed.svg")
|
||||
resp = MakeRequest(t, req, http.StatusSeeOther)
|
||||
assertBadge(t, resp, "pulls-0%20closed-blue")
|
||||
req = NewRequestf(t, "GET", "/user2/%s/actions/workflows/pr.yml/badge.svg?branch=main", repo.Name)
|
||||
resp = MakeRequest(t, req, http.StatusSeeOther)
|
||||
assertBadge(t, resp, "pr.yml-waiting-lightgrey")
|
||||
|
||||
// Pull requests disabled
|
||||
req = NewRequest(t, "GET", "/user2/minimal/badges/pulls.svg")
|
||||
resp = MakeRequest(t, req, http.StatusSeeOther)
|
||||
assertBadge(t, resp, "pulls-Not%20found-crimson")
|
||||
req = NewRequestf(t, "GET", "/user2/%s/actions/workflows/pr.yml/badge.svg?branch=no-such-branch", repo.Name)
|
||||
resp = MakeRequest(t, req, http.StatusSeeOther)
|
||||
assertBadge(t, resp, "pr.yml-Not%20found-crimson")
|
||||
|
||||
req = NewRequest(t, "GET", "/user2/minimal/badges/pulls/open.svg")
|
||||
resp = MakeRequest(t, req, http.StatusSeeOther)
|
||||
assertBadge(t, resp, "pulls-Not%20found-crimson")
|
||||
req = NewRequestf(t, "GET", "/user2/%s/actions/workflows/pr.yml/badge.svg?event=cron", repo.Name)
|
||||
resp = MakeRequest(t, req, http.StatusSeeOther)
|
||||
assertBadge(t, resp, "pr.yml-Not%20found-crimson")
|
||||
})
|
||||
|
||||
req = NewRequest(t, "GET", "/user2/minimal/badges/pulls/closed.svg")
|
||||
resp = MakeRequest(t, req, http.StatusSeeOther)
|
||||
assertBadge(t, resp, "pulls-Not%20found-crimson")
|
||||
})
|
||||
t.Run("Stars", func(t *testing.T) {
|
||||
defer tests.PrintCurrentTest(t)()
|
||||
|
||||
t.Run("Release", func(t *testing.T) {
|
||||
defer tests.PrintCurrentTest(t)()
|
||||
defer createMinimalRepo(t)()
|
||||
req := NewRequest(t, "GET", "/user2/repo1/badges/stars.svg")
|
||||
resp := MakeRequest(t, req, http.StatusSeeOther)
|
||||
|
||||
req := NewRequest(t, "GET", "/user2/repo1/badges/release.svg")
|
||||
resp := MakeRequest(t, req, http.StatusSeeOther)
|
||||
assertBadge(t, resp, "release-v1.1-blue")
|
||||
assertBadge(t, resp, "stars-0-blue")
|
||||
})
|
||||
|
||||
req = NewRequest(t, "GET", "/user2/minimal/badges/release.svg")
|
||||
resp = MakeRequest(t, req, http.StatusSeeOther)
|
||||
assertBadge(t, resp, "release-Not%20found-crimson")
|
||||
t.Run("Issues", func(t *testing.T) {
|
||||
defer tests.PrintCurrentTest(t)()
|
||||
repo, f := prep(t)
|
||||
defer f()
|
||||
|
||||
// Issues enabled
|
||||
req := NewRequest(t, "GET", "/user2/repo1/badges/issues.svg")
|
||||
resp := MakeRequest(t, req, http.StatusSeeOther)
|
||||
assertBadge(t, resp, "issues-2-blue")
|
||||
|
||||
req = NewRequest(t, "GET", "/user2/repo1/badges/issues/open.svg")
|
||||
resp = MakeRequest(t, req, http.StatusSeeOther)
|
||||
assertBadge(t, resp, "issues-1%20open-blue")
|
||||
|
||||
req = NewRequest(t, "GET", "/user2/repo1/badges/issues/closed.svg")
|
||||
resp = MakeRequest(t, req, http.StatusSeeOther)
|
||||
assertBadge(t, resp, "issues-1%20closed-blue")
|
||||
|
||||
// Issues disabled
|
||||
req = NewRequestf(t, "GET", "/user2/%s/badges/issues.svg", repo.Name)
|
||||
resp = MakeRequest(t, req, http.StatusSeeOther)
|
||||
assertBadge(t, resp, "issues-Not%20found-crimson")
|
||||
|
||||
req = NewRequestf(t, "GET", "/user2/%s/badges/issues/open.svg", repo.Name)
|
||||
resp = MakeRequest(t, req, http.StatusSeeOther)
|
||||
assertBadge(t, resp, "issues-Not%20found-crimson")
|
||||
|
||||
req = NewRequestf(t, "GET", "/user2/%s/badges/issues/closed.svg", repo.Name)
|
||||
resp = MakeRequest(t, req, http.StatusSeeOther)
|
||||
assertBadge(t, resp, "issues-Not%20found-crimson")
|
||||
})
|
||||
|
||||
t.Run("Pulls", func(t *testing.T) {
|
||||
defer tests.PrintCurrentTest(t)()
|
||||
repo, f := prep(t)
|
||||
defer f()
|
||||
|
||||
// Pull requests enabled
|
||||
req := NewRequest(t, "GET", "/user2/repo1/badges/pulls.svg")
|
||||
resp := MakeRequest(t, req, http.StatusSeeOther)
|
||||
assertBadge(t, resp, "pulls-3-blue")
|
||||
|
||||
req = NewRequest(t, "GET", "/user2/repo1/badges/pulls/open.svg")
|
||||
resp = MakeRequest(t, req, http.StatusSeeOther)
|
||||
assertBadge(t, resp, "pulls-3%20open-blue")
|
||||
|
||||
req = NewRequest(t, "GET", "/user2/repo1/badges/pulls/closed.svg")
|
||||
resp = MakeRequest(t, req, http.StatusSeeOther)
|
||||
assertBadge(t, resp, "pulls-0%20closed-blue")
|
||||
|
||||
// Pull requests disabled
|
||||
req = NewRequestf(t, "GET", "/user2/%s/badges/pulls.svg", repo.Name)
|
||||
resp = MakeRequest(t, req, http.StatusSeeOther)
|
||||
assertBadge(t, resp, "pulls-Not%20found-crimson")
|
||||
|
||||
req = NewRequestf(t, "GET", "/user2/%s/badges/pulls/open.svg", repo.Name)
|
||||
resp = MakeRequest(t, req, http.StatusSeeOther)
|
||||
assertBadge(t, resp, "pulls-Not%20found-crimson")
|
||||
|
||||
req = NewRequestf(t, "GET", "/user2/%s/badges/pulls/closed.svg", repo.Name)
|
||||
resp = MakeRequest(t, req, http.StatusSeeOther)
|
||||
assertBadge(t, resp, "pulls-Not%20found-crimson")
|
||||
})
|
||||
|
||||
t.Run("Release", func(t *testing.T) {
|
||||
defer tests.PrintCurrentTest(t)()
|
||||
repo, f := prep(t)
|
||||
defer f()
|
||||
|
||||
req := NewRequest(t, "GET", "/user2/repo1/badges/release.svg")
|
||||
resp := MakeRequest(t, req, http.StatusSeeOther)
|
||||
assertBadge(t, resp, "release-v1.1-blue")
|
||||
|
||||
req = NewRequestf(t, "GET", "/user2/%s/badges/release.svg", repo.Name)
|
||||
resp = MakeRequest(t, req, http.StatusSeeOther)
|
||||
assertBadge(t, resp, "release-Not%20found-crimson")
|
||||
})
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue