Unverified Commit af1aa24c authored by Łukasz Magiera's avatar Łukasz Magiera Committed by GitHub

Merge pull request #10 from ipfs/feat/add-wrap-fixes

Unixfs.Wrap Fixes
parents 55336c50 e12c21af
Pipeline #37 failed with stages
in 0 seconds
......@@ -33,9 +33,7 @@ type UnixfsAddSettings struct {
FsCache bool
NoCopy bool
Wrap bool
Hidden bool
StdinName string
Wrap bool
Events chan<- interface{}
Silent bool
......@@ -67,9 +65,7 @@ func UnixfsAddOptions(opts ...UnixfsAddOption) (*UnixfsAddSettings, cid.Prefix,
FsCache: false,
NoCopy: false,
Wrap: false,
Hidden: false,
StdinName: "",
Wrap: false,
Events: nil,
Silent: false,
......@@ -247,23 +243,6 @@ func (unixfsOpts) Wrap(wrap bool) UnixfsAddOption {
}
}
// Hidden enables adding of hidden files (files prefixed with '.')
func (unixfsOpts) Hidden(hidden bool) UnixfsAddOption {
return func(settings *UnixfsAddSettings) error {
settings.Hidden = hidden
return nil
}
}
// StdinName is the name set for files which don specify FilePath as
// os.Stdin.Name()
func (unixfsOpts) StdinName(name string) UnixfsAddOption {
return func(settings *UnixfsAddSettings) error {
settings.StdinName = name
return nil
}
}
// Events specifies channel which will be used to report events about ongoing
// Add operation.
//
......
......@@ -82,11 +82,14 @@ func flatDir() files.Node {
})
}
func wrapped(name string) func(f files.Node) files.Node {
func wrapped(names ...string) func(f files.Node) files.Node {
return func(f files.Node) files.Node {
return files.NewMapDirectory(map[string]files.Node{
name: f,
})
for i := range names {
f = files.NewMapDirectory(map[string]files.Node{
names[len(names)-i-1]: f,
})
}
return f
}
}
......@@ -241,16 +244,30 @@ func (tp *provider) TestAdd(t *testing.T) {
},
// multi file
{
name: "simpleDir",
name: "simpleDirNoWrap",
data: flatDir,
wrap: "t",
path: "/ipfs/QmRKGpFfR32FVXdvJiHfo4WJ5TDYBsM1P9raAp1p6APWSp",
},
{
name: "twoLevelDir",
data: twoLevelDir(),
wrap: "t",
path: "/ipfs/QmVG2ZYCkV1S4TK8URA3a4RupBF17A8yAr4FqsRDXVJASr",
name: "simpleDirWrap",
data: flatDir,
expect: wrapped("QmRKGpFfR32FVXdvJiHfo4WJ5TDYBsM1P9raAp1p6APWSp"),
path: "/ipfs/QmXxCaQkC8Z6Qws1nTkTQfCsL9y4XvWXnrPokp9bhmjC1L",
opts: []options.UnixfsAddOption{options.Unixfs.Wrap(true)},
},
{
name: "simpleDir",
data: flatDir,
wrap: "t",
expect: wrapped("t"),
path: "/ipfs/Qmc3nGXm1HtUVCmnXLQHvWcNwfdZGpfg2SRm1CxLf7Q2Rm",
},
{
name: "twoLevelDir",
data: twoLevelDir(),
wrap: "t",
expect: wrapped("t"),
path: "/ipfs/QmPwsL3T5sWhDmmAWZHAzyjKtMVDS9a11aHNRqb3xoVnmg",
},
// wrapped
{
......@@ -261,15 +278,6 @@ func (tp *provider) TestAdd(t *testing.T) {
},
wrap: "foo",
expect: wrapped("foo"),
opts: []options.UnixfsAddOption{options.Unixfs.Wrap(true)},
},
{
name: "addNotWrappedDirFile",
path: hello,
data: func() files.Node {
return files.NewBytesFile([]byte(helloStr))
},
wrap: "foo",
},
{
name: "stdinWrapped",
......@@ -284,43 +292,25 @@ func (tp *provider) TestAdd(t *testing.T) {
},
opts: []options.UnixfsAddOption{options.Unixfs.Wrap(true)},
},
{
name: "stdinNamed",
path: "/ipfs/QmQ6cGBmb3ZbdrQW1MRm1RJnYnaxCqfssz7CrTa9NEhQyS",
data: func() files.Node {
rf, err := files.NewReaderPathFile(os.Stdin.Name(), ioutil.NopCloser(strings.NewReader(helloStr)), nil)
if err != nil {
panic(err)
}
return rf
},
expect: func(files.Node) files.Node {
return files.NewMapDirectory(map[string]files.Node{
"test": files.NewBytesFile([]byte(helloStr)),
})
},
opts: []options.UnixfsAddOption{options.Unixfs.Wrap(true), options.Unixfs.StdinName("test")},
},
{
name: "twoLevelDirWrapped",
data: twoLevelDir(),
wrap: "t",
expect: wrapped("t"),
path: "/ipfs/QmPwsL3T5sWhDmmAWZHAzyjKtMVDS9a11aHNRqb3xoVnmg",
expect: wrapped("QmPwsL3T5sWhDmmAWZHAzyjKtMVDS9a11aHNRqb3xoVnmg", "t"),
path: "/ipfs/QmXzZwAh34pmNjuKsVGZfpbByis5S5qeZjCCUxa1ajZqzH",
opts: []options.UnixfsAddOption{options.Unixfs.Wrap(true)},
},
{
name: "twoLevelInlineHash",
data: twoLevelDir(),
wrap: "t",
expect: wrapped("t"),
path: "/ipfs/zBunoruKoyCHKkALNSWxDvj4L7yuQnMgQ4hUa9j1Z64tVcDEcu6Zdetyu7eeFCxMPfxb7YJvHeFHoFoHMkBUQf6vfdhmi",
expect: wrapped("zBunoruKoyCHKkALNSWxDvj4L7yuQnMgQ4hUa9j1Z64tVcDEcu6Zdetyu7eeFCxMPfxb7YJvHeFHoFoHMkBUQf6vfdhmi", "t"),
path: "/ipfs/QmUX6GykDGHTMtLmDkfjqs48QwQK82vou51xwaY9TSU7Zo",
opts: []options.UnixfsAddOption{options.Unixfs.Wrap(true), options.Unixfs.Inline(true), options.Unixfs.RawLeaves(true), options.Unixfs.Hash(mh.SHA3)},
},
// hidden
{
name: "hiddenFiles",
name: "hiddenFilesAdded",
data: func() files.Node {
return files.NewMapDirectory(map[string]files.Node{
".bar": files.NewBytesFile([]byte("hello2")),
......@@ -328,33 +318,9 @@ func (tp *provider) TestAdd(t *testing.T) {
"foo": files.NewBytesFile([]byte("hello1")),
})
},
wrap: "t",
path: "/ipfs/QmehGvpf2hY196MzDFmjL8Wy27S4jbgGDUAhBJyvXAwr3g",
opts: []options.UnixfsAddOption{options.Unixfs.Hidden(true)},
},
{
name: "hiddenFileAlwaysAdded",
data: func() files.Node {
return files.NewBytesFile([]byte(helloStr))
},
wrap: ".foo",
path: hello,
},
{
name: "hiddenFilesNotAdded",
data: func() files.Node {
return files.NewMapDirectory(map[string]files.Node{
".bar": files.NewBytesFile([]byte("hello2")),
"bar": files.NewBytesFile([]byte("hello2")),
"foo": files.NewBytesFile([]byte("hello1")),
})
},
expect: func(files.Node) files.Node {
return flatDir()
},
wrap: "t",
path: "/ipfs/QmRKGpFfR32FVXdvJiHfo4WJ5TDYBsM1P9raAp1p6APWSp",
opts: []options.UnixfsAddOption{options.Unixfs.Hidden(false)},
wrap: "t",
expect: wrapped("t"),
path: "/ipfs/QmPXLSBX382vJDLrGakcbrZDkU3grfkjMox7EgSC9KFbtQ",
},
// NoCopy
{
......@@ -392,10 +358,9 @@ func (tp *provider) TestAdd(t *testing.T) {
data: twoLevelDir(),
path: "/ipfs/QmVG2ZYCkV1S4TK8URA3a4RupBF17A8yAr4FqsRDXVJASr",
events: []coreiface.AddEvent{
{Name: "t/abc", Path: p("QmU7nuGs2djqK99UNsNgEPGh6GV4662p6WtsgccBNGTDxt"), Size: "62"},
{Name: "t", Path: p("QmVG2ZYCkV1S4TK8URA3a4RupBF17A8yAr4FqsRDXVJASr"), Size: "229"},
{Name: "abc", Path: p("QmU7nuGs2djqK99UNsNgEPGh6GV4662p6WtsgccBNGTDxt"), Size: "62"},
{Name: "", Path: p("QmVG2ZYCkV1S4TK8URA3a4RupBF17A8yAr4FqsRDXVJASr"), Size: "229"},
},
wrap: "t",
opts: []options.UnixfsAddOption{options.Unixfs.Silent(true)},
},
{
......@@ -403,13 +368,12 @@ func (tp *provider) TestAdd(t *testing.T) {
data: twoLevelDir(),
path: "/ipfs/QmVG2ZYCkV1S4TK8URA3a4RupBF17A8yAr4FqsRDXVJASr",
events: []coreiface.AddEvent{
{Name: "t/abc/def", Path: p("QmNyJpQkU1cEkBwMDhDNFstr42q55mqG5GE5Mgwug4xyGk"), Size: "13"},
{Name: "t/bar", Path: p("QmS21GuXiRMvJKHos4ZkEmQDmRBqRaF5tQS2CQCu2ne9sY"), Size: "14"},
{Name: "t/foo", Path: p("QmfAjGiVpTN56TXi6SBQtstit5BEw3sijKj1Qkxn6EXKzJ"), Size: "14"},
{Name: "t/abc", Path: p("QmU7nuGs2djqK99UNsNgEPGh6GV4662p6WtsgccBNGTDxt"), Size: "62"},
{Name: "t", Path: p("QmVG2ZYCkV1S4TK8URA3a4RupBF17A8yAr4FqsRDXVJASr"), Size: "229"},
{Name: "abc/def", Path: p("QmNyJpQkU1cEkBwMDhDNFstr42q55mqG5GE5Mgwug4xyGk"), Size: "13"},
{Name: "bar", Path: p("QmS21GuXiRMvJKHos4ZkEmQDmRBqRaF5tQS2CQCu2ne9sY"), Size: "14"},
{Name: "foo", Path: p("QmfAjGiVpTN56TXi6SBQtstit5BEw3sijKj1Qkxn6EXKzJ"), Size: "14"},
{Name: "abc", Path: p("QmU7nuGs2djqK99UNsNgEPGh6GV4662p6WtsgccBNGTDxt"), Size: "62"},
{Name: "", Path: p("QmVG2ZYCkV1S4TK8URA3a4RupBF17A8yAr4FqsRDXVJASr"), Size: "229"},
},
wrap: "t",
},
{
name: "progress1M",
......@@ -528,14 +492,14 @@ func (tp *provider) TestAdd(t *testing.T) {
_, origDir := orig.(files.Directory)
_, gotDir := got.(files.Directory)
if origDir != gotDir {
t.Fatal("file type mismatch")
}
if origName != gotName {
t.Errorf("file name mismatch, orig='%s', got='%s'", origName, gotName)
}
if origDir != gotDir {
t.Fatalf("file type mismatch on %s", origName)
}
if !gotDir {
defer orig.Close()
defer got.Close()
......@@ -742,10 +706,8 @@ func (tp *provider) TestLs(t *testing.T) {
r := strings.NewReader("content-of-file")
p, err := api.Unixfs().Add(ctx, files.NewMapDirectory(map[string]files.Node{
"0": files.NewMapDirectory(map[string]files.Node{
"name-of-file": files.NewReaderFile(r),
"name-of-symlink": files.NewLinkFile("/foo/bar", nil),
}),
"name-of-file": files.NewReaderFile(r),
"name-of-symlink": files.NewLinkFile("/foo/bar", nil),
}))
if err != nil {
t.Fatal(err)
......@@ -804,9 +766,7 @@ func (tp *provider) TestEntriesExpired(t *testing.T) {
r := strings.NewReader("content-of-file")
p, err := api.Unixfs().Add(ctx, files.NewMapDirectory(map[string]files.Node{
"0": files.NewMapDirectory(map[string]files.Node{
"name-of-file": files.NewReaderFile(r),
}),
"name-of-file": files.NewReaderFile(r),
}))
if err != nil {
t.Error(err)
......@@ -846,7 +806,7 @@ func (tp *provider) TestLsEmptyDir(t *testing.T) {
t.Error(err)
}
_, err = api.Unixfs().Add(ctx, files.NewMapDirectory(map[string]files.Node{"0": files.NewSliceDirectory([]files.DirEntry{})}))
_, err = api.Unixfs().Add(ctx, files.NewSliceDirectory([]files.DirEntry{}))
if err != nil {
t.Error(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