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