Commit 298a7cdb authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

Basic template parsing, tests, build command progress

parent 49256895
package packer package packer
import (
"io/ioutil"
)
type buildCommand byte type buildCommand byte
func (buildCommand) Run(env *Environment, args []string) int { func (buildCommand) Run(env *Environment, args []string) int {
if len(args) != 1 {
// TODO: Error message
return 1
}
// Read the file into a byte array so that we can parse the template
tplData, err := ioutil.ReadFile(args[0])
if err != nil {
// TODO: Error message
return 1
}
// Parse the template into a machine-usable format
_, err = ParseTemplate(tplData)
if err != nil {
// TODO: error message
return 1
}
return 0 return 0
} }
......
package packer
import (
"cgl.tideland.biz/asserts"
"testing"
)
func TestBuildCommand_Run_NoArgs(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
command := new(buildCommand)
result := command.Run(testEnvironment(), make([]string, 0))
assert.Equal(result, 1, "no args should error")
}
func TestBuildCommand_Run_MoreThanOneArg(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
command := new(buildCommand)
args := []string{"one", "two"}
result := command.Run(testEnvironment(), args)
assert.Equal(result, 1, "More than one arg should fail")
}
func TestBuildCommand_Run_MissingFile(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
command := new(buildCommand)
args := []string{"i-better-not-exist"}
result := command.Run(testEnvironment(), args)
assert.Equal(result, 1, "a non-existent file should error")
}
...@@ -27,12 +27,16 @@ type rawBuilderConfig struct { ...@@ -27,12 +27,16 @@ type rawBuilderConfig struct {
rawConfig interface{} rawConfig interface{}
} }
func parseTemplate(data []byte) error { func ParseTemplate(data []byte) (t *Template, err error) {
var rawTpl rawTemplate var rawTpl rawTemplate
err := json.Unmarshal(data, &rawTpl) err = json.Unmarshal(data, &rawTpl)
if err != nil { if err != nil {
return err return
} }
return nil t = &Template{
Name: rawTpl.Name,
}
return
} }
package packer
import (
"cgl.tideland.biz/asserts"
"testing"
)
func TestParseTemplate_Basic(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
data := `
{
"name": "my-image",
"builders": []
}
`
result, err := ParseTemplate([]byte(data))
assert.Nil(err, "should not error")
assert.NotNil(result, "template should not be nil")
assert.Equal(result.Name, "my-image", "name should be correct")
assert.Length(result.Builders, 0, "no builders")
}
func TestParseTemplate_Invalid(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
// Note there is an extra comma below for a purposeful
// syntax error in the JSON.
data := `
{
"name": "my-image",,
"builders": []
}
`
result, err := ParseTemplate([]byte(data))
assert.NotNil(err, "should have an error")
assert.Nil(result, "should have no result")
}
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