github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/validation/glob_pattern_test.go (about)

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