1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
|
package index
import (
"bytes"
"strings"
"time"
"github.com/go-git/go-git/v5/plumbing"
"github.com/google/go-cmp/cmp"
. "gopkg.in/check.v1"
)
func (s *IndexSuite) TestEncode(c *C) {
idx := &Index{
Version: 2,
Entries: []*Entry{{
CreatedAt: time.Now(),
ModifiedAt: time.Now(),
Dev: 4242,
Inode: 424242,
UID: 84,
GID: 8484,
Size: 42,
Stage: TheirMode,
Hash: plumbing.NewHash("e25b29c8946e0e192fae2edc1dabf7be71e8ecf3"),
Name: "foo",
}, {
CreatedAt: time.Now(),
ModifiedAt: time.Now(),
Name: "bar",
Size: 82,
}, {
CreatedAt: time.Now(),
ModifiedAt: time.Now(),
Name: strings.Repeat(" ", 20),
Size: 82,
}},
}
buf := bytes.NewBuffer(nil)
e := NewEncoder(buf)
err := e.Encode(idx)
c.Assert(err, IsNil)
output := &Index{}
d := NewDecoder(buf)
err = d.Decode(output)
c.Assert(err, IsNil)
c.Assert(cmp.Equal(idx, output), Equals, true)
c.Assert(output.Entries[0].Name, Equals, strings.Repeat(" ", 20))
c.Assert(output.Entries[1].Name, Equals, "bar")
c.Assert(output.Entries[2].Name, Equals, "foo")
}
func (s *IndexSuite) TestEncodeV4(c *C) {
idx := &Index{
Version: 4,
Entries: []*Entry{{
CreatedAt: time.Now(),
ModifiedAt: time.Now(),
Dev: 4242,
Inode: 424242,
UID: 84,
GID: 8484,
Size: 42,
Stage: TheirMode,
Hash: plumbing.NewHash("e25b29c8946e0e192fae2edc1dabf7be71e8ecf3"),
Name: "foo",
}, {
CreatedAt: time.Now(),
ModifiedAt: time.Now(),
Name: "bar",
Size: 82,
}, {
CreatedAt: time.Now(),
ModifiedAt: time.Now(),
Name: strings.Repeat(" ", 20),
Size: 82,
}, {
CreatedAt: time.Now(),
ModifiedAt: time.Now(),
Name: "baz/bar",
Size: 82,
}, {
CreatedAt: time.Now(),
ModifiedAt: time.Now(),
Name: "baz/bar/bar",
Size: 82,
}},
}
buf := bytes.NewBuffer(nil)
e := NewEncoder(buf)
err := e.Encode(idx)
c.Assert(err, IsNil)
output := &Index{}
d := NewDecoder(buf)
err = d.Decode(output)
c.Assert(err, IsNil)
c.Assert(cmp.Equal(idx, output), Equals, true)
c.Assert(output.Entries[0].Name, Equals, strings.Repeat(" ", 20))
c.Assert(output.Entries[1].Name, Equals, "bar")
c.Assert(output.Entries[2].Name, Equals, "baz/bar")
c.Assert(output.Entries[3].Name, Equals, "baz/bar/bar")
c.Assert(output.Entries[4].Name, Equals, "foo")
}
func (s *IndexSuite) TestEncodeUnsupportedVersion(c *C) {
idx := &Index{Version: 5}
buf := bytes.NewBuffer(nil)
e := NewEncoder(buf)
err := e.Encode(idx)
c.Assert(err, Equals, ErrUnsupportedVersion)
}
func (s *IndexSuite) TestEncodeWithIntentToAddUnsupportedVersion(c *C) {
idx := &Index{
Version: 3,
Entries: []*Entry{{IntentToAdd: true}},
}
buf := bytes.NewBuffer(nil)
e := NewEncoder(buf)
err := e.Encode(idx)
c.Assert(err, IsNil)
output := &Index{}
d := NewDecoder(buf)
err = d.Decode(output)
c.Assert(err, IsNil)
c.Assert(cmp.Equal(idx, output), Equals, true)
c.Assert(output.Entries[0].IntentToAdd, Equals, true)
}
func (s *IndexSuite) TestEncodeWithSkipWorktreeUnsupportedVersion(c *C) {
idx := &Index{
Version: 3,
Entries: []*Entry{{SkipWorktree: true}},
}
buf := bytes.NewBuffer(nil)
e := NewEncoder(buf)
err := e.Encode(idx)
c.Assert(err, IsNil)
output := &Index{}
d := NewDecoder(buf)
err = d.Decode(output)
c.Assert(err, IsNil)
c.Assert(cmp.Equal(idx, output), Equals, true)
c.Assert(output.Entries[0].SkipWorktree, Equals, true)
}
|