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