Skip to content

Commit

Permalink
Merge pull request #32 from ipfs/quickbuilder
Browse files Browse the repository at this point in the history
add helper to approximate test creation patter from ipfs-files
  • Loading branch information
willscott authored May 4, 2022
2 parents 31cb560 + f664db4 commit 650a615
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 0 deletions.
77 changes: 77 additions & 0 deletions data/builder/quick/quick.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Package quickbuilder is designed as a replacement for the existing ipfs-files
// constructor for a simple way to generate synthetic directory trees.
package quickbuilder

import (
"bytes"

"github.com/ipfs/go-unixfsnode/data/builder"
dagpb "github.com/ipld/go-codec-dagpb"
"github.com/ipld/go-ipld-prime"
)

// A Node represents the most basic form of a file or directory
type Node interface {
Size() (int64, error)
Link() ipld.Link
}

type lnkNode struct {
link ipld.Link
size int64
ls *ipld.LinkSystem
}

func (ln *lnkNode) Size() (int64, error) {
return ln.size, nil
}

func (ln *lnkNode) Link() ipld.Link {
return ln.link
}

// Builder provides the linksystem context for saving files & directories
type Builder struct {
ls *ipld.LinkSystem
}

// NewMapDirectory creates a unixfs directory from a list of named entries
func (b *Builder) NewMapDirectory(entries map[string]Node) Node {
lnks := make([]dagpb.PBLink, 0, len(entries))
for name, e := range entries {
sz, _ := e.Size()
entry, err := builder.BuildUnixFSDirectoryEntry(name, sz, e.Link())
if err != nil {
return nil
}
lnks = append(lnks, entry)
}
n, size, err := builder.BuildUnixFSDirectory(lnks, b.ls)
if err != nil {
panic(err)
}
return &lnkNode{
n,
int64(size),
b.ls,
}
}

// NewBytesFile creates a unixfs file from byte contents
func (b *Builder) NewBytesFile(data []byte) Node {
n, size, err := builder.BuildUnixFSFile(bytes.NewReader(data), "", b.ls)
if err != nil {
panic(err)
}
return &lnkNode{
n,
int64(size),
b.ls,
}
}

// Store provides a builder context for making unixfs files and directories
func Store(ls *ipld.LinkSystem, cb func(b *Builder) error) error {
b := Builder{ls}
return cb(&b)
}
35 changes: 35 additions & 0 deletions data/builder/quick/quick_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package quickbuilder_test

import (
"testing"

quickbuilder "github.com/ipfs/go-unixfsnode/data/builder/quick"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
"github.com/ipld/go-ipld-prime/storage/memstore"
)

func TestQuickBuilder(t *testing.T) {
ls := cidlink.DefaultLinkSystem()
store := memstore.Store{Bag: make(map[string][]byte)}
ls.SetReadStorage(&store)
ls.SetWriteStorage(&store)
err := quickbuilder.Store(&ls, func(b *quickbuilder.Builder) error {
b.NewMapDirectory(map[string]quickbuilder.Node{
"file.txt": b.NewBytesFile([]byte("1")),
"foo? #<'": b.NewMapDirectory(map[string]quickbuilder.Node{
"file.txt": b.NewBytesFile([]byte("2")),
"bar": b.NewMapDirectory(map[string]quickbuilder.Node{
"file.txt": b.NewBytesFile([]byte("3")),
}),
}),
})
return nil
})
if err != nil {
t.Fatal(err)
}

if len(store.Bag) != 6 {
t.Fatal("unexpected number of stored nodes")
}
}

0 comments on commit 650a615

Please sign in to comment.