Skip to content

Commit 229ce2e

Browse files
committed
Add back the "list" command
#180 (comment)
1 parent 3ad2fe8 commit 229ce2e

8 files changed

Lines changed: 1849 additions & 29 deletions

File tree

README.md

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -231,13 +231,8 @@ directory. The easiest way to copy the correct files is to use `copy`:
231231
% toml-test copy -toml 1.1.0 ./tests
232232

233233
Alternatively, the [tests/files-toml-1.0.0] and [tests/files-toml-1.1.0] files
234-
contain a list of files you should run for that TOML version. You can use them
235-
with something like:
236-
237-
<files-toml-1.0.0 while read l; do
238-
mkdir -p ~/my-test/"$(dirname "$l")"
239-
cp -r "$l" ~/my-test/"$l"
240-
done
234+
contain a list of files that should be run for that TOML version. This list is
235+
generated from the `toml-test list` output.
241236

242237
[tests/files-toml-1.0.0]: tests/files-toml-1.0.0
243238
[tests/files-toml-1.1.0]: tests/files-toml-1.1.0

cmd/toml-test/copy.go

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import (
55
"io"
66
"os"
77
"path/filepath"
8-
"sort"
9-
"strings"
108

119
tomltest "github.com/toml-lang/toml-test/v2"
1210
"zgo.at/zli"
@@ -61,22 +59,3 @@ date = %s
6159
`[1:], v, c, t.Format("2006-01-02"))), 0o0644)
6260
zli.F(err)
6361
}
64-
65-
func getList(r tomltest.Runner) []string {
66-
l, err := r.List()
67-
zli.F(err)
68-
69-
sort.Strings(l)
70-
n := make([]string, 0, len(l)*2)
71-
for _, ll := range l {
72-
if strings.HasPrefix(ll, "encoder/") {
73-
continue
74-
}
75-
76-
if strings.HasPrefix(ll, "valid/") {
77-
n = append(n, ll+".json")
78-
}
79-
n = append(n, ll+".toml")
80-
}
81-
return n
82-
}

cmd/toml-test/list.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"sort"
6+
"strings"
7+
8+
tomltest "github.com/toml-lang/toml-test/v2"
9+
"zgo.at/zli"
10+
)
11+
12+
func cmdList(f zli.Flags) {
13+
var (
14+
tomlVersion = f.String(tomltest.DefaultVersion, "toml")
15+
asJSON = f.Bool(false, "json")
16+
)
17+
zli.F(f.Parse())
18+
19+
l := getList(tomltest.NewRunner(tomltest.Runner{Version: tomlVersion.String()}))
20+
if asJSON.Bool() {
21+
newEnc().Encode(l)
22+
} else {
23+
for _, ll := range l {
24+
fmt.Println(ll)
25+
}
26+
}
27+
}
28+
29+
func getList(r tomltest.Runner) []string {
30+
l, err := r.List()
31+
zli.F(err)
32+
33+
sort.Strings(l)
34+
n := make([]string, 0, len(l)*2)
35+
for _, ll := range l {
36+
if strings.HasPrefix(ll, "encoder/") {
37+
continue
38+
}
39+
40+
if strings.HasPrefix(ll, "valid/") {
41+
n = append(n, ll+".json")
42+
}
43+
n = append(n, ll+".toml")
44+
}
45+
return n
46+
}

cmd/toml-test/main.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func main() {
2727
f := zli.NewFlags(os.Args)
2828
helpFlag := f.Bool(false, "h", "help")
2929
zli.F(f.Parse(zli.AllowUnknown()))
30-
cmd, err := f.ShiftCommand("help", "version", "test", "copy", "cp")
30+
cmd, err := f.ShiftCommand("help", "version", "test", "list", "ls", "copy", "cp")
3131
if errors.Is(err, zli.ErrCommandNoneGiven{}) {
3232
fmt.Print(usage)
3333
return
@@ -54,6 +54,8 @@ func main() {
5454
v := f.Bool(false, "v")
5555
zli.F(f.Parse())
5656
zli.PrintVersion(v.Bool())
57+
case "list", "ls":
58+
cmdList(f)
5759
case "copy", "cp":
5860
cmdCopy(f)
5961
case "test":

cmd/toml-test/usage.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import "strings"
55
var helpTopics = map[string]string{
66
"": usage,
77
"test": usageTest,
8+
"list": usageList,
9+
"ls": usageList,
810
"copy": usageCopy,
911
"cp": usageCopy,
1012
"version": usageVersion,
@@ -22,6 +24,7 @@ Commands:
2224
help Show help and exit.
2325
test Run tests. See "help test" for details.
2426
copy Write all test files to disk.
27+
list List test filenames.
2528
version Show version and exit.
2629
`[1:]
2730

@@ -227,6 +230,17 @@ updates.
227230
-toml TOML version to copy tests for (1.0, 1.1, or latest).
228231
`
229232

233+
var usageList = `
234+
The "list" command lists all testfiles.
235+
236+
This is mainly intended to generate the tests/files-toml-* files, which can
237+
make it more convenient to use the test cases without running toml-test.
238+
239+
\x1b[1mFlags:\x1b[0m
240+
241+
-toml TOML version to list tests for (1.0, 1.1, or latest).
242+
`
243+
230244
var usageVersion = `
231245
Show version and exit.
232246

gen.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ def gen_multi():
2727
if '/valid/' in f: # TODO: version?
2828
run_decoder('1.0.0', path, path[:-5] + '.json')
2929

30+
def gen_list():
31+
with open('tests/files-toml-1.0.0', 'w+') as fp:
32+
subprocess.run(['go', 'run', './cmd/toml-test', 'list', '-toml=1.0.0'], stdout=fp)
33+
with open('tests/files-toml-1.1.0', 'w+') as fp:
34+
subprocess.run(['go', 'run', './cmd/toml-test', 'list', '-toml=1.1.0'], stdout=fp)
35+
3036
def gen_spec(version, file):
3137
try:
3238
shutil.rmtree(f"{VALID_ROOT}-{version}")
@@ -165,3 +171,4 @@ def has_active_invalid(block):
165171
gen_multi()
166172
gen_spec('1.0.0', 'specs/v1.0.0.md')
167173
gen_spec('1.1.0', 'specs/v1.1.0.md')
174+
gen_list()

0 commit comments

Comments
 (0)