You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

protected_banch_list_test.go 1.9KB

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