Commit 590997df authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

packer: automatically validate when creating a core

parent e0a9215e
...@@ -49,11 +49,6 @@ func (m *Meta) Core(tpl *template.Template) (*packer.Core, error) { ...@@ -49,11 +49,6 @@ func (m *Meta) Core(tpl *template.Template) (*packer.Core, error) {
return nil, fmt.Errorf("Error initializing core: %s", err) return nil, fmt.Errorf("Error initializing core: %s", err)
} }
// Validate it
if err := core.Validate(); err != nil {
return nil, err
}
return core, nil return core, nil
} }
......
...@@ -72,6 +72,9 @@ func NewCore(c *CoreConfig) (*Core, error) { ...@@ -72,6 +72,9 @@ func NewCore(c *CoreConfig) (*Core, error) {
variables: c.Variables, variables: c.Variables,
builds: builds, builds: builds,
} }
if err := result.validate(); err != nil {
return nil, err
}
if err := result.init(); err != nil { if err := result.init(); err != nil {
return nil, err return nil, err
} }
...@@ -205,11 +208,11 @@ func (c *Core) Build(n string) (Build, error) { ...@@ -205,11 +208,11 @@ func (c *Core) Build(n string) (Build, error) {
}, nil }, nil
} }
// Validate does a full validation of the template. // validate does a full validation of the template.
// //
// This will automatically call template.Validate() in addition to doing // This will automatically call template.validate() in addition to doing
// richer semantic checks around variables and so on. // richer semantic checks around variables and so on.
func (c *Core) Validate() error { func (c *Core) validate() error {
// First validate the template in general, we can't do anything else // First validate the template in general, we can't do anything else
// unless the template itself is valid. // unless the template itself is valid.
if err := c.template.Validate(); err != nil { if err := c.template.Validate(); err != nil {
......
...@@ -376,15 +376,11 @@ func TestCoreValidate(t *testing.T) { ...@@ -376,15 +376,11 @@ func TestCoreValidate(t *testing.T) {
t.Fatalf("err: %s\n\n%s", tc.File, err) t.Fatalf("err: %s\n\n%s", tc.File, err)
} }
core, err := NewCore(&CoreConfig{ _, err = NewCore(&CoreConfig{
Template: tpl, Template: tpl,
Variables: tc.Vars, Variables: tc.Vars,
}) })
if err != nil { if (err != nil) != tc.Err {
t.Fatalf("err: %s\n\n%s", tc.File, err)
}
if err := core.Validate(); (err != nil) != tc.Err {
t.Fatalf("err: %s\n\n%s", tc.File, err) t.Fatalf("err: %s\n\n%s", tc.File, err)
} }
} }
......
{ {
"builders": [{ "builders": [{
"type": "test" "type": "test"
}, {
"name": "foo",
"type": "test"
}], }],
"provisioners": [{ "provisioners": [{
......
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