More environment testing

This commit is contained in:
Mitchell Hashimoto 2013-03-24 14:47:59 -07:00
parent 215dba3d38
commit 91c524c7ec
5 changed files with 49 additions and 4 deletions

View File

@ -3,6 +3,9 @@ all:
go get -d ./...
go build -a -o bin/packer
format:
go fmt ./...
test:
go test ./...

View File

@ -30,7 +30,7 @@ func NewEnvironment() *Environment {
env := &Environment{}
env.command = make(map[string]Command)
env.command["version"] = new(versionCommand)
env.ui = &ReaderWriterUi{ os.Stdin, os.Stdout }
env.ui = &ReaderWriterUi{os.Stdin, os.Stdout}
return env
}

View File

@ -1,3 +1,45 @@
package packer
import (
"cgl.tideland.biz/asserts"
"os"
"testing"
)
func TestEnvironment_DefaultCli_Empty(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
defaultEnv := NewEnvironment()
assert.Equal(defaultEnv.Cli([]string{}), 1, "CLI with no args")
}
func TestEnvironment_DefaultCli_Version(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
defaultEnv := NewEnvironment()
// Test the basic version options
assert.Equal(defaultEnv.Cli([]string{"version"}), 0, "version should work")
assert.Equal(defaultEnv.Cli([]string{"--version"}), 0, "--version should work")
assert.Equal(defaultEnv.Cli([]string{"-v"}), 0, "-v should work")
// Test the --version and -v can appear anywhere
assert.Equal(defaultEnv.Cli([]string{"bad", "-v"}), 0, "-v should work anywhere")
assert.Equal(defaultEnv.Cli([]string{"bad", "--version"}), 0, "--version should work anywhere")
// Test that "version" can't appear anywhere
assert.Equal(defaultEnv.Cli([]string{"bad", "version"}), 1, "version should NOT work anywhere")
}
func TestEnvironment_DefaultUi(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
defaultEnv := NewEnvironment()
assert.NotNil(defaultEnv.Ui(), "default UI should not be nil")
rwUi, ok := defaultEnv.Ui().(*ReaderWriterUi)
assert.True(ok, "default UI should be ReaderWriterUi")
assert.Equal(rwUi.Writer, os.Stdout, "default UI should go to stdout")
assert.Equal(rwUi.Reader, os.Stdin, "default UI should read from stdin")
}

View File

@ -2,12 +2,12 @@ package packer
import (
"bytes"
"testing"
"cgl.tideland.biz/asserts"
"testing"
)
// Our test Ui that just writes to bytes.Buffers.
var bufferUi = &ReaderWriterUi{ new(bytes.Buffer), new(bytes.Buffer) }
var bufferUi = &ReaderWriterUi{new(bytes.Buffer), new(bytes.Buffer)}
func TestReaderWriterUi_Say(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)

View File

@ -6,7 +6,7 @@ const Version = "0.1.0.dev"
type versionCommand byte
// Implement the Command interface by simply showing the version
func (versionCommand) Run(env *Environment, args[] string) int {
func (versionCommand) Run(env *Environment, args []string) int {
env.Ui().Say("Packer v%v\n", Version)
return 0
}