@@ -7,10 +7,13 @@ import (
77 "testing"
88
99 repo_model "code.gitea.io/gitea/models/repo"
10+ "code.gitea.io/gitea/models/unit"
1011 "code.gitea.io/gitea/models/unittest"
1112 "code.gitea.io/gitea/modules/contexttest"
1213 "code.gitea.io/gitea/modules/web"
1314 "code.gitea.io/gitea/services/forms"
15+
16+ "github.com/stretchr/testify/assert"
1417)
1518
1619func TestNewReleasePost (t * testing.T ) {
@@ -62,3 +65,57 @@ func TestNewReleasePost(t *testing.T) {
6265 ctx .Repo .GitRepo .Close ()
6366 }
6467}
68+
69+ func TestCalReleaseNumCommitsBehind (t * testing.T ) {
70+ unittest .PrepareTestEnv (t )
71+ ctx , _ := contexttest .MockContext (t , "user2/repo-release/releases" )
72+ contexttest .LoadUser (t , ctx , 2 )
73+ contexttest .LoadRepo (t , ctx , 57 )
74+ contexttest .LoadGitRepo (t , ctx )
75+ t .Cleanup (func () { ctx .Repo .GitRepo .Close () })
76+
77+ releases , err := repo_model .GetReleasesByRepoID (ctx , ctx .Repo .Repository .ID , repo_model.FindReleasesOptions {
78+ IncludeDrafts : ctx .Repo .CanWrite (unit .TypeReleases ),
79+ })
80+ assert .NoError (t , err )
81+
82+ countCache := make (map [string ]int64 )
83+ for _ , release := range releases {
84+ err := calReleaseNumCommitsBehind (ctx .Repo , release , countCache )
85+ assert .NoError (t , err )
86+ }
87+
88+ type computedFields struct {
89+ NumCommitsBehind int64
90+ TargetBehind string
91+ }
92+ expectedComputation := map [string ]computedFields {
93+ "v1.0" : {
94+ NumCommitsBehind : 3 ,
95+ TargetBehind : "main" ,
96+ },
97+ "v1.1" : {
98+ NumCommitsBehind : 1 ,
99+ TargetBehind : "main" ,
100+ },
101+ "v2.0" : {
102+ NumCommitsBehind : 0 ,
103+ TargetBehind : "main" ,
104+ },
105+ "non-existing-target-branch" : {
106+ NumCommitsBehind : 1 ,
107+ TargetBehind : "main" ,
108+ },
109+ "empty-target-branch" : {
110+ NumCommitsBehind : 1 ,
111+ TargetBehind : "main" ,
112+ },
113+ }
114+ for _ , r := range releases {
115+ actual := computedFields {
116+ NumCommitsBehind : r .NumCommitsBehind ,
117+ TargetBehind : r .TargetBehind ,
118+ }
119+ assert .Equal (t , expectedComputation [r .TagName ], actual , "wrong computed fields for %s: %#v" , r .TagName , r )
120+ }
121+ }
0 commit comments