golang.org/x/build@v0.0.0-20240506185731-218518f32b70/internal/task/updateproxytestrepo_test.go (about) 1 // Copyright 2023 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package task 6 7 import ( 8 "context" 9 "fmt" 10 "testing" 11 "time" 12 13 "golang.org/x/build/internal/workflow" 14 ) 15 16 func TestUpdateProxyTestRepo(t *testing.T) { 17 tc := []struct { 18 name string 19 old, new string 20 wantUpdate bool 21 }{ 22 {"minor version", "1.18.1", "1.18.5", true}, 23 {"update to rc", "1.20", "1.21rc1", true}, 24 {"update rc to point", "1.18rc1", "1.18.0", true}, 25 {"no update earlier major", "1.18.5", "1.17.4", false}, 26 {"no update earlier major rc", "1.18rc1", "1.17", false}, 27 } 28 29 for _, tt := range tc { 30 t.Run(tt.name, func(t *testing.T) { 31 fakeRepo := NewFakeRepo(t, "fake") 32 fakeGerrit := NewFakeGerrit(t, fakeRepo) 33 // We need to do this so we can push to the branch we checked out. 34 fakeRepo.runGit("config", "receive.denyCurrentBranch", "updateInstead") 35 36 fakeRepo.CommitOnBranch("master", map[string]string{ 37 "go.mod": fmt.Sprintf("module test\n\ngo %s\n", tt.old), 38 }) 39 fakeRepo.Tag("v1.0.0", "master") 40 41 upgradeGoVersion := &UpdateProxyTestRepoTasks{ 42 Git: &Git{}, 43 GerritURL: fakeRepo.dir.dir, 44 Branch: "master", 45 } 46 47 ctx, cancel := context.WithTimeout(context.Background(), time.Minute) 48 defer cancel() 49 if _, err := upgradeGoVersion.UpdateProxyTestRepo(&workflow.TaskContext{Context: ctx}, Published{Version: "go" + tt.new}); err != nil { 50 t.Fatal(err) 51 } 52 53 tags, err := fakeGerrit.ListTags(ctx, fakeRepo.name) 54 if err != nil { 55 t.Fatalf("unable to list tags: %v", err) 56 } 57 if len(tags) != 1 || tags[0] != "v1.0.0" { 58 t.Errorf("expect v1.0.0, got %v", tags) 59 } 60 61 checkCommit := func(commit string) { 62 value, err := fakeGerrit.ReadFile(ctx, fakeRepo.name, commit, "go.mod") 63 if err != nil { 64 t.Fatalf("unable to read go.mod: %v", err) 65 } 66 wantVersion := tt.new 67 if !tt.wantUpdate { 68 wantVersion = tt.old 69 } 70 71 want := fmt.Sprintf("module test\n\ngo %s\n", wantVersion) 72 if string(value) != want { 73 t.Errorf("expected %q, got %q", want, string(value)) 74 } 75 76 } 77 78 tag, err := fakeGerrit.GetTag(ctx, fakeRepo.name, "v1.0.0") 79 if err != nil { 80 t.Fatalf("unable to get tag v1.0.0: %v", err) 81 } 82 checkCommit(tag.Revision) 83 84 head, err := fakeGerrit.ReadBranchHead(ctx, fakeRepo.name, "master") 85 if err != nil { 86 t.Fatalf("unable to read branch head: %v", err) 87 } 88 checkCommit(head) 89 }) 90 } 91 }