@@ -158,7 +158,7 @@ func TestCreateBranchIfMinor(t *testing.T) {
158
158
t .Errorf ("createBranchIfMinor() should return nil but return err: %v" , err )
159
159
}
160
160
161
- // Created branch should have same revision as master branch's HEAD .
161
+ // Created branch should have same revision as master branch's head .
162
162
if tc .wantBranch != "" {
163
163
gotRevision , err := gerritClient .ReadBranchHead (ctx , "tools" , tc .wantBranch )
164
164
if err != nil {
@@ -171,3 +171,144 @@ func TestCreateBranchIfMinor(t *testing.T) {
171
171
})
172
172
}
173
173
}
174
+
175
+ func TestUpdateCodeReviewConfig (t * testing.T ) {
176
+ ctx := context .Background ()
177
+ testcases := []struct {
178
+ name string
179
+ version string
180
+ config string
181
+ wantCommit bool
182
+ wantConfig string
183
+ }{
184
+ {
185
+ name : "should update the codereview.cfg with version 1.2 for input minor release 1.2.0" ,
186
+ version : "v1.2.0" ,
187
+ config : "foo" ,
188
+ wantCommit : true ,
189
+ wantConfig : `issuerepo: golang/go
190
+ branch: gopls-release-branch.1.2
191
+ parent-branch: master
192
+ ` ,
193
+ },
194
+ {
195
+ name : "should update the codereview.cfg with version 1.2 for input patch release 1.2.3" ,
196
+ version : "v1.2.3" ,
197
+ config : "foo" ,
198
+ wantCommit : true ,
199
+ wantConfig : `issuerepo: golang/go
200
+ branch: gopls-release-branch.1.2
201
+ parent-branch: master
202
+ ` ,
203
+ },
204
+ {
205
+ name : "no need to update the config for a minor release 1.3.0" ,
206
+ version : "v1.3.0" ,
207
+ config : `issuerepo: golang/go
208
+ branch: gopls-release-branch.1.3
209
+ parent-branch: master
210
+ ` ,
211
+ wantCommit : false ,
212
+ wantConfig : `issuerepo: golang/go
213
+ branch: gopls-release-branch.1.3
214
+ parent-branch: master
215
+ ` ,
216
+ },
217
+ {
218
+ name : "no need to update the config for a patch release 1.3.3" ,
219
+ version : "v1.3.3" ,
220
+ config : `issuerepo: golang/go
221
+ branch: gopls-release-branch.1.3
222
+ parent-branch: master
223
+ ` ,
224
+ wantCommit : false ,
225
+ wantConfig : `issuerepo: golang/go
226
+ branch: gopls-release-branch.1.3
227
+ parent-branch: master
228
+ ` ,
229
+ },
230
+ }
231
+ for _ , tc := range testcases {
232
+ t .Run (tc .name , func (t * testing.T ) {
233
+ tools := NewFakeRepo (t , "tools" )
234
+ _ = tools .Commit (map [string ]string {
235
+ "go.mod" : "module golang.org/x/tools\n " ,
236
+ "go.sum" : "\n " ,
237
+ })
238
+ _ = tools .Commit (map [string ]string {
239
+ "codereview.cfg" : tc .config ,
240
+ })
241
+
242
+ gerritClient := NewFakeGerrit (t , tools )
243
+
244
+ headMaster , err := gerritClient .ReadBranchHead (ctx , "tools" , "master" )
245
+ if err != nil {
246
+ t .Fatalf ("ReadBranchHead should be able to get revision of master branch's head: %v" , err )
247
+ }
248
+
249
+ configMaster , err := gerritClient .ReadFile (ctx , "tools" , headMaster , "codereview.cfg" )
250
+ if err != nil {
251
+ t .Fatalf ("ReadFile should be able to read the codereview.cfg file from master branch head: %v" , err )
252
+ }
253
+
254
+ semv , _ := parseSemver (tc .version )
255
+ releaseBranch := goplsReleaseBranchName (semv )
256
+ if _ , err := gerritClient .CreateBranch (ctx , "tools" , releaseBranch , gerrit.BranchInput {Revision : headMaster }); err != nil {
257
+ t .Fatalf ("failed to create the branch %q: %v" , releaseBranch , err )
258
+ }
259
+
260
+ headRelease , err := gerritClient .ReadBranchHead (ctx , "tools" , releaseBranch )
261
+ if err != nil {
262
+ t .Fatalf ("ReadBranchHead should be able to get revision of release branch's head: %v" , err )
263
+ }
264
+
265
+ tasks := & ReleaseGoplsTasks {
266
+ Gerrit : gerritClient ,
267
+ CloudBuild : NewFakeCloudBuild (t , gerritClient , "" , nil , fakeGo ),
268
+ }
269
+
270
+ _ , err = tasks .updateCodeReviewConfig (& workflow.TaskContext {Context : ctx , Logger : & testLogger {t , "" }}, semv , nil )
271
+ if err != nil {
272
+ t .Fatalf ("updateCodeReviewConfig() returns error: %v" , err )
273
+ }
274
+
275
+ // master branch's head commit should not change.
276
+ headMasterAfter , err := gerritClient .ReadBranchHead (ctx , "tools" , "master" )
277
+ if err != nil {
278
+ t .Fatalf ("ReadBranchHead() should be able to get revision of master branch's head: %v" , err )
279
+ }
280
+ if headMasterAfter != headMaster {
281
+ t .Errorf ("updateCodeReviewConfig() should not change master branch's head, got = %s want = %s" , headMasterAfter , headMaster )
282
+ }
283
+
284
+ // master branch's head codereview.cfg content should not change.
285
+ configMasterAfter , err := gerritClient .ReadFile (ctx , "tools" , headMasterAfter , "codereview.cfg" )
286
+ if err != nil {
287
+ t .Fatalf ("ReadFile() should be able to read the codereview.cfg file from master branch head: %v" , err )
288
+ }
289
+ if diff := cmp .Diff (configMaster , configMasterAfter ); diff != "" {
290
+ t .Errorf ("updateCodeReviewConfig() should not change codereview.cfg content in master branch (-want +got) \n %s" , diff )
291
+ }
292
+
293
+ // verify the release branch commit have the expected behavior.
294
+ headReleaseAfter , err := gerritClient .ReadBranchHead (ctx , "tools" , releaseBranch )
295
+ if err != nil {
296
+ t .Fatalf ("ReadBranchHead() should be able to get revision of master branch's head: %v" , err )
297
+ }
298
+ if tc .wantCommit && headReleaseAfter == headRelease {
299
+ t .Errorf ("updateCodeReviewConfig() should have one commit to release branch, head of branch got = %s want = %s" , headRelease , headReleaseAfter )
300
+ } else if ! tc .wantCommit && headReleaseAfter != headRelease {
301
+ t .Errorf ("updateCodeReviewConfig() should have not change release branch's head, got = %s want = %s" , headRelease , headReleaseAfter )
302
+ }
303
+
304
+ // verify the release branch configreview.cfg have the expected content.
305
+ configReleaseAfter , err := gerritClient .ReadFile (ctx , "tools" , headReleaseAfter , "codereview.cfg" )
306
+ if err != nil {
307
+ t .Fatalf ("ReadFile() should be able to read the codereview.cfg file from release branch head: %v" , err )
308
+ }
309
+ if diff := cmp .Diff (tc .wantConfig , string (configReleaseAfter )); diff != "" {
310
+ t .Errorf ("codereview.cfg mismatch (-want +got) \n %s" , diff )
311
+ }
312
+ })
313
+ }
314
+ }
0 commit comments