github.com/golang/review@v0.0.0-20190122205339-266ee1edf5c3/git-codereview/change_test.go (about) 1 // Copyright 2014 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 main 6 7 import ( 8 "fmt" 9 "strings" 10 "testing" 11 ) 12 13 func TestChange(t *testing.T) { 14 gt := newGitTest(t) 15 defer gt.done() 16 17 t.Logf("master -> master") 18 testMain(t, "change", "master") 19 testRan(t, "git checkout -q master") 20 21 testCommitMsg = "foo: my commit msg" 22 t.Logf("master -> work") 23 testMain(t, "change", "work") 24 testRan(t, "git checkout -q -b work", 25 "git branch -q --set-upstream-to origin/master") 26 27 t.Logf("work -> master") 28 testMain(t, "change", "master") 29 testRan(t, "git checkout -q master") 30 31 t.Logf("master -> work with staged changes") 32 write(t, gt.client+"/file", "new content") 33 trun(t, gt.client, "git", "add", "file") 34 testMain(t, "change", "work") 35 testRan(t, "git checkout -q work", 36 "git commit -q --allow-empty -m foo: my commit msg") 37 38 t.Logf("master -> dev.branch") 39 testMain(t, "change", "dev.branch") 40 testRan(t, "git checkout -q -t -b dev.branch origin/dev.branch") 41 } 42 43 func TestChangeHEAD(t *testing.T) { 44 gt := newGitTest(t) 45 defer gt.done() 46 47 testMainDied(t, "change", "HeAd") 48 testPrintedStderr(t, "invalid branch name \"HeAd\": ref name HEAD is reserved for git") 49 } 50 51 func TestChangeAhead(t *testing.T) { 52 gt := newGitTest(t) 53 defer gt.done() 54 55 // commit to master (mistake) 56 write(t, gt.client+"/file", "new content") 57 trun(t, gt.client, "git", "add", "file") 58 trun(t, gt.client, "git", "commit", "-m", "msg") 59 60 testMainDied(t, "change", "work") 61 testPrintedStderr(t, "bad repo state: branch master is ahead of origin/master") 62 } 63 64 func TestMessageRE(t *testing.T) { 65 for _, c := range []struct { 66 in string 67 want bool 68 }{ 69 {"blah", false}, 70 {"[release-branch.go1.4] blah", false}, 71 {"[release-branch.go1.4] math: fix cosine", true}, 72 {"math: fix cosine", true}, 73 {"math/rand: make randomer", true}, 74 {"math/rand, crypto/rand: fix random sources", true}, 75 {"cmd/internal/rsc.io/x86: update from external repo", true}, 76 } { 77 got := messageRE.MatchString(c.in) 78 if got != c.want { 79 t.Errorf("MatchString(%q) = %v, want %v", c.in, got, c.want) 80 } 81 } 82 } 83 84 func TestChangeAmendCommit(t *testing.T) { 85 gt := newGitTest(t) 86 defer gt.done() 87 88 testCommitMsg = "foo: amended commit message" 89 gt.work(t) 90 91 write(t, gt.client+"/file", "new content in work to be amend") 92 trun(t, gt.client, "git", "add", "file") 93 testMain(t, "change") 94 } 95 96 func TestChangeFailAmendWithMultiplePending(t *testing.T) { 97 gt := newGitTest(t) 98 defer gt.done() 99 100 testCommitMsg = "foo: amended commit message" 101 gt.work(t) 102 gt.work(t) 103 104 write(t, gt.client+"/file", "new content in work to be amend") 105 trun(t, gt.client, "git", "add", "file") 106 testMainDied(t, "change") 107 testPrintedStderr(t, "multiple changes pending") 108 } 109 110 func TestChangeCL(t *testing.T) { 111 gt := newGitTest(t) 112 defer gt.done() 113 114 srv := newGerritServer(t) 115 defer srv.done() 116 117 // Ensure that 'change' with a CL accepts we have gerrit. Test address is injected by newGerritServer. 118 write(t, gt.server+"/codereview.cfg", "gerrit: on") 119 trun(t, gt.server, "git", "add", "codereview.cfg") 120 trun(t, gt.server, "git", "commit", "-m", "codereview.cfg on master") 121 trun(t, gt.client, "git", "pull") 122 defer srv.done() 123 124 hash1 := trim(trun(t, gt.server, "git", "rev-parse", "dev.branch")) 125 hash2 := trim(trun(t, gt.server, "git", "rev-parse", "release.branch")) 126 trun(t, gt.server, "git", "update-ref", "refs/changes/00/100/1", hash1) 127 trun(t, gt.server, "git", "update-ref", "refs/changes/00/100/2", hash2) 128 trun(t, gt.server, "git", "update-ref", "refs/changes/00/100/3", hash1) 129 srv.setReply("/a/changes/100", gerritReply{f: func() gerritReply { 130 changeJSON := `{ 131 "current_revision": "HASH", 132 "revisions": { 133 "HASH": { 134 "_number": 3 135 } 136 } 137 }` 138 changeJSON = strings.Replace(changeJSON, "HASH", hash1, -1) 139 return gerritReply{body: ")]}'\n" + changeJSON} 140 }}) 141 142 checkChangeCL := func(arg, ref, hash string) { 143 testMain(t, "change", "master") 144 testMain(t, "change", arg) 145 testRan(t, 146 fmt.Sprintf("git fetch -q origin %s", ref), 147 "git checkout -q FETCH_HEAD") 148 if hash != trim(trun(t, gt.client, "git", "rev-parse", "HEAD")) { 149 t.Fatalf("hash do not match for CL %s", arg) 150 } 151 } 152 153 checkChangeCL("100/1", "refs/changes/00/100/1", hash1) 154 checkChangeCL("100/2", "refs/changes/00/100/2", hash2) 155 checkChangeCL("100", "refs/changes/00/100/3", hash1) 156 } 157 158 func TestChangeWithMessage(t *testing.T) { 159 gt := newGitTest(t) 160 defer gt.done() 161 162 testMain(t, "change", "new_branch") 163 testMain(t, "change", "-m", "foo: some commit message") 164 testRan(t, "git commit -q --allow-empty -m foo: some commit message") 165 }