From 23077b75410d6fab51762236f195d92bd559fc6a Mon Sep 17 00:00:00 2001 From: Stefan Majewsky Date: Fri, 7 Jul 2017 12:14:06 +0200 Subject: many more `rtree get` tests --- pkg/rtree/get_test.go | 136 ++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 125 insertions(+), 11 deletions(-) (limited to 'pkg/rtree/get_test.go') diff --git a/pkg/rtree/get_test.go b/pkg/rtree/get_test.go index be7e092..8ab21f1 100644 --- a/pkg/rtree/get_test.go +++ b/pkg/rtree/get_test.go @@ -19,24 +19,138 @@ package rtree import ( + "fmt" "path/filepath" "testing" + + "github.com/majewsky/gofu/pkg/cli" ) -func TestGetRepoFromIndex(t *testing.T) { - idx := Index{ - Repos: []*Repo{ - { - CheckoutPath: "github.com/git/git", - Remotes: []Remote{ - {Name: "origin", URL: "gh:git/git"}, - }, +var testIndexWithTwoRepos = Index{ + Repos: []*Repo{ + { + CheckoutPath: "github.com/foo/bar", + Remotes: []Remote{ + {Name: "origin", URL: "https://github.com/foo/bar"}, + }, + }, + { + CheckoutPath: "github.com/git/git", + Remotes: []Remote{ + {Name: "origin", URL: "gh:git/git"}, }, }, - } + }, +} + +func TestGetExistingRepo(t *testing.T) { Test{ Args: []string{"get", "gh:git/git"}, - Index: idx, + Index: testIndexWithTwoRepos, + ExpectOutput: filepath.Join(RootPath, "/github.com/git/git") + "\n", + }.Run(t) +} + +func TestGetExistingRepoWithoutShortcut(t *testing.T) { + Test{ + Args: []string{"get", "https://github.com/git/git"}, + Index: testIndexWithTwoRepos, ExpectOutput: filepath.Join(RootPath, "/github.com/git/git") + "\n", - }.Run(t, "TestGetRepoFromIndex") + }.Run(t) +} + +func TestGetNewRepo(t *testing.T) { + target := filepath.Join(RootPath, "/github.com/another/repo") + + Test{ + Args: []string{"get", "gh:another/repo"}, + Index: testIndexWithTwoRepos, + ExpectOutput: target + "\n", + ExpectExecution: []RecordedCommand{ + {Cmd: cli.Command{ + Program: []string{"git", "clone", "gh:another/repo", target}, + }}, + }, + ExpectIndex: &Index{ + Repos: []*Repo{ + { + CheckoutPath: "github.com/another/repo", + Remotes: []Remote{ + {Name: "origin", URL: "gh:another/repo"}, + }, + }, + testIndexWithTwoRepos.Repos[0], + testIndexWithTwoRepos.Repos[1], + }, + }, + }.Run(t) +} + +func TestGetNewForkAsRemote(t *testing.T) { + target := filepath.Join(RootPath, "/github.com/git/git") + Test{ + Args: []string{"get", "https://example.com/git"}, + Index: testIndexWithTwoRepos, + Input: fmt.Sprintf("add as remote to %s\nmyfork\n", target), + ExpectOutput: target + "\n", + ExpectError: fmt.Sprintf( + "Found possible fork candidates. What to do? -> add as remote to %s\n"+ + "Existing remotes:\n\t(origin) gh:git/git\n"+ + "Enter remote name for https://example.com/git: myfork\n", + target, + ), + ExpectExecution: []RecordedCommand{ + {Cmd: cli.Command{ + Program: []string{"git", "remote", "add", "myfork", "https://example.com/git"}, + WorkDir: target, + }}, + {Cmd: cli.Command{ + Program: []string{"git", "remote", "update", "myfork"}, + WorkDir: target, + }}, + }, + ExpectIndex: &Index{ + Repos: []*Repo{ + testIndexWithTwoRepos.Repos[0], + { + CheckoutPath: "github.com/git/git", + Remotes: []Remote{ + {Name: "origin", URL: "gh:git/git"}, + {Name: "myfork", URL: "https://example.com/git"}, + }, + }, + }, + }, + }.Run(t) +} + +func TestGetNewForkAsSeparate(t *testing.T) { + target := filepath.Join(RootPath, "/example.com/git") + Test{ + Args: []string{"get", "https://example.com/git"}, + Index: testIndexWithTwoRepos, + Input: fmt.Sprintf("clone to %s\n", target), + ExpectOutput: target + "\n", + ExpectError: fmt.Sprintf( + "Found possible fork candidates. What to do? -> clone to %s\n", + target, + ), + ExpectExecution: []RecordedCommand{ + {Cmd: cli.Command{ + Program: []string{"git", "clone", "https://example.com/git", target}, + }}, + }, + ExpectIndex: &Index{ + Repos: []*Repo{ + { + CheckoutPath: "example.com/git", + Remotes: []Remote{ + {Name: "origin", URL: "https://example.com/git"}, + }, + }, + testIndexWithTwoRepos.Repos[0], + testIndexWithTwoRepos.Repos[1], + }, + }, + }.Run(t) } -- cgit v1.3.1