github.com/munnerz/test-infra@v0.0.0-20190108210205-ce3d181dc989/prow/plugins/help/help_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 help 18 19 import ( 20 "fmt" 21 "reflect" 22 "sort" 23 "testing" 24 25 "github.com/sirupsen/logrus" 26 "k8s.io/test-infra/prow/github" 27 "k8s.io/test-infra/prow/github/fakegithub" 28 "k8s.io/test-infra/prow/labels" 29 ) 30 31 type fakePruner struct{} 32 33 func (fp *fakePruner) PruneComments(shouldPrune func(github.IssueComment) bool) {} 34 35 func formatLabels(labels ...string) []string { 36 r := []string{} 37 for _, l := range labels { 38 r = append(r, fmt.Sprintf("%s/%s#%d:%s", "org", "repo", 1, l)) 39 } 40 if len(r) == 0 { 41 return nil 42 } 43 return r 44 } 45 46 func TestLabel(t *testing.T) { 47 type testCase struct { 48 name string 49 isPR bool 50 issueState string 51 action github.GenericCommentEventAction 52 body string 53 expectedNewLabels []string 54 expectedRemovedLabels []string 55 issueLabels []string 56 } 57 testcases := []testCase{ 58 { 59 name: "Ignore irrelevant comment", 60 body: "irrelelvant", 61 expectedNewLabels: []string{}, 62 expectedRemovedLabels: []string{}, 63 issueLabels: []string{}, 64 }, 65 { 66 name: "Ignore a PR", 67 isPR: true, 68 body: "/help", 69 expectedNewLabels: []string{}, 70 expectedRemovedLabels: []string{}, 71 issueLabels: []string{}, 72 }, 73 { 74 name: "Ignore a closed issue", 75 issueState: "closed", 76 body: "/help", 77 expectedNewLabels: []string{}, 78 expectedRemovedLabels: []string{}, 79 issueLabels: []string{}, 80 }, 81 { 82 name: "Ignore a non-created comment", 83 action: github.GenericCommentActionEdited, 84 body: "/help", 85 expectedNewLabels: []string{}, 86 expectedRemovedLabels: []string{}, 87 issueLabels: []string{}, 88 }, 89 { 90 name: "Want helpLabel", 91 body: "/help", 92 expectedNewLabels: formatLabels(labels.Help), 93 expectedRemovedLabels: []string{}, 94 issueLabels: []string{}, 95 }, 96 { 97 name: "Want helpLabel, already have it.", 98 body: "/help", 99 expectedNewLabels: []string{}, 100 expectedRemovedLabels: []string{}, 101 issueLabels: []string{labels.Help}, 102 }, 103 { 104 name: "Want to remove helpLabel, have it", 105 body: "/remove-help", 106 expectedNewLabels: []string{}, 107 expectedRemovedLabels: formatLabels(labels.Help), 108 issueLabels: []string{labels.Help}, 109 }, 110 { 111 name: "Want to remove helpLabel, don't have it", 112 body: "/remove-help", 113 expectedNewLabels: []string{}, 114 expectedRemovedLabels: []string{}, 115 issueLabels: []string{}, 116 }, 117 { 118 name: "Want to remove helpLabel and goodFirstIssueLabel, have helpLabel and goodFirstIssueLabel", 119 body: "/remove-help", 120 expectedNewLabels: []string{}, 121 expectedRemovedLabels: formatLabels(labels.Help, labels.GoodFirstIssue), 122 issueLabels: []string{labels.Help, labels.GoodFirstIssue}, 123 }, 124 { 125 name: "Want to add goodFirstIssueLabel and helpLabel, don't have both", 126 body: "/good-first-issue", 127 expectedNewLabels: formatLabels(labels.Help, labels.GoodFirstIssue), 128 expectedRemovedLabels: []string{}, 129 issueLabels: []string{}, 130 }, 131 { 132 name: "Want to add goodFirstIssueLabel and helpLabel, don't have goodFirstIssueLabel but have helpLabel", 133 body: "/good-first-issue", 134 expectedNewLabels: formatLabels(labels.GoodFirstIssue), 135 expectedRemovedLabels: []string{}, 136 issueLabels: []string{labels.Help}, 137 }, 138 { 139 name: "Want to add goodFirstIssueLabel and helpLabel, have both", 140 body: "/good-first-issue", 141 expectedNewLabels: []string{}, 142 expectedRemovedLabels: []string{}, 143 issueLabels: []string{labels.Help, labels.GoodFirstIssue}, 144 }, 145 { 146 name: "Want to remove goodFirstIssueLabel, have helpLabel and goodFirstIssueLabel", 147 body: "/remove-good-first-issue", 148 expectedNewLabels: []string{}, 149 expectedRemovedLabels: formatLabels(labels.GoodFirstIssue), 150 issueLabels: []string{labels.Help, labels.GoodFirstIssue}, 151 }, 152 { 153 name: "Want to remove goodFirstIssueLabel, have goodFirstIssueLabel", 154 body: "/remove-good-first-issue", 155 expectedNewLabels: []string{}, 156 expectedRemovedLabels: formatLabels(labels.GoodFirstIssue), 157 issueLabels: []string{labels.GoodFirstIssue}, 158 }, 159 { 160 name: "Want to remove goodFirstIssueLabel, have helpLabel but don't have goodFirstIssueLabel", 161 body: "/remove-good-first-issue", 162 expectedNewLabels: []string{}, 163 expectedRemovedLabels: []string{}, 164 issueLabels: []string{labels.Help}, 165 }, 166 { 167 name: "Want to remove goodFirstIssueLabel, but don't have it", 168 body: "/remove-good-first-issue", 169 expectedNewLabels: []string{}, 170 expectedRemovedLabels: []string{}, 171 issueLabels: []string{}, 172 }, 173 } 174 175 for _, tc := range testcases { 176 sort.Strings(tc.expectedNewLabels) 177 fakeClient := &fakegithub.FakeClient{ 178 Issues: make([]github.Issue, 1), 179 IssueComments: make(map[int][]github.IssueComment), 180 RepoLabelsExisting: []string{labels.Help, labels.GoodFirstIssue}, 181 IssueLabelsAdded: []string{}, 182 IssueLabelsRemoved: []string{}, 183 } 184 // Add initial labels 185 for _, label := range tc.issueLabels { 186 fakeClient.AddLabel("org", "repo", 1, label) 187 } 188 189 if len(tc.issueState) == 0 { 190 tc.issueState = "open" 191 } 192 if len(tc.action) == 0 { 193 tc.action = github.GenericCommentActionCreated 194 } 195 196 e := &github.GenericCommentEvent{ 197 IsPR: tc.isPR, 198 IssueState: tc.issueState, 199 Action: tc.action, 200 Body: tc.body, 201 Number: 1, 202 Repo: github.Repo{Owner: github.User{Login: "org"}, Name: "repo"}, 203 User: github.User{Login: "Alice"}, 204 } 205 err := handle(fakeClient, logrus.WithField("plugin", pluginName), &fakePruner{}, e) 206 if err != nil { 207 t.Errorf("For case %s, didn't expect error from label test: %v", tc.name, err) 208 continue 209 } 210 211 // Check that all the correct labels (and only the correct labels) were added. 212 expectLabels := append(formatLabels(tc.issueLabels...), tc.expectedNewLabels...) 213 if expectLabels == nil { 214 expectLabels = []string{} 215 } 216 sort.Strings(expectLabels) 217 sort.Strings(fakeClient.IssueLabelsAdded) 218 if !reflect.DeepEqual(expectLabels, fakeClient.IssueLabelsAdded) { 219 t.Errorf("(%s): Expected the labels %q to be added, but %q were added.", tc.name, expectLabels, fakeClient.IssueLabelsAdded) 220 } 221 222 sort.Strings(tc.expectedRemovedLabels) 223 sort.Strings(fakeClient.IssueLabelsRemoved) 224 if !reflect.DeepEqual(tc.expectedRemovedLabels, fakeClient.IssueLabelsRemoved) { 225 t.Errorf("(%s): Expected the labels %q to be removed, but %q were removed.", tc.name, tc.expectedRemovedLabels, fakeClient.IssueLabelsRemoved) 226 } 227 } 228 }