This commit is contained in:
Adrien Delorme 2020-03-03 11:15:56 +01:00
parent 52e5b7051e
commit 50896d4ddf
6 changed files with 68 additions and 12 deletions

View File

@ -60,7 +60,7 @@ func testParse(t *testing.T, tests []parseTest) {
t.Run(tt.name, func(t *testing.T) {
gotCfg, gotDiags := tt.parser.parse(tt.args.filename, tt.args.vars)
if tt.parseWantDiags == (gotDiags == nil) {
t.Fatalf("Parser.parse() unexpected diagnostics. %s", gotDiags)
t.Fatalf("Parser.parse() unexpected %q diagnostics.", gotDiags)
}
if tt.parseWantDiagHasErrors != gotDiags.HasErrors() {
t.Fatalf("Parser.parse() unexpected diagnostics HasErrors. %s", gotDiags)
@ -97,6 +97,7 @@ func testParse(t *testing.T, tests []parseTest) {
packer.CoreBuild{},
packer.CoreBuildProvisioner{},
packer.CoreBuildPostProcessor{},
null.Builder{},
),
); diff != "" {
t.Fatalf("Parser.getBuilds() wrong packer builds. %s", diff)

View File

@ -101,10 +101,10 @@ func (p *Parser) parse(filename string, vars map[string]string) (*PackerConfig,
}
}
_, moreDiags := cfg.InputVariables.Values()
diags = append(diags, moreDiags...)
_, moreDiags = cfg.LocalVariables.Values()
diags = append(diags, moreDiags...)
// _, moreDiags := cfg.InputVariables.Values()
// diags = append(diags, moreDiags...)
// _, moreDiags = cfg.LocalVariables.Values()
// diags = append(diags, moreDiags...)
// parse var files
{

View File

@ -0,0 +1,15 @@
variable "foo" {
type = string
}
build {
sources = [
"source.null.null-builder",
]
}
source "null" "null-builder" {
communicator = "none"
}

View File

@ -59,7 +59,12 @@ func (v *Variable) Value() (cty.Value, *hcl.Diagnostic) {
}
}
return cty.UnknownVal(v.Type), &hcl.Diagnostic{
value := cty.NullVal(cty.DynamicPseudoType)
if v.Type != cty.NilType {
cty.NullVal(v.Type)
}
return value, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: fmt.Sprintf("Unset variable %q", v.Name),
Detail: "A used variable must be set or have a default value; see " +
@ -248,6 +253,7 @@ func (variables Variables) collectVariableValues(env []string, files []*hcl.File
if moreDiags.HasErrors() {
continue
}
val, valDiags := expr.Value(nil)
diags = append(diags, valDiags...)

View File

@ -10,6 +10,7 @@ import (
"github.com/zclconf/go-cty/cty/convert"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/packer/builder/null"
"github.com/hashicorp/packer/packer"
)
@ -39,8 +40,8 @@ func TestParse_variables(t *testing.T) {
},
},
LocalVariables: Variables{
"owner": &Variable{},
"service_name": &Variable{},
"owner": &Variable{Name: "owner"},
"service_name": &Variable{Name: "service_name"},
},
},
false, false,
@ -103,13 +104,13 @@ func TestParse_variables(t *testing.T) {
},
},
},
true, true,
true, false,
[]packer.Build{},
false,
},
{"unset variable",
{"unset used variable",
defaultParser,
parseTestArgs{"testdata/variables/unset_string_variable.pkr.hcl", nil},
parseTestArgs{"testdata/variables/unset_used_string_variable.pkr.hcl", nil},
&PackerConfig{
Basedir: filepath.Join("testdata", "variables"),
InputVariables: Variables{
@ -131,7 +132,40 @@ func TestParse_variables(t *testing.T) {
},
true, true,
[]packer.Build{},
true,
false,
},
{"unset unused variable",
defaultParser,
parseTestArgs{"testdata/variables/unset_unused_string_variable.pkr.hcl", nil},
&PackerConfig{
Basedir: filepath.Join("testdata", "variables"),
InputVariables: Variables{
"foo": &Variable{
Name: "foo",
},
},
Sources: map[SourceRef]*SourceBlock{
SourceRef{"null", "null-builder"}: &SourceBlock{
Name: "null-builder",
Type: "null",
},
},
Builds: Builds{
&BuildBlock{
Sources: []SourceRef{SourceRef{"null", "null-builder"}},
},
},
},
false, false,
[]packer.Build{
&packer.CoreBuild{
Type: "null",
Builder: &null.Builder{},
Provisioners: []packer.CoreBuildProvisioner{},
PostProcessors: [][]packer.CoreBuildPostProcessor{},
},
},
false,
},
}
testParse(t, tests)