gitea源码

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package validation
  4. import (
  5. "testing"
  6. "code.gitea.io/gitea/modules/glob"
  7. "gitea.com/go-chi/binding"
  8. )
  9. func getGlobPatternErrorString(pattern string) string {
  10. // It would be unwise to rely on that glob
  11. // compilation errors don't ever change.
  12. if _, err := glob.Compile(pattern); err != nil {
  13. return err.Error()
  14. }
  15. return ""
  16. }
  17. func Test_GlobPatternValidation(t *testing.T) {
  18. AddBindingRules()
  19. globValidationTestCases := []validationTestCase{
  20. {
  21. description: "Empty glob pattern",
  22. data: TestForm{
  23. GlobPattern: "",
  24. },
  25. expectedErrors: binding.Errors{},
  26. },
  27. {
  28. description: "Valid glob",
  29. data: TestForm{
  30. GlobPattern: "{master,release*}",
  31. },
  32. expectedErrors: binding.Errors{},
  33. },
  34. {
  35. description: "Invalid glob",
  36. data: TestForm{
  37. GlobPattern: "[a-",
  38. },
  39. expectedErrors: binding.Errors{
  40. binding.Error{
  41. FieldNames: []string{"GlobPattern"},
  42. Classification: ErrGlobPattern,
  43. Message: getGlobPatternErrorString("[a-"),
  44. },
  45. },
  46. },
  47. }
  48. for _, testCase := range globValidationTestCases {
  49. t.Run(testCase.description, func(t *testing.T) {
  50. performValidationTest(t, testCase)
  51. })
  52. }
  53. }