1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-06-30 01:52:26 +08:00

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

sharness test: ipfs add -r
This commit is contained in:
Juan Batiz-Benet
2015-01-06 13:18:13 -08:00
4 changed files with 44 additions and 0 deletions

View File

@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"io" "io"
"path" "path"
"sort"
cmds "github.com/jbenet/go-ipfs/commands" cmds "github.com/jbenet/go-ipfs/commands"
core "github.com/jbenet/go-ipfs/core" core "github.com/jbenet/go-ipfs/core"
@ -82,6 +83,8 @@ remains to be implemented.
return nil, u.ErrCast() return nil, u.ErrCast()
} }
sort.Stable(val)
var buf bytes.Buffer var buf bytes.Buffer
for i, obj := range val.Objects { for i, obj := range val.Objects {
if val.Quiet { if val.Quiet {
@ -197,3 +200,16 @@ func addDagnode(output *AddOutput, name string, dn *dag.Node) error {
output.Names = append(output.Names, name) output.Names = append(output.Names, name)
return nil 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]
}

View File

@ -2,6 +2,7 @@ package merkledag
import ( import (
"fmt" "fmt"
"sort"
mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash" 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 n.Links[i].Hash = h
} }
sort.Stable(LinkSlice(n.Links)) // keep links sorted
n.Data = pbn.GetData() n.Data = pbn.GetData()
return nil return nil
@ -59,6 +61,8 @@ func (n *Node) Marshal() ([]byte, error) {
func (n *Node) getPBNode() *pb.PBNode { func (n *Node) getPBNode() *pb.PBNode {
pbn := &pb.PBNode{} pbn := &pb.PBNode{}
pbn.Links = make([]*pb.PBLink, len(n.Links)) pbn.Links = make([]*pb.PBLink, len(n.Links))
sort.Stable(LinkSlice(n.Links)) // keep links sorted
for i, l := range n.Links { for i, l := range n.Links {
pbn.Links[i] = &pb.PBLink{} pbn.Links[i] = &pb.PBLink{}
pbn.Links[i].Name = &l.Name 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. // Encoded returns the encoded raw data version of a Node instance.
// It may use a cached encoded version, unless the force flag is given. // It may use a cached encoded version, unless the force flag is given.
func (n *Node) Encoded(force bool) ([]byte, error) { func (n *Node) Encoded(force bool) ([]byte, error) {
sort.Stable(LinkSlice(n.Links)) // keep links sorted
if n.encoded == nil || force { if n.encoded == nil || force {
var err error var err error
n.encoded, err = n.Marshal() n.encoded, err = n.Marshal()

View File

@ -66,6 +66,12 @@ type Link struct {
Node *Node 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 // MakeLink creates a link to the given node
func MakeLink(n *Node) (*Link, error) { func MakeLink(n *Node) (*Link, error) {
s, err := n.Size() s, err := n.Size()

View File

@ -67,6 +67,23 @@ test_expect_success "'ipfs add -q' output looks good" '
test_cmp expected actual 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" ' test_expect_success "go-random is installed" '
type random type random
' '