|
| 1 | +// Copyright 2024 The Gitea Authors. |
| 2 | +// All rights reserved. |
| 3 | +// SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +package pull |
| 6 | + |
| 7 | +import ( |
| 8 | + "testing" |
| 9 | + |
| 10 | + git_model "code.gitea.io/gitea/models/git" |
| 11 | + "code.gitea.io/gitea/modules/structs" |
| 12 | + |
| 13 | + "github.com/stretchr/testify/assert" |
| 14 | +) |
| 15 | + |
| 16 | +func TestMergeRequiredContextsCommitStatus(t *testing.T) { |
| 17 | + testCases := [][]*git_model.CommitStatus{ |
| 18 | + { |
| 19 | + {Context: "Build 1", State: structs.CommitStatusSuccess}, |
| 20 | + {Context: "Build 2", State: structs.CommitStatusSuccess}, |
| 21 | + {Context: "Build 3", State: structs.CommitStatusSuccess}, |
| 22 | + }, |
| 23 | + { |
| 24 | + {Context: "Build 1", State: structs.CommitStatusSuccess}, |
| 25 | + {Context: "Build 2", State: structs.CommitStatusSuccess}, |
| 26 | + {Context: "Build 2t", State: structs.CommitStatusPending}, |
| 27 | + }, |
| 28 | + { |
| 29 | + {Context: "Build 1", State: structs.CommitStatusSuccess}, |
| 30 | + {Context: "Build 2", State: structs.CommitStatusSuccess}, |
| 31 | + {Context: "Build 2t", State: structs.CommitStatusFailure}, |
| 32 | + }, |
| 33 | + { |
| 34 | + {Context: "Build 1", State: structs.CommitStatusSuccess}, |
| 35 | + {Context: "Build 2", State: structs.CommitStatusSuccess}, |
| 36 | + {Context: "Build 2t", State: structs.CommitStatusSuccess}, |
| 37 | + }, |
| 38 | + { |
| 39 | + {Context: "Build 1", State: structs.CommitStatusSuccess}, |
| 40 | + {Context: "Build 2", State: structs.CommitStatusSuccess}, |
| 41 | + {Context: "Build 2t", State: structs.CommitStatusSuccess}, |
| 42 | + }, |
| 43 | + } |
| 44 | + testCasesRequiredContexts := [][]string{ |
| 45 | + {"Build*"}, |
| 46 | + {"Build*", "Build 2t*"}, |
| 47 | + {"Build*", "Build 2t*"}, |
| 48 | + {"Build*", "Build 2t*", "Build 3*"}, |
| 49 | + {"Build*", "Build *", "Build 2t*", "Build 1*"}, |
| 50 | + } |
| 51 | + |
| 52 | + testCasesExpected := []structs.CommitStatusState{ |
| 53 | + structs.CommitStatusSuccess, |
| 54 | + structs.CommitStatusPending, |
| 55 | + structs.CommitStatusFailure, |
| 56 | + structs.CommitStatusPending, |
| 57 | + structs.CommitStatusSuccess, |
| 58 | + } |
| 59 | + |
| 60 | + for i, commitStatuses := range testCases { |
| 61 | + if MergeRequiredContextsCommitStatus(commitStatuses, testCasesRequiredContexts[i]) != testCasesExpected[i] { |
| 62 | + assert.Fail(t, "Test case failed", "Test case %d failed", i+1) |
| 63 | + } |
| 64 | + } |
| 65 | +} |
0 commit comments