Skip to content

Commit

Permalink
fork: add test for custom target options
Browse files Browse the repository at this point in the history
This test should cover the new feature that enable the user to use a custom
target name, namespace and path values to the project being forked.

Signed-off-by: Bruno Meneguele <bmeneg@redhat.com>
  • Loading branch information
bmeneg committed Dec 18, 2020
1 parent 54d13ae commit d980c61
Showing 1 changed file with 124 additions and 41 deletions.
165 changes: 124 additions & 41 deletions cmd/fork_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,56 +12,139 @@ import (
lab "github.com/zaquestion/lab/internal/gitlab"
)

func Test_fork(t *testing.T) {
t.Parallel()
func cleanupFork(t *testing.T, project string) {
// Failing to find the project will fail the test and is a legit
// failure case since its the only thing asserting the project exists
// (was forked)
p, err := lab.FindProject(project)
if err != nil {
t.Fatal(errors.Wrap(err, "failed to find project "+project+" for cleanup"))
}
err = lab.ProjectDelete(p.ID)
if err != nil {
t.Fatal(errors.Wrap(err, "failed to delete project "+project+" during cleanup"))
}
}

repo := copyTestRepo(t)
func Test_fork(t *testing.T) {
tests := []struct {
desc string
args []string
name string
path string
namespace string
}{
{
desc: "do_fork",
args: []string{},
name: "fork_test",
path: "",
namespace: "",
},
{
desc: "do_fork_name",
args: []string{"-n", "fork_test_name"},
name: "fork_test_name",
path: "",
namespace: "",
},
{
desc: "do_fork_path",
args: []string{"-p", "fork_test_path"},
name: "fork_test",
path: "fork_test_path",
namespace: "",
},
{
desc: "do_fork_name_path",
args: []string{"-n", "fork_test_name_1", "-p", "fork_test_path_1"},
name: "fork_test_name_1",
path: "fork_test_path_1",
namespace: "",
},
{
desc: "do_fork_namespace",
args: []string{"-m", "lab-testing-test-group"},
name: "fork_test",
path: "",
namespace: "lab-testing-test-group",
},
{
desc: "do_fork_namespace_name",
args: []string{"-m", "lab-testing-test-group", "-n", "fork_test_name"},
name: "fork_test_name",
path: "",
namespace: "lab-testing-test-group",
},
{
desc: "do_fork_namespace_path",
args: []string{"-m", "lab-testing-test-group", "-p", "fork_test_path"},
name: "fork_test",
path: "fork_test_path",
namespace: "lab-testing-test-group",
},
{
desc: "do_fork_namespace_name_path",
args: []string{"-m", "lab-testing-test-group", "-n", "fork_test_name_1", "-p", "fork_test_path_1"},
name: "fork_test_name_1",
path: "fork_test_path_1",
namespace: "lab-testing-test-group",
},
}

// remove the .git/config so no remotes exist
os.Remove(path.Join(repo, ".git/config"))
t.Parallel()

cmd := exec.Command("git", "remote", "add", "origin",
"git@gitlab.com:zaquestion/fork_test")
cmd.Dir = repo
err := cmd.Run()
if err != nil {
t.Fatal(err)
}
for _, test := range tests {
test := test
t.Run(test.desc, func(t *testing.T) {
// remove the .git/config so no remotes exist
repo := copyTestRepo(t)
os.Remove(path.Join(repo, ".git/config"))

t.Run("do_fork", func(t *testing.T) {
cmd = exec.Command(labBinaryPath, "fork")
cmd.Dir = repo
b, err := cmd.CombinedOutput()
if err != nil {
t.Fatal(err)
}
cmd := exec.Command("git", "remote", "add", "origin", "git@gitlab.com:zaquestion/fork_test")
cmd.Dir = repo
err := cmd.Run()
if err != nil {
t.Fatal(err)
}

out := string(b)
args := []string{"fork"}
if len(test.args) > 0 {
args = append(args, test.args...)
}
cmd = exec.Command(labBinaryPath, args...)
cmd.Dir = repo
b, err := cmd.CombinedOutput()
out := string(b)
if err != nil {
t.Log(out)
t.Fatal(err)
}

require.Contains(t, out, "From gitlab.com:lab-testing/fork_test")
require.Contains(t, out, "new remote: lab-testing")
namespace := "lab-testing"
if test.namespace != "" {
namespace = test.namespace
}
name := test.name
if test.path != "" {
name = test.path
}
project := namespace + "/" + name

cmd = exec.Command("git", "remote", "-v")
cmd.Dir = repo
require.Contains(t, out, "From gitlab.com:"+project)
require.Contains(t, out, "new remote: "+namespace)

b, err = cmd.CombinedOutput()
if err != nil {
t.Fatal(err)
}
require.Contains(t, string(b), "lab-testing")
})
time.Sleep(2 * time.Second)
cmd = exec.Command("git", "remote", "-v")
cmd.Dir = repo

// Failing to find the project will fail the test and is a legit
// failure case since its the only thing asserting the project exists
// (was forked)
p, err := lab.FindProject("fork_test")
if err != nil {
t.Fatal(errors.Wrap(err, "failed to find project for cleanup"))
}
err = lab.ProjectDelete(p.ID)
if err != nil {
t.Fatal(errors.Wrap(err, "failed to delete project during cleanup"))
b, err = cmd.CombinedOutput()
if err != nil {
t.Fatal(err)
}
require.Contains(t, string(b), namespace)
time.Sleep(2 * time.Second)
cleanupFork(t, project)
})
}
}

Expand Down

0 comments on commit d980c61

Please sign in to comment.