Skip to content

Commit

Permalink
Merge pull request #61 from zaquestion/tests-cmd-3
Browse files Browse the repository at this point in the history
(tests) Tests for cmd/root, cmd/clone, and cmd/fork
  • Loading branch information
zaquestion authored Dec 15, 2017
2 parents c4062a8 + 27d23f4 commit 6540c63
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 13 deletions.
25 changes: 25 additions & 0 deletions cmd/clone_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package cmd

import (
"os/exec"
"testing"

"github.com/stretchr/testify/assert"
)

func Test_clone(t *testing.T) {
repo := copyTestRepo(t)
cmd := exec.Command("../lab_bin", "clone", "test")
cmd.Dir = repo

b, err := cmd.CombinedOutput()
if err != nil {
t.Fatal(err)
}

out := string(b)

assert.Contains(t, out, "Cloning into 'test'...")
assert.Contains(t, out, " * [new branch] master -> upstream/master")
assert.Contains(t, out, "new remote: upstream")
}
43 changes: 43 additions & 0 deletions cmd/fork_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package cmd

import (
"os/exec"
"testing"

"github.com/stretchr/testify/require"
)

func Test_fork(t *testing.T) {
repo := copyTestRepo(t)
// remove the remote so we can test adding it back
// NOTE: we aren't actually going to test that forks are created on
// GitLab, just that lab behaves correctly when a fork exists.
cmd := exec.Command("git", "remote", "remove", "lab-testing")
cmd.Dir = repo
err := cmd.Run()
if err != nil {
t.Fatal(err)
}

cmd = exec.Command("../lab_bin", "fork")
cmd.Dir = repo

b, err := cmd.CombinedOutput()
if err != nil {
t.Fatal(err)
}

out := string(b)

require.Contains(t, out, "From gitlab.com:lab-testing/test")
require.Contains(t, out, "new remote: lab-testing")

cmd = exec.Command("git", "remote", "-v")
cmd.Dir = repo

b, err = cmd.CombinedOutput()
if err != nil {
t.Fatal(err)
}
require.Contains(t, string(b), "lab-testing")
}
99 changes: 99 additions & 0 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/zaquestion/lab/internal/git"
)
Expand Down Expand Up @@ -64,3 +65,101 @@ func TestRootCloneNoArg(t *testing.T) {
b, _ := cmd.CombinedOutput()
require.Contains(t, string(b), "You must specify a repository to clone.")
}

func TestRootGitCmd(t *testing.T) {
cmd := exec.Command("../lab_bin", "log", "-n", "1")
b, _ := cmd.CombinedOutput()
require.Contains(t, string(b), `commit cd64a7caea4f3ee5696a190379aff1a7f636e598
Author: Zaq? Wiedmann <zaquestion@gmail.com>
Date: Sat Sep 2 20:58:39 2017 -0700
Added additional commit for LastCommitMessage and meeting requirements for Log test (>1 commit)`)
}

func TestRootNoArg(t *testing.T) {
cmd := exec.Command("../lab_bin")
b, _ := cmd.CombinedOutput()
assert.Contains(t, string(b), "usage: git [--version] [--help] [-C <path>] [-c name=value]")
assert.Contains(t, string(b), `These GitLab commands are provided by lab:
fork Fork a remote repository on GitLab and add as remote`)
}

func Test_parseArgsRemote(t *testing.T) {
tests := []struct {
Name string
Args []string
ExpectedString string
ExpectedInt int64
ExpectedErr string
}{
{
Name: "No Args",
Args: nil,
ExpectedString: "",
ExpectedInt: 0,
ExpectedErr: "",
},
{
Name: "1 arg remote",
Args: []string{"origin"},
ExpectedString: "origin",
ExpectedInt: 0,
ExpectedErr: "",
},
{
Name: "1 arg non remote",
Args: []string{"foo"},
ExpectedString: "",
ExpectedInt: 0,
ExpectedErr: "foo is not a valid remote or number",
},
{
Name: "1 arg page",
Args: []string{"100"},
ExpectedString: "",
ExpectedInt: 100,
ExpectedErr: "",
},
{
Name: "1 arg invalid page",
Args: []string{"asdf100"},
ExpectedString: "",
ExpectedInt: 0,
ExpectedErr: "asdf100 is not a valid remote or number",
},
{
Name: "2 arg remote page",
Args: []string{"origin", "100"},
ExpectedString: "origin",
ExpectedInt: 100,
ExpectedErr: "",
},
{
Name: "2 arg invalid remote valid page",
Args: []string{"foo", "100"},
ExpectedString: "",
ExpectedInt: 0,
ExpectedErr: "foo is not a valid remote",
},
{
Name: "2 arg valid remote invalid page",
Args: []string{"foo", "asdf100"},
ExpectedString: "",
ExpectedInt: 0,
ExpectedErr: "strconv.ParseInt: parsing \"asdf100\": invalid syntax",
},
}
for _, test := range tests {
t.Run(test.Name, func(t *testing.T) {
test := test
t.Parallel()
s, i, err := parseArgsRemote(test.Args)
if err != nil {
assert.EqualError(t, err, test.ExpectedErr)
}
assert.Equal(t, test.ExpectedString, s)
assert.Equal(t, test.ExpectedInt, i)
})
}
}
13 changes: 0 additions & 13 deletions internal/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,19 +209,6 @@ func FindProject(project string) (*gitlab.Project, error) {
return target, nil
}

// ClonePath returns the ssh url to the GitLab project
func ClonePath(project string) (string, error) {
target, err := FindProject(project)
if err != nil {
return "", err
}

if target != nil {
return target.SSHURLToRepo, nil
}
return project, nil
}

// Fork creates a user fork of a GitLab project
func Fork(project string) (string, error) {
if !strings.Contains(project, "/") {
Expand Down

0 comments on commit 6540c63

Please sign in to comment.