mirror of
https://gitea.com/Lydanne/buildx.git
synced 2025-05-19 01:47:43 +08:00
bake: test indexof hcl func and make it private
Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
This commit is contained in:
parent
5777d980b5
commit
0a7f5c4d94
@ -1445,6 +1445,39 @@ func TestVarUnsupportedType(t *testing.T) {
|
|||||||
require.Error(t, err)
|
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 {
|
func ptrstr(s interface{}) *string {
|
||||||
var n *string
|
var n *string
|
||||||
if reflect.ValueOf(s).Kind() == reflect.String {
|
if reflect.ValueOf(s).Kind() == reflect.String {
|
||||||
|
@ -53,7 +53,7 @@ var stdlibFunctions = map[string]function.Function{
|
|||||||
"hasindex": stdlib.HasIndexFunc,
|
"hasindex": stdlib.HasIndexFunc,
|
||||||
"indent": stdlib.IndentFunc,
|
"indent": stdlib.IndentFunc,
|
||||||
"index": stdlib.IndexFunc,
|
"index": stdlib.IndexFunc,
|
||||||
"indexof": IndexOfFunc, // behaving as expected by hcl definition of index (stdlib.IndexFunc is not compatible)
|
"indexof": indexOfFunc,
|
||||||
"int": stdlib.IntFunc,
|
"int": stdlib.IntFunc,
|
||||||
"join": stdlib.JoinFunc,
|
"join": stdlib.JoinFunc,
|
||||||
"jsondecode": stdlib.JSONDecodeFunc,
|
"jsondecode": stdlib.JSONDecodeFunc,
|
||||||
@ -117,10 +117,9 @@ var stdlibFunctions = map[string]function.Function{
|
|||||||
"zipmap": stdlib.ZipmapFunc,
|
"zipmap": stdlib.ZipmapFunc,
|
||||||
}
|
}
|
||||||
|
|
||||||
// IndexOfFunc constructs a function that finds the element index for a given value in a list.
|
// indexOfFunc constructs a function that finds the element index for a given
|
||||||
//
|
// value in a list.
|
||||||
// This function was imported from terraform's collection functions.
|
var indexOfFunc = function.New(&function.Spec{
|
||||||
var IndexOfFunc = function.New(&function.Spec{
|
|
||||||
Params: []function.Parameter{
|
Params: []function.Parameter{
|
||||||
{
|
{
|
||||||
Name: "list",
|
Name: "list",
|
||||||
|
49
bake/hclparser/stdlib_test.go
Normal file
49
bake/hclparser/stdlib_test.go
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
package hclparser
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/zclconf/go-cty/cty"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestIndexOf(t *testing.T) {
|
||||||
|
type testCase struct {
|
||||||
|
input cty.Value
|
||||||
|
key cty.Value
|
||||||
|
want cty.Value
|
||||||
|
wantErr bool
|
||||||
|
}
|
||||||
|
tests := map[string]testCase{
|
||||||
|
"index 0": {
|
||||||
|
input: cty.TupleVal([]cty.Value{cty.StringVal("one"), cty.NumberIntVal(2.0), cty.NumberIntVal(3), cty.StringVal("four")}),
|
||||||
|
key: cty.StringVal("one"),
|
||||||
|
want: cty.NumberIntVal(0),
|
||||||
|
},
|
||||||
|
"index 3": {
|
||||||
|
input: cty.TupleVal([]cty.Value{cty.StringVal("one"), cty.NumberIntVal(2.0), cty.NumberIntVal(3), cty.StringVal("four")}),
|
||||||
|
key: cty.StringVal("four"),
|
||||||
|
want: cty.NumberIntVal(3),
|
||||||
|
},
|
||||||
|
"index -1": {
|
||||||
|
input: cty.TupleVal([]cty.Value{cty.StringVal("one"), cty.NumberIntVal(2.0), cty.NumberIntVal(3), cty.StringVal("four")}),
|
||||||
|
key: cty.StringVal("3"),
|
||||||
|
wantErr: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for name, test := range tests {
|
||||||
|
name, test := name, test
|
||||||
|
t.Run(name, func(t *testing.T) {
|
||||||
|
got, err := indexOfFunc.Call([]cty.Value{test.input, test.key})
|
||||||
|
if err != nil {
|
||||||
|
if test.wantErr {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
t.Fatalf("unexpected error: %s", err)
|
||||||
|
}
|
||||||
|
if !got.RawEquals(test.want) {
|
||||||
|
t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, test.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user