code.gitea.io/gitea@v1.21.7/models/git/protected_branch_test.go (about)

     1  // Copyright 2022 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package git
     5  
     6  import (
     7  	"fmt"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func TestBranchRuleMatch(t *testing.T) {
    14  	kases := []struct {
    15  		Rule          string
    16  		BranchName    string
    17  		ExpectedMatch bool
    18  	}{
    19  		{
    20  			Rule:          "release/*",
    21  			BranchName:    "release/v1.17",
    22  			ExpectedMatch: true,
    23  		},
    24  		{
    25  			Rule:          "release/**/v1.17",
    26  			BranchName:    "release/test/v1.17",
    27  			ExpectedMatch: true,
    28  		},
    29  		{
    30  			Rule:          "release/**/v1.17",
    31  			BranchName:    "release/test/1/v1.17",
    32  			ExpectedMatch: true,
    33  		},
    34  		{
    35  			Rule:          "release/*/v1.17",
    36  			BranchName:    "release/test/1/v1.17",
    37  			ExpectedMatch: false,
    38  		},
    39  		{
    40  			Rule:          "release/v*",
    41  			BranchName:    "release/v1.16",
    42  			ExpectedMatch: true,
    43  		},
    44  		{
    45  			Rule:          "*",
    46  			BranchName:    "release/v1.16",
    47  			ExpectedMatch: false,
    48  		},
    49  		{
    50  			Rule:          "**",
    51  			BranchName:    "release/v1.16",
    52  			ExpectedMatch: true,
    53  		},
    54  		{
    55  			Rule:          "main",
    56  			BranchName:    "main",
    57  			ExpectedMatch: true,
    58  		},
    59  		{
    60  			Rule:          "master",
    61  			BranchName:    "main",
    62  			ExpectedMatch: false,
    63  		},
    64  	}
    65  
    66  	for _, kase := range kases {
    67  		pb := ProtectedBranch{RuleName: kase.Rule}
    68  		var should, infact string
    69  		if !kase.ExpectedMatch {
    70  			should = " not"
    71  		} else {
    72  			infact = " not"
    73  		}
    74  		assert.EqualValues(t, kase.ExpectedMatch, pb.Match(kase.BranchName),
    75  			fmt.Sprintf("%s should%s match %s but it is%s", kase.BranchName, should, kase.Rule, infact),
    76  		)
    77  	}
    78  }