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.3KB

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