sigs.k8s.io/prow@v0.0.0-20240503223140-c5e374dc7eb1/pkg/simplifypath/simplify_test.go (about)

     1  /*
     2  Copyright 2019 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package simplifypath
    18  
    19  import (
    20  	"strings"
    21  	"testing"
    22  
    23  	"k8s.io/apimachinery/pkg/util/diff"
    24  )
    25  
    26  func TestLiteral(t *testing.T) {
    27  	l := L("fragment", Node{})
    28  	if !l.Matches("fragment") {
    29  		t.Errorf("expected literal to match fragment, but didn't")
    30  	}
    31  	if actual, expected := l.Represent(), "fragment"; actual != expected {
    32  		t.Errorf("expected literal to be represented by %v, but saw: %v", expected, actual)
    33  	}
    34  }
    35  
    36  func TestEmptyLiteral(t *testing.T) {
    37  	l := L("", Node{})
    38  	if !l.Matches(strings.Split("/", "/")[0]) {
    39  		t.Errorf("expected empty literal to match root, but didn't")
    40  	}
    41  	if actual, expected := l.Represent(), ""; actual != expected {
    42  		t.Errorf("expected empty literal to be represented by %v, but saw: %v", expected, actual)
    43  	}
    44  }
    45  
    46  func TestVariable(t *testing.T) {
    47  	l := V("variable", Node{})
    48  	if !l.Matches("variable") {
    49  		t.Errorf("expected variable to match itself, but didn't")
    50  	}
    51  	if !l.Matches("askdljfhasdjfas") {
    52  		t.Errorf("expected variable to match random string, but didn't")
    53  	}
    54  	if actual, expected := l.Represent(), ":variable"; actual != expected {
    55  		t.Errorf("expected literal to be represented by %v, but saw: %v", expected, actual)
    56  	}
    57  }
    58  
    59  func TestSimplify(t *testing.T) {
    60  	s := NewSimplifier(L("", // shadow element mimicing the root
    61  		L(""),
    62  		L("repos",
    63  			V("owner",
    64  				V("repo",
    65  					L("branches", V("branch", L("protection",
    66  						L("restrictions", L("users"), L("teams")),
    67  						L("required_status_checks", L("contexts")),
    68  						L("required_pull_request_reviews"),
    69  						L("required_signatures"),
    70  						L("enforce_admins"))),
    71  					),
    72  				),
    73  			),
    74  		),
    75  		L("labels", VGreedy("labelname")),
    76  		L("view", L("gs", V("bucket", L("logs", V("job", V("build")))))),
    77  	))
    78  
    79  	var testCases = []struct {
    80  		name, path, expected string
    81  	}{
    82  		{
    83  			name:     "root",
    84  			path:     "/",
    85  			expected: "/"},
    86  		{
    87  			name:     "repo branches",
    88  			path:     "/repos/testOwner/testRepo/branches",
    89  			expected: "/repos/:owner/:repo/branches"},
    90  		{
    91  			name:     "repo branches by name",
    92  			path:     "/repos/testOwner/testRepo/branches/testBranch",
    93  			expected: "/repos/:owner/:repo/branches/:branch"},
    94  		{
    95  			name:     "repo branches protection by name ",
    96  			path:     "/repos/testOwner/testRepo/branches/testBranch/protection",
    97  			expected: "/repos/:owner/:repo/branches/:branch/protection"},
    98  		{
    99  			name:     "repo branches protection (required status checks) by name ",
   100  			path:     "/repos/testOwner/testRepo/branches/testBranch/protection/required_status_checks",
   101  			expected: "/repos/:owner/:repo/branches/:branch/protection/required_status_checks"},
   102  		{
   103  			name:     "repo branches protection (required status checks, contexts) by name ",
   104  			path:     "/repos/testOwner/testRepo/branches/testBranch/protection/required_status_checks/contexts",
   105  			expected: "/repos/:owner/:repo/branches/:branch/protection/required_status_checks/contexts"},
   106  		{
   107  			name:     "repo branches protection (required pull request reviews) by name ",
   108  			path:     "/repos/testOwner/testRepo/branches/testBranch/protection/required_pull_request_reviews",
   109  			expected: "/repos/:owner/:repo/branches/:branch/protection/required_pull_request_reviews"},
   110  		{
   111  			name:     "repo branches protection (required signatures) by name ",
   112  			path:     "/repos/testOwner/testRepo/branches/testBranch/protection/required_signatures",
   113  			expected: "/repos/:owner/:repo/branches/:branch/protection/required_signatures"},
   114  		{
   115  			name:     "repo branches protection (enforce admins) by name ",
   116  			path:     "/repos/testOwner/testRepo/branches/testBranch/protection/enforce_admins",
   117  			expected: "/repos/:owner/:repo/branches/:branch/protection/enforce_admins"},
   118  		{
   119  			name:     "repo branches protection (restrictions) by name ",
   120  			path:     "/repos/testOwner/testRepo/branches/testBranch/protection/restrictions",
   121  			expected: "/repos/:owner/:repo/branches/:branch/protection/restrictions"},
   122  		{
   123  			name:     "repo branches protection (restrictions for teams) by name ",
   124  			path:     "/repos/testOwner/testRepo/branches/testBranch/protection/restrictions/teams",
   125  			expected: "/repos/:owner/:repo/branches/:branch/protection/restrictions/teams"},
   126  		{
   127  			name:     "repo branches protection (restrictions for users) by name ",
   128  			path:     "/repos/testOwner/testRepo/branches/testBranch/protection/restrictions/users",
   129  			expected: "/repos/:owner/:repo/branches/:branch/protection/restrictions/users"},
   130  		{
   131  			name:     "Label without slash matches",
   132  			path:     "/labels/lgtm",
   133  			expected: "/labels/:labelname"},
   134  		{
   135  			name:     "Label with slash matches due to greedyness",
   136  			path:     "/labels/labels/do-not-merge/hold",
   137  			expected: "/labels/:labelname"},
   138  		{
   139  			name:     "deck's build page",
   140  			path:     "/view/gs/origin-ci-test/logs/release-openshift-origin-installer-launch-azure-modern/1496462115936931840",
   141  			expected: "/view/gs/:bucket/logs/:job/:build"},
   142  	}
   143  	for _, testCase := range testCases {
   144  		t.Run(testCase.name, func(t *testing.T) {
   145  			if actual, expected := s.Simplify(testCase.path), testCase.expected; actual != expected {
   146  				t.Errorf("%s: got incorrect simplification: %v", testCase.name, diff.StringDiff(actual, expected))
   147  			}
   148  		})
   149  	}
   150  }