Require Prepare to be called on Build
This commit is contained in:
parent
f579ff05f2
commit
8f08c5d8a2
@ -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)
|
||||
}
|
||||
|
@ -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())
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user