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_branch_test.go 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright 2022 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 TestBranchRuleMatch(t *testing.T) {
  10. kases := []struct {
  11. Rule string
  12. BranchName string
  13. ExpectedMatch bool
  14. }{
  15. {
  16. Rule: "release/*",
  17. BranchName: "release/v1.17",
  18. ExpectedMatch: true,
  19. },
  20. {
  21. Rule: "release/**/v1.17",
  22. BranchName: "release/test/v1.17",
  23. ExpectedMatch: true,
  24. },
  25. {
  26. Rule: "release/**/v1.17",
  27. BranchName: "release/test/1/v1.17",
  28. ExpectedMatch: true,
  29. },
  30. {
  31. Rule: "release/*/v1.17",
  32. BranchName: "release/test/1/v1.17",
  33. ExpectedMatch: false,
  34. },
  35. {
  36. Rule: "release/v*",
  37. BranchName: "release/v1.16",
  38. ExpectedMatch: true,
  39. },
  40. {
  41. Rule: "*",
  42. BranchName: "release/v1.16",
  43. ExpectedMatch: false,
  44. },
  45. {
  46. Rule: "**",
  47. BranchName: "release/v1.16",
  48. ExpectedMatch: true,
  49. },
  50. {
  51. Rule: "main",
  52. BranchName: "main",
  53. ExpectedMatch: true,
  54. },
  55. {
  56. Rule: "master",
  57. BranchName: "main",
  58. ExpectedMatch: false,
  59. },
  60. }
  61. for _, kase := range kases {
  62. pb := ProtectedBranch{RuleName: kase.Rule}
  63. var should, infact string
  64. if !kase.ExpectedMatch {
  65. should = " not"
  66. } else {
  67. infact = " not"
  68. }
  69. assert.EqualValues(t, kase.ExpectedMatch, pb.Match(kase.BranchName),
  70. fmt.Sprintf("%s should%s match %s but it is%s", kase.BranchName, should, kase.Rule, infact),
  71. )
  72. }
  73. }