continue and realise and unused undefaulted value triggers an error

This commit is contained in:
Adrien Delorme 2020-02-17 18:05:15 +01:00
parent cebfb1c735
commit 3c213e6eaf
5 changed files with 53 additions and 27 deletions

View File

@ -8,7 +8,7 @@ build {
provisioner "shell" {
name = "provisioner that does something"
not_squashed = var.foo
string = "string${var.proxmox_username}"
string = "string"
int = "${41 + 1}"
int64 = "${42 + 1}"
bool = "true"

View File

@ -10,10 +10,6 @@ variable "image_id" {
default = "image-id-default"
}
variable "proxmox_username" {
type = string
}
variable "port" {
type = number
default = 42

View File

@ -21,14 +21,23 @@ func TestParser_complete(t *testing.T) {
&PackerConfig{
Basedir: "testdata/complete",
InputVariables: Variables{
"foo": &Variable{},
"image_id": &Variable{},
"port": &Variable{},
"availability_zone_names": &Variable{},
"proxmox_username": &Variable{},
"foo": &Variable{
Name: "foo",
},
"image_id": &Variable{
Name: "image_id",
},
"port": &Variable{
Name: "port",
},
"availability_zone_names": &Variable{
Name: "availability_zone_names",
},
},
LocalVariables: Variables{
"feefoo": &Variable{},
"feefoo": &Variable{
Name: "feefoo",
},
},
Sources: map[SourceRef]*SourceBlock{
refVBIsoUbuntu1204: {Type: "virtualbox-iso", Name: "ubuntu-1204"},

View File

@ -31,6 +31,8 @@ type Variable struct {
// declaration, the type of the default variable will be used. This will
// allow to ensure that users set this variable correctly.
Type cty.Type
// Common name of the variable
Name string
// Description of the variable
Description string
// When Sensitive is set to true Packer will try it best to hide/obfuscate
@ -59,7 +61,7 @@ func (v *Variable) Value() (cty.Value, *hcl.Diagnostic) {
return cty.UnknownVal(v.Type), &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Unset variable",
Summary: fmt.Sprintf("Unset variable %q", v.Name),
Detail: "A used variable must be set or have a default value; see " +
"https://packer.io/docs/configuration/from-1.5/syntax.html for details.",
Context: v.block.DefRange.Ptr(),
@ -74,8 +76,10 @@ func (variables Variables) Values() (map[string]cty.Value, hcl.Diagnostics) {
for k, v := range variables {
var diag *hcl.Diagnostic
res[k], diag = v.Value()
if diag != nil {
diags = append(diags, diag)
}
}
return res, diags
}
@ -107,8 +111,10 @@ func (variables *Variables) decodeConfigMap(block *hcl.Block, ectx *hcl.EvalCont
continue
}
(*variables)[key] = &Variable{
Name: key,
DefaultValue: value,
Type: value.Type(),
block: block,
}
}
@ -142,7 +148,10 @@ func (variables *Variables) decodeConfig(block *hcl.Block, ectx *hcl.EvalContext
return diags
}
name := block.Labels[0]
res := &Variable{
Name: name,
Description: b.Description,
Sensitive: b.Sensitive,
block: block,
@ -200,7 +209,7 @@ func (variables *Variables) decodeConfig(block *hcl.Block, ectx *hcl.EvalContext
})
}
(*variables)[block.Labels[0]] = res
(*variables)[name] = res
return diags
}

View File

@ -23,15 +23,17 @@ func TestParse_variables(t *testing.T) {
&PackerConfig{
Basedir: filepath.Join("testdata", "variables"),
InputVariables: Variables{
"image_name": &Variable{},
"key": &Variable{},
"my_secret": &Variable{},
"image_id": &Variable{},
"port": &Variable{},
"image_name": &Variable{Name: "image_name"},
"key": &Variable{Name: "key"},
"my_secret": &Variable{Name: "my_secret"},
"image_id": &Variable{Name: "image_id"},
"port": &Variable{Name: "port"},
"availability_zone_names": &Variable{
Name: "availability_zone_names",
Description: fmt.Sprintln("Describing is awesome ;D"),
},
"super_secret_password": &Variable{
Name: "super_secret_password",
Sensitive: true,
Description: fmt.Sprintln("Handle with care plz"),
},
@ -51,7 +53,9 @@ func TestParse_variables(t *testing.T) {
&PackerConfig{
Basedir: filepath.Join("testdata", "variables"),
InputVariables: Variables{
"boolean_value": &Variable{},
"boolean_value": &Variable{
Name: "boolean_value",
},
},
},
true, true,
@ -64,7 +68,9 @@ func TestParse_variables(t *testing.T) {
&PackerConfig{
Basedir: filepath.Join("testdata", "variables"),
InputVariables: Variables{
"boolean_value": &Variable{},
"boolean_value": &Variable{
Name: "boolean_value",
},
},
},
true, true,
@ -77,23 +83,27 @@ func TestParse_variables(t *testing.T) {
&PackerConfig{
Basedir: filepath.Join("testdata", "variables"),
InputVariables: Variables{
"broken_type": &Variable{},
"broken_type": &Variable{
Name: "broken_type",
},
},
},
true, true,
[]packer.Build{},
false,
},
{"invalid default type",
{"unknown key",
defaultParser,
parseTestArgs{"testdata/variables/unknown_key.pkr.hcl", nil},
&PackerConfig{
Basedir: filepath.Join("testdata", "variables"),
InputVariables: Variables{
"broken_type": &Variable{},
"broken_type": &Variable{
Name: "broken_type",
},
},
true, false,
},
true, true,
[]packer.Build{},
false,
},
@ -103,7 +113,9 @@ func TestParse_variables(t *testing.T) {
&PackerConfig{
Basedir: filepath.Join("testdata", "variables"),
InputVariables: Variables{
"foo": &Variable{},
"foo": &Variable{
Name: "foo",
},
},
Sources: map[SourceRef]*SourceBlock{
SourceRef{"null", "null-builder"}: &SourceBlock{
@ -117,7 +129,7 @@ func TestParse_variables(t *testing.T) {
},
},
},
false, false,
true, true,
[]packer.Build{},
true,
},