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

Commit af9c6ce

Browse files
authored
Merge pull request #478 from mcuadros/test-worktree
worktree: test improvemnts on empty worktree
2 parents b54325f + 1354c5f commit af9c6ce

File tree

2 files changed

+61
-36
lines changed

2 files changed

+61
-36
lines changed

common_test.go

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package git
33
import (
44
"testing"
55

6+
billy "gopkg.in/src-d/go-billy.v3"
67
"gopkg.in/src-d/go-git.v4/plumbing"
78
"gopkg.in/src-d/go-git.v4/plumbing/format/packfile"
89
"gopkg.in/src-d/go-git.v4/plumbing/transport"
@@ -12,7 +13,7 @@ import (
1213
"github.com/src-d/go-git-fixtures"
1314
. "gopkg.in/check.v1"
1415
"gopkg.in/src-d/go-billy.v3/memfs"
15-
"gopkg.in/src-d/go-billy.v3/osfs"
16+
"gopkg.in/src-d/go-billy.v3/util"
1617
)
1718

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

45+
// NewRepository returns a new repository using the .git folder, if the fixture
46+
// is tagged as worktree the filesystem from fixture is used, otherwise a new
47+
// memfs filesystem is used as worktree.
4448
func (s *BaseSuite) NewRepository(f *fixtures.Fixture) *Repository {
45-
fs := osfs.New(f.DotGit().Root())
46-
st, err := filesystem.NewStorage(fs)
49+
var worktree, dotgit billy.Filesystem
50+
if f.Is("worktree") {
51+
r, err := PlainOpen(f.Worktree().Root())
52+
if err != nil {
53+
panic(err)
54+
}
55+
56+
return r
57+
}
58+
59+
dotgit = f.DotGit()
60+
worktree = memfs.New()
61+
62+
st, err := filesystem.NewStorage(dotgit)
63+
if err != nil {
64+
panic(err)
65+
}
66+
67+
r, err := Open(st, worktree)
68+
if err != nil {
69+
panic(err)
70+
}
71+
72+
return r
73+
}
74+
75+
// NewRepositoryWithEmptyWorktree returns a new repository using the .git folder
76+
// from the fixture but without a empty memfs worktree, the index and the
77+
// modules are deleted from the .git folder.
78+
func (s *BaseSuite) NewRepositoryWithEmptyWorktree(f *fixtures.Fixture) *Repository {
79+
dotgit := f.DotGit()
80+
err := dotgit.Remove("index")
4781
if err != nil {
4882
panic(err)
4983
}
5084

51-
r, err := Open(st, fs)
85+
err = util.RemoveAll(dotgit, "modules")
86+
if err != nil {
87+
panic(err)
88+
}
89+
90+
worktree := memfs.New()
91+
92+
st, err := filesystem.NewStorage(dotgit)
93+
if err != nil {
94+
panic(err)
95+
}
96+
97+
r, err := Open(st, worktree)
5298
if err != nil {
5399
panic(err)
54100
}
55101

56102
return r
103+
57104
}
58105

59106
func (s *BaseSuite) NewRepositoryFromPackfile(f *fixtures.Fixture) *Repository {

worktree_test.go

Lines changed: 10 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,8 @@ type WorktreeSuite struct {
2525
var _ = Suite(&WorktreeSuite{})
2626

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

3332
func (s *WorktreeSuite) TestCheckout(c *C) {
@@ -87,13 +86,9 @@ func (s *WorktreeSuite) TestCheckoutSymlink(c *C) {
8786

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

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

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

107102
func (s *WorktreeSuite) TestCheckoutSubmoduleInitialized(c *C) {
108103
url := "https://github.com/git-fixtures/submodule.git"
109-
w := &Worktree{
110-
r: s.NewRepository(fixtures.ByURL(url).One()),
111-
fs: memfs.New(),
112-
}
104+
r := s.NewRepository(fixtures.ByURL(url).One())
113105

114-
err := w.r.Storer.SetIndex(&index.Index{Version: 2})
106+
w, err := r.Worktree()
115107
c.Assert(err, IsNil)
116108

117-
err = w.Checkout(&CheckoutOptions{})
118-
c.Assert(err, IsNil)
119109
sub, err := w.Submodules()
120110
c.Assert(err, IsNil)
121111

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

229219
func (s *WorktreeSuite) TestCheckoutTag(c *C) {
230220
f := fixtures.ByTag("tags").One()
231-
232-
fs := memfs.New()
233-
w := &Worktree{
234-
r: s.NewRepository(f),
235-
fs: fs,
236-
}
237-
238-
// we delete the index, since the fixture comes with a real index
239-
err := w.r.Storer.SetIndex(&index.Index{Version: 2})
221+
r := s.NewRepositoryWithEmptyWorktree(f)
222+
w, err := r.Worktree()
240223
c.Assert(err, IsNil)
241224

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

286-
w := &Worktree{
287-
r: s.NewRepository(f),
288-
fs: memfs.New(),
289-
}
290-
291-
// we delete the index, since the fixture comes with a real index
292-
err := w.r.Storer.SetIndex(&index.Index{Version: 2})
270+
w, err := r.Worktree()
293271
c.Assert(err, IsNil)
294272

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

0 commit comments

Comments
 (0)