Skip to content

Commit

Permalink
Add tests for new 'internal/copy' pkg and extend existing cmd tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewdale committed Jan 1, 2019
1 parent d05ff83 commit 34fb0e9
Show file tree
Hide file tree
Showing 12 changed files with 97 additions and 33 deletions.
18 changes: 15 additions & 3 deletions cmd/issue_create_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package cmd

import (
"net/url"
"os/exec"
"path"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -16,11 +18,21 @@ func Test_issueCreate(t *testing.T) {

b, err := cmd.CombinedOutput()
if err != nil {
t.Log(string(b))
t.Fatal(err)
t.Fatalf("Error creating issue: %s (%s)", string(b), err)
}
out := getAppOutput(b)[0]

require.Contains(t, out, "https://gitlab.com/lab-testing/test/issues/")

require.Contains(t, string(b), "https://gitlab.com/lab-testing/test/issues/")
// Get the issue ID from the returned URL and close the issue.
u, err := url.Parse(out)
require.NoError(t, err, "Error parsing URL")
id := path.Base(u.Path)

cmd = exec.Command(labBinaryPath, "issue", "close", "lab-testing", id)
cmd.Dir = repo
b, err = cmd.CombinedOutput()
require.NoError(t, err, "Error closing issue %s: %s", id, string(b))
}

func Test_issueMsg(t *testing.T) {
Expand Down
3 changes: 1 addition & 2 deletions cmd/mr_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,7 @@ func Test_mrFilterByTargetBranch(t *testing.T) {
t.Fatal(err)
}

mrs := strings.Split(string(b), "\n")
mrs = truncAppOutput(mrs)
mrs := getAppOutput(b)
assert.Empty(t, mrs, "Expected to find no MRs for non-existent branch")
}

Expand Down
16 changes: 1 addition & 15 deletions cmd/project_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,8 @@ func Test_projectList_many(t *testing.T) {
if err != nil {
t.Fatal(err)
}
projects := strings.Split(string(b), "\n")
t.Log(projects)

projects = truncAppOutput(projects)
projects := getAppOutput(b)
assert.Equal(t, 101, len(projects), "Expected 101 projects listed")
assert.NotContains(t, projects, "PASS")
}

// truncAppOutput truncates the list of strings returned from the "lab" test
// app to remove the test-specific output. It use "PASS" as a marker for the end
// of the app output and the beginning of the test output.
func truncAppOutput(output []string) []string {
for i, line := range output {
if line == "PASS" {
return output[:i]
}
}
return output
}
13 changes: 13 additions & 0 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,3 +421,16 @@ func copyTestRepo(log fatalLogger) string {
}
return dst
}

// getAppOutput splits and truncates the list of strings returned from the "lab"
// test binary to remove the test-specific output. It use "PASS" as a marker for
// the end of the app output and the beginning of the test output.
func getAppOutput(output []byte) []string {
lines := strings.Split(string(output), "\n")
for i, line := range lines {
if line == "PASS" {
return lines[:i]
}
}
return lines
}
13 changes: 0 additions & 13 deletions internal/copy/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ func Copy(src, dest string) error {
// Because this "copy" could be called recursively,
// "info" MUST be given here, NOT nil.
func copy(src, dest string, info os.FileInfo) error {
if info.Mode()&os.ModeSymlink != 0 {
return lcopy(src, dest, info)
}
if info.IsDir() {
return dcopy(src, dest, info)
}
Expand Down Expand Up @@ -83,13 +80,3 @@ func dcopy(srcdir, destdir string, info os.FileInfo) error {
}
return nil
}

// lcopy is for a symlink,
// with just creating a new symlink by replicating src symlink.
func lcopy(src, dest string, info os.FileInfo) error {
src, err := os.Readlink(src)
if err != nil {
return err
}
return os.Symlink(src, dest)
}
59 changes: 59 additions & 0 deletions internal/copy/copy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package copy

import (
"os"
"path/filepath"
"testing"

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

func TestMain(m *testing.M) {
os.MkdirAll("testdata.copy", os.ModePerm)
code := m.Run()
os.RemoveAll("testdata.copy")
os.Exit(code)
}

func TestCopy(t *testing.T) {

require.NoError(t, Copy("./testdata/case00", "./testdata.copy/case00"))
info, err := os.Stat("./testdata.copy/case00/README.md")
require.NoError(t, err)
assert.False(t, info.IsDir())

assert.Error(
t,
Copy("NOT/EXISTING/SOURCE/PATH", "anywhere"),
"Expected error when src doesn't exist")
assert.NoError(
t,
Copy("testdata/case01/README.md", "testdata.copy/case01/README.md"),
"No error when src is just a file")

dest := "foobar"
for i := 0; i < 8; i++ {
dest = dest + dest
}
err = Copy("testdata/case00", filepath.Join("testdata/case00", dest))
assert.Error(t, err)
assert.IsType(t, &os.PathError{}, err, "Expected error when filename is too long")

err = Copy("testdata/case02", "testdata.copy/case00/README.md")
assert.Error(t, err)
assert.IsType(
t,
&os.PathError{},
err,
"Expect error when creating a directory on existing filename")

assert.Error(
t,
Copy("testdata/case04/README.md", "testdata/case04"),
"Expected error when copying file to an existing dir")
assert.Error(
t,
Copy("testdata/case04/README.md", "testdata/case04/README.md/foobar"),
"Expected error when copying file to an existing file")
}
Empty file.
Empty file.
Empty file.
Empty file.
1 change: 1 addition & 0 deletions internal/copy/testdata/example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Example Test
7 changes: 7 additions & 0 deletions internal/gitlab/gitlab_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"testing"

"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/zaquestion/lab/internal/copy"
)
Expand Down Expand Up @@ -45,6 +46,12 @@ func TestMain(m *testing.M) {
os.Exit(code)
}

func TestGetProject(t *testing.T) {
project, err := GetProject("lab-testing/test")
require.NoError(t, err)
assert.Equal(t, 5694926, project.ID, "Expected 'lab-testing/test' to be project 5694926")
}

func TestUser(t *testing.T) {
// Should get set by Init() after TestMain()
require.Equal(t, "lab-testing", User())
Expand Down

0 comments on commit 34fb0e9

Please sign in to comment.