Commit ac2a4807 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

packer: fix required var check to work properly

parent fd4b01cf
...@@ -126,14 +126,19 @@ func (b *coreBuild) Prepare(userVars map[string]string) (err error) { ...@@ -126,14 +126,19 @@ func (b *coreBuild) Prepare(userVars map[string]string) (err error) {
b.prepareCalled = true b.prepareCalled = true
// Compile the variables // Compile the variables
varErrs := make([]error, 0)
variables := make(map[string]string) variables := make(map[string]string)
for k, v := range b.variables { for k, v := range b.variables {
if !v.Required {
variables[k] = v.Default variables[k] = v.Default
if v.Required {
if _, ok := userVars[k]; !ok {
varErrs = append(varErrs,
fmt.Errorf("Required user variable '%s' not set", k))
}
} }
} }
varErrs := make([]error, 0)
if userVars != nil { if userVars != nil {
for k, v := range userVars { for k, v := range userVars {
if _, ok := variables[k]; !ok { if _, ok := variables[k]; !ok {
...@@ -146,18 +151,6 @@ func (b *coreBuild) Prepare(userVars map[string]string) (err error) { ...@@ -146,18 +151,6 @@ func (b *coreBuild) Prepare(userVars map[string]string) (err error) {
} }
} }
// Verify all required variables have been set.
for k, v := range b.variables {
if !v.Required {
continue
}
if _, ok := variables[k]; !ok {
varErrs = append(
varErrs, fmt.Errorf("Required user variable '%s' not set", k))
}
}
// If there were any problem with variables, return an error right // If there were any problem with variables, return an error right
// away because we can't be certain anything else will actually work. // away because we can't be certain anything else will actually work.
if len(varErrs) > 0 { if len(varErrs) > 0 {
......
...@@ -175,6 +175,14 @@ func TestBuildPrepare_variablesRequired(t *testing.T) { ...@@ -175,6 +175,14 @@ func TestBuildPrepare_variablesRequired(t *testing.T) {
if err == nil { if err == nil {
t.Fatal("should have had error") t.Fatal("should have had error")
} }
// Test with setting the value
build = testBuild()
build.variables["foo"] = coreBuildVariable{Required: true}
err = build.Prepare(map[string]string{"foo": ""})
if err != nil {
t.Fatalf("should not have error: %s", err)
}
} }
func TestBuild_Run(t *testing.T) { func TestBuild_Run(t *testing.T) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment