@@ -7,12 +7,14 @@ import (
77 "flag"
88 "fmt"
99 "io"
10+ "math/rand/v2"
1011 "net/http"
1112 "os"
1213 "os/exec"
1314 "path"
1415 "path/filepath"
1516 "regexp"
17+ "strconv"
1618 "strings"
1719 "testing"
1820 "text/template"
@@ -64,7 +66,7 @@ type CheckFuncCtx struct {
6466 Result interface {}
6567
6668 // Meta bag
67- Meta testMetadata
69+ Meta TestMetadata
6870
6971 // Scaleway client
7072 Client * scw.Client
@@ -78,14 +80,20 @@ type CheckFuncCtx struct {
7880 LogBuffer string
7981}
8082
81- // testMetadata contains arbitrary data that can be passed along a test lifecycle.
82- type testMetadata map [string ]interface {}
83+ var testRenderHelpers = map [string ]any {
84+ "randint" : func () string {
85+ return strconv .FormatUint (rand .Uint64 (), 10 ) //nolint:gosec // Use weak random for a non-important use
86+ },
87+ }
88+
89+ // TestMetadata contains arbitrary data that can be passed along a test lifecycle.
90+ type TestMetadata map [string ]interface {}
8391
84- // render renders a go template using where content of Meta can be used
85- func (meta testMetadata ) render (strTpl string ) string {
92+ // Render renders a go template using where content of Meta can be used
93+ func (meta TestMetadata ) Render (strTpl string ) string {
8694 t := meta ["t" ].(* testing.T )
8795 buf := & bytes.Buffer {}
88- require .NoError (t , template .Must (template .New ("tpl" ).Parse (strTpl )).Execute (buf , meta ))
96+ require .NoError (t , template .Must (template .New ("tpl" ).Funcs ( testRenderHelpers ). Parse (strTpl )).Execute (buf , meta ))
8997 return buf .String ()
9098}
9199
@@ -105,7 +113,7 @@ type AfterFunc func(ctx *AfterFuncCtx) error
105113
106114type ExecFuncCtx struct {
107115 T * testing.T
108- Meta testMetadata
116+ Meta TestMetadata
109117 Client * scw.Client
110118}
111119
@@ -115,7 +123,7 @@ type BeforeFuncCtx struct {
115123 T * testing.T
116124 Client * scw.Client
117125 ExecuteCmd func (args []string ) interface {}
118- Meta testMetadata
126+ Meta TestMetadata
119127 OverrideEnv map [string ]string
120128 Logger * Logger
121129}
@@ -124,7 +132,7 @@ type AfterFuncCtx struct {
124132 T * testing.T
125133 Client * scw.Client
126134 ExecuteCmd func (args []string ) interface {}
127- Meta testMetadata
135+ Meta TestMetadata
128136 CmdResult interface {}
129137 OverrideEnv map [string ]string
130138 Logger * Logger
@@ -326,7 +334,7 @@ func Test(config *TestConfig) func(t *testing.T) {
326334 client = createTestClient (t , config , httpClient )
327335 }
328336
329- meta := testMetadata {
337+ meta := TestMetadata {
330338 "t" : t ,
331339 }
332340
@@ -412,7 +420,7 @@ func Test(config *TestConfig) func(t *testing.T) {
412420 Meta : meta ,
413421 OverrideEnv : overrideEnv ,
414422 Logger : testLogger ,
415- }))
423+ }), "error executing BeforeFunc" )
416424 testLogger .Debug ("End BeforeFunc" )
417425 }
418426
@@ -424,9 +432,9 @@ func Test(config *TestConfig) func(t *testing.T) {
424432 if config .Cmd != "" {
425433 renderedArgs = cmdToArgs (meta , config .Cmd )
426434 } else {
427- // We render raw arguments from meta
435+ // We Render raw arguments from meta
428436 for _ , arg := range rawArgs {
429- renderedArgs = append (renderedArgs , meta .render (arg ))
437+ renderedArgs = append (renderedArgs , meta .Render (arg ))
430438 }
431439 }
432440
@@ -491,8 +499,8 @@ func Test(config *TestConfig) func(t *testing.T) {
491499 }
492500}
493501
494- func cmdToArgs (meta testMetadata , s string ) []string {
495- return strings .Split (meta .render (s ), " " )
502+ func cmdToArgs (meta TestMetadata , s string ) []string {
503+ return strings .Split (meta .Render (s ), " " )
496504}
497505
498506// BeforeFuncCombine combines multiple before functions into one.
@@ -553,7 +561,13 @@ func ExecStoreBeforeCmd(metaKey, cmd string) BeforeFunc {
553561func BeforeFuncOsExec (cmd string , args ... string ) BeforeFunc {
554562 return func (ctx * BeforeFuncCtx ) error {
555563 ctx .Logger .Debugf ("BeforeFuncOsExec: cmd=%s args=%s\n " , cmd , args )
556- return exec .Command (cmd , args ... ).Run ()
564+ err := exec .Command (cmd , args ... ).Run ()
565+ if err != nil {
566+ formattedCmd := strings .Join (append ([]string {cmd }, args ... ), " " )
567+ return fmt .Errorf ("failed to execute cmd %q: %w" , formattedCmd , err )
568+ }
569+
570+ return nil
557571 }
558572}
559573
@@ -571,7 +585,7 @@ func ExecBeforeCmd(cmd string) BeforeFunc {
571585func ExecBeforeCmdArgs (args []string ) BeforeFunc {
572586 return func (ctx * BeforeFuncCtx ) error {
573587 for i := range args {
574- args [i ] = ctx .Meta .render (args [i ])
588+ args [i ] = ctx .Meta .Render (args [i ])
575589 }
576590 ctx .Logger .Debugf ("ExecBeforeCmdArgs: args=%s\n " , args )
577591 ctx .ExecuteCmd (args )
@@ -734,7 +748,7 @@ func TestCheckStdout(stdout string) TestCheck {
734748
735749func OverrideExecSimple (cmdStr string , exitCode int ) OverrideExecTestFunc {
736750 return func (ctx * ExecFuncCtx , cmd * exec.Cmd ) (int , error ) {
737- assert .Equal (ctx .T , ctx .Meta .render (cmdStr ), strings .Join (cmd .Args , " " ))
751+ assert .Equal (ctx .T , ctx .Meta .Render (cmdStr ), strings .Join (cmd .Args , " " ))
738752 return exitCode , nil
739753 }
740754}
0 commit comments