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.

glob_pattern_test.go 1.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package validation
  4. import (
  5. "testing"
  6. "gitea.com/go-chi/binding"
  7. "github.com/gobwas/glob"
  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. var globValidationTestCases = []validationTestCase{
  18. {
  19. description: "Empty glob pattern",
  20. data: TestForm{
  21. GlobPattern: "",
  22. },
  23. expectedErrors: binding.Errors{},
  24. },
  25. {
  26. description: "Valid glob",
  27. data: TestForm{
  28. GlobPattern: "{master,release*}",
  29. },
  30. expectedErrors: binding.Errors{},
  31. },
  32. {
  33. description: "Invalid glob",
  34. data: TestForm{
  35. GlobPattern: "[a-",
  36. },
  37. expectedErrors: binding.Errors{
  38. binding.Error{
  39. FieldNames: []string{"GlobPattern"},
  40. Classification: ErrGlobPattern,
  41. Message: getGlobPatternErrorString("[a-"),
  42. },
  43. },
  44. },
  45. }
  46. func Test_GlobPatternValidation(t *testing.T) {
  47. AddBindingRules()
  48. for _, testCase := range globValidationTestCases {
  49. t.Run(testCase.description, func(t *testing.T) {
  50. performValidationTest(t, testCase)
  51. })
  52. }
  53. }