diff --git a/remote_repository_test.go b/remote_repository_test.go index 4a86882..b4dfd21 100644 --- a/remote_repository_test.go +++ b/remote_repository_test.go @@ -1,7 +1,13 @@ package main import ( + "errors" + "fmt" "net/url" + "os/exec" + "strings" + + "github.com/motemen/ghq/utils" . "github.com/onsi/gomega" ) import "testing" @@ -14,7 +20,7 @@ func parseURL(urlString string) *url.URL { return u } -func TestNewRemoteRepository(t *testing.T) { +func TestNewRemoteRepositoryGitHub(t *testing.T) { RegisterTestingT(t) var ( @@ -34,3 +40,42 @@ func TestNewRemoteRepository(t *testing.T) { repo, err = NewRemoteRepository(parseURL("https://example.com/motemen/pusheen-explorer")) Expect(err).NotTo(BeNil()) } + +func TestNewRemoteRepositoryGoogleCode(t *testing.T) { + RegisterTestingT(t) + + var ( + repo RemoteRepository + err error + ) + + repo, err = NewRemoteRepository(parseURL("https://code.google.com/p/vim/")) + Expect(err).To(BeNil()) + Expect(repo.IsValid()).To(Equal(true)) + utils.CommandRunner = NewFakeRunner(map[string]error{ + "hg identify": nil, + "git ls-remote": errors.New(""), + }) + Expect(repo.VCS()).To(Equal(MercurialBackend)) + + repo, err = NewRemoteRepository(parseURL("https://code.google.com/p/git-core")) + Expect(err).To(BeNil()) + Expect(repo.IsValid()).To(Equal(true)) + utils.CommandRunner = NewFakeRunner(map[string]error{ + "hg identify": errors.New(""), + "git ls-remote": nil, + }) + Expect(repo.VCS()).To(Equal(GitBackend)) +} + +func NewFakeRunner(dispatch map[string]error) utils.RunFunc { + return func(cmd *exec.Cmd) error { + cmdString := strings.Join(cmd.Args, " ") + for cmdPrefix, err := range dispatch { + if strings.Index(cmdString, cmdPrefix) == 0 { + return err + } + } + panic(fmt.Sprintf("No fake dispatch found for: %s", cmdString)) + } +} diff --git a/utils/run.go b/utils/run.go index da3b5bc..6dd5be4 100644 --- a/utils/run.go +++ b/utils/run.go @@ -33,10 +33,16 @@ func RunInDir(dir, command string, args ...string) error { return RunCommand(cmd) } +type RunFunc func(*exec.Cmd) error + +var CommandRunner RunFunc = func(cmd *exec.Cmd) error { + return cmd.Run() +} + func RunCommand(cmd *exec.Cmd) error { Log(cmd.Args[0], strings.Join(cmd.Args[1:], " ")) - err := cmd.Run() + err := CommandRunner(cmd) if err != nil { return &RunError{cmd, err} }