aboutsummaryrefslogtreecommitdiffstats
path: root/utils/merkletrie/filesystem/node_test.go
diff options
context:
space:
mode:
authorMáximo Cuadros <mcuadros@gmail.com>2017-04-09 14:03:00 +0200
committerMáximo Cuadros <mcuadros@gmail.com>2017-04-11 04:35:24 +0200
commit9e0ae961aeb4969eb2756a961bc7721e4788fc21 (patch)
tree232ebfbfff83413cf5728e44e82682a0f77eb9c8 /utils/merkletrie/filesystem/node_test.go
parent6a00b3004a9bc9f86a041b59dc27e6b24695fc13 (diff)
downloadgo-git-9e0ae961aeb4969eb2756a961bc7721e4788fc21.tar.gz
merkletrie: node support for billy filesystems
Diffstat (limited to 'utils/merkletrie/filesystem/node_test.go')
-rw-r--r--utils/merkletrie/filesystem/node_test.go127
1 files changed, 127 insertions, 0 deletions
diff --git a/utils/merkletrie/filesystem/node_test.go b/utils/merkletrie/filesystem/node_test.go
new file mode 100644
index 0000000..291af6b
--- /dev/null
+++ b/utils/merkletrie/filesystem/node_test.go
@@ -0,0 +1,127 @@
+package filesystem
+
+import (
+ "io"
+ "os"
+ "testing"
+
+ . "gopkg.in/check.v1"
+ "gopkg.in/src-d/go-billy.v2"
+ "gopkg.in/src-d/go-billy.v2/memfs"
+ "gopkg.in/src-d/go-git.v4/utils/merkletrie"
+)
+
+func Test(t *testing.T) { TestingT(t) }
+
+type NoderSuite struct{}
+
+var _ = Suite(&NoderSuite{})
+
+func (s *NoderSuite) TestDiff(c *C) {
+ fsA := memfs.New()
+ WriteFile(fsA, "foo", []byte("foo"), 0644)
+ WriteFile(fsA, "qux/bar", []byte("foo"), 0644)
+ WriteFile(fsA, "qux/qux", []byte("foo"), 0644)
+
+ fsB := memfs.New()
+ WriteFile(fsB, "foo", []byte("foo"), 0644)
+ WriteFile(fsB, "qux/bar", []byte("foo"), 0644)
+ WriteFile(fsB, "qux/qux", []byte("foo"), 0644)
+
+ nodeA, err := NewRootNode(fsA)
+ c.Assert(err, IsNil)
+ nodeB, err := NewRootNode(fsB)
+ c.Assert(err, IsNil)
+
+ ch, err := merkletrie.DiffTree(nodeA, nodeB, IsEquals)
+ c.Assert(err, IsNil)
+ c.Assert(ch, HasLen, 0)
+}
+
+func (s *NoderSuite) TestDiffChangeContent(c *C) {
+ fsA := memfs.New()
+ WriteFile(fsA, "foo", []byte("foo"), 0644)
+ WriteFile(fsA, "qux/bar", []byte("foo"), 0644)
+ WriteFile(fsA, "qux/qux", []byte("foo"), 0644)
+
+ fsB := memfs.New()
+ WriteFile(fsB, "foo", []byte("foo"), 0644)
+ WriteFile(fsB, "qux/bar", []byte("bar"), 0644)
+ WriteFile(fsB, "qux/qux", []byte("foo"), 0644)
+
+ nodeA, err := NewRootNode(fsA)
+ c.Assert(err, IsNil)
+ nodeB, err := NewRootNode(fsB)
+ c.Assert(err, IsNil)
+
+ ch, err := merkletrie.DiffTree(nodeA, nodeB, IsEquals)
+ c.Assert(err, IsNil)
+ c.Assert(ch, HasLen, 1)
+}
+
+func (s *NoderSuite) TestDiffChangeMissing(c *C) {
+ fsA := memfs.New()
+ WriteFile(fsA, "foo", []byte("foo"), 0644)
+
+ fsB := memfs.New()
+ WriteFile(fsB, "bar", []byte("bar"), 0644)
+
+ nodeA, err := NewRootNode(fsA)
+ c.Assert(err, IsNil)
+ nodeB, err := NewRootNode(fsB)
+ c.Assert(err, IsNil)
+
+ ch, err := merkletrie.DiffTree(nodeA, nodeB, IsEquals)
+ c.Assert(err, IsNil)
+ c.Assert(ch, HasLen, 2)
+}
+
+func (s *NoderSuite) TestDiffChangeMode(c *C) {
+ fsA := memfs.New()
+ WriteFile(fsA, "foo", []byte("foo"), 0644)
+
+ fsB := memfs.New()
+ WriteFile(fsB, "foo", []byte("foo"), 0755)
+
+ nodeA, err := NewRootNode(fsA)
+ c.Assert(err, IsNil)
+ nodeB, err := NewRootNode(fsB)
+ c.Assert(err, IsNil)
+
+ ch, err := merkletrie.DiffTree(nodeA, nodeB, IsEquals)
+ c.Assert(err, IsNil)
+ c.Assert(ch, HasLen, 1)
+}
+
+func (s *NoderSuite) TestDiffChangeModeNotRelevant(c *C) {
+ fsA := memfs.New()
+ WriteFile(fsA, "foo", []byte("foo"), 0644)
+
+ fsB := memfs.New()
+ WriteFile(fsB, "foo", []byte("foo"), 0655)
+
+ nodeA, err := NewRootNode(fsA)
+ c.Assert(err, IsNil)
+ nodeB, err := NewRootNode(fsB)
+ c.Assert(err, IsNil)
+
+ ch, err := merkletrie.DiffTree(nodeA, nodeB, IsEquals)
+ c.Assert(err, IsNil)
+ c.Assert(ch, HasLen, 0)
+}
+
+func WriteFile(fs billy.Filesystem, filename string, data []byte, perm os.FileMode) error {
+ f, err := fs.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, perm)
+ if err != nil {
+ return err
+ }
+
+ n, err := f.Write(data)
+ if err == nil && n < len(data) {
+ err = io.ErrShortWrite
+ }
+ if err1 := f.Close(); err == nil {
+ err = err1
+ }
+ return err
+}