github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/review/git-codereview/pending_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  	"io/ioutil"
     9  	"os"
    10  	"os/exec"
    11  	"regexp"
    12  	"strings"
    13  	"testing"
    14  )
    15  
    16  func TestPendingNone(t *testing.T) {
    17  	gt := newGitTest(t)
    18  	defer gt.done()
    19  
    20  	testPending(t, `
    21  		master (current branch)
    22  
    23  	`)
    24  }
    25  
    26  func TestPendingNoneBranch(t *testing.T) {
    27  	gt := newGitTest(t)
    28  	defer gt.done()
    29  
    30  	trun(t, gt.client, "git", "checkout", "-b", "work")
    31  
    32  	testPending(t, `
    33  		work (current branch)
    34  
    35  	`)
    36  }
    37  
    38  func TestPendingBasic(t *testing.T) {
    39  	gt := newGitTest(t)
    40  	defer gt.done()
    41  	gt.work(t)
    42  
    43  	testPending(t, `
    44  		work REVHASH..REVHASH (current branch)
    45  		+ REVHASH
    46  			msg
    47  			
    48  			Change-Id: I123456789
    49  		
    50  			Files in this change:
    51  				file
    52  
    53  	`)
    54  }
    55  
    56  func TestPendingComplex(t *testing.T) {
    57  	gt := newGitTest(t)
    58  	defer gt.done()
    59  	gt.work(t)
    60  
    61  	write(t, gt.server+"/file", "v2")
    62  	trun(t, gt.server, "git", "commit", "-a", "-m", "v2")
    63  
    64  	write(t, gt.server+"/file", "v3")
    65  	trun(t, gt.server, "git", "commit", "-a", "-m", "v3")
    66  
    67  	trun(t, gt.client, "git", "fetch")
    68  	trun(t, gt.client, "git", "checkout", "-b", "work3ignored", "-t", "origin/master")
    69  
    70  	write(t, gt.server+"/file", "v4")
    71  	trun(t, gt.server, "git", "commit", "-a", "-m", "v4")
    72  
    73  	trun(t, gt.client, "git", "fetch")
    74  	trun(t, gt.client, "git", "checkout", "-b", "work2", "-t", "origin/master")
    75  	write(t, gt.client+"/file", "modify")
    76  	write(t, gt.client+"/file1", "new")
    77  	trun(t, gt.client, "git", "add", "file", "file1")
    78  	trun(t, gt.client, "git", "commit", "-m", "some changes")
    79  	write(t, gt.client+"/file1", "modify")
    80  	write(t, gt.client+"/afile1", "new")
    81  	trun(t, gt.client, "git", "add", "file1", "afile1")
    82  	write(t, gt.client+"/file1", "modify again")
    83  	write(t, gt.client+"/file", "modify again")
    84  	write(t, gt.client+"/bfile", "untracked")
    85  
    86  	testPending(t, `
    87  		work2 REVHASH..REVHASH (current branch)
    88  		+ uncommitted changes
    89  			Files untracked:
    90  				bfile
    91  			Files unstaged:
    92  				file
    93  				file1
    94  			Files staged:
    95  				afile1
    96  				file1
    97  
    98  		+ REVHASH
    99  			some changes
   100  
   101  			Files in this change:
   102  				file
   103  				file1
   104  
   105  		work REVHASH..REVHASH (3 behind)
   106  		+ REVHASH
   107  			msg
   108  			
   109  			Change-Id: I123456789
   110  		
   111  			Files in this change:
   112  				file
   113  
   114  	`)
   115  
   116  	testPendingArgs(t, []string{"-c"}, `
   117  		work2 REVHASH..REVHASH (current branch)
   118  		+ uncommitted changes
   119  			Files untracked:
   120  				bfile
   121  			Files unstaged:
   122  				file
   123  				file1
   124  			Files staged:
   125  				afile1
   126  				file1
   127  
   128  		+ REVHASH
   129  			some changes
   130  		
   131  			Files in this change:
   132  				file
   133  				file1
   134  		
   135  	`)
   136  
   137  	testPendingArgs(t, []string{"-c", "-s"}, `
   138  		work2 REVHASH..REVHASH (current branch)
   139  		+ uncommitted changes
   140  			Files untracked:
   141  				bfile
   142  			Files unstaged:
   143  				file
   144  				file1
   145  			Files staged:
   146  				afile1
   147  				file1
   148  		+ REVHASH some changes
   149  		
   150  	`)
   151  }
   152  
   153  func TestPendingErrors(t *testing.T) {
   154  	gt := newGitTest(t)
   155  	defer gt.done()
   156  
   157  	trun(t, gt.client, "git", "checkout", "master")
   158  	write(t, gt.client+"/file", "v3")
   159  	trun(t, gt.client, "git", "commit", "-a", "-m", "v3")
   160  
   161  	testPending(t, `
   162  		master REVHASH..REVHASH (current branch)
   163  			ERROR: Branch contains 1 commit not on origin/master.
   164  				Do not commit directly to master branch.
   165  		
   166  		+ REVHASH
   167  			v3
   168  		
   169  			Files in this change:
   170  				file
   171  
   172  	`)
   173  
   174  	testPendingArgs(t, []string{"-s"}, `
   175  		master REVHASH..REVHASH (current branch)
   176  			ERROR: Branch contains 1 commit not on origin/master.
   177  				Do not commit directly to master branch.
   178  		+ REVHASH v3
   179  
   180  	`)
   181  }
   182  
   183  func TestPendingMultiChange(t *testing.T) {
   184  	gt := newGitTest(t)
   185  	defer gt.done()
   186  
   187  	gt.work(t)
   188  	write(t, gt.client+"/file", "v2")
   189  	trun(t, gt.client, "git", "commit", "-a", "-m", "v2")
   190  
   191  	write(t, gt.client+"/file", "v4")
   192  	trun(t, gt.client, "git", "add", "file")
   193  
   194  	write(t, gt.client+"/file", "v5")
   195  	write(t, gt.client+"/file2", "v6")
   196  
   197  	testPending(t, `
   198  		work REVHASH..REVHASH (current branch)
   199  		+ uncommitted changes
   200  			Files untracked:
   201  				file2
   202  			Files unstaged:
   203  				file
   204  			Files staged:
   205  				file
   206  		
   207  		+ REVHASH
   208  			v2
   209  		
   210  			Files in this change:
   211  				file
   212  
   213  		+ REVHASH
   214  			msg
   215  			
   216  			Change-Id: I123456789
   217  		
   218  			Files in this change:
   219  				file
   220  
   221  	`)
   222  
   223  	testPendingArgs(t, []string{"-s"}, `
   224  		work REVHASH..REVHASH (current branch)
   225  		+ uncommitted changes
   226  			Files untracked:
   227  				file2
   228  			Files unstaged:
   229  				file
   230  			Files staged:
   231  				file
   232  		+ REVHASH v2
   233  		+ REVHASH msg
   234  
   235  	`)
   236  }
   237  
   238  func TestPendingGerrit(t *testing.T) {
   239  	gt := newGitTest(t)
   240  	defer gt.done()
   241  	gt.work(t)
   242  
   243  	srv := newGerritServer(t)
   244  	defer srv.done()
   245  
   246  	// Test error from Gerrit server.
   247  	testPending(t, `
   248  		work REVHASH..REVHASH (current branch)
   249  		+ REVHASH
   250  			msg
   251  			
   252  			Change-Id: I123456789
   253  		
   254  			Files in this change:
   255  				file
   256  
   257  	`)
   258  
   259  	testPendingReply(srv, "I123456789", CurrentBranch().Pending()[0].Hash, "MERGED")
   260  
   261  	// Test local mode does not talk to any server.
   262  	// Make client 1 behind server.
   263  	// The '1 behind' should not show up, nor any Gerrit information.
   264  	write(t, gt.server+"/file", "v4")
   265  	trun(t, gt.server, "git", "add", "file")
   266  	trun(t, gt.server, "git", "commit", "-m", "msg")
   267  	testPendingArgs(t, []string{"-l"}, `
   268  		work REVHASH..REVHASH (current branch)
   269  		+ REVHASH
   270  			msg
   271  			
   272  			Change-Id: I123456789
   273  
   274  			Files in this change:
   275  				file
   276  
   277  	`)
   278  
   279  	testPendingArgs(t, []string{"-l", "-s"}, `
   280  		work REVHASH..REVHASH (current branch)
   281  		+ REVHASH msg
   282  
   283  	`)
   284  
   285  	// Without -l, the 1 behind should appear, as should Gerrit information.
   286  	testPending(t, `
   287  		work REVHASH..REVHASH (current branch, all mailed, all submitted, 1 behind)
   288  		+ REVHASH http://127.0.0.1:PORT/1234 (mailed, submitted)
   289  			msg
   290  			
   291  			Change-Id: I123456789
   292  		
   293  			Code-Review:
   294  				+1 Grace Emlin
   295  				-2 George Opher
   296  			Other-Label:
   297  				+2 The Owner
   298  			Files in this change:
   299  				file
   300  
   301  	`)
   302  
   303  	testPendingArgs(t, []string{"-s"}, `
   304  		work REVHASH..REVHASH (current branch, all mailed, all submitted, 1 behind)
   305  		+ REVHASH msg (CL 1234 -2 +1, mailed, submitted)
   306  
   307  	`)
   308  
   309  	// Since pending did a fetch, 1 behind should show up even with -l.
   310  	testPendingArgs(t, []string{"-l"}, `
   311  		work REVHASH..REVHASH (current branch, 1 behind)
   312  		+ REVHASH
   313  			msg
   314  			
   315  			Change-Id: I123456789
   316  
   317  			Files in this change:
   318  				file
   319  
   320  	`)
   321  	testPendingArgs(t, []string{"-l", "-s"}, `
   322  		work REVHASH..REVHASH (current branch, 1 behind)
   323  		+ REVHASH msg
   324  
   325  	`)
   326  }
   327  
   328  func TestPendingGerritMultiChange(t *testing.T) {
   329  	gt := newGitTest(t)
   330  	defer gt.done()
   331  
   332  	gt.work(t)
   333  	hash1 := CurrentBranch().Pending()[0].Hash
   334  
   335  	write(t, gt.client+"/file", "v2")
   336  	trun(t, gt.client, "git", "commit", "-a", "-m", "v2\n\nChange-Id: I2345")
   337  	hash2 := CurrentBranch().Pending()[0].Hash
   338  
   339  	write(t, gt.client+"/file", "v4")
   340  	trun(t, gt.client, "git", "add", "file")
   341  
   342  	write(t, gt.client+"/file", "v5")
   343  	write(t, gt.client+"/file2", "v6")
   344  
   345  	srv := newGerritServer(t)
   346  	defer srv.done()
   347  
   348  	testPendingReply(srv, "I123456789", hash1, "MERGED")
   349  	testPendingReply(srv, "I2345", hash2, "NEW")
   350  
   351  	testPending(t, `
   352  		work REVHASH..REVHASH (current branch, all mailed)
   353  		+ uncommitted changes
   354  			Files untracked:
   355  				file2
   356  			Files unstaged:
   357  				file
   358  			Files staged:
   359  				file
   360  		
   361  		+ REVHASH http://127.0.0.1:PORT/1234 (mailed)
   362  			v2
   363  			
   364  			Change-Id: I2345
   365  
   366  			Code-Review:
   367  				+1 Grace Emlin
   368  				-2 George Opher
   369  			Other-Label:
   370  				+2 The Owner
   371  			Files in this change:
   372  				file
   373  
   374  		+ REVHASH http://127.0.0.1:PORT/1234 (mailed, submitted)
   375  			msg
   376  			
   377  			Change-Id: I123456789
   378  		
   379  			Code-Review:
   380  				+1 Grace Emlin
   381  				-2 George Opher
   382  			Other-Label:
   383  				+2 The Owner
   384  			Files in this change:
   385  				file
   386  
   387  	`)
   388  
   389  	testPendingArgs(t, []string{"-s"}, `
   390  		work REVHASH..REVHASH (current branch, all mailed)
   391  		+ uncommitted changes
   392  			Files untracked:
   393  				file2
   394  			Files unstaged:
   395  				file
   396  			Files staged:
   397  				file
   398  		+ REVHASH v2 (CL 1234 -2 +1, mailed)
   399  		+ REVHASH msg (CL 1234 -2 +1, mailed, submitted)
   400  
   401  	`)
   402  }
   403  
   404  func testPendingReply(srv *gerritServer, id, rev, status string) {
   405  	srv.setJSON(id, `{
   406  		"current_revision": "`+rev+`",
   407  		"status": "`+status+`",
   408  		"_number": 1234,
   409  		"owner": {"_id": 42},
   410  		"labels": {
   411  			"Code-Review": {
   412  				"all": [
   413  					{
   414  						"_id": 42,
   415  						"value": 0
   416  					},
   417  					{
   418  						"_id": 43,
   419  						"name": "George Opher",
   420  						"value": -2
   421  					},
   422  					{
   423  						"_id": 44,
   424  						"name": "Grace Emlin",
   425  						"value": 1
   426  					}
   427  				]
   428  			},
   429  			"Trybot-Spam": {
   430  				"all": [
   431  					{
   432  						"_account_id": 42,
   433  						"name": "The Owner",
   434  						"value": 0
   435  					}
   436  				]
   437  			},
   438  			"Other-Label": {
   439  				"all": [
   440  					{
   441  						"_id": 43,
   442  						"name": "George Opher",
   443  						"value": 0
   444  					},
   445  					{
   446  						"_account_id": 42,
   447  						"name": "The Owner",
   448  						"value": 2
   449  					}
   450  				]
   451  			}
   452  		}
   453  	}`)
   454  }
   455  
   456  func testPending(t *testing.T, want string) {
   457  	testPendingArgs(t, nil, want)
   458  }
   459  
   460  func testPendingArgs(t *testing.T, args []string, want string) {
   461  	// fake auth information to avoid Gerrit error
   462  	if auth.host == "" {
   463  		auth.host = "gerrit.fake"
   464  		auth.user = "not-a-user"
   465  		defer func() {
   466  			auth.host = ""
   467  			auth.user = ""
   468  		}()
   469  	}
   470  
   471  	want = strings.Replace(want, "\n\t", "\n", -1)
   472  	want = strings.Replace(want, "\n\t", "\n", -1)
   473  	want = strings.TrimPrefix(want, "\n")
   474  
   475  	testMain(t, append([]string{"pending"}, args...)...)
   476  	out := testStdout.Bytes()
   477  
   478  	out = regexp.MustCompile(`\b[0-9a-f]{7}\b`).ReplaceAllLiteral(out, []byte("REVHASH"))
   479  	out = regexp.MustCompile(`\b127\.0\.0\.1:\d+\b`).ReplaceAllLiteral(out, []byte("127.0.0.1:PORT"))
   480  
   481  	if string(out) != want {
   482  		t.Errorf("invalid pending output:\n%s\nwant:\n%s", out, want)
   483  		if d, err := diff([]byte(want), out); err == nil {
   484  			t.Errorf("diff want actual:\n%s", d)
   485  		}
   486  	}
   487  }
   488  
   489  func diff(b1, b2 []byte) (data []byte, err error) {
   490  	f1, err := ioutil.TempFile("", "gofmt")
   491  	if err != nil {
   492  		return
   493  	}
   494  	defer os.Remove(f1.Name())
   495  	defer f1.Close()
   496  
   497  	f2, err := ioutil.TempFile("", "gofmt")
   498  	if err != nil {
   499  		return
   500  	}
   501  	defer os.Remove(f2.Name())
   502  	defer f2.Close()
   503  
   504  	f1.Write(b1)
   505  	f2.Write(b2)
   506  
   507  	data, err = exec.Command("diff", "-u", f1.Name(), f2.Name()).CombinedOutput()
   508  	if len(data) > 0 {
   509  		// diff exits with a non-zero status when the files don't match.
   510  		// Ignore that failure as long as we get output.
   511  		err = nil
   512  	}
   513  	return
   514  
   515  }