提交 caa7ac0e 作者: Juan Batiz-Benet

Merge pull request #495 from jbenet/sharness-add-recursive

sharness test: ipfs add -r
......@@ -6,6 +6,7 @@ import (
"fmt"
"io"
"path"
"sort"
cmds "github.com/jbenet/go-ipfs/commands"
core "github.com/jbenet/go-ipfs/core"
......@@ -82,6 +83,8 @@ remains to be implemented.
return nil, u.ErrCast()
}
sort.Stable(val)
var buf bytes.Buffer
for i, obj := range val.Objects {
if val.Quiet {
......@@ -197,3 +200,16 @@ func addDagnode(output *AddOutput, name string, dn *dag.Node) error {
output.Names = append(output.Names, name)
return nil
}
// Sort interface implementation to sort add output by name
func (a AddOutput) Len() int {
return len(a.Names)
}
func (a AddOutput) Swap(i, j int) {
a.Names[i], a.Names[j] = a.Names[j], a.Names[i]
a.Objects[i], a.Objects[j] = a.Objects[j], a.Objects[i]
}
func (a AddOutput) Less(i, j int) bool {
return a.Names[i] < a.Names[j]
}
......@@ -2,6 +2,7 @@ package merkledag
import (
"fmt"
"sort"
mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
......@@ -30,6 +31,7 @@ func (n *Node) Unmarshal(encoded []byte) error {
}
n.Links[i].Hash = h
}
sort.Stable(LinkSlice(n.Links)) // keep links sorted
n.Data = pbn.GetData()
return nil
......@@ -59,6 +61,8 @@ func (n *Node) Marshal() ([]byte, error) {
func (n *Node) getPBNode() *pb.PBNode {
pbn := &pb.PBNode{}
pbn.Links = make([]*pb.PBLink, len(n.Links))
sort.Stable(LinkSlice(n.Links)) // keep links sorted
for i, l := range n.Links {
pbn.Links[i] = &pb.PBLink{}
pbn.Links[i].Name = &l.Name
......@@ -73,6 +77,7 @@ func (n *Node) getPBNode() *pb.PBNode {
// Encoded returns the encoded raw data version of a Node instance.
// It may use a cached encoded version, unless the force flag is given.
func (n *Node) Encoded(force bool) ([]byte, error) {
sort.Stable(LinkSlice(n.Links)) // keep links sorted
if n.encoded == nil || force {
var err error
n.encoded, err = n.Marshal()
......
......@@ -66,6 +66,12 @@ type Link struct {
Node *Node
}
type LinkSlice []*Link
func (ls LinkSlice) Len() int { return len(ls) }
func (ls LinkSlice) Swap(a, b int) { ls[a], ls[b] = ls[b], ls[a] }
func (ls LinkSlice) Less(a, b int) bool { return ls[a].Name < ls[b].Name }
// MakeLink creates a link to the given node
func MakeLink(n *Node) (*Link, error) {
s, err := n.Size()
......
......@@ -67,6 +67,23 @@ test_expect_success "'ipfs add -q' output looks good" '
test_cmp expected actual
'
test_expect_success "'ipfs add -r' succeeds" '
mkdir mountdir/planets &&
echo "Hello Mars!" >mountdir/planets/mars.txt &&
echo "Hello Venus!" >mountdir/planets/venus.txt &&
ipfs add -r mountdir/planets >actual
'
test_expect_success "'ipfs add -r' output looks good" '
PLANETS="QmWSgS32xQEcXMeqd3YPJLrNBLSdsfYCep2U7CFkyrjXwY" &&
MARS="QmPrrHqJzto9m7SyiRzarwkqPcCSsKR2EB1AyqJfe8L8tN" &&
VENUS="QmU5kp3BH3B8tnWUU2Pikdb2maksBNkb92FHRr56hyghh4" &&
echo "added $PLANETS mountdir/planets" >expected &&
echo "added $MARS mountdir/planets/mars.txt" >>expected &&
echo "added $VENUS mountdir/planets/venus.txt" >>expected &&
test_cmp expected actual
'
test_expect_success "go-random is installed" '
type random
'
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论