From 8350414577b80dd81e60ef7ad3c5c09af5d89494 Mon Sep 17 00:00:00 2001 From: CrazyMax <1951866+crazy-max@users.noreply.github.com> Date: Thu, 11 Apr 2024 17:09:35 +0200 Subject: [PATCH] bake: test indexOf hcl func and make it private Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com> --- bake/hcl_test.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/bake/hcl_test.go b/bake/hcl_test.go index 31abc43e616e..f33722c72acc 100644 --- a/bake/hcl_test.go +++ b/bake/hcl_test.go @@ -1445,6 +1445,39 @@ func TestVarUnsupportedType(t *testing.T) { require.Error(t, err) } +func TestHCLIndexOfFunc(t *testing.T) { + dt := []byte(` + variable "APP_VERSIONS" { + default = [ + "1.42.4", + "1.42.3" + ] + } + target "default" { + args = { + APP_VERSION = app_version + } + matrix = { + app_version = APP_VERSIONS + } + name="app-${replace(app_version, ".", "-")}" + tags = [ + "app:${app_version}", + indexof(APP_VERSIONS, app_version) == 0 ? "app:latest" : "", + ] + } + `) + + c, err := ParseFile(dt, "docker-bake.hcl") + require.NoError(t, err) + + require.Equal(t, 2, len(c.Targets)) + require.Equal(t, "app-1-42-4", c.Targets[0].Name) + require.Equal(t, "app:latest", c.Targets[0].Tags[1]) + require.Equal(t, "app-1-42-3", c.Targets[1].Name) + require.Empty(t, c.Targets[1].Tags[1]) +} + func ptrstr(s interface{}) *string { var n *string if reflect.ValueOf(s).Kind() == reflect.String {