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
...@@ -34,8 +34,6 @@ type UnixfsAddSettings struct { ...@@ -34,8 +34,6 @@ type UnixfsAddSettings struct {
NoCopy bool NoCopy bool
Wrap bool Wrap bool
Hidden bool
StdinName string
Events chan<- interface{} Events chan<- interface{}
Silent bool Silent bool
...@@ -68,8 +66,6 @@ func UnixfsAddOptions(opts ...UnixfsAddOption) (*UnixfsAddSettings, cid.Prefix, ...@@ -68,8 +66,6 @@ func UnixfsAddOptions(opts ...UnixfsAddOption) (*UnixfsAddSettings, cid.Prefix,
NoCopy: false, NoCopy: false,
Wrap: false, Wrap: false,
Hidden: false,
StdinName: "",
Events: nil, Events: nil,
Silent: false, Silent: false,
...@@ -247,23 +243,6 @@ func (unixfsOpts) Wrap(wrap bool) UnixfsAddOption { ...@@ -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 // Events specifies channel which will be used to report events about ongoing
// Add operation. // Add operation.
// //
......
...@@ -82,12 +82,15 @@ func flatDir() files.Node { ...@@ -82,12 +82,15 @@ 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 func(f files.Node) files.Node {
return files.NewMapDirectory(map[string]files.Node{ for i := range names {
name: f, f = files.NewMapDirectory(map[string]files.Node{
names[len(names)-i-1]: f,
}) })
} }
return f
}
} }
func (tp *provider) TestAdd(t *testing.T) { func (tp *provider) TestAdd(t *testing.T) {
...@@ -240,17 +243,31 @@ func (tp *provider) TestAdd(t *testing.T) { ...@@ -240,17 +243,31 @@ func (tp *provider) TestAdd(t *testing.T) {
opts: []options.UnixfsAddOption{options.Unixfs.HashOnly(true)}, opts: []options.UnixfsAddOption{options.Unixfs.HashOnly(true)},
}, },
// multi file // multi file
{
name: "simpleDirNoWrap",
data: flatDir,
path: "/ipfs/QmRKGpFfR32FVXdvJiHfo4WJ5TDYBsM1P9raAp1p6APWSp",
},
{
name: "simpleDirWrap",
data: flatDir,
expect: wrapped("QmRKGpFfR32FVXdvJiHfo4WJ5TDYBsM1P9raAp1p6APWSp"),
path: "/ipfs/QmXxCaQkC8Z6Qws1nTkTQfCsL9y4XvWXnrPokp9bhmjC1L",
opts: []options.UnixfsAddOption{options.Unixfs.Wrap(true)},
},
{ {
name: "simpleDir", name: "simpleDir",
data: flatDir, data: flatDir,
wrap: "t", wrap: "t",
path: "/ipfs/QmRKGpFfR32FVXdvJiHfo4WJ5TDYBsM1P9raAp1p6APWSp", expect: wrapped("t"),
path: "/ipfs/Qmc3nGXm1HtUVCmnXLQHvWcNwfdZGpfg2SRm1CxLf7Q2Rm",
}, },
{ {
name: "twoLevelDir", name: "twoLevelDir",
data: twoLevelDir(), data: twoLevelDir(),
wrap: "t", wrap: "t",
path: "/ipfs/QmVG2ZYCkV1S4TK8URA3a4RupBF17A8yAr4FqsRDXVJASr", expect: wrapped("t"),
path: "/ipfs/QmPwsL3T5sWhDmmAWZHAzyjKtMVDS9a11aHNRqb3xoVnmg",
}, },
// wrapped // wrapped
{ {
...@@ -261,15 +278,6 @@ func (tp *provider) TestAdd(t *testing.T) { ...@@ -261,15 +278,6 @@ func (tp *provider) TestAdd(t *testing.T) {
}, },
wrap: "foo", wrap: "foo",
expect: wrapped("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", name: "stdinWrapped",
...@@ -284,43 +292,25 @@ func (tp *provider) TestAdd(t *testing.T) { ...@@ -284,43 +292,25 @@ func (tp *provider) TestAdd(t *testing.T) {
}, },
opts: []options.UnixfsAddOption{options.Unixfs.Wrap(true)}, 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", name: "twoLevelDirWrapped",
data: twoLevelDir(), data: twoLevelDir(),
wrap: "t", wrap: "t",
expect: wrapped("t"), expect: wrapped("QmPwsL3T5sWhDmmAWZHAzyjKtMVDS9a11aHNRqb3xoVnmg", "t"),
path: "/ipfs/QmPwsL3T5sWhDmmAWZHAzyjKtMVDS9a11aHNRqb3xoVnmg", path: "/ipfs/QmXzZwAh34pmNjuKsVGZfpbByis5S5qeZjCCUxa1ajZqzH",
opts: []options.UnixfsAddOption{options.Unixfs.Wrap(true)}, opts: []options.UnixfsAddOption{options.Unixfs.Wrap(true)},
}, },
{ {
name: "twoLevelInlineHash", name: "twoLevelInlineHash",
data: twoLevelDir(), data: twoLevelDir(),
wrap: "t", wrap: "t",
expect: wrapped("t"), expect: wrapped("zBunoruKoyCHKkALNSWxDvj4L7yuQnMgQ4hUa9j1Z64tVcDEcu6Zdetyu7eeFCxMPfxb7YJvHeFHoFoHMkBUQf6vfdhmi", "t"),
path: "/ipfs/zBunoruKoyCHKkALNSWxDvj4L7yuQnMgQ4hUa9j1Z64tVcDEcu6Zdetyu7eeFCxMPfxb7YJvHeFHoFoHMkBUQf6vfdhmi", path: "/ipfs/QmUX6GykDGHTMtLmDkfjqs48QwQK82vou51xwaY9TSU7Zo",
opts: []options.UnixfsAddOption{options.Unixfs.Wrap(true), options.Unixfs.Inline(true), options.Unixfs.RawLeaves(true), options.Unixfs.Hash(mh.SHA3)}, opts: []options.UnixfsAddOption{options.Unixfs.Wrap(true), options.Unixfs.Inline(true), options.Unixfs.RawLeaves(true), options.Unixfs.Hash(mh.SHA3)},
}, },
// hidden // hidden
{ {
name: "hiddenFiles", name: "hiddenFilesAdded",
data: func() files.Node { data: func() files.Node {
return files.NewMapDirectory(map[string]files.Node{ return files.NewMapDirectory(map[string]files.Node{
".bar": files.NewBytesFile([]byte("hello2")), ".bar": files.NewBytesFile([]byte("hello2")),
...@@ -329,32 +319,8 @@ func (tp *provider) TestAdd(t *testing.T) { ...@@ -329,32 +319,8 @@ func (tp *provider) TestAdd(t *testing.T) {
}) })
}, },
wrap: "t", wrap: "t",
path: "/ipfs/QmehGvpf2hY196MzDFmjL8Wy27S4jbgGDUAhBJyvXAwr3g", expect: wrapped("t"),
opts: []options.UnixfsAddOption{options.Unixfs.Hidden(true)}, path: "/ipfs/QmPXLSBX382vJDLrGakcbrZDkU3grfkjMox7EgSC9KFbtQ",
},
{
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)},
}, },
// NoCopy // NoCopy
{ {
...@@ -392,10 +358,9 @@ func (tp *provider) TestAdd(t *testing.T) { ...@@ -392,10 +358,9 @@ func (tp *provider) TestAdd(t *testing.T) {
data: twoLevelDir(), data: twoLevelDir(),
path: "/ipfs/QmVG2ZYCkV1S4TK8URA3a4RupBF17A8yAr4FqsRDXVJASr", path: "/ipfs/QmVG2ZYCkV1S4TK8URA3a4RupBF17A8yAr4FqsRDXVJASr",
events: []coreiface.AddEvent{ events: []coreiface.AddEvent{
{Name: "t/abc", Path: p("QmU7nuGs2djqK99UNsNgEPGh6GV4662p6WtsgccBNGTDxt"), Size: "62"}, {Name: "abc", Path: p("QmU7nuGs2djqK99UNsNgEPGh6GV4662p6WtsgccBNGTDxt"), Size: "62"},
{Name: "t", Path: p("QmVG2ZYCkV1S4TK8URA3a4RupBF17A8yAr4FqsRDXVJASr"), Size: "229"}, {Name: "", Path: p("QmVG2ZYCkV1S4TK8URA3a4RupBF17A8yAr4FqsRDXVJASr"), Size: "229"},
}, },
wrap: "t",
opts: []options.UnixfsAddOption{options.Unixfs.Silent(true)}, opts: []options.UnixfsAddOption{options.Unixfs.Silent(true)},
}, },
{ {
...@@ -403,13 +368,12 @@ func (tp *provider) TestAdd(t *testing.T) { ...@@ -403,13 +368,12 @@ func (tp *provider) TestAdd(t *testing.T) {
data: twoLevelDir(), data: twoLevelDir(),
path: "/ipfs/QmVG2ZYCkV1S4TK8URA3a4RupBF17A8yAr4FqsRDXVJASr", path: "/ipfs/QmVG2ZYCkV1S4TK8URA3a4RupBF17A8yAr4FqsRDXVJASr",
events: []coreiface.AddEvent{ events: []coreiface.AddEvent{
{Name: "t/abc/def", Path: p("QmNyJpQkU1cEkBwMDhDNFstr42q55mqG5GE5Mgwug4xyGk"), Size: "13"}, {Name: "abc/def", Path: p("QmNyJpQkU1cEkBwMDhDNFstr42q55mqG5GE5Mgwug4xyGk"), Size: "13"},
{Name: "t/bar", Path: p("QmS21GuXiRMvJKHos4ZkEmQDmRBqRaF5tQS2CQCu2ne9sY"), Size: "14"}, {Name: "bar", Path: p("QmS21GuXiRMvJKHos4ZkEmQDmRBqRaF5tQS2CQCu2ne9sY"), Size: "14"},
{Name: "t/foo", Path: p("QmfAjGiVpTN56TXi6SBQtstit5BEw3sijKj1Qkxn6EXKzJ"), Size: "14"}, {Name: "foo", Path: p("QmfAjGiVpTN56TXi6SBQtstit5BEw3sijKj1Qkxn6EXKzJ"), Size: "14"},
{Name: "t/abc", Path: p("QmU7nuGs2djqK99UNsNgEPGh6GV4662p6WtsgccBNGTDxt"), Size: "62"}, {Name: "abc", Path: p("QmU7nuGs2djqK99UNsNgEPGh6GV4662p6WtsgccBNGTDxt"), Size: "62"},
{Name: "t", Path: p("QmVG2ZYCkV1S4TK8URA3a4RupBF17A8yAr4FqsRDXVJASr"), Size: "229"}, {Name: "", Path: p("QmVG2ZYCkV1S4TK8URA3a4RupBF17A8yAr4FqsRDXVJASr"), Size: "229"},
}, },
wrap: "t",
}, },
{ {
name: "progress1M", name: "progress1M",
...@@ -528,14 +492,14 @@ func (tp *provider) TestAdd(t *testing.T) { ...@@ -528,14 +492,14 @@ func (tp *provider) TestAdd(t *testing.T) {
_, origDir := orig.(files.Directory) _, origDir := orig.(files.Directory)
_, gotDir := got.(files.Directory) _, gotDir := got.(files.Directory)
if origDir != gotDir {
t.Fatal("file type mismatch")
}
if origName != gotName { if origName != gotName {
t.Errorf("file name mismatch, orig='%s', got='%s'", 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 { if !gotDir {
defer orig.Close() defer orig.Close()
defer got.Close() defer got.Close()
...@@ -742,10 +706,8 @@ func (tp *provider) TestLs(t *testing.T) { ...@@ -742,10 +706,8 @@ func (tp *provider) TestLs(t *testing.T) {
r := strings.NewReader("content-of-file") r := strings.NewReader("content-of-file")
p, err := api.Unixfs().Add(ctx, files.NewMapDirectory(map[string]files.Node{ 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),
"name-of-symlink": files.NewLinkFile("/foo/bar", nil), "name-of-symlink": files.NewLinkFile("/foo/bar", nil),
}),
})) }))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
...@@ -804,9 +766,7 @@ func (tp *provider) TestEntriesExpired(t *testing.T) { ...@@ -804,9 +766,7 @@ func (tp *provider) TestEntriesExpired(t *testing.T) {
r := strings.NewReader("content-of-file") r := strings.NewReader("content-of-file")
p, err := api.Unixfs().Add(ctx, files.NewMapDirectory(map[string]files.Node{ 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 { if err != nil {
t.Error(err) t.Error(err)
...@@ -846,7 +806,7 @@ func (tp *provider) TestLsEmptyDir(t *testing.T) { ...@@ -846,7 +806,7 @@ func (tp *provider) TestLsEmptyDir(t *testing.T) {
t.Error(err) 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 { if err != nil {
t.Error(err) 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