test remote repository detection

This commit is contained in:
motemen 2014-05-11 23:46:43 +09:00
parent 817fbe8708
commit 30a198dc84
2 changed files with 53 additions and 2 deletions

View file

@ -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))
}
}

View file

@ -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}
}