1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-09-15 11:13:37 +08:00
Files
kubo/commands/http/multifilereader_test.go
Juan Batiz-Benet 978c9fa16f cmds/add: use dagutils.Editor, like patch
This changes the pin behavior. It uses the filenames given through
the api, and allows files to be streamed faltly (not a hierarchy),
which is easier for other things (like vinyl in node-ipfs-api land).
Files can also be entirely out of order, and the garbage intermediate
directories will not be pinned (gc-ed later).

The changes also mean the output of add has changed slightly-- it
no longer shows the local path added, but rather the dag path
relative to the added roots. This is a small difference, but changes
tests.

The dagutils.Editor creates a lot of chaff (intermediate objects)
along the way. Wonder how we might minimize the writes to the
datastore...

This commit also removes the "NilRepo()" part of the --only-hash
mode. We need to store at least in an in-mem repo/datastore because
otherwise the dagutils.Editor breaks.

License: MIT
Signed-off-by: Juan Batiz-Benet <juan@benet.ai>
2015-08-12 08:24:06 +02:00

107 lines
2.9 KiB
Go

package http
import (
"io"
"io/ioutil"
"mime/multipart"
"strings"
"testing"
files "github.com/ipfs/go-ipfs/commands/files"
)
func TestOutput(t *testing.T) {
text := "Some text! :)"
fileset := []files.File{
files.NewReaderFile("file.txt", "file.txt", ioutil.NopCloser(strings.NewReader(text)), nil),
files.NewSliceFile("boop", "boop", []files.File{
files.NewReaderFile("boop/a.txt", "boop/a.txt", ioutil.NopCloser(strings.NewReader("bleep")), nil),
files.NewReaderFile("boop/b.txt", "boop/b.txt", ioutil.NopCloser(strings.NewReader("bloop")), nil),
}),
files.NewReaderFile("beep.txt", "beep.txt", ioutil.NopCloser(strings.NewReader("beep")), nil),
}
sf := files.NewSliceFile("", "", fileset)
buf := make([]byte, 20)
// testing output by reading it with the go stdlib "mime/multipart" Reader
mfr := NewMultiFileReader(sf, true)
mpReader := multipart.NewReader(mfr, mfr.Boundary())
part, err := mpReader.NextPart()
if part == nil || err != nil {
t.Error("Expected non-nil part, nil error")
}
mpf, err := files.NewFileFromPart(part)
if mpf == nil || err != nil {
t.Error("Expected non-nil MultipartFile, nil error")
}
if mpf.IsDirectory() {
t.Error("Expected file to not be a directory")
}
if mpf.FileName() != "file.txt" {
t.Error("Expected filename to be \"file.txt\"")
}
if n, err := mpf.Read(buf); n != len(text) || err != nil {
t.Error("Expected to read from file", n, err)
}
if string(buf[:len(text)]) != text {
t.Error("Data read was different than expected")
}
part, err = mpReader.NextPart()
if part == nil || err != nil {
t.Error("Expected non-nil part, nil error")
}
mpf, err = files.NewFileFromPart(part)
if mpf == nil || err != nil {
t.Error("Expected non-nil MultipartFile, nil error")
}
if !mpf.IsDirectory() {
t.Error("Expected file to be a directory")
}
if mpf.FileName() != "boop" {
t.Error("Expected filename to be \"boop\"")
}
child, err := mpf.NextFile()
if child == nil || err != nil {
t.Error("Expected to be able to read a child file")
}
if child.IsDirectory() {
t.Error("Expected file to not be a directory")
}
if child.FileName() != "boop/a.txt" {
t.Error("Expected filename to be \"some/file/path\"")
}
child, err = mpf.NextFile()
if child == nil || err != nil {
t.Error("Expected to be able to read a child file")
}
if child.IsDirectory() {
t.Error("Expected file to not be a directory")
}
if child.FileName() != "boop/b.txt" {
t.Error("Expected filename to be \"some/file/path\"")
}
child, err = mpf.NextFile()
if child != nil || err != io.EOF {
t.Error("Expected to get (nil, io.EOF)")
}
part, err = mpReader.NextPart()
if part == nil || err != nil {
t.Error("Expected non-nil part, nil error")
}
mpf, err = files.NewFileFromPart(part)
if mpf == nil || err != nil {
t.Error("Expected non-nil MultipartFile, nil error")
}
part, err = mpReader.NextPart()
if part != nil || err != io.EOF {
t.Error("Expected to get (nil, io.EOF)")
}
}