bake: default variable to null if not defined

Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax 2024-06-17 10:38:33 +02:00
parent 8ca18dee2d
commit 70dad8451d
No known key found for this signature in database
GPG Key ID: ADE44D8C9D44FBE4
2 changed files with 44 additions and 1 deletions

View File

@ -394,6 +394,44 @@ func TestHCLTypedVariables(t *testing.T) {
require.Contains(t, err.Error(), "failed to parse IS_FOO as bool")
}
func TestHCLNullVariableDefault(t *testing.T) {
dt := []byte(`
variable "FOO" {}
target "default" {
args = {
isNull = equal(FOO, null)
isEmptyString = equal(FOO, "")
}
}`)
c, err := ParseFile(dt, "docker-bake.hcl")
require.NoError(t, err)
require.Len(t, c.Targets, 1)
require.Len(t, c.Targets[0].Args, 2)
require.Equal(t, "true", *c.Targets[0].Args["isNull"])
require.Equal(t, "false", *c.Targets[0].Args["isEmptyString"])
}
func TestHCLEmptyVariable(t *testing.T) {
dt := []byte(`
variable "FOO" {
default = ""
}
target "default" {
args = {
isNull = equal(FOO, null)
isEmptyString = equal(FOO, "")
}
}`)
c, err := ParseFile(dt, "docker-bake.hcl")
require.NoError(t, err)
require.Len(t, c.Targets, 1)
require.Len(t, c.Targets[0].Args, 2)
require.Equal(t, "false", *c.Targets[0].Args["isNull"])
require.Equal(t, "true", *c.Targets[0].Args["isEmptyString"])
}
func TestHCLNullVariables(t *testing.T) {
dt := []byte(`
variable "FOO" {

View File

@ -266,7 +266,12 @@ func (p *parser) resolveValue(ectx *hcl.EvalContext, name string) (err error) {
if def == nil {
val, ok := p.opt.Vars[name]
if !ok {
val, _ = p.opt.LookupVar(name)
val, ok = p.opt.LookupVar(name)
if !ok {
vv := cty.NullVal(cty.DynamicPseudoType)
v = &vv
return
}
}
vv := cty.StringVal(val)
v = &vv