|
| 1 | +// Copyright 2020 The Gitea Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a MIT-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package gitea |
| 6 | + |
| 7 | +import ( |
| 8 | + "log" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | +) |
| 13 | + |
| 14 | +func TestCommitStatus(t *testing.T) { |
| 15 | + log.Println("== TestCommitStatus ==") |
| 16 | + c := newTestClient() |
| 17 | + user, _, err := c.GetMyUserInfo() |
| 18 | + assert.NoError(t, err) |
| 19 | + |
| 20 | + var repoName = "CommitStatuses" |
| 21 | + origRepo, err := createTestRepo(t, repoName, c) |
| 22 | + if !assert.NoError(t, err) { |
| 23 | + return |
| 24 | + } |
| 25 | + |
| 26 | + commits, _, _ := c.ListRepoCommits(user.UserName, repoName, ListCommitOptions{ |
| 27 | + ListOptions: ListOptions{}, |
| 28 | + SHA: origRepo.DefaultBranch, |
| 29 | + }) |
| 30 | + if !assert.Len(t, commits, 1) { |
| 31 | + return |
| 32 | + } |
| 33 | + sha := commits[0].SHA |
| 34 | + |
| 35 | + combiStats, resp, err := c.GetCombinedStatus(user.UserName, repoName, sha) |
| 36 | + assert.NoError(t, err) |
| 37 | + assert.NotNil(t, resp) |
| 38 | + assert.NotNil(t, combiStats) |
| 39 | + assert.EqualValues(t, 0, combiStats.TotalCount) |
| 40 | + |
| 41 | + statuses, resp, err := c.ListStatuses(user.UserName, repoName, sha, ListStatusesOption{}) |
| 42 | + assert.NoError(t, err) |
| 43 | + assert.NotNil(t, resp) |
| 44 | + assert.NotNil(t, statuses) |
| 45 | + assert.Len(t, statuses, 0) |
| 46 | + |
| 47 | + createStatus(t, c, user.UserName, repoName, sha, "http://dummy.test", "start testing", "ultraCI", StatusPending) |
| 48 | + createStatus(t, c, user.UserName, repoName, sha, "https://more.secure", "just a warning", "warn/bot", StatusWarning) |
| 49 | + createStatus(t, c, user.UserName, repoName, sha, "http://dummy.test", "test failed", "ultraCI", StatusFailure) |
| 50 | + createStatus(t, c, user.UserName, repoName, sha, "http://dummy.test", "start testing", "ultraCI", StatusPending) |
| 51 | + createStatus(t, c, user.UserName, repoName, sha, "http://dummy.test", "test passed", "ultraCI", StatusSuccess) |
| 52 | + |
| 53 | + statuses, resp, err = c.ListStatuses(user.UserName, repoName, sha, ListStatusesOption{}) |
| 54 | + assert.NoError(t, err) |
| 55 | + assert.NotNil(t, resp) |
| 56 | + assert.NotNil(t, statuses) |
| 57 | + assert.Len(t, statuses, 5) |
| 58 | + |
| 59 | + combiStats, resp, err = c.GetCombinedStatus(user.UserName, repoName, sha) |
| 60 | + assert.NoError(t, err) |
| 61 | + assert.NotNil(t, resp) |
| 62 | + assert.NotNil(t, combiStats) |
| 63 | + assert.EqualValues(t, 2, combiStats.TotalCount) |
| 64 | + assert.EqualValues(t, StatusState("warning"), combiStats.State) |
| 65 | + assert.Len(t, combiStats.Statuses, 2) |
| 66 | +} |
| 67 | + |
| 68 | +func createStatus(t *testing.T, c *Client, userName, repoName, sha, url, desc, context string, state StatusState) { |
| 69 | + stats, resp, err := c.CreateStatus(userName, repoName, sha, CreateStatusOption{ |
| 70 | + State: state, |
| 71 | + TargetURL: url, |
| 72 | + Description: desc, |
| 73 | + Context: context, |
| 74 | + }) |
| 75 | + assert.NoError(t, err) |
| 76 | + assert.NotNil(t, resp) |
| 77 | + assert.NotNil(t, stats) |
| 78 | + assert.EqualValues(t, state, stats.State) |
| 79 | +} |
0 commit comments