github.com/zppinho/prow@v0.0.0-20240510014325-1738badeb017/pkg/plugins/wip/wip-label_test.go (about)

     1  /*
     2  Copyright 2017 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 wip
    18  
    19  import (
    20  	"fmt"
    21  	"testing"
    22  
    23  	"github.com/sirupsen/logrus"
    24  
    25  	"sigs.k8s.io/prow/pkg/github"
    26  	"sigs.k8s.io/prow/pkg/github/fakegithub"
    27  	"sigs.k8s.io/prow/pkg/labels"
    28  )
    29  
    30  func TestWipLabel(t *testing.T) {
    31  	const (
    32  		wipTitle     = "[WIP] title"
    33  		regularTitle = "title"
    34  	)
    35  
    36  	var testcases = []struct {
    37  		name          string
    38  		title         string
    39  		draft         bool
    40  		hasLabel      bool
    41  		shouldLabel   bool
    42  		shouldUnlabel bool
    43  	}{
    44  		{
    45  			name:          "regular PR, need nothing",
    46  			title:         regularTitle,
    47  			draft:         false,
    48  			hasLabel:      false,
    49  			shouldLabel:   false,
    50  			shouldUnlabel: false,
    51  		},
    52  		{
    53  			name:          "wip title PR, needs label",
    54  			title:         wipTitle,
    55  			draft:         false,
    56  			hasLabel:      false,
    57  			shouldLabel:   true,
    58  			shouldUnlabel: false,
    59  		},
    60  		{
    61  			name:          "draft PR, needs label",
    62  			title:         regularTitle,
    63  			draft:         true,
    64  			hasLabel:      false,
    65  			shouldLabel:   true,
    66  			shouldUnlabel: false,
    67  		},
    68  		{
    69  			name:          "regular PR, remove label",
    70  			title:         regularTitle,
    71  			draft:         false,
    72  			hasLabel:      true,
    73  			shouldLabel:   false,
    74  			shouldUnlabel: true,
    75  		},
    76  		{
    77  			name:          "wip title PR, nothing to do",
    78  			title:         wipTitle,
    79  			draft:         false,
    80  			hasLabel:      true,
    81  			shouldLabel:   false,
    82  			shouldUnlabel: false,
    83  		},
    84  		{
    85  			name:          "draft PR, nothing to do",
    86  			title:         regularTitle,
    87  			draft:         true,
    88  			hasLabel:      true,
    89  			shouldLabel:   false,
    90  			shouldUnlabel: false,
    91  		},
    92  	}
    93  	for _, tc := range testcases {
    94  		fc := fakegithub.NewFakeClient()
    95  		fc.PullRequests = make(map[int]*github.PullRequest)
    96  		fc.IssueComments = make(map[int][]github.IssueComment)
    97  		org, repo, number := "org", "repo", 5
    98  		e := &event{
    99  			org:      org,
   100  			repo:     repo,
   101  			number:   number,
   102  			title:    tc.title,
   103  			draft:    tc.draft,
   104  			hasLabel: tc.hasLabel,
   105  		}
   106  
   107  		if err := handle(fc, logrus.WithField("plugin", PluginName), e); err != nil {
   108  			t.Errorf("For case %s, didn't expect error from wip: %v", tc.name, err)
   109  			continue
   110  		}
   111  
   112  		fakeLabel := fmt.Sprintf("%s/%s#%d:%s", org, repo, number, labels.WorkInProgress)
   113  		if tc.shouldLabel {
   114  			if len(fc.IssueLabelsAdded) != 1 || fc.IssueLabelsAdded[0] != fakeLabel {
   115  				t.Errorf("For case %s: expected to add %q Label but instead added: %v", tc.name, labels.WorkInProgress, fc.IssueLabelsAdded)
   116  			}
   117  		} else if len(fc.IssueLabelsAdded) > 0 {
   118  			t.Errorf("For case %s, expected to not add %q Label but added: %v", tc.name, labels.WorkInProgress, fc.IssueLabelsAdded)
   119  		}
   120  		if tc.shouldUnlabel {
   121  			if len(fc.IssueLabelsRemoved) != 1 || fc.IssueLabelsRemoved[0] != fakeLabel {
   122  				t.Errorf("For case %s: expected to remove %q Label but instead removed: %v", tc.name, labels.WorkInProgress, fc.IssueLabelsRemoved)
   123  			}
   124  		} else if len(fc.IssueLabelsRemoved) > 0 {
   125  			t.Errorf("For case %s, expected to not remove %q Label but removed: %v", tc.name, labels.WorkInProgress, fc.IssueLabelsRemoved)
   126  		}
   127  	}
   128  }
   129  
   130  func TestHasWipPrefix(t *testing.T) {
   131  	var tests = []struct {
   132  		title    string
   133  		expected bool
   134  	}{
   135  		{
   136  			title:    "dummy title",
   137  			expected: false,
   138  		},
   139  		{
   140  			title:    "WIP dummy title",
   141  			expected: true,
   142  		},
   143  		{
   144  			title:    "WIP: dummy title",
   145  			expected: true,
   146  		},
   147  		{
   148  			title:    "[WIP] dummy title",
   149  			expected: true,
   150  		},
   151  		{
   152  			title:    "(WIP) dummy title",
   153  			expected: true,
   154  		},
   155  		{
   156  			title:    "<WIP> dummy title",
   157  			expected: true,
   158  		},
   159  		{
   160  			title:    "wip dummy title",
   161  			expected: true,
   162  		},
   163  		{
   164  			title:    "[wip] dummy title",
   165  			expected: true,
   166  		},
   167  		{
   168  			title:    "(wip) dummy title",
   169  			expected: true,
   170  		},
   171  		{
   172  			title:    "<wip> dummy title",
   173  			expected: true,
   174  		},
   175  		{
   176  			title:    "Wipe out GCP project before reusing",
   177  			expected: false,
   178  		},
   179  	}
   180  
   181  	for _, test := range tests {
   182  		if actual, expected := titleRegex.MatchString(test.title), test.expected; actual != expected {
   183  			t.Errorf("for title %q, got WIP prefix match %v but got %v", test.title, actual, expected)
   184  		}
   185  	}
   186  }