Basic template parsing, tests, build command progress
This commit is contained in:
parent
49256895cc
commit
298a7cdbe4
|
@ -1,8 +1,31 @@
|
|||
package packer
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
)
|
||||
|
||||
type buildCommand byte
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
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 {
|
|||
rawConfig interface{}
|
||||
}
|
||||
|
||||
func parseTemplate(data []byte) error {
|
||||
func ParseTemplate(data []byte) (t *Template, err error) {
|
||||
var rawTpl rawTemplate
|
||||
err := json.Unmarshal(data, &rawTpl)
|
||||
err = json.Unmarshal(data, &rawTpl)
|
||||
if err != nil {
|
||||
return err
|
||||
return
|
||||
}
|
||||
|
||||
return nil
|
||||
t = &Template{
|
||||
Name: rawTpl.Name,
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
|
|
@ -0,0 +1,40 @@
|
|||
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")
|
||||
}
|
Loading…
Reference in New Issue