From 590997df449b3ac219fcc9bdc8a2f5bf3026213e Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 28 May 2015 14:42:53 -0700 Subject: [PATCH] packer: automatically validate when creating a core --- command/meta.go | 5 ----- packer/core.go | 9 ++++++--- packer/core_test.go | 8 ++------ packer/test-fixtures/build-prov-skip.json | 3 +++ 4 files changed, 11 insertions(+), 14 deletions(-) diff --git a/command/meta.go b/command/meta.go index 250f94f6b..e55aebf42 100644 --- a/command/meta.go +++ b/command/meta.go @@ -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 } diff --git a/packer/core.go b/packer/core.go index 678e44e08..d4cb9fea5 100644 --- a/packer/core.go +++ b/packer/core.go @@ -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 { diff --git a/packer/core_test.go b/packer/core_test.go index 09059909b..9df7ca85c 100644 --- a/packer/core_test.go +++ b/packer/core_test.go @@ -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) } } diff --git a/packer/test-fixtures/build-prov-skip.json b/packer/test-fixtures/build-prov-skip.json index bd9fa5072..24d597183 100644 --- a/packer/test-fixtures/build-prov-skip.json +++ b/packer/test-fixtures/build-prov-skip.json @@ -1,6 +1,9 @@ { "builders": [{ "type": "test" + }, { + "name": "foo", + "type": "test" }], "provisioners": [{