github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/review/git-codereview/sync_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 TestSync(t *testing.T) {
    10  	gt := newGitTest(t)
    11  	defer gt.done()
    12  
    13  	testMain(t, "change", "work")
    14  
    15  	// check for error with unstaged changes
    16  	write(t, gt.client+"/file1", "")
    17  	trun(t, gt.client, "git", "add", "file1")
    18  	write(t, gt.client+"/file1", "actual content")
    19  	testMainDied(t, "sync")
    20  	testPrintedStderr(t, "cannot sync: unstaged changes exist",
    21  		"git status", "git stash", "git add", "git-codereview change")
    22  	testNoStdout(t)
    23  
    24  	// check for error with staged changes
    25  	trun(t, gt.client, "git", "add", "file1")
    26  	testMainDied(t, "sync")
    27  	testPrintedStderr(t, "cannot sync: staged changes exist",
    28  		"git status", "!git stash", "!git add", "git-codereview change")
    29  	testNoStdout(t)
    30  
    31  	// check for success after stash
    32  	trun(t, gt.client, "git", "stash")
    33  	testMain(t, "sync")
    34  	testNoStdout(t)
    35  	testNoStderr(t)
    36  
    37  	// make server 1 step ahead of client
    38  	write(t, gt.server+"/file", "new content")
    39  	trun(t, gt.server, "git", "add", "file")
    40  	trun(t, gt.server, "git", "commit", "-m", "msg")
    41  
    42  	// check for success
    43  	testMain(t, "sync")
    44  	testNoStdout(t)
    45  	testNoStderr(t)
    46  }
    47  
    48  func TestSyncRebase(t *testing.T) {
    49  	gt := newGitTest(t)
    50  	defer gt.done()
    51  
    52  	// client 3 ahead
    53  	gt.work(t)
    54  	gt.work(t)
    55  	gt.work(t)
    56  
    57  	b := CurrentBranch()
    58  	if len(b.Pending()) != 3 {
    59  		t.Fatalf("have %d pending CLs, want 3", len(b.Pending()))
    60  	}
    61  	top := b.Pending()[0].Hash
    62  
    63  	// check for success for sync no-op
    64  	testMain(t, "sync")
    65  	testNoStdout(t)
    66  	testNoStderr(t)
    67  
    68  	b = CurrentBranch()
    69  	if len(b.Pending()) != 3 {
    70  		t.Fatalf("have %d pending CLs after no-op sync, want 3", len(b.Pending()))
    71  	}
    72  	if b.Pending()[0].Hash != top {
    73  		t.Fatalf("CL hashes changed during no-op sync")
    74  	}
    75  
    76  	// submit first two CLs - gt.serverWork does same thing gt.work does, but on server
    77  
    78  	gt.serverWork(t)
    79  	gt.serverWorkUnrelated(t) // wedge in unrelated work to get different hashes
    80  	gt.serverWork(t)
    81  
    82  	testMain(t, "sync")
    83  	testNoStdout(t)
    84  	testNoStderr(t)
    85  
    86  	// there should be one left, and it should be a different hash
    87  	b = CurrentBranch()
    88  	if len(b.Pending()) != 1 {
    89  		t.Fatalf("have %d pending CLs after submitting two, want 1", len(b.Pending()))
    90  	}
    91  	if b.Pending()[0].Hash == top {
    92  		t.Fatalf("CL hashes DID NOT change during sync after submit")
    93  	}
    94  
    95  	// submit final change
    96  	gt.serverWork(t)
    97  
    98  	testMain(t, "sync")
    99  	testNoStdout(t)
   100  	testNoStderr(t)
   101  
   102  	// there should be none left
   103  	b = CurrentBranch()
   104  	if len(b.Pending()) != 0 {
   105  		t.Fatalf("have %d pending CLs after final sync, want 0", len(b.Pending()))
   106  	}
   107  }