Clone test repo instead of querying on top of it

This commit is contained in:
Jingwen Owen Ou 2014-02-10 12:51:35 -08:00
Родитель 0477058672
Коммит 12751d5be8
2 изменённых файлов: 52 добавлений и 12 удалений

Просмотреть файл

@ -1,33 +1,73 @@
package fixtures
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/github/hub/cmd"
)
var pwd string
func init() {
// need to cache it before all tests run
pwd, _ = os.Getwd()
}
type TestRepo struct {
Pwd string
pwd string
dir string
Remote string
}
func (g *TestRepo) Setup() {
g.Pwd, _ = os.Getwd()
fixturePath := filepath.Join(g.Pwd, "..", "fixtures", "test.git")
err := os.Chdir(fixturePath)
func (r *TestRepo) Setup() (err error) {
r.dir, err = ioutil.TempDir("", "test-repo")
if err != nil {
panic(err)
return
}
targetPath := filepath.Join(r.dir, "test.git")
err = r.clone(r.Remote, targetPath)
if err != nil {
return
}
return os.Chdir(targetPath)
}
func (g *TestRepo) TearDown() {
err := os.Chdir(g.Pwd)
func (r *TestRepo) clone(repo, dir string) error {
cmd := cmd.New("git").WithArgs("clone", repo, dir)
output, err := cmd.ExecOutput()
if err != nil {
panic(err)
err = fmt.Errorf("error cloning %s to %s: %s", repo, dir, output)
}
return err
}
func (r *TestRepo) TearDown() error {
err := os.Remove(r.dir)
if err != nil {
return err
}
err = os.Chdir(r.pwd)
if err != nil {
return err
}
return nil
}
func SetupTestRepo() *TestRepo {
repo := &TestRepo{}
repo.Setup()
remotePath := filepath.Join(pwd, "..", "fixtures", "test.git")
repo := &TestRepo{pwd: pwd, Remote: remotePath}
err := repo.Setup()
if err != nil {
panic(err)
}
return repo
}

Просмотреть файл

@ -13,5 +13,5 @@ func TestOriginRemote(t *testing.T) {
gitRemote, _ := OriginRemote()
assert.Equal(t, "origin", gitRemote.Name)
assert.Equal(t, "https://github.com/test/test.git.git", gitRemote.URL.String())
assert.Equal(t, repo.Remote, gitRemote.URL.String())
}