From 66f08b836000cb79d2a5b76d3e15e1f81dc77e0b Mon Sep 17 00:00:00 2001 From: Máximo Cuadros Date: Thu, 1 Feb 2018 17:10:42 +0100 Subject: new methods Worktree.[AddGlob|AddDirectory] MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Máximo Cuadros --- worktree_test.go | 160 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 160 insertions(+) (limited to 'worktree_test.go') diff --git a/worktree_test.go b/worktree_test.go index e51e89a..8f30e82 100644 --- a/worktree_test.go +++ b/worktree_test.go @@ -1115,6 +1115,40 @@ func (s *WorktreeSuite) TestAddUnmodified(c *C) { c.Assert(err, IsNil) } +func (s *WorktreeSuite) TestAddRemoved(c *C) { + fs := memfs.New() + w := &Worktree{ + r: s.Repository, + Filesystem: fs, + } + + err := w.Checkout(&CheckoutOptions{Force: true}) + c.Assert(err, IsNil) + + idx, err := w.r.Storer.Index() + c.Assert(err, IsNil) + c.Assert(idx.Entries, HasLen, 9) + + err = w.Filesystem.Remove("LICENSE") + c.Assert(err, IsNil) + + hash, err := w.Add("LICENSE") + c.Assert(err, IsNil) + c.Assert(hash.String(), Equals, "c192bd6a24ea1ab01d78686e417c8bdc7c3d197f") + + e, err := idx.Entry("LICENSE") + c.Assert(err, IsNil) + c.Assert(e.Hash, Equals, hash) + c.Assert(e.Mode, Equals, filemode.Regular) + + status, err := w.Status() + c.Assert(err, IsNil) + c.Assert(status, HasLen, 1) + + file := status.File("LICENSE") + c.Assert(file.Staging, Equals, Deleted) +} + func (s *WorktreeSuite) TestAddSymlink(c *C) { dir, err := ioutil.TempDir("", "checkout") c.Assert(err, IsNil) @@ -1141,7 +1175,133 @@ func (s *WorktreeSuite) TestAddSymlink(c *C) { c.Assert(err, IsNil) c.Assert(obj, NotNil) c.Assert(obj.Size(), Equals, int64(3)) +} + +func (s *WorktreeSuite) TestAddDirectory(c *C) { + fs := memfs.New() + w := &Worktree{ + r: s.Repository, + Filesystem: fs, + } + + err := w.Checkout(&CheckoutOptions{Force: true}) + c.Assert(err, IsNil) + + idx, err := w.r.Storer.Index() + c.Assert(err, IsNil) + c.Assert(idx.Entries, HasLen, 9) + + err = util.WriteFile(w.Filesystem, "qux/foo", []byte("FOO"), 0755) + c.Assert(err, IsNil) + err = util.WriteFile(w.Filesystem, "qux/baz/bar", []byte("BAR"), 0755) + c.Assert(err, IsNil) + + err = w.AddDirectory("qux") + c.Assert(err, IsNil) + + idx, err = w.r.Storer.Index() + c.Assert(err, IsNil) + c.Assert(idx.Entries, HasLen, 11) + + e, err := idx.Entry("qux/foo") + c.Assert(err, IsNil) + c.Assert(e.Mode, Equals, filemode.Executable) + + e, err = idx.Entry("qux/baz/bar") + c.Assert(err, IsNil) + c.Assert(e.Mode, Equals, filemode.Executable) + + status, err := w.Status() + c.Assert(err, IsNil) + c.Assert(status, HasLen, 2) + + file := status.File("qux/foo") + c.Assert(file.Staging, Equals, Added) + c.Assert(file.Worktree, Equals, Unmodified) + + file = status.File("qux/baz/bar") + c.Assert(file.Staging, Equals, Added) + c.Assert(file.Worktree, Equals, Unmodified) +} + +func (s *WorktreeSuite) TestAddDirectoryErrorNotDirectory(c *C) { + r, _ := Init(memory.NewStorage(), memfs.New()) + w, _ := r.Worktree() + + err := util.WriteFile(w.Filesystem, "foo", []byte("FOO"), 0755) + c.Assert(err, IsNil) + + err = w.AddDirectory("foo") + c.Assert(err, Equals, ErrNotDirectory) +} + +func (s *WorktreeSuite) TestAddDirectoryErrorNotFound(c *C) { + r, _ := Init(memory.NewStorage(), memfs.New()) + w, _ := r.Worktree() + + err := w.AddDirectory("foo") + c.Assert(err, NotNil) +} + +func (s *WorktreeSuite) TestAddGlob(c *C) { + fs := memfs.New() + w := &Worktree{ + r: s.Repository, + Filesystem: fs, + } + + err := w.Checkout(&CheckoutOptions{Force: true}) + c.Assert(err, IsNil) + + idx, err := w.r.Storer.Index() + c.Assert(err, IsNil) + c.Assert(idx.Entries, HasLen, 9) + + err = util.WriteFile(w.Filesystem, "qux/qux", []byte("QUX"), 0755) + c.Assert(err, IsNil) + err = util.WriteFile(w.Filesystem, "qux/baz", []byte("BAZ"), 0755) + c.Assert(err, IsNil) + err = util.WriteFile(w.Filesystem, "qux/bar/baz", []byte("BAZ"), 0755) + c.Assert(err, IsNil) + + err = w.AddGlob("qux/b*") + c.Assert(err, IsNil) + + idx, err = w.r.Storer.Index() + c.Assert(err, IsNil) + c.Assert(idx.Entries, HasLen, 11) + + e, err := idx.Entry("qux/baz") + c.Assert(err, IsNil) + c.Assert(e.Mode, Equals, filemode.Executable) + + e, err = idx.Entry("qux/bar/baz") + c.Assert(err, IsNil) + c.Assert(e.Mode, Equals, filemode.Executable) + + status, err := w.Status() + c.Assert(err, IsNil) + c.Assert(status, HasLen, 3) + + file := status.File("qux/qux") + c.Assert(file.Staging, Equals, Untracked) + c.Assert(file.Worktree, Equals, Untracked) + + file = status.File("qux/baz") + c.Assert(file.Staging, Equals, Added) + c.Assert(file.Worktree, Equals, Unmodified) + + file = status.File("qux/bar/baz") + c.Assert(file.Staging, Equals, Added) + c.Assert(file.Worktree, Equals, Unmodified) +} + +func (s *WorktreeSuite) TestAddGlobErrorNoMatches(c *C) { + r, _ := Init(memory.NewStorage(), memfs.New()) + w, _ := r.Worktree() + err := w.AddGlob("foo") + c.Assert(err, Equals, ErrGlobNoMatches) } func (s *WorktreeSuite) TestRemove(c *C) { -- cgit From 6d23b50e27312f3ba3e839153c2c0db5237c827d Mon Sep 17 00:00:00 2001 From: Máximo Cuadros Date: Mon, 26 Feb 2018 00:26:41 +0100 Subject: new methods Worktree.[AddGlob|RemoveBlob] and recursive Worktree.[Add|Remove] MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Máximo Cuadros --- worktree_test.go | 139 +++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 125 insertions(+), 14 deletions(-) (limited to 'worktree_test.go') diff --git a/worktree_test.go b/worktree_test.go index 8f30e82..cb2e5e2 100644 --- a/worktree_test.go +++ b/worktree_test.go @@ -1196,8 +1196,9 @@ func (s *WorktreeSuite) TestAddDirectory(c *C) { err = util.WriteFile(w.Filesystem, "qux/baz/bar", []byte("BAR"), 0755) c.Assert(err, IsNil) - err = w.AddDirectory("qux") + h, err := w.Add("qux") c.Assert(err, IsNil) + c.Assert(h.IsZero(), Equals, true) idx, err = w.r.Storer.Index() c.Assert(err, IsNil) @@ -1224,23 +1225,13 @@ func (s *WorktreeSuite) TestAddDirectory(c *C) { c.Assert(file.Worktree, Equals, Unmodified) } -func (s *WorktreeSuite) TestAddDirectoryErrorNotDirectory(c *C) { - r, _ := Init(memory.NewStorage(), memfs.New()) - w, _ := r.Worktree() - - err := util.WriteFile(w.Filesystem, "foo", []byte("FOO"), 0755) - c.Assert(err, IsNil) - - err = w.AddDirectory("foo") - c.Assert(err, Equals, ErrNotDirectory) -} - func (s *WorktreeSuite) TestAddDirectoryErrorNotFound(c *C) { r, _ := Init(memory.NewStorage(), memfs.New()) w, _ := r.Worktree() - err := w.AddDirectory("foo") + h, err := w.Add("foo") c.Assert(err, NotNil) + c.Assert(h.IsZero(), Equals, true) } func (s *WorktreeSuite) TestAddGlob(c *C) { @@ -1264,7 +1255,7 @@ func (s *WorktreeSuite) TestAddGlob(c *C) { err = util.WriteFile(w.Filesystem, "qux/bar/baz", []byte("BAZ"), 0755) c.Assert(err, IsNil) - err = w.AddGlob("qux/b*") + err = w.AddGlob(w.Filesystem.Join("qux", "b*")) c.Assert(err, IsNil) idx, err = w.r.Storer.Index() @@ -1339,6 +1330,58 @@ func (s *WorktreeSuite) TestRemoveNotExistentEntry(c *C) { c.Assert(err, NotNil) } +func (s *WorktreeSuite) TestRemoveDirectory(c *C) { + fs := memfs.New() + w := &Worktree{ + r: s.Repository, + Filesystem: fs, + } + + err := w.Checkout(&CheckoutOptions{Force: true}) + c.Assert(err, IsNil) + + hash, err := w.Remove("json") + c.Assert(hash.IsZero(), Equals, true) + c.Assert(err, IsNil) + + status, err := w.Status() + c.Assert(err, IsNil) + c.Assert(status, HasLen, 2) + c.Assert(status.File("json/long.json").Staging, Equals, Deleted) + c.Assert(status.File("json/short.json").Staging, Equals, Deleted) + + _, err = w.Filesystem.Stat("json") + c.Assert(os.IsNotExist(err), Equals, true) +} + +func (s *WorktreeSuite) TestRemoveDirectoryUntracked(c *C) { + fs := memfs.New() + w := &Worktree{ + r: s.Repository, + Filesystem: fs, + } + + err := w.Checkout(&CheckoutOptions{Force: true}) + c.Assert(err, IsNil) + + err = util.WriteFile(w.Filesystem, "json/foo", []byte("FOO"), 0755) + c.Assert(err, IsNil) + + hash, err := w.Remove("json") + c.Assert(hash.IsZero(), Equals, true) + c.Assert(err, IsNil) + + status, err := w.Status() + c.Assert(err, IsNil) + c.Assert(status, HasLen, 3) + c.Assert(status.File("json/long.json").Staging, Equals, Deleted) + c.Assert(status.File("json/short.json").Staging, Equals, Deleted) + c.Assert(status.File("json/foo").Staging, Equals, Untracked) + + _, err = w.Filesystem.Stat("json") + c.Assert(err, IsNil) +} + func (s *WorktreeSuite) TestRemoveDeletedFromWorktree(c *C) { fs := memfs.New() w := &Worktree{ @@ -1362,6 +1405,74 @@ func (s *WorktreeSuite) TestRemoveDeletedFromWorktree(c *C) { c.Assert(status.File("LICENSE").Staging, Equals, Deleted) } +func (s *WorktreeSuite) TestRemoveGlob(c *C) { + fs := memfs.New() + w := &Worktree{ + r: s.Repository, + Filesystem: fs, + } + + err := w.Checkout(&CheckoutOptions{Force: true}) + c.Assert(err, IsNil) + + err = w.RemoveGlob(w.Filesystem.Join("json", "l*")) + c.Assert(err, IsNil) + + status, err := w.Status() + c.Assert(err, IsNil) + c.Assert(status, HasLen, 1) + c.Assert(status.File("json/long.json").Staging, Equals, Deleted) +} + +func (s *WorktreeSuite) TestRemoveGlobDirectory(c *C) { + fs := memfs.New() + w := &Worktree{ + r: s.Repository, + Filesystem: fs, + } + + err := w.Checkout(&CheckoutOptions{Force: true}) + c.Assert(err, IsNil) + + err = w.RemoveGlob("js*") + c.Assert(err, IsNil) + + status, err := w.Status() + c.Assert(err, IsNil) + c.Assert(status, HasLen, 2) + c.Assert(status.File("json/short.json").Staging, Equals, Deleted) + c.Assert(status.File("json/long.json").Staging, Equals, Deleted) + + _, err = w.Filesystem.Stat("json") + c.Assert(os.IsNotExist(err), Equals, true) +} + +func (s *WorktreeSuite) TestRemoveGlobDirectoryDeleted(c *C) { + fs := memfs.New() + w := &Worktree{ + r: s.Repository, + Filesystem: fs, + } + + err := w.Checkout(&CheckoutOptions{Force: true}) + c.Assert(err, IsNil) + + err = fs.Remove("json/short.json") + c.Assert(err, IsNil) + + err = util.WriteFile(w.Filesystem, "json/foo", []byte("FOO"), 0755) + c.Assert(err, IsNil) + + err = w.RemoveGlob("js*") + c.Assert(err, IsNil) + + status, err := w.Status() + c.Assert(err, IsNil) + c.Assert(status, HasLen, 3) + c.Assert(status.File("json/short.json").Staging, Equals, Deleted) + c.Assert(status.File("json/long.json").Staging, Equals, Deleted) +} + func (s *WorktreeSuite) TestMove(c *C) { fs := memfs.New() w := &Worktree{ -- cgit