summaryrefslogtreecommitdiffstats
path: root/services/pull/commit_status_test.go
blob: 592acdd55cd4394c33cc48b30298b0c03a81214a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright 2024 The Gitea Authors.
// All rights reserved.
// SPDX-License-Identifier: MIT

package pull

import (
	"testing"

	git_model "code.gitea.io/gitea/models/git"
	"code.gitea.io/gitea/modules/structs"

	"github.com/stretchr/testify/assert"
)

func TestMergeRequiredContextsCommitStatus(t *testing.T) {
	testCases := [][]*git_model.CommitStatus{
		{
			{Context: "Build 1", State: structs.CommitStatusSuccess},
			{Context: "Build 2", State: structs.CommitStatusSuccess},
			{Context: "Build 3", State: structs.CommitStatusSuccess},
		},
		{
			{Context: "Build 1", State: structs.CommitStatusSuccess},
			{Context: "Build 2", State: structs.CommitStatusSuccess},
			{Context: "Build 2t", State: structs.CommitStatusPending},
		},
		{
			{Context: "Build 1", State: structs.CommitStatusSuccess},
			{Context: "Build 2", State: structs.CommitStatusSuccess},
			{Context: "Build 2t", State: structs.CommitStatusFailure},
		},
		{
			{Context: "Build 1", State: structs.CommitStatusSuccess},
			{Context: "Build 2", State: structs.CommitStatusSuccess},
			{Context: "Build 2t", State: structs.CommitStatusSuccess},
		},
		{
			{Context: "Build 1", State: structs.CommitStatusSuccess},
			{Context: "Build 2", State: structs.CommitStatusSuccess},
			{Context: "Build 2t", State: structs.CommitStatusSuccess},
		},
	}
	testCasesRequiredContexts := [][]string{
		{"Build*"},
		{"Build*", "Build 2t*"},
		{"Build*", "Build 2t*"},
		{"Build*", "Build 2t*", "Build 3*"},
		{"Build*", "Build *", "Build 2t*", "Build 1*"},
	}

	testCasesExpected := []structs.CommitStatusState{
		structs.CommitStatusSuccess,
		structs.CommitStatusPending,
		structs.CommitStatusFailure,
		structs.CommitStatusPending,
		structs.CommitStatusSuccess,
	}

	for i, commitStatuses := range testCases {
		if MergeRequiredContextsCommitStatus(commitStatuses, testCasesRequiredContexts[i]) != testCasesExpected[i] {
			assert.Fail(t, "Test case failed", "Test case %d failed", i+1)
		}
	}
}