github.com/golang/review@v0.0.0-20190122205339-266ee1edf5c3/git-codereview/mail_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  	"testing"
    10  )
    11  
    12  func TestMail(t *testing.T) {
    13  	gt := newGitTest(t)
    14  	defer gt.done()
    15  	gt.work(t)
    16  
    17  	h := CurrentBranch().Pending()[0].ShortHash
    18  
    19  	// fake auth information to avoid Gerrit error
    20  	auth.host = "gerrit.fake"
    21  	auth.user = "not-a-user"
    22  	defer func() {
    23  		auth.host = ""
    24  		auth.user = ""
    25  	}()
    26  
    27  	testMain(t, "mail")
    28  	testRan(t,
    29  		"git push -q origin HEAD:refs/for/master",
    30  		"git tag -f work.mailed "+h)
    31  }
    32  
    33  func TestDoNotMail(t *testing.T) {
    34  	gt := newGitTest(t)
    35  	defer gt.done()
    36  	gt.work(t)
    37  	trun(t, gt.client, "git", "commit", "--amend", "-m", "This is my commit.\n\nDO NOT MAIL\n")
    38  
    39  	testMainDied(t, "mail")
    40  	testPrintedStderr(t, "DO NOT MAIL")
    41  
    42  	trun(t, gt.client, "git", "commit", "--amend", "-m", "fixup! This is my commit.")
    43  
    44  	testMainDied(t, "mail")
    45  	testPrintedStderr(t, "fixup! commit")
    46  
    47  	trun(t, gt.client, "git", "commit", "--amend", "-m", "squash! This is my commit.")
    48  
    49  	testMainDied(t, "mail")
    50  	testPrintedStderr(t, "squash! commit")
    51  
    52  	trun(t, gt.client, "git", "commit", "--amend", "-m", "This is my commit.\n\nDO NOT MAIL\n")
    53  
    54  	// Do not mail even when the DO NOT MAIL is a parent of the thing we asked to mail.
    55  	gt.work(t)
    56  	testMainDied(t, "mail", "HEAD")
    57  	testPrintedStderr(t, "DO NOT MAIL")
    58  }
    59  
    60  func TestDoNotMailTempFiles(t *testing.T) {
    61  	// fake auth information to avoid Gerrit error
    62  	auth.host = "gerrit.fake"
    63  	auth.user = "not-a-user"
    64  	defer func() {
    65  		auth.host = ""
    66  		auth.user = ""
    67  	}()
    68  
    69  	testFile := func(file string) {
    70  		gt := newGitTest(t)
    71  		defer gt.done()
    72  		gt.work(t)
    73  		gt.workFile(t, file)
    74  		testMainDied(t, "mail", "HEAD")
    75  		testPrintedStderr(t, "cannot mail temporary")
    76  	}
    77  
    78  	testFile("vim-backup.go~")
    79  	testFile("#emacs-auto-save.go#")
    80  	testFile(".#emacs-lock.go")
    81  
    82  	// Do not mail when a parent of the thing we asked to mail has temporary files.
    83  	gt := newGitTest(t)
    84  	defer gt.done()
    85  	gt.work(t)
    86  	gt.workFile(t, "backup~")
    87  	gt.work(t)
    88  	testMainDied(t, "mail", "HEAD")
    89  	testPrintedStderr(t, "cannot mail temporary")
    90  }
    91  
    92  func TestMailNonPrintables(t *testing.T) {
    93  	gt := newGitTest(t)
    94  	defer gt.done()
    95  	gt.work(t)
    96  
    97  	// fake auth information to avoid Gerrit error
    98  	auth.host = "gerrit.fake"
    99  	auth.user = "not-a-user"
   100  	defer func() {
   101  		auth.host = ""
   102  		auth.user = ""
   103  	}()
   104  
   105  	trun(t, gt.client, "git", "commit", "--amend", "-m", "This is my commit.\x10\n\n")
   106  	testMainDied(t, "mail")
   107  	testPrintedStderr(t, "message with non-printable")
   108  
   109  	// This should be mailed.
   110  	trun(t, gt.client, "git", "commit", "--amend", "-m", "Printable unicode: \u263A \u0020. Spaces: \v \f \r \t\n\n")
   111  	testMain(t, "mail", "HEAD")
   112  }
   113  
   114  func TestMailGitHub(t *testing.T) {
   115  	gt := newGitTest(t)
   116  	defer gt.done()
   117  	gt.work(t)
   118  
   119  	trun(t, gt.client, "git", "config", "remote.origin.url", "https://github.com/golang/go")
   120  
   121  	testMainDied(t, "mail")
   122  	testPrintedStderr(t, "git origin must be a Gerrit host, not GitHub: https://github.com/golang/go")
   123  }
   124  
   125  func TestMailAmbiguousRevision(t *testing.T) {
   126  	gt := newGitTest(t)
   127  	defer gt.done()
   128  	gt.work(t)
   129  
   130  	t.Logf("creating file that conflicts with revision parameter")
   131  	b := CurrentBranch()
   132  	mkdir(t, gt.client+"/origin")
   133  	write(t, gt.client+"/"+b.Branchpoint()+"..HEAD", "foo")
   134  
   135  	testMain(t, "mail", "-diff")
   136  }
   137  
   138  func TestMailMultiple(t *testing.T) {
   139  	gt := newGitTest(t)
   140  	defer gt.done()
   141  
   142  	srv := newGerritServer(t)
   143  	defer srv.done()
   144  
   145  	gt.work(t)
   146  	gt.work(t)
   147  	gt.work(t)
   148  
   149  	testMainDied(t, "mail")
   150  	testPrintedStderr(t, "cannot mail: multiple changes pending")
   151  
   152  	// Mail first two and test non-HEAD mail.
   153  	h := CurrentBranch().Pending()[1].ShortHash
   154  	testMain(t, "mail", "HEAD^")
   155  	testRan(t,
   156  		"git push -q origin "+h+":refs/for/master",
   157  		"git tag -f work.mailed "+h)
   158  
   159  	// Mail HEAD.
   160  	h = CurrentBranch().Pending()[0].ShortHash
   161  	testMain(t, "mail", "HEAD")
   162  	testRan(t,
   163  		"git push -q origin HEAD:refs/for/master",
   164  		"git tag -f work.mailed "+h)
   165  }
   166  
   167  var reviewerLog = []string{
   168  	"Fake 1 <r1@fake.com>",
   169  	"Fake 1 <r1@fake.com>",
   170  	"Fake 1 <r1@fake.com>",
   171  	"Reviewer 1 <r1@golang.org>",
   172  	"Reviewer 1 <r1@golang.org>",
   173  	"Reviewer 1 <r1@golang.org>",
   174  	"Reviewer 1 <r1@golang.org>",
   175  	"Reviewer 1 <r1@golang.org>",
   176  	"Other <other@golang.org>",
   177  	"<anon@golang.org>",
   178  }
   179  
   180  func TestMailShort(t *testing.T) {
   181  	gt := newGitTest(t)
   182  	defer gt.done()
   183  
   184  	// fake auth information to avoid Gerrit error
   185  	auth.host = "gerrit.fake"
   186  	auth.user = "not-a-user"
   187  	defer func() {
   188  		auth.host = ""
   189  		auth.user = ""
   190  	}()
   191  
   192  	// Seed commit history with reviewers.
   193  	for i, addr := range reviewerLog {
   194  		write(t, gt.server+"/file", fmt.Sprintf("v%d", i))
   195  		trun(t, gt.server, "git", "commit", "-a", "-m", "msg\n\nReviewed-by: "+addr+"\n")
   196  	}
   197  	trun(t, gt.client, "git", "pull")
   198  
   199  	// Do some work.
   200  	gt.work(t)
   201  
   202  	h := CurrentBranch().Pending()[0].ShortHash
   203  
   204  	testMain(t, "mail")
   205  	testRan(t,
   206  		"git push -q origin HEAD:refs/for/master",
   207  		"git tag -f work.mailed "+h)
   208  
   209  	testMain(t, "mail", "-r", "r1")
   210  	testRan(t,
   211  		"git push -q origin HEAD:refs/for/master%r=r1@golang.org",
   212  		"git tag -f work.mailed "+h)
   213  
   214  	testMain(t, "mail", "-r", "other,anon", "-cc", "r1,full@email.com")
   215  	testRan(t,
   216  		"git push -q origin HEAD:refs/for/master%r=other@golang.org,r=anon@golang.org,cc=r1@golang.org,cc=full@email.com",
   217  		"git tag -f work.mailed "+h)
   218  
   219  	testMainDied(t, "mail", "-r", "other", "-r", "anon,r1,missing")
   220  	testPrintedStderr(t, "unknown reviewer: missing")
   221  }
   222  
   223  func TestMailTopic(t *testing.T) {
   224  	gt := newGitTest(t)
   225  	defer gt.done()
   226  	gt.work(t)
   227  
   228  	h := CurrentBranch().Pending()[0].ShortHash
   229  
   230  	// fake auth information to avoid Gerrit error
   231  	auth.host = "gerrit.fake"
   232  	auth.user = "not-a-user"
   233  	defer func() {
   234  		auth.host = ""
   235  		auth.user = ""
   236  	}()
   237  
   238  	testMainDied(t, "mail", "-topic", "contains,comma")
   239  	testPrintedStderr(t, "topic may not contain a comma")
   240  
   241  	testMain(t, "mail", "-topic", "test-topic")
   242  	testRan(t,
   243  		"git push -q origin HEAD:refs/for/master%topic=test-topic",
   244  		"git tag -f work.mailed "+h)
   245  }
   246  
   247  func TestMailHashtag(t *testing.T) {
   248  	gt := newGitTest(t)
   249  	defer gt.done()
   250  	gt.work(t)
   251  
   252  	h := CurrentBranch().Pending()[0].ShortHash
   253  
   254  	// fake auth information to avoid Gerrit error
   255  	auth.host = "gerrit.fake"
   256  	auth.user = "not-a-user"
   257  	defer func() {
   258  		auth.host = ""
   259  		auth.user = ""
   260  	}()
   261  
   262  	testMain(t, "mail", "-hashtag", "test1,test2")
   263  	testRan(t,
   264  		"git push -q origin HEAD:refs/for/master%hashtag=test1,hashtag=test2",
   265  		"git tag -f work.mailed "+h)
   266  	testMain(t, "mail", "-hashtag", "")
   267  	testRan(t,
   268  		"git push -q origin HEAD:refs/for/master",
   269  		"git tag -f work.mailed "+h)
   270  
   271  	testMainDied(t, "mail", "-hashtag", "test1,,test3")
   272  	testPrintedStderr(t, "hashtag may not contain empty tags")
   273  }
   274  
   275  func TestMailEmpty(t *testing.T) {
   276  	gt := newGitTest(t)
   277  	defer gt.done()
   278  
   279  	// fake auth information to avoid Gerrit error
   280  	auth.host = "gerrit.fake"
   281  	auth.user = "not-a-user"
   282  	defer func() {
   283  		auth.host = ""
   284  		auth.user = ""
   285  	}()
   286  
   287  	testMain(t, "change", "work")
   288  	testRan(t, "git checkout -q -b work",
   289  		"git branch -q --set-upstream-to origin/master")
   290  
   291  	t.Logf("creating empty change")
   292  	testCommitMsg = "foo: this commit will be empty"
   293  	testMain(t, "change")
   294  	testRan(t, "git commit -q --allow-empty -m foo: this commit will be empty")
   295  
   296  	h := CurrentBranch().Pending()[0].ShortHash
   297  
   298  	testMainDied(t, "mail")
   299  	testPrintedStderr(t, "cannot mail: commit "+h+" is empty")
   300  }