2017-12-08 05:22:02 +00:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
2022-11-27 18:20:29 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2017-12-08 05:22:02 +00:00
|
|
|
|
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2024-01-15 02:19:25 +00:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-08-25 02:31:57 +00:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2023-09-28 13:21:47 +00:00
|
|
|
"code.gitea.io/gitea/models/unit"
|
2021-11-12 14:36:47 +00:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2021-01-26 15:36:53 +00:00
|
|
|
"code.gitea.io/gitea/modules/web"
|
2024-02-27 07:12:22 +00:00
|
|
|
"code.gitea.io/gitea/services/contexttest"
|
2021-04-06 19:44:05 +00:00
|
|
|
"code.gitea.io/gitea/services/forms"
|
2023-05-10 03:43:55 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2024-07-30 19:41:10 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2017-12-08 05:22:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewReleasePost(t *testing.T) {
|
|
|
|
for _, testCase := range []struct {
|
|
|
|
RepoID int64
|
|
|
|
UserID int64
|
|
|
|
TagName string
|
2021-04-06 19:44:05 +00:00
|
|
|
Form forms.NewReleaseForm
|
2017-12-08 05:22:02 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
RepoID: 1,
|
|
|
|
UserID: 2,
|
|
|
|
TagName: "v1.1", // pre-existing tag
|
2021-04-06 19:44:05 +00:00
|
|
|
Form: forms.NewReleaseForm{
|
2017-12-08 05:22:02 +00:00
|
|
|
TagName: "newtag",
|
|
|
|
Target: "master",
|
|
|
|
Title: "title",
|
|
|
|
Content: "content",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
RepoID: 1,
|
|
|
|
UserID: 2,
|
|
|
|
TagName: "newtag",
|
2021-04-06 19:44:05 +00:00
|
|
|
Form: forms.NewReleaseForm{
|
2017-12-08 05:22:02 +00:00
|
|
|
TagName: "newtag",
|
|
|
|
Target: "master",
|
|
|
|
Title: "title",
|
|
|
|
Content: "content",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
} {
|
2021-11-12 14:36:47 +00:00
|
|
|
unittest.PrepareTestEnv(t)
|
2017-12-08 05:22:02 +00:00
|
|
|
|
2023-09-01 11:26:07 +00:00
|
|
|
ctx, _ := contexttest.MockContext(t, "user2/repo1/releases/new")
|
|
|
|
contexttest.LoadUser(t, ctx, 2)
|
|
|
|
contexttest.LoadRepo(t, ctx, 1)
|
|
|
|
contexttest.LoadGitRepo(t, ctx)
|
2021-01-26 15:36:53 +00:00
|
|
|
web.SetForm(ctx, &testCase.Form)
|
|
|
|
NewReleasePost(ctx)
|
2022-08-25 02:31:57 +00:00
|
|
|
unittest.AssertExistsAndLoadBean(t, &repo_model.Release{
|
2017-12-08 05:22:02 +00:00
|
|
|
RepoID: 1,
|
|
|
|
PublisherID: 2,
|
|
|
|
TagName: testCase.Form.TagName,
|
|
|
|
Target: testCase.Form.Target,
|
|
|
|
Title: testCase.Form.Title,
|
|
|
|
Note: testCase.Form.Content,
|
2021-11-16 08:53:21 +00:00
|
|
|
}, unittest.Cond("is_draft=?", len(testCase.Form.Draft) > 0))
|
2019-11-13 07:01:19 +00:00
|
|
|
ctx.Repo.GitRepo.Close()
|
2017-12-08 05:22:02 +00:00
|
|
|
}
|
|
|
|
}
|
2023-05-10 03:43:55 +00:00
|
|
|
|
2023-09-28 13:21:47 +00:00
|
|
|
func TestCalReleaseNumCommitsBehind(t *testing.T) {
|
2023-05-10 03:43:55 +00:00
|
|
|
unittest.PrepareTestEnv(t)
|
2023-09-01 11:26:07 +00:00
|
|
|
ctx, _ := contexttest.MockContext(t, "user2/repo-release/releases")
|
|
|
|
contexttest.LoadUser(t, ctx, 2)
|
|
|
|
contexttest.LoadRepo(t, ctx, 57)
|
|
|
|
contexttest.LoadGitRepo(t, ctx)
|
2023-05-10 03:43:55 +00:00
|
|
|
t.Cleanup(func() { ctx.Repo.GitRepo.Close() })
|
|
|
|
|
2024-01-15 02:19:25 +00:00
|
|
|
releases, err := db.Find[repo_model.Release](ctx, repo_model.FindReleasesOptions{
|
2023-09-28 13:21:47 +00:00
|
|
|
IncludeDrafts: ctx.Repo.CanWrite(unit.TypeReleases),
|
2024-01-15 02:19:25 +00:00
|
|
|
RepoID: ctx.Repo.Repository.ID,
|
2023-09-28 13:21:47 +00:00
|
|
|
})
|
2024-07-30 19:41:10 +00:00
|
|
|
require.NoError(t, err)
|
2023-09-28 13:21:47 +00:00
|
|
|
|
|
|
|
countCache := make(map[string]int64)
|
|
|
|
for _, release := range releases {
|
|
|
|
err := calReleaseNumCommitsBehind(ctx.Repo, release, countCache)
|
2024-07-30 19:41:10 +00:00
|
|
|
require.NoError(t, err)
|
2023-09-28 13:21:47 +00:00
|
|
|
}
|
|
|
|
|
2023-05-10 03:43:55 +00:00
|
|
|
type computedFields struct {
|
|
|
|
NumCommitsBehind int64
|
|
|
|
TargetBehind string
|
|
|
|
}
|
|
|
|
expectedComputation := map[string]computedFields{
|
|
|
|
"v1.0": {
|
|
|
|
NumCommitsBehind: 3,
|
|
|
|
TargetBehind: "main",
|
|
|
|
},
|
|
|
|
"v1.1": {
|
|
|
|
NumCommitsBehind: 1,
|
|
|
|
TargetBehind: "main",
|
|
|
|
},
|
|
|
|
"v2.0": {
|
|
|
|
NumCommitsBehind: 0,
|
|
|
|
TargetBehind: "main",
|
|
|
|
},
|
|
|
|
"non-existing-target-branch": {
|
|
|
|
NumCommitsBehind: 1,
|
|
|
|
TargetBehind: "main",
|
|
|
|
},
|
|
|
|
"empty-target-branch": {
|
|
|
|
NumCommitsBehind: 1,
|
|
|
|
TargetBehind: "main",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, r := range releases {
|
|
|
|
actual := computedFields{
|
|
|
|
NumCommitsBehind: r.NumCommitsBehind,
|
|
|
|
TargetBehind: r.TargetBehind,
|
|
|
|
}
|
|
|
|
assert.Equal(t, expectedComputation[r.TagName], actual, "wrong computed fields for %s: %#v", r.TagName, r)
|
|
|
|
}
|
|
|
|
}
|