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
2324var 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+
2533var 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
3038func 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
181202func newEnc () * json.Encoder {
0 commit comments