From d6c960c0693a242e55a0efe47a32f90a8463503c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20L=C3=A9one?= Date: Wed, 24 Jun 2020 11:26:50 +0200 Subject: [PATCH 1/2] chore(core): refactor metadata and rename Tpl to render --- internal/core/testing.go | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/internal/core/testing.go b/internal/core/testing.go index 4903e64ece..1e51dd1328 100644 --- a/internal/core/testing.go +++ b/internal/core/testing.go @@ -56,7 +56,7 @@ type CheckFuncCtx struct { Result interface{} // Meta bag - Meta TestMeta + Meta metadata // Scaleway client Client *scw.Client @@ -67,11 +67,11 @@ type CheckFuncCtx struct { Logger *Logger } -// TestMeta contains arbitrary data that can be passed along a test lifecycle. -type TestMeta map[string]interface{} +// metadata contains arbitrary data that can be passed along a test lifecycle. +type metadata map[string]interface{} -// Tpl render a go template using where content of meta can be used -func (meta TestMeta) Tpl(strTpl string) string { +// render renders a go template using where content of meta can be used +func (meta metadata) render(strTpl string) string { t := meta["t"].(*testing.T) buf := &bytes.Buffer{} require.NoError(t, template.Must(template.New("tpl").Parse(strTpl)).Execute(buf, meta)) @@ -87,7 +87,7 @@ type AfterFunc func(ctx *AfterFuncCtx) error type ExecFuncCtx struct { T *testing.T - Meta TestMeta + Meta metadata Client *scw.Client } @@ -97,7 +97,7 @@ type BeforeFuncCtx struct { T *testing.T Client *scw.Client ExecuteCmd func(args []string) interface{} - Meta TestMeta + Meta metadata OverrideEnv map[string]string Logger *Logger } @@ -106,7 +106,7 @@ type AfterFuncCtx struct { T *testing.T Client *scw.Client ExecuteCmd func(args []string) interface{} - Meta TestMeta + Meta metadata CmdResult interface{} OverrideEnv map[string]string Logger *Logger @@ -295,7 +295,7 @@ func Test(config *TestConfig) func(t *testing.T) { client = createTestClient(t, config, httpClient) } - meta := TestMeta{ + meta := metadata{ "t": t, } @@ -435,8 +435,8 @@ func Test(config *TestConfig) func(t *testing.T) { } } -func cmdToArgs(meta TestMeta, s string) []string { - return strings.Split(meta.Tpl(s), " ") +func cmdToArgs(meta metadata, s string) []string { + return strings.Split(meta.render(s), " ") } // BeforeFuncCombine combines multiple before functions into one. @@ -563,7 +563,7 @@ func TestCheckStdout(stdout string) TestCheck { func OverrideExecSimple(cmdStr string, exitCode int) OverrideExecTestFunc { return func(ctx *ExecFuncCtx, cmd *exec.Cmd) (int, error) { - assert.Equal(ctx.T, ctx.Meta.Tpl(cmdStr), strings.Join(cmd.Args, " ")) + assert.Equal(ctx.T, ctx.Meta.render(cmdStr), strings.Join(cmd.Args, " ")) return exitCode, nil } } From 11f27062a3faa34504cb7008babacc0306b60950 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20L=C3=A9one?= Date: Wed, 24 Jun 2020 17:16:08 +0200 Subject: [PATCH 2/2] Fix --- internal/core/testing.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/internal/core/testing.go b/internal/core/testing.go index 1e51dd1328..ec2faa880c 100644 --- a/internal/core/testing.go +++ b/internal/core/testing.go @@ -56,7 +56,7 @@ type CheckFuncCtx struct { Result interface{} // Meta bag - Meta metadata + Meta testMetadata // Scaleway client Client *scw.Client @@ -67,11 +67,11 @@ type CheckFuncCtx struct { Logger *Logger } -// metadata contains arbitrary data that can be passed along a test lifecycle. -type metadata map[string]interface{} +// testMetadata contains arbitrary data that can be passed along a test lifecycle. +type testMetadata map[string]interface{} // render renders a go template using where content of meta can be used -func (meta metadata) render(strTpl string) string { +func (meta testMetadata) render(strTpl string) string { t := meta["t"].(*testing.T) buf := &bytes.Buffer{} require.NoError(t, template.Must(template.New("tpl").Parse(strTpl)).Execute(buf, meta)) @@ -87,7 +87,7 @@ type AfterFunc func(ctx *AfterFuncCtx) error type ExecFuncCtx struct { T *testing.T - Meta metadata + Meta testMetadata Client *scw.Client } @@ -97,7 +97,7 @@ type BeforeFuncCtx struct { T *testing.T Client *scw.Client ExecuteCmd func(args []string) interface{} - Meta metadata + Meta testMetadata OverrideEnv map[string]string Logger *Logger } @@ -106,7 +106,7 @@ type AfterFuncCtx struct { T *testing.T Client *scw.Client ExecuteCmd func(args []string) interface{} - Meta metadata + Meta testMetadata CmdResult interface{} OverrideEnv map[string]string Logger *Logger @@ -295,7 +295,7 @@ func Test(config *TestConfig) func(t *testing.T) { client = createTestClient(t, config, httpClient) } - meta := metadata{ + meta := testMetadata{ "t": t, } @@ -435,7 +435,7 @@ func Test(config *TestConfig) func(t *testing.T) { } } -func cmdToArgs(meta metadata, s string) []string { +func cmdToArgs(meta testMetadata, s string) []string { return strings.Split(meta.render(s), " ") }