github.com/engineyard/workflow-cli@v2.21.6+incompatible/pkg/git/git_test.go (about)

     1  package git
     2  
     3  import (
     4  	"errors"
     5  	"os"
     6  	"os/exec"
     7  	"path/filepath"
     8  	"reflect"
     9  	"testing"
    10  
    11  	"github.com/arschles/assert"
    12  )
    13  
    14  func TestRepositoryURL(t *testing.T) {
    15  	t.Parallel()
    16  
    17  	actual := RepositoryURL("deis.example.com", "app")
    18  	assert.Equal(t, actual, "ssh://git@deis-builder.example.com:2222/app.git", "url")
    19  	actual = RepositoryURL("deis.10.245.1.3.xip.io:31350", "velcro-underdog")
    20  	assert.Equal(t, actual, "ssh://git@deis-builder.10.245.1.3.xip.io:2222/velcro-underdog.git", "url")
    21  }
    22  
    23  func TestGetRemotes(t *testing.T) {
    24  	t.Parallel()
    25  
    26  	expected := []remote{
    27  		{"test", "ssh://test.com/test.git"},
    28  		{"example", "ssh://example.com:2222/example.git"},
    29  	}
    30  
    31  	actual, err := getRemotes(func(cmd []string) (string, error) {
    32  		assert.Equal(t, cmd, []string{"remote", "-v"}, "args")
    33  		return `test	ssh://test.com/test.git (fetch)
    34  test	ssh://test.com/test.git (push)
    35  example	ssh://example.com:2222/example.git (fetch)
    36  example	ssh://example.com:2222/example.git (push)
    37  `, nil
    38  	})
    39  
    40  	assert.NoErr(t, err)
    41  	assert.Equal(t, actual, expected, "remotes")
    42  
    43  	_, err = getRemotes(func(cmd []string) (string, error) {
    44  		assert.Equal(t, cmd, []string{"remote", "-v"}, "args")
    45  		return `fakeoutput(push)
    46  `, nil
    47  	})
    48  
    49  	assert.Err(t, err, ErrInvalidRepositoryList)
    50  
    51  	testErr := errors.New("test error")
    52  
    53  	_, err = getRemotes(func(cmd []string) (string, error) {
    54  		assert.Equal(t, cmd, []string{"remote", "-v"}, "args")
    55  		return "", testErr
    56  	})
    57  
    58  	assert.Err(t, err, testErr)
    59  }
    60  
    61  func TestRemoteURL(t *testing.T) {
    62  	t.Parallel()
    63  
    64  	url, err := RemoteURL(func(cmd []string) (string, error) {
    65  		assert.Equal(t, cmd, []string{"remote", "-v"}, "args")
    66  		return `test	ssh://test.com/test.git (fetch)
    67  test	ssh://test.com/test.git (push)
    68  example	ssh://example.com:2222/example.git (fetch)
    69  example	ssh://example.com:2222/example.git (push)
    70  `, nil
    71  	}, "test")
    72  
    73  	assert.NoErr(t, err)
    74  	assert.Equal(t, url, "ssh://test.com/test.git", "remote url")
    75  
    76  	_, err = RemoteURL(func(cmd []string) (string, error) {
    77  		assert.Equal(t, cmd, []string{"remote", "-v"}, "args")
    78  		return `test	ssh://test.com/test.git (fetch)
    79  test	ssh://test.com/test.git (push)
    80  example	ssh://example.com:2222/example.git (fetch)
    81  example	ssh://example.com:2222/example.git (push)
    82  `, nil
    83  	}, "foo")
    84  
    85  	assert.Err(t, err, ErrRemoteNotFound)
    86  
    87  	testErr := errors.New("test error")
    88  
    89  	_, err = RemoteURL(func(cmd []string) (string, error) {
    90  		assert.Equal(t, cmd, []string{"remote", "-v"}, "args")
    91  		return "", testErr
    92  	}, "test")
    93  
    94  	assert.Err(t, err, testErr)
    95  }
    96  
    97  func TestFindRemoteURL(t *testing.T) {
    98  	t.Parallel()
    99  
   100  	url, err := findRemote(func(cmd []string) (string, error) {
   101  		assert.Equal(t, cmd, []string{"remote", "-v"}, "args")
   102  		return `test	ssh://test.com/test.git (fetch)
   103  test	ssh://test.com/test.git (push)
   104  deis	ssh://git@deis-builder.example.com:2222/test.git (fetch)
   105  deis	ssh://git@deis-builder.example.com:2222/test.git (push)
   106  `, nil
   107  	}, "deis.example.com")
   108  
   109  	assert.NoErr(t, err)
   110  	assert.Equal(t, url, "ssh://git@deis-builder.example.com:2222/test.git", "remote url")
   111  
   112  	_, err = findRemote(func(cmd []string) (string, error) {
   113  		assert.Equal(t, cmd, []string{"remote", "-v"}, "args")
   114  		return `test	ssh://test.com/test.git (fetch)
   115  test	ssh://test.com/test.git (push)
   116  example	ssh://example.com:2222/example.git (fetch)
   117  example	ssh://example.com:2222/example.git (push)
   118  `, nil
   119  	}, "deis.test.com")
   120  
   121  	assert.Err(t, err, ErrRemoteNotFound)
   122  
   123  	testErr := errors.New("test error")
   124  
   125  	_, err = findRemote(func(cmd []string) (string, error) {
   126  		assert.Equal(t, cmd, []string{"remote", "-v"}, "args")
   127  		return "", testErr
   128  	}, "deis.test.com")
   129  
   130  	assert.Err(t, err, testErr)
   131  }
   132  
   133  func TestDetectAppName(t *testing.T) {
   134  	t.Parallel()
   135  
   136  	app, err := DetectAppName(func(cmd []string) (string, error) {
   137  		assert.Equal(t, cmd, []string{"remote", "-v"}, "args")
   138  		return `deis	ssh://git@deis-builder.example.com:2222/test.git (fetch)
   139  deis	ssh://git@deis-builder.example.com:2222/test.git (push)
   140  `, nil
   141  	}, "deis.example.com")
   142  
   143  	assert.NoErr(t, err)
   144  	assert.Equal(t, app, "test", "app")
   145  
   146  	app, err = DetectAppName(func(cmd []string) (string, error) {
   147  		assert.Equal(t, cmd, []string{"remote", "-v"}, "args")
   148  		return "", errors.New("test error")
   149  	}, "deis.test.com")
   150  	assert.NoErr(t, err)
   151  	wd, err := os.Getwd()
   152  	assert.NoErr(t, err)
   153  	assert.Equal(t, app, filepath.Base(wd), "app")
   154  }
   155  
   156  func TestRemoteNamesFromAppID(t *testing.T) {
   157  	t.Parallel()
   158  
   159  	apps, err := remoteNamesFromAppID(func(cmd []string) (string, error) {
   160  		assert.Equal(t, cmd, []string{"remote", "-v"}, "args")
   161  		return `test	ssh://test.com/test.git (fetch)
   162  test	ssh://test.com/test.git (push)
   163  deis	ssh://git@deis-builder.example.com:2222/test.git (fetch)
   164  deis	ssh://git@deis-builder.example.com:2222/test.git (push)
   165  two	ssh://git@deis-builder.example.com:2222/test.git (fetch)
   166  two	ssh://git@deis-builder.example.com:2222/test.git (push)
   167  `, nil
   168  	}, "deis.example.com", "test")
   169  
   170  	assert.NoErr(t, err)
   171  	assert.Equal(t, apps, []string{"deis", "two"}, "remote url")
   172  
   173  	_, err = remoteNamesFromAppID(func(cmd []string) (string, error) {
   174  		assert.Equal(t, cmd, []string{"remote", "-v"}, "args")
   175  		return `test	ssh://test.com/test.git (fetch)
   176  test	ssh://test.com/test.git (push)
   177  deis	ssh://git@deis-builder.example.com:2222/test.git (fetch)
   178  deis	ssh://git@deis-builder.example.com:2222/test.git (push)
   179  two	ssh://git@deis-builder.example.com:2222/test.git (fetch)
   180  two	ssh://git@deis-builder.example.com:2222/test.git (push)
   181  `, nil
   182  	}, "deis.test.com", "other")
   183  
   184  	assert.Err(t, err, ErrRemoteNotFound)
   185  
   186  	testErr := errors.New("test error")
   187  
   188  	_, err = remoteNamesFromAppID(func(cmd []string) (string, error) {
   189  		assert.Equal(t, cmd, []string{"remote", "-v"}, "args")
   190  		return "", testErr
   191  	}, "deis.test.com", "test")
   192  
   193  	assert.Err(t, err, testErr)
   194  }
   195  
   196  func TestDeleteRemote(t *testing.T) {
   197  	t.Parallel()
   198  
   199  	err := DeleteRemote(func(cmd []string) (string, error) {
   200  		assert.Equal(t, cmd, []string{"remote", "remove", "test"}, "args")
   201  		return "", nil
   202  	}, "test")
   203  	assert.NoErr(t, err)
   204  }
   205  
   206  func TestDeleteAppRemotes(t *testing.T) {
   207  	t.Parallel()
   208  
   209  	err := DeleteAppRemotes(func(cmd []string) (string, error) {
   210  		if reflect.DeepEqual(cmd, []string{"remote", "-v"}) {
   211  			return `deis	ssh://git@deis-builder.example.com:2222/test.git (push)
   212  `, nil
   213  		} else if reflect.DeepEqual(cmd, []string{"remote", "remove", "deis"}) {
   214  			return "", nil
   215  		} else {
   216  			t.Errorf("unexpected command %v", cmd)
   217  			return "", nil
   218  		}
   219  	}, "deis.example.com", "test")
   220  
   221  	assert.NoErr(t, err)
   222  
   223  	testErr := errors.New("test error")
   224  
   225  	err = DeleteAppRemotes(func(cmd []string) (string, error) {
   226  		return "", testErr
   227  	}, "deis.example.com", "test")
   228  
   229  	assert.Err(t, testErr, err)
   230  
   231  	err = DeleteAppRemotes(func(cmd []string) (string, error) {
   232  		if reflect.DeepEqual(cmd, []string{"remote", "-v"}) {
   233  			return `deis	ssh://git@deis-builder.example.com:2222/test.git (push)
   234  `, nil
   235  		} else if reflect.DeepEqual(cmd, []string{"remote", "remove", "deis"}) {
   236  			return "", testErr
   237  		} else {
   238  			t.Errorf("unexpected command %v", cmd)
   239  			return "", nil
   240  		}
   241  	}, "deis.example.com", "test")
   242  
   243  	assert.Err(t, testErr, err)
   244  }
   245  
   246  func TestInit(t *testing.T) {
   247  	t.Parallel()
   248  
   249  	err := Init(func(cmd []string) (string, error) {
   250  		assert.Equal(t, cmd, []string{"init"}, "args")
   251  		return "", nil
   252  	})
   253  	assert.NoErr(t, err)
   254  }
   255  
   256  func TestCreateRemote(t *testing.T) {
   257  	t.Parallel()
   258  
   259  	err := CreateRemote(func(cmd []string) (string, error) {
   260  		assert.Equal(t, cmd, []string{"remote", "add", "deis", "ssh://git@deis-builder.example.com:2222/testing.git"}, "args")
   261  		return "", nil
   262  	}, "deis.example.com", "deis", "testing")
   263  	assert.NoErr(t, err)
   264  }
   265  
   266  func TestGitError(t *testing.T) {
   267  	t.Parallel()
   268  
   269  	exitErr := exec.ExitError{Stderr: []byte("fake error")}
   270  	assert.Equal(t, gitError(&exitErr, []string{"fake"}).Error(), `Error when running 'git fake'
   271  fake error`, "error")
   272  }