Skip to content

Commit

Permalink
Add -cat flag to create large files
Browse files Browse the repository at this point in the history
  • Loading branch information
arp242 committed Oct 1, 2023
1 parent c674626 commit 42c577f
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
65 changes: 65 additions & 0 deletions cmd/toml-test/main.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package main

import (
"bytes"
"encoding/json"
"fmt"
"io/fs"
"os"
"path/filepath"
"sort"
"strconv"
"strings"

"github.com/BurntSushi/toml"
tomltest "github.com/toml-lang/toml-test"
"zgo.at/zli"
)
Expand All @@ -28,6 +31,7 @@ func parseFlags() (tomltest.Runner, []string, int, string, bool) {
skip = f.StringList(nil, "skip")
run = f.StringList(nil, "run")
listFiles = f.Bool(false, "list-files")
cat = f.Int(0, "cat")
)
zli.F(f.Parse())
if help.Bool() {
Expand All @@ -38,6 +42,67 @@ func parseFlags() (tomltest.Runner, []string, int, string, bool) {
zli.PrintVersion(versionFlag.Int() > 1)
zli.Exit(0)
}
if cat.Set() {
fsys := tomltest.EmbeddedTests()
f, err := fs.ReadFile(fsys, "files-toml-"+tomlVersion.String())
zli.F(err)
gather := make(map[string]map[string]any) /// file -> decoded
for _, line := range strings.Split(string(f), "\n") {
if strings.HasPrefix(line, "valid/") && strings.HasSuffix(line, ".toml") {
var t map[string]any
_, err := toml.DecodeFS(fsys, line, &t)
zli.F(err)
gather[line] = t
}
}

var (
out = new(bytes.Buffer)
wrote int
keys []string
i int
)
for k := range gather {
keys = append(keys, k)
}
sort.Strings(keys)
outer:
for {
for _, line := range keys {
t := gather[line]
p := line + "-" + strconv.Itoa(i)

var prefix func(tbl map[string]any) map[string]any
prefix = func(tbl map[string]any) map[string]any {
newTbl := make(map[string]any)
for k, v := range tbl {
switch vv := v.(type) {
case map[string]any:
k = p + "-" + k
v = prefix(vv)
}
newTbl[k] = v
}
return newTbl
}
t = prefix(t)

err = toml.NewEncoder(out).Encode(map[string]any{
p: t,
})
zli.F(err)

fmt.Println(out.String())
wrote += out.Len() + 1
if wrote > cat.Int()*1024 {
break outer
}
out.Reset()
}
i++
}
zli.Exit(0)
}

r := tomltest.Runner{
Encoder: encoder.Bool(),
Expand Down
8 changes: 8 additions & 0 deletions cmd/toml-test/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ Flags:
running anything. This takes the -toml flag in to account, but
none of the other flags.
-cat Keep outputting (valid) TOML from testcases until the file
reaches this many KB. Useful for generating performance tests.
E.g. to create 1M and 100M files:
$ toml-test -cat 1024 >1M.toml
$ toml-test -cat $(( 1024 * 100 )) >100M.toml
-v List all tests, even passing ones. Add twice to show detailed
output for passing tests.
Expand Down

0 comments on commit 42c577f

Please sign in to comment.