Commit c6f8e7e2 authored by Łukasz Magiera's avatar Łukasz Magiera

coreapi unixfs: test recursive adding

License: MIT
Signed-off-by: default avatarŁukasz Magiera <magik6k@gmail.com>
parent 8dda6957
......@@ -163,7 +163,7 @@ func newUnixfsFile(ctx context.Context, dserv ipld.DAGService, nd ipld.Node, nam
switch dn := nd.(type) {
case *dag.ProtoNode:
fsn, err := ft.FSNodeFromBytes(nd.RawData())
fsn, err := ft.FSNodeFromBytes(dn.Data())
if err != nil {
return nil, err
}
......@@ -172,7 +172,6 @@ func newUnixfsFile(ctx context.Context, dserv ipld.DAGService, nd ipld.Node, nam
}
case *dag.RawNode:
r := ioutil.NopCloser(bytes.NewReader(dn.RawData()))
fi := &sizeInfo{
size: int64(len(dn.RawData())),
......
......@@ -146,6 +146,7 @@ func TestAdd(t *testing.T) {
data func() files.File
path string
err string
recursive bool
opts []options.UnixfsAddOption
}{
// Simple cases
......@@ -231,11 +232,45 @@ func TestAdd(t *testing.T) {
path: hello,
opts: []options.UnixfsAddOption{options.Unixfs.Local(true)},
},
// multi file
{
name: "simpleDir",
data: func() files.File {
return files.NewSliceFile("t", "t", []files.File{
files.NewReaderFile("t/bar", "t/bar", ioutil.NopCloser(strings.NewReader("hello2")), nil),
files.NewReaderFile("t/foo", "t/foo", ioutil.NopCloser(strings.NewReader("hello1")), nil),
})
},
recursive: true,
path: "/ipfs/QmRKGpFfR32FVXdvJiHfo4WJ5TDYBsM1P9raAp1p6APWSp",
},
{
name: "twoLevelDir",
data: func() files.File {
return files.NewSliceFile("t", "t", []files.File{
files.NewSliceFile("t/abc", "t/abc", []files.File{
files.NewReaderFile("t/abc/def", "t/abc/def", ioutil.NopCloser(strings.NewReader("world")), nil),
}),
files.NewReaderFile("t/bar", "t/bar", ioutil.NopCloser(strings.NewReader("hello2")), nil),
files.NewReaderFile("t/foo", "t/foo", ioutil.NopCloser(strings.NewReader("hello1")), nil),
})
},
recursive: true,
path: "/ipfs/QmVG2ZYCkV1S4TK8URA3a4RupBF17A8yAr4FqsRDXVJASr",
},
}
for _, testCase := range cases {
t.Run(testCase.name, func(t *testing.T) {
p, err := api.Unixfs().Add(ctx, testCase.data(), testCase.opts...)
data := testCase.data()
if testCase.recursive {
data = files.NewSliceFile("", "", []files.File{
data,
})
}
p, err := api.Unixfs().Add(ctx, data, testCase.opts...)
if testCase.err != "" {
if err == nil {
t.Fatalf("expected an error: %s", testCase.err)
......@@ -246,27 +281,64 @@ func TestAdd(t *testing.T) {
return
}
if err != nil {
t.Error(err)
t.Fatal(err)
}
if p.String() != testCase.path {
t.Errorf("expected path %s, got: %s", testCase.path, p)
}
/*r, err := api.Unixfs().Cat(ctx, p)
var cmpFile func(orig files.File, got files.File)
cmpFile = func(orig files.File, got files.File) {
if orig.IsDirectory() != got.IsDirectory() {
t.Fatal("file type mismatch")
}
if !orig.IsDirectory() {
defer orig.Close()
defer got.Close()
do, err := ioutil.ReadAll(orig)
if err != nil {
t.Fatal(err)
}
buf := make([]byte, len(testCase.data))
_, err = io.ReadFull(r, buf)
dg, err := ioutil.ReadAll(got)
if err != nil {
t.Error(err)
t.Fatal(err)
}
if string(buf) != testCase.data {
t.Fatalf("expected [%s], got [%s] [err=%s]", helloStr, string(buf), err)
}*/
if !bytes.Equal(do, dg) {
t.Fatal("data not equal")
}
return
}
for {
fo, err := orig.NextFile()
fg, err2 := got.NextFile()
if err != nil {
if err == io.EOF && err2 == io.EOF {
break
}
t.Fatal(err)
}
if err2 != nil {
t.Fatal(err)
}
cmpFile(fo, fg)
}
}
f, err := api.Unixfs().Get(ctx, p)
if err != nil {
t.Fatal(err)
}
cmpFile(testCase.data(), f)
})
}
}
......
......@@ -415,14 +415,14 @@ func (adder *Adder) AddAllAndPin(file files.File) (ipld.Node, error) {
// single files.File f is treated as a directory, affecting hidden file
// semantics.
for {
file, err := file.NextFile()
f, err := file.NextFile()
if err == io.EOF {
// Finished the list of files.
break
} else if err != nil {
return nil, err
}
if err := adder.addFile(file); err != nil {
if err := adder.addFile(f); err != nil {
return nil, err
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment