Commit f579ff05 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

Initial work on Build

parent 05e254a2
...@@ -20,7 +20,7 @@ type Build struct { ...@@ -20,7 +20,7 @@ type Build struct {
// Run is where the actual build should take place. It takes a Build and a Ui. // Run is where the actual build should take place. It takes a Build and a Ui.
type Builder interface { type Builder interface {
Prepare(config interface{}) Prepare(config interface{})
Run(build Build, ui Ui) Run(build *Build, ui Ui)
} }
// This factory is responsible for returning Builders for the given name. // This factory is responsible for returning Builders for the given name.
...@@ -38,3 +38,14 @@ type NilBuilderFactory byte ...@@ -38,3 +38,14 @@ type NilBuilderFactory byte
func (NilBuilderFactory) CreateBuilder(name string) Builder { func (NilBuilderFactory) CreateBuilder(name string) Builder {
return nil return nil
} }
// 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.builder.Prepare(config)
}
// Runs the actual build. Prepare must be called prior to running this.
func (b *Build) Run(ui Ui) {
b.builder.Run(b, ui)
}
package packer
import (
"cgl.tideland.biz/asserts"
"testing"
)
type TestBuilder struct {
prepareCalled bool
prepareConfig interface{}
runCalled bool
runBuild *Build
runUi Ui
}
func (tb *TestBuilder) Prepare(config interface{}) {
tb.prepareCalled = true
tb.prepareConfig = config
}
func (tb *TestBuilder) Run(b *Build, ui Ui) {
tb.runCalled = true
tb.runBuild = b
tb.runUi = ui
}
func testBuild() *Build {
return &Build{
name: "test",
builder: &TestBuilder{},
}
}
func TestBuild_Prepare(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
build := testBuild()
build.Prepare(42)
builder := build.builder.(*TestBuilder)
assert.True(builder.prepareCalled, "prepare should be called")
assert.Equal(builder.prepareConfig, 42, "prepare config should be 42")
}
func TestBuild_Run(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
ui := testUi()
build := testBuild()
build.Run(ui)
builder := build.builder.(*TestBuilder)
assert.True(builder.runCalled, "run should be called")
assert.Equal(builder.runBuild, build, "run should be called with build")
assert.Equal(builder.runUi, ui, "run should be called with ui")
}
...@@ -6,12 +6,18 @@ import ( ...@@ -6,12 +6,18 @@ import (
"testing" "testing"
) )
// Our test Ui that just writes to bytes.Buffers. func testUi() *ReaderWriterUi {
var bufferUi = &ReaderWriterUi{new(bytes.Buffer), new(bytes.Buffer)} return &ReaderWriterUi{
new(bytes.Buffer),
new(bytes.Buffer),
}
}
func TestReaderWriterUi_Say(t *testing.T) { func TestReaderWriterUi_Say(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true) assert := asserts.NewTestingAsserts(t, true)
bufferUi := testUi()
bufferUi.Say("foo") bufferUi.Say("foo")
assert.Equal(readWriter(bufferUi), "foo", "basic output") assert.Equal(readWriter(bufferUi), "foo", "basic output")
......
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