@@ -6,12 +6,16 @@ package integration
6
6
import (
7
7
"fmt"
8
8
"net/http"
9
+ "strconv"
9
10
"testing"
10
11
11
12
repo_model "code.gitea.io/gitea/models/repo"
12
13
"code.gitea.io/gitea/models/unittest"
13
14
user_model "code.gitea.io/gitea/models/user"
15
+ "code.gitea.io/gitea/modules/git"
14
16
"code.gitea.io/gitea/tests"
17
+
18
+ "github.com/stretchr/testify/assert"
15
19
)
16
20
17
21
func TestChangeDefaultBranch(t *testing.T) {
@@ -38,3 +42,96 @@ func TestChangeDefaultBranch(t *testing.T) {
38
42
})
39
43
session.MakeRequest(t, req, http.StatusNotFound)
40
44
}
45
+
46
+ func checkDivergence(t *testing.T, session *TestSession, branchesURL, expectedDefaultBranch string, expectedBranchToDivergence map[string]git.DivergeObject) {
47
+ req := NewRequest(t, "GET", branchesURL)
48
+ resp := session.MakeRequest(t, req, http.StatusOK)
49
+
50
+ htmlDoc := NewHTMLParser(t, resp.Body)
51
+
52
+ branchNodes := htmlDoc.doc.Find(".branch-name").Nodes
53
+ branchNames := []string{}
54
+ for _, node := range branchNodes {
55
+ branchNames = append(branchNames, node.FirstChild.Data)
56
+ }
57
+
58
+ expectBranchCount := len(expectedBranchToDivergence)
59
+
60
+ assert.Len(t, branchNames, expectBranchCount+1)
61
+ assert.Equal(t, expectedDefaultBranch, branchNames[0])
62
+
63
+ allCountBehindNodes := htmlDoc.doc.Find(".count-behind").Nodes
64
+ allCountAheadNodes := htmlDoc.doc.Find(".count-ahead").Nodes
65
+
66
+ assert.Len(t, allCountAheadNodes, expectBranchCount)
67
+ assert.Len(t, allCountBehindNodes, expectBranchCount)
68
+
69
+ for i := range expectBranchCount {
70
+ branchName := branchNames[i+1]
71
+ assert.Contains(t, expectedBranchToDivergence, branchName)
72
+
73
+ expectedCountAhead := expectedBranchToDivergence[branchName].Ahead
74
+ expectedCountBehind := expectedBranchToDivergence[branchName].Behind
75
+ countAhead, err := strconv.Atoi(allCountAheadNodes[i].FirstChild.Data)
76
+ assert.NoError(t, err)
77
+ countBehind, err := strconv.Atoi(allCountBehindNodes[i].FirstChild.Data)
78
+ assert.NoError(t, err)
79
+
80
+ assert.Equal(t, expectedCountAhead, countAhead)
81
+ assert.Equal(t, expectedCountBehind, countBehind)
82
+ }
83
+ }
84
+
85
+ func TestChangeDefaultBranchDivergence(t *testing.T) {
86
+ defer tests.PrepareTestEnv(t)()
87
+ repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 16})
88
+ owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
89
+
90
+ session := loginUser(t, owner.Name)
91
+ branchesURL := fmt.Sprintf("/%s/%s/branches", owner.Name, repo.Name)
92
+ settingsBranchesURL := fmt.Sprintf("/%s/%s/settings/branches", owner.Name, repo.Name)
93
+
94
+ // check branch divergence before switching default branch
95
+ expectedBranchToDivergenceBefore := map[string]git.DivergeObject{
96
+ "not-signed": {
97
+ Ahead: 0,
98
+ Behind: 0,
99
+ },
100
+ "good-sign-not-yet-validated": {
101
+ Ahead: 0,
102
+ Behind: 1,
103
+ },
104
+ "good-sign": {
105
+ Ahead: 1,
106
+ Behind: 3,
107
+ },
108
+ }
109
+ checkDivergence(t, session, branchesURL, "master", expectedBranchToDivergenceBefore)
110
+
111
+ // switch default branch
112
+ newDefaultBranch := "good-sign-not-yet-validated"
113
+ csrf := GetUserCSRFToken(t, session)
114
+ req := NewRequestWithValues(t, "POST", settingsBranchesURL, map[string]string{
115
+ "_csrf": csrf,
116
+ "action": "default_branch",
117
+ "branch": newDefaultBranch,
118
+ })
119
+ session.MakeRequest(t, req, http.StatusSeeOther)
120
+
121
+ // check branch divergence after switching default branch
122
+ expectedBranchToDivergenceAfter := map[string]git.DivergeObject{
123
+ "master": {
124
+ Ahead: 1,
125
+ Behind: 0,
126
+ },
127
+ "not-signed": {
128
+ Ahead: 1,
129
+ Behind: 0,
130
+ },
131
+ "good-sign": {
132
+ Ahead: 1,
133
+ Behind: 2,
134
+ },
135
+ }
136
+ checkDivergence(t, session, branchesURL, newDefaultBranch, expectedBranchToDivergenceAfter)
137
+ }
0 commit comments