Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

worktree: test improvemnts on empty worktree #478

Merged
merged 1 commit into from
Jul 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 51 additions & 4 deletions common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package git
import (
"testing"

billy "gopkg.in/src-d/go-billy.v3"
"gopkg.in/src-d/go-git.v4/plumbing"
"gopkg.in/src-d/go-git.v4/plumbing/format/packfile"
"gopkg.in/src-d/go-git.v4/plumbing/transport"
Expand All @@ -12,7 +13,7 @@ import (
"github.com/src-d/go-git-fixtures"
. "gopkg.in/check.v1"
"gopkg.in/src-d/go-billy.v3/memfs"
"gopkg.in/src-d/go-billy.v3/osfs"
"gopkg.in/src-d/go-billy.v3/util"
)

func Test(t *testing.T) { TestingT(t) }
Expand Down Expand Up @@ -41,19 +42,65 @@ func (s *BaseSuite) buildBasicRepository(c *C) {
s.Repository = s.NewRepository(f)
}

// NewRepository returns a new repository using the .git folder, if the fixture
// is tagged as worktree the filesystem from fixture is used, otherwise a new
// memfs filesystem is used as worktree.
func (s *BaseSuite) NewRepository(f *fixtures.Fixture) *Repository {
fs := osfs.New(f.DotGit().Root())
st, err := filesystem.NewStorage(fs)
var worktree, dotgit billy.Filesystem
if f.Is("worktree") {
r, err := PlainOpen(f.Worktree().Root())
if err != nil {
panic(err)
}

return r
}

dotgit = f.DotGit()
worktree = memfs.New()

st, err := filesystem.NewStorage(dotgit)
if err != nil {
panic(err)
}

r, err := Open(st, worktree)
if err != nil {
panic(err)
}

return r
}

// NewRepositoryWithEmptyWorktree returns a new repository using the .git folder
// from the fixture but without a empty memfs worktree, the index and the
// modules are deleted from the .git folder.
func (s *BaseSuite) NewRepositoryWithEmptyWorktree(f *fixtures.Fixture) *Repository {
dotgit := f.DotGit()
err := dotgit.Remove("index")
if err != nil {
panic(err)
}

r, err := Open(st, fs)
err = util.RemoveAll(dotgit, "modules")
if err != nil {
panic(err)
}

worktree := memfs.New()

st, err := filesystem.NewStorage(dotgit)
if err != nil {
panic(err)
}

r, err := Open(st, worktree)
if err != nil {
panic(err)
}

return r

}

func (s *BaseSuite) NewRepositoryFromPackfile(f *fixtures.Fixture) *Repository {
Expand Down
42 changes: 10 additions & 32 deletions worktree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ type WorktreeSuite struct {
var _ = Suite(&WorktreeSuite{})

func (s *WorktreeSuite) SetUpTest(c *C) {
s.buildBasicRepository(c)
// the index is removed if not the Repository will be not clean
c.Assert(s.Repository.Storer.SetIndex(&index.Index{Version: 2}), IsNil)
f := fixtures.Basic().One()
s.Repository = s.NewRepositoryWithEmptyWorktree(f)
}

func (s *WorktreeSuite) TestCheckout(c *C) {
Expand Down Expand Up @@ -87,13 +86,9 @@ func (s *WorktreeSuite) TestCheckoutSymlink(c *C) {

func (s *WorktreeSuite) TestCheckoutSubmodule(c *C) {
url := "https://github.com/git-fixtures/submodule.git"
w := &Worktree{
r: s.NewRepository(fixtures.ByURL(url).One()),
fs: memfs.New(),
}
r := s.NewRepositoryWithEmptyWorktree(fixtures.ByURL(url).One())

// we delete the index, since the fixture comes with a real index
err := w.r.Storer.SetIndex(&index.Index{Version: 2})
w, err := r.Worktree()
c.Assert(err, IsNil)

err = w.Checkout(&CheckoutOptions{})
Expand All @@ -106,16 +101,11 @@ func (s *WorktreeSuite) TestCheckoutSubmodule(c *C) {

func (s *WorktreeSuite) TestCheckoutSubmoduleInitialized(c *C) {
url := "https://github.com/git-fixtures/submodule.git"
w := &Worktree{
r: s.NewRepository(fixtures.ByURL(url).One()),
fs: memfs.New(),
}
r := s.NewRepository(fixtures.ByURL(url).One())

err := w.r.Storer.SetIndex(&index.Index{Version: 2})
w, err := r.Worktree()
c.Assert(err, IsNil)

err = w.Checkout(&CheckoutOptions{})
c.Assert(err, IsNil)
sub, err := w.Submodules()
c.Assert(err, IsNil)

Expand Down Expand Up @@ -228,15 +218,8 @@ func (s *WorktreeSuite) TestCheckoutChange(c *C) {

func (s *WorktreeSuite) TestCheckoutTag(c *C) {
f := fixtures.ByTag("tags").One()

fs := memfs.New()
w := &Worktree{
r: s.NewRepository(f),
fs: fs,
}

// we delete the index, since the fixture comes with a real index
err := w.r.Storer.SetIndex(&index.Index{Version: 2})
r := s.NewRepositoryWithEmptyWorktree(f)
w, err := r.Worktree()
c.Assert(err, IsNil)

err = w.Checkout(&CheckoutOptions{})
Expand Down Expand Up @@ -282,14 +265,9 @@ func (s *WorktreeSuite) TestCheckoutBisectSubmodules(c *C) {
// checking every commit over the previous commit
func (s *WorktreeSuite) testCheckoutBisect(c *C, url string) {
f := fixtures.ByURL(url).One()
r := s.NewRepositoryWithEmptyWorktree(f)

w := &Worktree{
r: s.NewRepository(f),
fs: memfs.New(),
}

// we delete the index, since the fixture comes with a real index
err := w.r.Storer.SetIndex(&index.Index{Version: 2})
w, err := r.Worktree()
c.Assert(err, IsNil)

iter, err := w.r.Log(&LogOptions{})
Expand Down