github.com/abhinav/git-fu@v0.6.1-0.20171029234004-54218d68c11b/git/gateway_test.go (about)

     1  package git
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"os/exec"
     7  	"testing"
     8  
     9  	"github.com/abhinav/git-pr/gateway"
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  func TestPushNoRefs(t *testing.T) {
    14  	dir, err := ioutil.TempDir("", "git-pr")
    15  	require.NoError(t, err, "couldn't create a temporary directory")
    16  	defer os.RemoveAll(dir)
    17  
    18  	restore, err := chdir(dir)
    19  	require.NoError(t, err, "could not cd into %v", dir)
    20  	defer restore()
    21  
    22  	require.NoError(t, exec.Command("git", "init").Run(),
    23  		"failed to set up git repo")
    24  
    25  	gw, err := NewGateway(dir)
    26  	require.NoError(t, err, "could not set up gateway")
    27  
    28  	// We don't have any remotes but that isn't a problem simply because this
    29  	// operation shouldn't do *anything* at all
    30  	err = gw.Push(&gateway.PushRequest{
    31  		Remote: "origin",
    32  		Refs:   make(map[string]string),
    33  		Force:  true,
    34  	})
    35  	require.NoError(t, err)
    36  }
    37  
    38  func chdir(dir string) (restore func(), _ error) {
    39  	oldDir, err := os.Getwd()
    40  	if err != nil {
    41  		return nil, err
    42  	}
    43  
    44  	if err := os.Chdir(dir); err != nil {
    45  		return nil, err
    46  	}
    47  
    48  	return func() { os.Chdir(oldDir) }, nil
    49  }