From 8f08c5d8a27f772e6ed45a7613f233563ee9ae19 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 20 Apr 2013 20:03:53 -0600 Subject: [PATCH] Require Prepare to be called on Build --- packer/build.go | 7 +++++++ packer/build_test.go | 13 +++++++++++++ 2 files changed, 20 insertions(+) diff --git a/packer/build.go b/packer/build.go index 8f93d1b31..67f9b80e8 100644 --- a/packer/build.go +++ b/packer/build.go @@ -7,6 +7,8 @@ package packer type Build struct { name string builder Builder + + prepareCalled bool } // Implementers of Builder are responsible for actually building images @@ -42,10 +44,15 @@ func (NilBuilderFactory) CreateBuilder(name string) Builder { // Prepare prepares the build by doing some initialization for the builder // and any hooks. This _must_ be called prior to Run. func (b *Build) Prepare(config interface{}) { + b.prepareCalled = true b.builder.Prepare(config) } // Runs the actual build. Prepare must be called prior to running this. func (b *Build) Run(ui Ui) { + if !b.prepareCalled { + panic("Prepare must be called first") + } + b.builder.Run(b, ui) } diff --git a/packer/build_test.go b/packer/build_test.go index cfa05f7ac..7359a05f2 100644 --- a/packer/build_test.go +++ b/packer/build_test.go @@ -49,6 +49,7 @@ func TestBuild_Run(t *testing.T) { ui := testUi() build := testBuild() + build.Prepare(nil) build.Run(ui) builder := build.builder.(*TestBuilder) @@ -57,3 +58,15 @@ func TestBuild_Run(t *testing.T) { assert.Equal(builder.runBuild, build, "run should be called with build") assert.Equal(builder.runUi, ui, "run should be called with ui") } + +func TestBuild_RunBeforePrepare(t *testing.T) { + assert := asserts.NewTestingAsserts(t, true) + + defer func() { + p := recover() + assert.NotNil(p, "should panic") + assert.Equal(p.(string), "Prepare must be called first", "right panic") + }() + + testBuild().Run(testUi()) +}