github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/review/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 "testing"
     8  
     9  func TestChange(t *testing.T) {
    10  	gt := newGitTest(t)
    11  	defer gt.done()
    12  
    13  	t.Logf("master -> master")
    14  	testMain(t, "change", "master")
    15  	testRan(t, "git checkout -q master")
    16  
    17  	testCommitMsg = "foo: my commit msg"
    18  	t.Logf("master -> work")
    19  	testMain(t, "change", "work")
    20  	testRan(t, "git checkout -q -b work",
    21  		"git branch -q --set-upstream-to origin/master")
    22  
    23  	t.Logf("work -> master")
    24  	testMain(t, "change", "master")
    25  	testRan(t, "git checkout -q master")
    26  
    27  	t.Logf("master -> work with staged changes")
    28  	write(t, gt.client+"/file", "new content")
    29  	trun(t, gt.client, "git", "add", "file")
    30  	testMain(t, "change", "work")
    31  	testRan(t, "git checkout -q work",
    32  		"git commit -q --allow-empty -m foo: my commit msg")
    33  
    34  	t.Logf("master -> dev.branch")
    35  	testMain(t, "change", "dev.branch")
    36  	testRan(t, "git checkout -q -t -b dev.branch origin/dev.branch")
    37  }
    38  
    39  func TestChangeHEAD(t *testing.T) {
    40  	gt := newGitTest(t)
    41  	defer gt.done()
    42  
    43  	testMainDied(t, "change", "HeAd")
    44  	testPrintedStderr(t, "invalid branch name \"HeAd\": ref name HEAD is reserved for git")
    45  }
    46  
    47  func TestChangeAhead(t *testing.T) {
    48  	gt := newGitTest(t)
    49  	defer gt.done()
    50  
    51  	// commit to master (mistake)
    52  	write(t, gt.client+"/file", "new content")
    53  	trun(t, gt.client, "git", "add", "file")
    54  	trun(t, gt.client, "git", "commit", "-m", "msg")
    55  
    56  	testMainDied(t, "change", "work")
    57  	testPrintedStderr(t, "bad repo state: branch master is ahead of origin/master")
    58  }
    59  
    60  func TestMessageRE(t *testing.T) {
    61  	for _, c := range []struct {
    62  		in   string
    63  		want bool
    64  	}{
    65  		{"blah", false},
    66  		{"[release-branch.go1.4] blah", false},
    67  		{"[release-branch.go1.4] math: fix cosine", true},
    68  		{"math: fix cosine", true},
    69  		{"math/rand: make randomer", true},
    70  		{"math/rand, crypto/rand: fix random sources", true},
    71  		{"cmd/internal/rsc.io/x86: update from external repo", true},
    72  	} {
    73  		got := messageRE.MatchString(c.in)
    74  		if got != c.want {
    75  			t.Errorf("MatchString(%q) = %v, want %v", c.in, got, c.want)
    76  		}
    77  	}
    78  }