diff --git a/cmd/issue_create_test.go b/cmd/issue_create_test.go index 04863820..03d2190a 100644 --- a/cmd/issue_create_test.go +++ b/cmd/issue_create_test.go @@ -1,7 +1,9 @@ package cmd import ( + "net/url" "os/exec" + "path" "testing" "github.com/stretchr/testify/assert" @@ -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) { diff --git a/cmd/mr_list_test.go b/cmd/mr_list_test.go index 2c8c7b0d..4a7e2601 100644 --- a/cmd/mr_list_test.go +++ b/cmd/mr_list_test.go @@ -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") } diff --git a/cmd/project_list_test.go b/cmd/project_list_test.go index 3e7b9a2e..eaa9f969 100644 --- a/cmd/project_list_test.go +++ b/cmd/project_list_test.go @@ -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 -} diff --git a/cmd/root_test.go b/cmd/root_test.go index efb3ffbe..a852602c 100644 --- a/cmd/root_test.go +++ b/cmd/root_test.go @@ -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 +} diff --git a/internal/copy/copy.go b/internal/copy/copy.go index 8defea65..0f97d216 100644 --- a/internal/copy/copy.go +++ b/internal/copy/copy.go @@ -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) } @@ -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) -} diff --git a/internal/copy/copy_test.go b/internal/copy/copy_test.go new file mode 100644 index 00000000..d3af3204 --- /dev/null +++ b/internal/copy/copy_test.go @@ -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") +} diff --git a/internal/copy/testdata/case00/README.md b/internal/copy/testdata/case00/README.md new file mode 100644 index 00000000..e69de29b diff --git a/internal/copy/testdata/case01/README.md b/internal/copy/testdata/case01/README.md new file mode 100644 index 00000000..e69de29b diff --git a/internal/copy/testdata/case02/README.md b/internal/copy/testdata/case02/README.md new file mode 100644 index 00000000..e69de29b diff --git a/internal/copy/testdata/case04/README.md b/internal/copy/testdata/case04/README.md new file mode 100644 index 00000000..e69de29b diff --git a/internal/copy/testdata/example/README.md b/internal/copy/testdata/example/README.md new file mode 100644 index 00000000..affed003 --- /dev/null +++ b/internal/copy/testdata/example/README.md @@ -0,0 +1 @@ +# Example Test \ No newline at end of file diff --git a/internal/gitlab/gitlab_test.go b/internal/gitlab/gitlab_test.go index 0d59136a..c0825971 100644 --- a/internal/gitlab/gitlab_test.go +++ b/internal/gitlab/gitlab_test.go @@ -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" ) @@ -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())