github.com/purpleclay/gitz@v0.8.2-0.20240515052600-43f80eea2fe1/checkout_test.go (about)

     1  package git_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	git "github.com/purpleclay/gitz"
     7  	"github.com/purpleclay/gitz/gittest"
     8  	"github.com/stretchr/testify/assert"
     9  	"github.com/stretchr/testify/require"
    10  )
    11  
    12  func TestCheckout(t *testing.T) {
    13  	log := `(HEAD -> branch-checkout, origin/branch-checkout) pass tests
    14  write tests for branch checkout
    15  (main, origin/main) docs: update existing project README`
    16  	gittest.InitRepository(t, gittest.WithLog(log))
    17  
    18  	client, _ := git.NewClient()
    19  	out, err := client.Checkout("main")
    20  	require.NoError(t, err)
    21  
    22  	// Inspect the raw git output
    23  	assert.Contains(t, out, "Switched to branch 'main'")
    24  	assert.Equal(t, gittest.LastCommit(t).Message, "docs: update existing project README")
    25  }
    26  
    27  func TestCheckoutCreatesLocalBranch(t *testing.T) {
    28  	gittest.InitRepository(t)
    29  
    30  	client, _ := git.NewClient()
    31  	_, err := client.Checkout("testing")
    32  	require.NoError(t, err)
    33  
    34  	branches := gittest.Branches(t)
    35  	remoteBranches := gittest.RemoteBranches(t)
    36  
    37  	assert.Contains(t, branches, "testing")
    38  	assert.NotContains(t, remoteBranches, "testing")
    39  }
    40  
    41  func TestCheckoutQueryingBranchesError(t *testing.T) {
    42  	nonWorkingDirectory(t)
    43  
    44  	client, _ := git.NewClient()
    45  	_, err := client.Checkout("testing")
    46  
    47  	require.Error(t, err)
    48  }