packer: automatically validate when creating a core

This commit is contained in:
Mitchell Hashimoto 2015-05-28 14:42:53 -07:00
parent e0a9215e47
commit 590997df44
4 changed files with 11 additions and 14 deletions

View File

@ -49,11 +49,6 @@ func (m *Meta) Core(tpl *template.Template) (*packer.Core, error) {
return nil, fmt.Errorf("Error initializing core: %s", err)
}
// Validate it
if err := core.Validate(); err != nil {
return nil, err
}
return core, nil
}

View File

@ -72,6 +72,9 @@ func NewCore(c *CoreConfig) (*Core, error) {
variables: c.Variables,
builds: builds,
}
if err := result.validate(); err != nil {
return nil, err
}
if err := result.init(); err != nil {
return nil, err
}
@ -205,11 +208,11 @@ func (c *Core) Build(n string) (Build, error) {
}, 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.
func (c *Core) Validate() error {
func (c *Core) validate() error {
// First validate the template in general, we can't do anything else
// unless the template itself is valid.
if err := c.template.Validate(); err != nil {

View File

@ -376,15 +376,11 @@ func TestCoreValidate(t *testing.T) {
t.Fatalf("err: %s\n\n%s", tc.File, err)
}
core, err := NewCore(&CoreConfig{
_, err = NewCore(&CoreConfig{
Template: tpl,
Variables: tc.Vars,
})
if err != nil {
t.Fatalf("err: %s\n\n%s", tc.File, err)
}
if err := core.Validate(); (err != nil) != tc.Err {
if (err != nil) != tc.Err {
t.Fatalf("err: %s\n\n%s", tc.File, err)
}
}

View File

@ -1,6 +1,9 @@
{
"builders": [{
"type": "test"
}, {
"name": "foo",
"type": "test"
}],
"provisioners": [{