Skip to content
This repository has been archived by the owner on Jul 30, 2020. It is now read-only.

Commit

Permalink
gitcache: refactor test setup to testutil
Browse files Browse the repository at this point in the history
The git repo setup stuff is needed for other tests. Move it to a util
package.
  • Loading branch information
Brandon Philips committed Aug 1, 2019
1 parent 790afc8 commit 558be66
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 28 deletions.
40 changes: 12 additions & 28 deletions gitcache/gitcache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
Expand All @@ -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")
}
}
41 changes: 41 additions & 0 deletions internal/testutil/testutil.go
Original file line number Diff line number Diff line change
@@ -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.