github.com/nfisher/gitit@v0.0.7-0.20240131193748-bc8dd26542cc/cmd/status_test.go (about)

     1  package cmd_test
     2  
     3  import (
     4  	"bytes"
     5  	"github.com/nfisher/gitit/assert"
     6  	. "github.com/nfisher/gitit/cmd"
     7  	"io"
     8  	"testing"
     9  )
    10  
    11  const simpleBranch = `Not in a stack
    12  On branch master
    13  `
    14  
    15  func Test_status_on_branch_returns_success(t *testing.T) {
    16  	repo, repoclose := CreateRepo(t)
    17  	defer repoclose()
    18  	InitialCommit(t, repo)
    19  
    20  	var buf bytes.Buffer
    21  	i := Exec(Flags{SubCommand: "status"}, &buf)
    22  	assert.Int(t, i).Equals(Success)
    23  	assert.String(t, string(buf.Bytes())).Equals(simpleBranch)
    24  }
    25  
    26  const smallStack = `In stack kb1234
    27  On branch kb1234/003_ui
    28  
    29  Local Stack:
    30      001_docs
    31      002_api
    32      003_ui
    33  `
    34  
    35  func Test_status_on_stack_returns_success(t *testing.T) {
    36  	repo, repoclose := CreateRepo(t)
    37  	defer repoclose()
    38  
    39  	CreateThreeLayerStack(t, repo)
    40  
    41  	var buf bytes.Buffer
    42  	i := Exec(Flags{SubCommand: "status"}, &buf)
    43  	assert.Int(t, i).Equals(Success)
    44  	assert.String(t, string(buf.Bytes())).Equals(smallStack)
    45  }
    46  
    47  func Test_status_returns_not_repository_with_empty_directory(t *testing.T) {
    48  	tdclose := CreateBareDir(t)
    49  	defer tdclose()
    50  
    51  	i := Exec(Flags{SubCommand: "status"}, io.Discard)
    52  	assert.Int(t, i).Equals(ErrNotRepository)
    53  }
    54  
    55  func Test_status_with_default_remote_successful(t *testing.T) {
    56  	server, srvclose := LaunchServer(t)
    57  	defer srvclose()
    58  
    59  	repo, repoclose := CreateRepo(t)
    60  	defer repoclose()
    61  
    62  	CreateThreeLayerStack(t, repo)
    63  	CreateRemote(t, repo, server)
    64  
    65  	PushBranch(t, repo, "kb1234/001_docs")
    66  	PushBranch(t, repo, "kb1234/002_api")
    67  
    68  	var buf bytes.Buffer
    69  	i := Exec(Flags{SubCommand: "status"}, &buf)
    70  	assert.Int(t, i).Equals(Success)
    71  	assert.String(t, string(buf.Bytes())).Equals(`In stack kb1234
    72  On branch kb1234/003_ui
    73  Remote origin
    74  
    75  Local Stack (+ ahead, = same, ∇ diverged):
    76      (=) 001_docs
    77      (=) 002_api
    78      (+) 003_ui
    79  `)
    80  }
    81  
    82  func Test_status_with_diverged_remote_successful(t *testing.T) {
    83  	// Create 2 workspaces and interleave a timeline of pushes as follows:
    84  	// 1. A - initial push 001,002,003.
    85  	// 2. B - pull 001, 002, 003.
    86  	// 3. A - push change 002.
    87  	// 4. B - push change 003.
    88  
    89  	server, srvclose := LaunchServer(t)
    90  	defer srvclose()
    91  
    92  	repo1, r1close := CreateRepo(t)
    93  	defer r1close()
    94  
    95  	wt1 := WorkTree(t, repo1)
    96  
    97  	CreateThreeLayerStack(t, repo1)
    98  	CreateRemote(t, repo1, server)
    99  
   100  	PushBranch(t, repo1, "kb1234/001_docs")
   101  	PushBranch(t, repo1, "kb1234/002_api")
   102  	PushBranch(t, repo1, "kb1234/003_ui")
   103  
   104  	CheckoutBranch(t, wt1, "kb1234/002_api")
   105  	Commit(t, wt1, map[string]string{"api.js": "function api() { return true; }"}, "Update api.js")
   106  
   107  	repo2, r2close := CloneRepo(t, server, "kb1234/003_ui")
   108  	defer r2close()
   109  
   110  	FetchRefs(t, repo2, "kb1234/*")
   111  
   112  	wt2 := WorkTree(t, repo2)
   113  	Commit(t, wt2, map[string]string{"ui.js": "function ui() { return true; }"}, "Update ui.js")
   114  
   115  	var buf2 bytes.Buffer
   116  	i := Exec(Flags{SubCommand: "status"}, &buf2)
   117  	assert.Int(t, i).Equals(Success)
   118  	assert.String(t, string(buf2.Bytes())).Equals(`In stack kb1234
   119  On branch kb1234/003_ui
   120  Remote origin
   121  
   122  Local Stack (+ ahead, = same, ∇ diverged):
   123      (=) 001_docs
   124      (=) 002_api
   125      (+) 003_ui
   126  `)
   127  	PushBranch(t, repo2, "kb1234/003_ui")
   128  
   129  	Chdir(t, wt1)
   130  
   131  	var buf1 bytes.Buffer
   132  	i = Exec(Flags{SubCommand: "status"}, &buf1)
   133  	assert.Int(t, i).Equals(Success)
   134  	assert.String(t, string(buf1.Bytes())).Equals(`In stack kb1234
   135  On branch kb1234/002_api
   136  Remote origin
   137  
   138  Local Stack (+ ahead, = same, ∇ diverged):
   139      (=) 001_docs
   140      (+) 002_api
   141      (∇) 003_ui
   142  `)
   143  }