Skip to content

Commit fb4438a

Browse files
authored
Improve git test (#7086)
* Ensure that the lfs files are created with a different prefix * Reduce the replication in git_test.go
1 parent de6ef14 commit fb4438a

File tree

3 files changed

+280
-224
lines changed

3 files changed

+280
-224
lines changed

integrations/api_helper_for_declarative_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@
55
package integrations
66

77
import (
8+
"encoding/json"
89
"fmt"
910
"io/ioutil"
1011
"net/http"
1112
"testing"
1213

14+
"code.gitea.io/gitea/models"
15+
"code.gitea.io/gitea/modules/auth"
1316
api "code.gitea.io/gitea/modules/structs"
1417
"github.com/stretchr/testify/assert"
1518
)
@@ -150,3 +153,42 @@ func doAPICreateDeployKey(ctx APITestContext, keyname, keyFile string, readOnly
150153
ctx.Session.MakeRequest(t, req, http.StatusCreated)
151154
}
152155
}
156+
157+
func doAPICreatePullRequest(ctx APITestContext, owner, repo, baseBranch, headBranch string) func(*testing.T) (api.PullRequest, error) {
158+
return func(t *testing.T) (api.PullRequest, error) {
159+
urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/pulls?token=%s",
160+
owner, repo, ctx.Token)
161+
req := NewRequestWithJSON(t, http.MethodPost, urlStr, &api.CreatePullRequestOption{
162+
Head: headBranch,
163+
Base: baseBranch,
164+
Title: fmt.Sprintf("create a pr from %s to %s", headBranch, baseBranch),
165+
})
166+
167+
expected := 201
168+
if ctx.ExpectedCode != 0 {
169+
expected = ctx.ExpectedCode
170+
}
171+
resp := ctx.Session.MakeRequest(t, req, expected)
172+
decoder := json.NewDecoder(resp.Body)
173+
pr := api.PullRequest{}
174+
err := decoder.Decode(&pr)
175+
return pr, err
176+
}
177+
}
178+
179+
func doAPIMergePullRequest(ctx APITestContext, owner, repo string, index int64) func(*testing.T) {
180+
return func(t *testing.T) {
181+
urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/merge?token=%s",
182+
owner, repo, index, ctx.Token)
183+
req := NewRequestWithJSON(t, http.MethodPost, urlStr, &auth.MergePullRequestForm{
184+
MergeMessageField: "doAPIMergePullRequest Merge",
185+
Do: string(models.MergeStyleMerge),
186+
})
187+
188+
if ctx.ExpectedCode != 0 {
189+
ctx.Session.MakeRequest(t, req, ctx.ExpectedCode)
190+
return
191+
}
192+
ctx.Session.MakeRequest(t, req, 200)
193+
}
194+
}

integrations/git_helper_for_declarative_test.go

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,16 +112,44 @@ func doGitAddRemote(dstPath, remoteName string, u *url.URL) func(*testing.T) {
112112
}
113113
}
114114

115-
func doGitPushTestRepository(dstPath, remoteName, branch string) func(*testing.T) {
115+
func doGitPushTestRepository(dstPath string, args ...string) func(*testing.T) {
116116
return func(t *testing.T) {
117-
_, err := git.NewCommand("push", "-u", remoteName, branch).RunInDir(dstPath)
117+
_, err := git.NewCommand(append([]string{"push", "-u"}, args...)...).RunInDir(dstPath)
118118
assert.NoError(t, err)
119119
}
120120
}
121121

122-
func doGitPushTestRepositoryFail(dstPath, remoteName, branch string) func(*testing.T) {
122+
func doGitPushTestRepositoryFail(dstPath string, args ...string) func(*testing.T) {
123123
return func(t *testing.T) {
124-
_, err := git.NewCommand("push", "-u", remoteName, branch).RunInDir(dstPath)
124+
_, err := git.NewCommand(append([]string{"push"}, args...)...).RunInDir(dstPath)
125125
assert.Error(t, err)
126126
}
127127
}
128+
129+
func doGitCreateBranch(dstPath, branch string) func(*testing.T) {
130+
return func(t *testing.T) {
131+
_, err := git.NewCommand("checkout", "-b", branch).RunInDir(dstPath)
132+
assert.NoError(t, err)
133+
}
134+
}
135+
136+
func doGitCheckoutBranch(dstPath string, args ...string) func(*testing.T) {
137+
return func(t *testing.T) {
138+
_, err := git.NewCommand(append([]string{"checkout"}, args...)...).RunInDir(dstPath)
139+
assert.NoError(t, err)
140+
}
141+
}
142+
143+
func doGitMerge(dstPath string, args ...string) func(*testing.T) {
144+
return func(t *testing.T) {
145+
_, err := git.NewCommand(append([]string{"merge"}, args...)...).RunInDir(dstPath)
146+
assert.NoError(t, err)
147+
}
148+
}
149+
150+
func doGitPull(dstPath string, args ...string) func(*testing.T) {
151+
return func(t *testing.T) {
152+
_, err := git.NewCommand(append([]string{"pull"}, args...)...).RunInDir(dstPath)
153+
assert.NoError(t, err)
154+
}
155+
}

0 commit comments

Comments
 (0)