code.gitea.io/gitea@v1.19.3/modules/validation/glob_pattern_test.go (about)

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