Skip to content

Commit 85cb006

Browse files
committed
add CommitRepoAction tests for tag/branch creation/deletion
Signed-off-by: David Schneiderbauer <[email protected]>
1 parent 0bd387e commit 85cb006

File tree

2 files changed

+110
-41
lines changed

2 files changed

+110
-41
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
65f1bf27bc3bf70f64657658635e66094edbcb4d

models/action_test.go

Lines changed: 109 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package models
22

33
import (
4+
"os"
45
"path"
56
"strings"
67
"testing"
78

9+
"code.gitea.io/git"
810
"code.gitea.io/gitea/modules/setting"
911

12+
"github.com/Unknwon/com"
1013
"github.com/stretchr/testify/assert"
1114
)
1215

@@ -202,55 +205,120 @@ func TestUpdateIssuesCommit(t *testing.T) {
202205
CheckConsistencyFor(t, &Action{})
203206
}
204207

205-
func TestCommitRepoAction(t *testing.T) {
206-
assert.NoError(t, PrepareTestDatabase())
207-
208-
user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
209-
repo := AssertExistsAndLoadBean(t, &Repository{ID: 2, OwnerID: user.ID}).(*Repository)
210-
repo.Owner = user
208+
func testCorrectRepoAction(t *testing.T, opts CommitRepoActionOptions, actionBean *Action) {
209+
AssertNotExistsBean(t, actionBean)
210+
assert.NoError(t, CommitRepoAction(opts))
211+
AssertExistsAndLoadBean(t, actionBean)
212+
CheckConsistencyFor(t, &Action{})
213+
}
211214

212-
pushCommits := NewPushCommits()
213-
pushCommits.Commits = []*PushCommit{
215+
func TestCommitRepoAction(t *testing.T) {
216+
samples := []struct {
217+
userID int64
218+
repositoryID int64
219+
commitRepoActionOptions CommitRepoActionOptions
220+
action Action
221+
}{
214222
{
215-
Sha1: "abcdef1",
216-
CommitterEmail: "[email protected]",
217-
CommitterName: "User Two",
218-
AuthorEmail: "[email protected]",
219-
AuthorName: "User Four",
220-
Message: "message1",
223+
userID: 2,
224+
repositoryID: 2,
225+
commitRepoActionOptions: CommitRepoActionOptions{
226+
RefFullName: "refName",
227+
OldCommitID: "oldCommitID",
228+
NewCommitID: "newCommitID",
229+
Commits: &PushCommits{
230+
avatars: make(map[string]string),
231+
Commits: []*PushCommit{
232+
{
233+
Sha1: "abcdef1",
234+
CommitterEmail: "[email protected]",
235+
CommitterName: "User Two",
236+
AuthorEmail: "[email protected]",
237+
AuthorName: "User Four",
238+
Message: "message1",
239+
},
240+
{
241+
Sha1: "abcdef2",
242+
CommitterEmail: "[email protected]",
243+
CommitterName: "User Two",
244+
AuthorEmail: "[email protected]",
245+
AuthorName: "User Two",
246+
Message: "message2",
247+
},
248+
},
249+
Len: 2,
250+
},
251+
},
252+
action: Action{
253+
OpType: ActionCommitRepo,
254+
RefName: "refName",
255+
},
221256
},
222257
{
223-
Sha1: "abcdef2",
224-
CommitterEmail: "[email protected]",
225-
CommitterName: "User Two",
226-
AuthorEmail: "[email protected]",
227-
AuthorName: "User Two",
228-
Message: "message2",
258+
userID: 2,
259+
repositoryID: 1,
260+
commitRepoActionOptions: CommitRepoActionOptions{
261+
RefFullName: git.TagPrefix + "v1.1",
262+
OldCommitID: git.EmptySHA,
263+
NewCommitID: "newCommitID",
264+
Commits: &PushCommits{},
265+
},
266+
action: Action{
267+
OpType: ActionPushTag,
268+
RefName: "v1.1",
269+
},
270+
},
271+
{
272+
userID: 2,
273+
repositoryID: 1,
274+
commitRepoActionOptions: CommitRepoActionOptions{
275+
RefFullName: git.TagPrefix + "v1.1",
276+
OldCommitID: "oldCommitID",
277+
NewCommitID: git.EmptySHA,
278+
Commits: &PushCommits{},
279+
},
280+
action: Action{
281+
OpType: ActionDeleteTag,
282+
RefName: "v1.1",
283+
},
284+
},
285+
{
286+
userID: 2,
287+
repositoryID: 1,
288+
commitRepoActionOptions: CommitRepoActionOptions{
289+
RefFullName: git.BranchPrefix + "feature/1",
290+
OldCommitID: "oldCommitID",
291+
NewCommitID: git.EmptySHA,
292+
Commits: &PushCommits{},
293+
},
294+
action: Action{
295+
OpType: ActionDeleteBranch,
296+
RefName: "feature/1",
297+
},
229298
},
230299
}
231-
pushCommits.Len = len(pushCommits.Commits)
232300

233-
actionBean := &Action{
234-
OpType: ActionCommitRepo,
235-
ActUserID: user.ID,
236-
ActUser: user,
237-
RepoID: repo.ID,
238-
Repo: repo,
239-
RefName: "refName",
240-
IsPrivate: repo.IsPrivate,
301+
for _, s := range samples {
302+
assert.NoError(t, PrepareTestDatabase())
303+
assert.NoError(t, os.RemoveAll(setting.RepoRootPath))
304+
assert.NoError(t, com.CopyDir("../integrations/gitea-repositories-meta", setting.RepoRootPath))
305+
306+
user := AssertExistsAndLoadBean(t, &User{ID: s.userID}).(*User)
307+
repo := AssertExistsAndLoadBean(t, &Repository{ID: s.repositoryID, OwnerID: user.ID}).(*Repository)
308+
repo.Owner = user
309+
310+
s.commitRepoActionOptions.PusherName = user.Name
311+
s.commitRepoActionOptions.RepoOwnerID = user.ID
312+
s.commitRepoActionOptions.RepoName = repo.Name
313+
314+
s.action.ActUserID = user.ID
315+
s.action.ActUser = user
316+
s.action.RepoID = repo.ID
317+
s.action.Repo = repo
318+
s.action.IsPrivate = repo.IsPrivate
319+
320+
testCorrectRepoAction(t, s.commitRepoActionOptions, &s.action)
241321
}
242-
AssertNotExistsBean(t, actionBean)
243-
assert.NoError(t, CommitRepoAction(CommitRepoActionOptions{
244-
PusherName: user.Name,
245-
RepoOwnerID: user.ID,
246-
RepoName: repo.Name,
247-
RefFullName: "refName",
248-
OldCommitID: "oldCommitID",
249-
NewCommitID: "newCommitID",
250-
Commits: pushCommits,
251-
}))
252-
AssertExistsAndLoadBean(t, actionBean)
253-
CheckConsistencyFor(t, &Action{})
254322
}
255323

256324
func TestTransferRepoAction(t *testing.T) {

0 commit comments

Comments
 (0)