Skip to content
Permalink
Browse files

gitcache: refactor test setup to testutil

The git repo setup stuff is needed for other tests. Move it to a util
package.
  • Loading branch information
philips committed Aug 1, 2019
1 parent 790afc8 commit 558be66ca9ac38be6592e9d9ea1e683eb0c42260
Showing with 53 additions and 28 deletions.
  1. +12 −28 gitcache/gitcache_test.go
  2. +41 −0 internal/testutil/testutil.go
@@ -9,7 +9,8 @@ import (
"testing"

"gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing/object"

"github.com/merklecounty/rget/internal/testutil"
)

func TestPrefix(t *testing.T) {
@@ -25,34 +26,8 @@ func TestPrefix(t *testing.T) {
t.Fatal(err)
}

r, err := git.PlainInit(url, false)
if err != nil {
t.Fatal(err)
}

w, err := r.Worktree()
if err != nil {
t.Fatal(err)
}

err = ioutil.WriteFile(filepath.Join(url, "README"), []byte("Hello world"), 0755)
if err != nil {
t.Fatal(err)
}

_, err = w.Add("README")
if err != nil {
t.Fatal(err)
}

_, err = w.Commit("README\n", &git.CommitOptions{Author: &object.Signature{
Name: "Zohra",
}})
if err != nil {
t.Fatal(err)
}
testutil.EmptyGitRepo(t, url)

println(url)
gc, err := NewGitCache(filepath.Join(url, git.GitDirName), nil, filepath.Join(dir, "cache"))
if err != nil {
t.Fatal(err)
@@ -74,4 +49,13 @@ func TestPrefix(t *testing.T) {
if !reflect.DeepEqual(matches, expected) {
t.Errorf("matches = %v; want %v", matches, expected)
}

matches, err = gc.Prefix(ctx, "woo")
if err != nil {
t.Fatalf("prefix: %v", err)
}

if len(matches) != 0 {
t.Fatalf("prefix returned non-zero list")
}
}
@@ -0,0 +1,41 @@
package testutil

import (
"io/ioutil"
"path/filepath"
"testing"

"gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing/object"
)

func EmptyGitRepo(t *testing.T, url string) string {
r, err := git.PlainInit(url, false)
if err != nil {
t.Fatal(err)
}

w, err := r.Worktree()
if err != nil {
t.Fatal(err)
}

err = ioutil.WriteFile(filepath.Join(url, "README"), []byte("Hello world"), 0755)
if err != nil {
t.Fatal(err)
}

_, err = w.Add("README")
if err != nil {
t.Fatal(err)
}

_, err = w.Commit("README\n", &git.CommitOptions{Author: &object.Signature{
Name: "Zohra",
}})
if err != nil {
t.Fatal(err)
}

return filepath.Join(url, git.GitDirName)
}

0 comments on commit 558be66

Please sign in to comment.
You can’t perform that action at this time.