|
1 | 1 | package models
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "os" |
4 | 5 | "path"
|
5 | 6 | "strings"
|
6 | 7 | "testing"
|
7 | 8 |
|
| 9 | + "code.gitea.io/git" |
8 | 10 | "code.gitea.io/gitea/modules/setting"
|
9 | 11 |
|
| 12 | + "github.com/Unknwon/com" |
10 | 13 | "github.com/stretchr/testify/assert"
|
11 | 14 | )
|
12 | 15 |
|
@@ -202,55 +205,120 @@ func TestUpdateIssuesCommit(t *testing.T) {
|
202 | 205 | CheckConsistencyFor(t, &Action{})
|
203 | 206 | }
|
204 | 207 |
|
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 | +} |
211 | 214 |
|
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 | + }{ |
214 | 222 | {
|
215 |
| - Sha1: "abcdef1", |
216 |
| - CommitterEmail: "[email protected]", |
217 |
| - CommitterName: "User Two", |
218 |
| - |
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 | + |
| 237 | + AuthorName: "User Four", |
| 238 | + Message: "message1", |
| 239 | + }, |
| 240 | + { |
| 241 | + Sha1: "abcdef2", |
| 242 | + CommitterEmail: "[email protected]", |
| 243 | + CommitterName: "User Two", |
| 244 | + |
| 245 | + AuthorName: "User Two", |
| 246 | + Message: "message2", |
| 247 | + }, |
| 248 | + }, |
| 249 | + Len: 2, |
| 250 | + }, |
| 251 | + }, |
| 252 | + action: Action{ |
| 253 | + OpType: ActionCommitRepo, |
| 254 | + RefName: "refName", |
| 255 | + }, |
221 | 256 | },
|
222 | 257 | {
|
223 |
| - Sha1: "abcdef2", |
224 |
| - CommitterEmail: "[email protected]", |
225 |
| - CommitterName: "User Two", |
226 |
| - |
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 | + }, |
229 | 298 | },
|
230 | 299 | }
|
231 |
| - pushCommits.Len = len(pushCommits.Commits) |
232 | 300 |
|
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) |
241 | 321 | }
|
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{}) |
254 | 322 | }
|
255 | 323 |
|
256 | 324 | func TestTransferRepoAction(t *testing.T) {
|
|
0 commit comments