Skip to content

Commit bf39611

Browse files
committed
Add -setup flag
1 parent b97e905 commit bf39611

4 files changed

Lines changed: 58 additions & 6 deletions

File tree

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
Unreleased
2+
----------
3+
- Add `-setup` flag to run one or more commands before running the test suite;
4+
useful for setting up or compiling the decoder program or script in one
5+
command. For example:
6+
7+
toml-test test \
8+
-setup='go build ./cmd/toml-test-decoder' -decoder=./toml-test-decoder \
9+
-setup='go build ./cmd/toml-test-encoder' -encoder=./toml-test-encoder
10+
111
v2.2.0 2026-03-30
212
-----------------
313
This contains several minor bug fixes to the test runner and one additional

cmd/toml-test/script.gotxt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ decoder="{{join .Decoder " "}}"
88
{{if .Encoder}}encoder="{{join .Encoder " "}}"{{else}}
99
encoder= # No encoder tests{{end}}
1010

11+
# Setup command(s), one -setup per array entry.
12+
setup=({{range $s := .Setup}}
13+
{{"\t"}}-setup {{$s | quote}}{{end}}
14+
)
15+
1116
# Version of the TOML specification to test.
1217
toml={{.TOML}}
1318

@@ -52,4 +57,4 @@ if ! command -v "$tt" >/dev/null; then
5257
fi
5358

5459
# Run toml-test
55-
"$tt" test -toml="$toml" -skip-must-err ${skip[@]} -decoder="$decoder" -encoder="${encoder:-}" "$@"
60+
"$tt" test -toml="$toml" -skip-must-err ${skip[@]} "${setup[@]}" -decoder="$decoder" -encoder="${encoder:-}" "$@"

cmd/toml-test/test.go

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/json"
66
"fmt"
77
"os"
8+
"os/exec"
89
"path/filepath"
910
"runtime"
1011
"strings"
@@ -22,13 +23,31 @@ var hlErr = zli.Color256(224).Bg() | zli.Color256(0) | zli.Bold
2223
//go:embed script.gotxt
2324
var script []byte
2425

26+
func shellQuote(s string) string {
27+
if strings.Contains(s, "'") {
28+
s = strings.ReplaceAll(s, `'`, `'"'"'`) // cmd'foo → 'cmd'"'"'foo'
29+
}
30+
return "'" + s + "'"
31+
}
32+
2533
var scriptTemplate = template.Must(template.New("").
2634
Option("missingkey=error").
27-
Funcs(template.FuncMap{"join": strings.Join}).
35+
Funcs(template.FuncMap{"join": strings.Join, "quote": shellQuote}).
2836
Parse(string(script)))
2937

3038
func cmdTest(f zli.Flags) {
31-
runner, verbose, script, asJSON := parseTestFlags(f)
39+
runner, verbose, script, asJSON, setup := parseTestFlags(f)
40+
41+
for _, s := range setup {
42+
f := strings.Fields(s)
43+
if verbose > 0 {
44+
fmt.Printf("SETUP %v\n", s)
45+
}
46+
out, err := exec.Command(f[0], f[1:]...).CombinedOutput()
47+
if err != nil {
48+
zli.Fatalf("error running -setup=%q: %s: command output:\n%s", s, err, out)
49+
}
50+
}
3251

3352
tests, err := runner.Run()
3453
zli.F(err)
@@ -54,12 +73,13 @@ func cmdTest(f zli.Flags) {
5473
err := scriptTemplate.Execute(os.Stdout, struct {
5574
Decoder []string
5675
Encoder []string
76+
Setup []string
5777
TOML string
5878
Version string
5979
FailedValid []string
6080
FailedEncoder []string
6181
FailedInvalid []string
62-
}{runner.Decoder.Cmd(), enc, runner.Version, v, failedValid, failedEncoder, failedInvalid})
82+
}{runner.Decoder.Cmd(), enc, setup, runner.Version, v, failedValid, failedEncoder, failedInvalid})
6383
zli.F(err)
6484
return
6585
}
@@ -76,10 +96,11 @@ func cmdTest(f zli.Flags) {
7696
zli.Exit(0)
7797
}
7898

79-
func parseTestFlags(f zli.Flags) (tomltest.Runner, int, bool, bool) {
99+
func parseTestFlags(f zli.Flags) (tomltest.Runner, int, bool, bool, []string) {
80100
var (
81101
decoder = f.String("", "decoder")
82102
encoder = f.String("", "encoder")
103+
setup = f.StringList(nil, "setup")
83104
tomlVersion = f.String(tomltest.DefaultVersion, "toml")
84105
verbose = f.IntCounter(0, "v")
85106
color = f.String("always", "color")
@@ -175,7 +196,7 @@ func parseTestFlags(f zli.Flags) (tomltest.Runner, int, bool, bool) {
175196
}
176197
}
177198

178-
return runner, verbose.Int(), script.Bool(), asJSON.Bool()
199+
return runner, verbose.Int(), script.Bool(), asJSON.Bool(), setup.Strings()
179200
}
180201

181202
func newEnc() *json.Encoder {

cmd/toml-test/usage.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,22 @@ There are three types of tests:
135135
specified in the toml-test README. May be omitted if writing
136136
TOML isn't supported.
137137
138+
-setup Run once before any tests, to setup/compile the decoder.
139+
toml-test exits with an error and won't run any tests if
140+
this exits with non-zero code. Like -decoder and -encoder,
141+
this isn't run through a shell but arguments are split on
142+
whitespace. This flag can be added more than once to run
143+
several commands (which are run in the order they are given
144+
on the commandline).
145+
146+
For example:
147+
148+
toml-test test \
149+
-setup='go build ./cmd/toml-test-decoder' \
150+
-setup='go build ./cmd/toml-test-encoder' \
151+
-decoder=./toml-test-decoder \
152+
-encoder=./toml-test-encoder
153+
138154
-json Output report as JSON rather than text.
139155
140156
-script Print a small bash/zsh script with -skip flag for failing

0 commit comments

Comments
 (0)