|
| 1 | +// Copyright 2023 The Gitea Authors. All rights reserved. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +package git |
| 5 | + |
| 6 | +import ( |
| 7 | + "fmt" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | +) |
| 12 | + |
| 13 | +func TestBranchRuleMatchPriority(t *testing.T) { |
| 14 | + kases := []struct { |
| 15 | + Rules []string |
| 16 | + BranchName string |
| 17 | + ExpectedMatchIdx int |
| 18 | + }{ |
| 19 | + { |
| 20 | + Rules: []string{"release/*", "release/v1.17"}, |
| 21 | + BranchName: "release/v1.17", |
| 22 | + ExpectedMatchIdx: 1, |
| 23 | + }, |
| 24 | + { |
| 25 | + Rules: []string{"release/v1.17", "release/*"}, |
| 26 | + BranchName: "release/v1.17", |
| 27 | + ExpectedMatchIdx: 0, |
| 28 | + }, |
| 29 | + { |
| 30 | + Rules: []string{"release/**/v1.17", "release/test/v1.17"}, |
| 31 | + BranchName: "release/test/v1.17", |
| 32 | + ExpectedMatchIdx: 1, |
| 33 | + }, |
| 34 | + { |
| 35 | + Rules: []string{"release/test/v1.17", "release/**/v1.17"}, |
| 36 | + BranchName: "release/test/v1.17", |
| 37 | + ExpectedMatchIdx: 0, |
| 38 | + }, |
| 39 | + { |
| 40 | + Rules: []string{"release/**", "release/v1.0.0"}, |
| 41 | + BranchName: "release/v1.0.0", |
| 42 | + ExpectedMatchIdx: 1, |
| 43 | + }, |
| 44 | + { |
| 45 | + Rules: []string{"release/v1.0.0", "release/**"}, |
| 46 | + BranchName: "release/v1.0.0", |
| 47 | + ExpectedMatchIdx: 0, |
| 48 | + }, |
| 49 | + { |
| 50 | + Rules: []string{"release/**", "release/v1.0.0"}, |
| 51 | + BranchName: "release/v2.0.0", |
| 52 | + ExpectedMatchIdx: 0, |
| 53 | + }, |
| 54 | + { |
| 55 | + Rules: []string{"release/*", "release/v1.0.0"}, |
| 56 | + BranchName: "release/1/v2.0.0", |
| 57 | + ExpectedMatchIdx: -1, |
| 58 | + }, |
| 59 | + } |
| 60 | + |
| 61 | + for _, kase := range kases { |
| 62 | + var pbs ProtectedBranchRules |
| 63 | + for _, rule := range kase.Rules { |
| 64 | + pbs = append(pbs, &ProtectedBranch{RuleName: rule}) |
| 65 | + } |
| 66 | + pbs.sort() |
| 67 | + matchedPB := pbs.GetFirstMatched(kase.BranchName) |
| 68 | + if matchedPB == nil { |
| 69 | + if kase.ExpectedMatchIdx >= 0 { |
| 70 | + assert.Error(t, fmt.Errorf("no matched rules but expected %s[%d]", kase.Rules[kase.ExpectedMatchIdx], kase.ExpectedMatchIdx)) |
| 71 | + } |
| 72 | + } else { |
| 73 | + assert.EqualValues(t, kase.Rules[kase.ExpectedMatchIdx], matchedPB.RuleName) |
| 74 | + } |
| 75 | + } |
| 76 | +} |
0 commit comments