| 1 |
package fsnoder |
| 2 |
|
| 3 |
import ( |
| 4 |
"testing" |
| 5 |
|
| 6 |
"github.com/go-git/go-git/v5/utils/merkletrie/noder" |
| 7 |
|
| 8 |
. "gopkg.in/check.v1" |
| 9 |
) |
| 10 |
|
| 11 |
func Test(t *testing.T) { TestingT(t) } |
| 12 |
|
| 13 |
type FileSuite struct{} |
| 14 |
|
| 15 |
var _ = Suite(&FileSuite{}) |
| 16 |
|
| 17 |
var ( |
| 18 |
HashOfEmptyFile = []byte{0xcb, 0xf2, 0x9c, 0xe4, 0x84, 0x22, 0x23, 0x25} // fnv64 basis offset |
| 19 |
HashOfContents = []byte{0xee, 0x7e, 0xf3, 0xd0, 0xc2, 0xb5, 0xef, 0x83} // hash of "contents" |
| 20 |
) |
| 21 |
|
| 22 |
func (s *FileSuite) TestNewFileEmpty(c *C) { |
| 23 |
f, err := newFile("name", "") |
| 24 |
c.Assert(err, IsNil) |
| 25 |
|
| 26 |
c.Assert(f.Hash(), DeepEquals, HashOfEmptyFile) |
| 27 |
c.Assert(f.Name(), Equals, "name") |
| 28 |
c.Assert(f.IsDir(), Equals, false) |
| 29 |
assertChildren(c, f, noder.NoChildren) |
| 30 |
c.Assert(f.String(), Equals, "name<>") |
| 31 |
} |
| 32 |
|
| 33 |
func (s *FileSuite) TestNewFileWithContents(c *C) { |
| 34 |
f, err := newFile("name", "contents") |
| 35 |
c.Assert(err, IsNil) |
| 36 |
|
| 37 |
c.Assert(f.Hash(), DeepEquals, HashOfContents) |
| 38 |
c.Assert(f.Name(), Equals, "name") |
| 39 |
c.Assert(f.IsDir(), Equals, false) |
| 40 |
assertChildren(c, f, noder.NoChildren) |
| 41 |
c.Assert(f.String(), Equals, "name<contents>") |
| 42 |
} |
| 43 |
|
| 44 |
func (s *FileSuite) TestNewfileErrorEmptyName(c *C) { |
| 45 |
_, err := newFile("", "contents") |
| 46 |
c.Assert(err, Not(IsNil)) |
| 47 |
} |
| 48 |
|
| 49 |
func (s *FileSuite) TestDifferentContentsHaveDifferentHash(c *C) { |
| 50 |
f1, err := newFile("name", "contents") |
| 51 |
c.Assert(err, IsNil) |
| 52 |
|
| 53 |
f2, err := newFile("name", "foo") |
| 54 |
c.Assert(err, IsNil) |
| 55 |
|
| 56 |
c.Assert(f1.Hash(), Not(DeepEquals), f2.Hash()) |
| 57 |
} |
| 58 |
|
| 59 |
func (s *FileSuite) TestSameContentsHaveSameHash(c *C) { |
| 60 |
f1, err := newFile("name1", "contents") |
| 61 |
c.Assert(err, IsNil) |
| 62 |
|
| 63 |
f2, err := newFile("name2", "contents") |
| 64 |
c.Assert(err, IsNil) |
| 65 |
|
| 66 |
c.Assert(f1.Hash(), DeepEquals, f2.Hash()) |
| 67 |
} |