github.com/shashidharatd/test-infra@v0.0.0-20171006011030-71304e1ca560/prow/plugins/lgtm/lgtm_test.go (about) 1 /* 2 Copyright 2016 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 lgtm 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 TestLGTMComment(t *testing.T) { 30 // "a" is the author, "a", "r1", and "r2" are reviewers. 31 var testcases = []struct { 32 name string 33 body string 34 commenter string 35 hasLGTM bool 36 shouldToggle bool 37 shouldComment bool 38 shouldAssign bool 39 }{ 40 { 41 name: "non-lgtm comment", 42 body: "uh oh", 43 commenter: "o", 44 hasLGTM: false, 45 shouldToggle: false, 46 }, 47 { 48 name: "lgtm comment by reviewer, no lgtm on pr", 49 body: "/lgtm", 50 commenter: "r1", 51 hasLGTM: false, 52 shouldToggle: true, 53 }, 54 { 55 name: "LGTM comment by reviewer, no lgtm on pr", 56 body: "/LGTM", 57 commenter: "r1", 58 hasLGTM: false, 59 shouldToggle: true, 60 }, 61 { 62 name: "lgtm comment by reviewer, lgtm on pr", 63 body: "/lgtm", 64 commenter: "r1", 65 hasLGTM: true, 66 shouldToggle: false, 67 }, 68 { 69 name: "lgtm comment by author", 70 body: "/lgtm", 71 commenter: "a", 72 hasLGTM: false, 73 shouldToggle: false, 74 shouldComment: true, 75 }, 76 { 77 name: "lgtm cancel by author", 78 body: "/lgtm cancel", 79 commenter: "a", 80 hasLGTM: true, 81 shouldToggle: true, 82 }, 83 { 84 name: "lgtm comment by non-reviewer", 85 body: "/lgtm", 86 commenter: "o", 87 hasLGTM: false, 88 shouldToggle: true, 89 shouldComment: false, 90 shouldAssign: true, 91 }, 92 { 93 name: "lgtm comment by non-reviewer, with trailing space", 94 body: "/lgtm ", 95 commenter: "o", 96 hasLGTM: false, 97 shouldToggle: true, 98 shouldComment: false, 99 shouldAssign: true, 100 }, 101 { 102 name: "lgtm comment by non-reviewer, with no-issue", 103 body: "/lgtm no-issue", 104 commenter: "o", 105 hasLGTM: false, 106 shouldToggle: true, 107 shouldComment: false, 108 shouldAssign: true, 109 }, 110 { 111 name: "lgtm comment by non-reviewer, with no-issue and trailing space", 112 body: "/lgtm no-issue \r", 113 commenter: "o", 114 hasLGTM: false, 115 shouldToggle: true, 116 shouldComment: false, 117 shouldAssign: true, 118 }, 119 { 120 name: "lgtm comment by rando", 121 body: "/lgtm", 122 commenter: "not-in-the-org", 123 hasLGTM: false, 124 shouldToggle: false, 125 shouldComment: true, 126 shouldAssign: false, 127 }, 128 { 129 name: "lgtm cancel by non-reviewer", 130 body: "/lgtm cancel", 131 commenter: "o", 132 hasLGTM: true, 133 shouldToggle: true, 134 shouldComment: false, 135 shouldAssign: true, 136 }, 137 { 138 name: "lgtm cancel by rando", 139 body: "/lgtm cancel", 140 commenter: "not-in-the-org", 141 hasLGTM: true, 142 shouldToggle: false, 143 shouldComment: true, 144 shouldAssign: false, 145 }, 146 { 147 name: "lgtm cancel comment by reviewer", 148 body: "/lgtm cancel", 149 commenter: "r1", 150 hasLGTM: true, 151 shouldToggle: true, 152 }, 153 { 154 name: "lgtm cancel comment by reviewer, with trailing space", 155 body: "/lgtm cancel \r", 156 commenter: "r1", 157 hasLGTM: true, 158 shouldToggle: true, 159 }, 160 { 161 name: "lgtm cancel comment by reviewer, no lgtm", 162 body: "/lgtm cancel", 163 commenter: "r1", 164 hasLGTM: false, 165 shouldToggle: false, 166 }, 167 } 168 for _, tc := range testcases { 169 fc := &fakegithub.FakeClient{ 170 IssueComments: make(map[int][]github.IssueComment), 171 } 172 e := &github.GenericCommentEvent{ 173 Action: github.GenericCommentActionCreated, 174 IssueState: "open", 175 IsPR: true, 176 Body: tc.body, 177 User: github.User{Login: tc.commenter}, 178 IssueAuthor: github.User{Login: "a"}, 179 Number: 5, 180 Assignees: []github.User{{Login: "a"}, {Login: "r1"}, {Login: "r2"}}, 181 Repo: github.Repo{Owner: github.User{Login: "org"}, Name: "repo"}, 182 HTMLURL: "<url>", 183 } 184 if tc.hasLGTM { 185 fc.LabelsAdded = []string{"org/repo#5:" + lgtmLabel} 186 } 187 if err := handle(fc, logrus.WithField("plugin", pluginName), e); err != nil { 188 t.Errorf("For case %s, didn't expect error from lgtmComment: %v", tc.name, err) 189 continue 190 } 191 if tc.shouldAssign { 192 found := false 193 for _, a := range fc.AssigneesAdded { 194 if a == fmt.Sprintf("%s/%s#%d:%s", "org", "repo", 5, tc.commenter) { 195 found = true 196 break 197 } 198 } 199 if !found || len(fc.AssigneesAdded) != 1 { 200 t.Errorf("For case %s, should have assigned %s but added assignees are %s", tc.name, tc.commenter, fc.AssigneesAdded) 201 } 202 } else if len(fc.AssigneesAdded) != 0 { 203 t.Errorf("For case %s, should not have assigned anyone but assigned %s", tc.name, fc.AssigneesAdded) 204 } 205 if tc.shouldToggle { 206 if tc.hasLGTM { 207 if len(fc.LabelsRemoved) == 0 { 208 t.Errorf("For case %s, should have removed LGTM.", tc.name) 209 } else if len(fc.LabelsAdded) > 1 { 210 t.Errorf("For case %s, should not have added LGTM.", tc.name) 211 } 212 } else { 213 if len(fc.LabelsAdded) == 0 { 214 t.Errorf("For case %s, should have added LGTM.", tc.name) 215 } else if len(fc.LabelsRemoved) > 0 { 216 t.Errorf("For case %s, should not have removed LGTM.", tc.name) 217 } 218 } 219 } else if len(fc.LabelsRemoved) > 0 { 220 t.Errorf("For case %s, should not have removed LGTM.", tc.name) 221 } else if (tc.hasLGTM && len(fc.LabelsAdded) > 1) || (!tc.hasLGTM && len(fc.LabelsAdded) > 0) { 222 t.Errorf("For case %s, should not have added LGTM.", tc.name) 223 } 224 if tc.shouldComment && len(fc.IssueComments[5]) != 1 { 225 t.Errorf("For case %s, should have commented.", tc.name) 226 } else if !tc.shouldComment && len(fc.IssueComments[5]) != 0 { 227 t.Errorf("For case %s, should not have commented.", tc.name) 228 } 229 } 230 }