Commit 8f08c5d8 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

Require Prepare to be called on Build

parent f579ff05
...@@ -7,6 +7,8 @@ package packer ...@@ -7,6 +7,8 @@ package packer
type Build struct { type Build struct {
name string name string
builder Builder builder Builder
prepareCalled bool
} }
// Implementers of Builder are responsible for actually building images // Implementers of Builder are responsible for actually building images
...@@ -42,10 +44,15 @@ func (NilBuilderFactory) CreateBuilder(name string) Builder { ...@@ -42,10 +44,15 @@ func (NilBuilderFactory) CreateBuilder(name string) Builder {
// Prepare prepares the build by doing some initialization for the builder // Prepare prepares the build by doing some initialization for the builder
// and any hooks. This _must_ be called prior to Run. // and any hooks. This _must_ be called prior to Run.
func (b *Build) Prepare(config interface{}) { func (b *Build) Prepare(config interface{}) {
b.prepareCalled = true
b.builder.Prepare(config) b.builder.Prepare(config)
} }
// Runs the actual build. Prepare must be called prior to running this. // Runs the actual build. Prepare must be called prior to running this.
func (b *Build) Run(ui Ui) { func (b *Build) Run(ui Ui) {
if !b.prepareCalled {
panic("Prepare must be called first")
}
b.builder.Run(b, ui) b.builder.Run(b, ui)
} }
...@@ -49,6 +49,7 @@ func TestBuild_Run(t *testing.T) { ...@@ -49,6 +49,7 @@ func TestBuild_Run(t *testing.T) {
ui := testUi() ui := testUi()
build := testBuild() build := testBuild()
build.Prepare(nil)
build.Run(ui) build.Run(ui)
builder := build.builder.(*TestBuilder) builder := build.builder.(*TestBuilder)
...@@ -57,3 +58,15 @@ func TestBuild_Run(t *testing.T) { ...@@ -57,3 +58,15 @@ func TestBuild_Run(t *testing.T) {
assert.Equal(builder.runBuild, build, "run should be called with build") assert.Equal(builder.runBuild, build, "run should be called with build")
assert.Equal(builder.runUi, ui, "run should be called with ui") 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())
}
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