github.com/abayer/test-infra@v0.0.5/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 ) 29 30 type fakePruner struct{} 31 32 func (fp *fakePruner) PruneComments(shouldPrune func(github.IssueComment) bool) {} 33 34 func formatLabels(labels ...string) []string { 35 r := []string{} 36 for _, l := range labels { 37 r = append(r, fmt.Sprintf("%s/%s#%d:%s", "org", "repo", 1, l)) 38 } 39 if len(r) == 0 { 40 return nil 41 } 42 return r 43 } 44 45 func TestLabel(t *testing.T) { 46 type testCase struct { 47 name string 48 body string 49 expectedNewLabels []string 50 expectedRemovedLabels []string 51 issueLabels []string 52 } 53 testcases := []testCase{ 54 { 55 name: "Irrelevant comment", 56 body: "irrelelvant", 57 expectedNewLabels: []string{}, 58 expectedRemovedLabels: []string{}, 59 issueLabels: []string{}, 60 }, 61 { 62 name: "Want helpLabel", 63 body: "/help", 64 expectedNewLabels: formatLabels(helpLabel), 65 expectedRemovedLabels: []string{}, 66 issueLabels: []string{}, 67 }, 68 { 69 name: "Want helpLabel, already have it.", 70 body: "/help", 71 expectedNewLabels: []string{}, 72 expectedRemovedLabels: []string{}, 73 issueLabels: []string{helpLabel}, 74 }, 75 { 76 name: "Want to remove helpLabel, have it", 77 body: "/remove-help", 78 expectedNewLabels: []string{}, 79 expectedRemovedLabels: formatLabels(helpLabel), 80 issueLabels: []string{helpLabel}, 81 }, 82 { 83 name: "Want to remove helpLabel, don't have it", 84 body: "/remove-help", 85 expectedNewLabels: []string{}, 86 expectedRemovedLabels: []string{}, 87 issueLabels: []string{}, 88 }, 89 { 90 name: "Want to remove helpLabel and goodFirstIssueLabel, have helpLabel and goodFirstIssueLabel", 91 body: "/remove-help", 92 expectedNewLabels: []string{}, 93 expectedRemovedLabels: formatLabels(helpLabel, goodFirstIssueLabel), 94 issueLabels: []string{helpLabel, goodFirstIssueLabel}, 95 }, 96 { 97 name: "Want to add goodFirstIssueLabel and helpLabel, don't have both", 98 body: "/good-first-issue", 99 expectedNewLabels: formatLabels(helpLabel, goodFirstIssueLabel), 100 expectedRemovedLabels: []string{}, 101 issueLabels: []string{}, 102 }, 103 { 104 name: "Want to add goodFirstIssueLabel and helpLabel, don't have goodFirstIssueLabel but have helpLabel", 105 body: "/good-first-issue", 106 expectedNewLabels: formatLabels(goodFirstIssueLabel), 107 expectedRemovedLabels: []string{}, 108 issueLabels: []string{helpLabel}, 109 }, 110 { 111 name: "Want to add goodFirstIssueLabel and helpLabel, have both", 112 body: "/good-first-issue", 113 expectedNewLabels: []string{}, 114 expectedRemovedLabels: []string{}, 115 issueLabels: []string{helpLabel, goodFirstIssueLabel}, 116 }, 117 { 118 name: "Want to remove goodFirstIssueLabel, have helpLabel and goodFirstIssueLabel", 119 body: "/remove-good-first-issue", 120 expectedNewLabels: []string{}, 121 expectedRemovedLabels: formatLabels(goodFirstIssueLabel), 122 issueLabels: []string{helpLabel, goodFirstIssueLabel}, 123 }, 124 { 125 name: "Want to remove goodFirstIssueLabel, have goodFirstIssueLabel", 126 body: "/remove-good-first-issue", 127 expectedNewLabels: []string{}, 128 expectedRemovedLabels: formatLabels(goodFirstIssueLabel), 129 issueLabels: []string{goodFirstIssueLabel}, 130 }, 131 { 132 name: "Want to remove goodFirstIssueLabel, have helpLabel but don't have goodFirstIssueLabel", 133 body: "/remove-good-first-issue", 134 expectedNewLabels: []string{}, 135 expectedRemovedLabels: []string{}, 136 issueLabels: []string{helpLabel}, 137 }, 138 { 139 name: "Want to remove goodFirstIssueLabel, but don't have it", 140 body: "/remove-good-first-issue", 141 expectedNewLabels: []string{}, 142 expectedRemovedLabels: []string{}, 143 issueLabels: []string{}, 144 }, 145 } 146 147 for _, tc := range testcases { 148 sort.Strings(tc.expectedNewLabels) 149 fakeClient := &fakegithub.FakeClient{ 150 Issues: make([]github.Issue, 1), 151 IssueComments: make(map[int][]github.IssueComment), 152 ExistingLabels: []string{helpLabel, goodFirstIssueLabel}, 153 LabelsAdded: []string{}, 154 LabelsRemoved: []string{}, 155 } 156 // Add initial labels 157 for _, label := range tc.issueLabels { 158 fakeClient.AddLabel("org", "repo", 1, label) 159 } 160 e := &github.GenericCommentEvent{ 161 Action: github.GenericCommentActionCreated, 162 Body: tc.body, 163 Number: 1, 164 Repo: github.Repo{Owner: github.User{Login: "org"}, Name: "repo"}, 165 User: github.User{Login: "Alice"}, 166 } 167 err := handle(fakeClient, logrus.WithField("plugin", pluginName), &fakePruner{}, e) 168 if err != nil { 169 t.Errorf("For case %s, didn't expect error from label test: %v", tc.name, err) 170 continue 171 } 172 173 // Check that all the correct labels (and only the correct labels) were added. 174 expectLabels := append(formatLabels(tc.issueLabels...), tc.expectedNewLabels...) 175 if expectLabels == nil { 176 expectLabels = []string{} 177 } 178 sort.Strings(expectLabels) 179 sort.Strings(fakeClient.LabelsAdded) 180 if !reflect.DeepEqual(expectLabels, fakeClient.LabelsAdded) { 181 t.Errorf("(%s): Expected the labels %q to be added, but %q were added.", tc.name, expectLabels, fakeClient.LabelsAdded) 182 } 183 184 sort.Strings(tc.expectedRemovedLabels) 185 sort.Strings(fakeClient.LabelsRemoved) 186 if !reflect.DeepEqual(tc.expectedRemovedLabels, fakeClient.LabelsRemoved) { 187 t.Errorf("(%s): Expected the labels %q to be removed, but %q were removed.", tc.name, tc.expectedRemovedLabels, fakeClient.LabelsRemoved) 188 } 189 } 190 }