Skip to content

Commit c0b150b

Browse files
committed
feat: testutil generator enhancements
1 parent f06958f commit c0b150b

File tree

2 files changed

+61
-9
lines changed

2 files changed

+61
-9
lines changed

testutil/generator.go

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"math/big"
88
"sort"
99
"strings"
10-
"testing"
1110

1211
"github.com/ipfs/go-cid"
1312
"github.com/ipfs/go-unixfsnode/data/builder"
@@ -23,7 +22,7 @@ import (
2322
// GenerateFile generates a random unixfs file of the given size, storing the
2423
// blocks in the provided LinkSystem and returns a DirEntry representation of
2524
// the file.
26-
func GenerateFile(t *testing.T, linkSys *linking.LinkSystem, randReader io.Reader, size int) DirEntry {
25+
func GenerateFile(t require.TestingT, linkSys *linking.LinkSystem, randReader io.Reader, size int) DirEntry {
2726
// a file of `size` random bytes, packaged into unixfs DAGs, stored in the remote blockstore
2827
delimited := io.LimitReader(randReader, int64(size))
2928
var buf bytes.Buffer
@@ -54,15 +53,15 @@ func GenerateFile(t *testing.T, linkSys *linking.LinkSystem, randReader io.Reade
5453
// the root directory will be built as HAMT sharded (with a low "width" to
5554
// maximise the chance of collisions and therefore greater depth for smaller
5655
// number of files).
57-
func GenerateDirectory(t *testing.T, linkSys *linking.LinkSystem, randReader io.Reader, targetSize int, rootSharded bool) DirEntry {
56+
func GenerateDirectory(t require.TestingT, linkSys *linking.LinkSystem, randReader io.Reader, targetSize int, rootSharded bool) DirEntry {
5857
return GenerateDirectoryFrom(t, linkSys, randReader, targetSize, "", rootSharded)
5958
}
6059

6160
// GenerateDirectoryFrom is the same as GenerateDirectory but allows the caller
6261
// to specify a directory path to start from. This is useful for generating
6362
// nested directories.
6463
func GenerateDirectoryFrom(
65-
t *testing.T,
64+
t require.TestingT,
6665
linkSys *linking.LinkSystem,
6766
randReader io.Reader,
6867
targetSize int,
@@ -91,7 +90,8 @@ func GenerateDirectoryFrom(
9190
break
9291
}
9392
}
94-
child := GenerateDirectoryFrom(t, linkSys, randReader, targetSize-curSize, dir+"/"+newDir, false)
93+
sharded := rndInt(randReader, 6) == 0
94+
child := GenerateDirectoryFrom(t, linkSys, randReader, targetSize-curSize, dir+"/"+newDir, sharded)
9595
children = append(children, child)
9696
curSize += int(child.TSize)
9797
default: // 4 in 6 chance of making a new file
@@ -124,12 +124,46 @@ func GenerateDirectoryFrom(
124124
return dirEntry
125125
}
126126

127+
// GenerateDirectoryFor will build a directory with name `dir` whose children
128+
// are generated by the caller using the provided mkchild function. This is
129+
// useful for generating directories with a specific structure where you need
130+
// to have greater control over the children.
131+
func GenerateDirectoryFor(
132+
t require.TestingT,
133+
linkSys *linking.LinkSystem,
134+
randReader io.Reader,
135+
dir string,
136+
sharded bool,
137+
mkchild func(name string) *DirEntry,
138+
) DirEntry {
139+
children := make([]DirEntry, 0)
140+
for {
141+
var name string
142+
for {
143+
var err error
144+
name, err = namegen.RandomDirectoryName(randReader)
145+
require.NoError(t, err)
146+
if !isDupe(children, name) {
147+
break
148+
}
149+
}
150+
child := mkchild(dir + "/" + name)
151+
if child == nil {
152+
break
153+
}
154+
children = append(children, *child)
155+
}
156+
dirEntry := BuildDirectory(t, linkSys, children, sharded)
157+
dirEntry.Path = dir
158+
return dirEntry
159+
}
160+
127161
// BuildDirectory builds a directory from the given children, storing the
128162
// blocks in the provided LinkSystem and returns a DirEntry representation of
129163
// the directory. If sharded is true, the root directory will be built as HAMT
130164
// sharded (with a low "width" to maximise the chance of collisions and
131165
// therefore greater depth for smaller number of files).
132-
func BuildDirectory(t *testing.T, linkSys *linking.LinkSystem, children []DirEntry, sharded bool) DirEntry {
166+
func BuildDirectory(t require.TestingT, linkSys *linking.LinkSystem, children []DirEntry, sharded bool) DirEntry {
133167
// create stable sorted children, which should match the encoded form
134168
// in dag-pb
135169
sort.Slice(children, func(i, j int) bool {
@@ -197,8 +231,16 @@ func cidCollector(ls *ipld.LinkSystem, cids *[]cid.Cid) (ipld.BlockWriteOpener,
197231
}
198232

199233
func isDupe(children []DirEntry, name string) bool {
234+
if strings.Contains(name, ".") {
235+
name = name[:strings.LastIndex(name, ".")]
236+
}
200237
for _, child := range children {
201-
if strings.HasSuffix(child.Path, "/"+name) {
238+
childName := child.Path[strings.LastIndex(child.Path, "/")+1:]
239+
// remove suffix
240+
if strings.Contains(childName, ".") {
241+
childName = childName[:strings.LastIndex(childName, ".")]
242+
}
243+
if childName == name {
202244
return true
203245
}
204246
}

testutil/namegen/namegen.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,21 @@ func RandomFileName(r io.Reader) (string, error) {
3333
if err != nil {
3434
return "", err
3535
}
36-
extIndex, err := getRandomIndex(r, len(extensions))
36+
ext, err := RandomFileExtension(r)
3737
if err != nil {
3838
return "", err
3939
}
40-
return words[wordIndex] + extensions[extIndex], nil
40+
return words[wordIndex] + ext, nil
41+
}
42+
43+
// RandomFileExtension returns a random file extension, including '.'. This may
44+
// also return an empty string.
45+
func RandomFileExtension(r io.Reader) (string, error) {
46+
index, err := getRandomIndex(r, len(extensions))
47+
if err != nil {
48+
return "", err
49+
}
50+
return extensions[index], nil
4151
}
4252

4353
const wordData = `jabberwocky Snark whiffling borogoves mome raths brillig slithy toves outgrabe

0 commit comments

Comments
 (0)