Commit 91c524c7 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

More environment testing

parent 215dba3d
...@@ -3,6 +3,9 @@ all: ...@@ -3,6 +3,9 @@ all:
go get -d ./... go get -d ./...
go build -a -o bin/packer go build -a -o bin/packer
format:
go fmt ./...
test: test:
go test ./... go test ./...
......
...@@ -30,7 +30,7 @@ func NewEnvironment() *Environment { ...@@ -30,7 +30,7 @@ func NewEnvironment() *Environment {
env := &Environment{} env := &Environment{}
env.command = make(map[string]Command) env.command = make(map[string]Command)
env.command["version"] = new(versionCommand) env.command["version"] = new(versionCommand)
env.ui = &ReaderWriterUi{ os.Stdin, os.Stdout } env.ui = &ReaderWriterUi{os.Stdin, os.Stdout}
return env return env
} }
......
package packer 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")
}
...@@ -2,12 +2,12 @@ package packer ...@@ -2,12 +2,12 @@ package packer
import ( import (
"bytes" "bytes"
"testing"
"cgl.tideland.biz/asserts" "cgl.tideland.biz/asserts"
"testing"
) )
// Our test Ui that just writes to bytes.Buffers. // 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) { func TestReaderWriterUi_Say(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true) assert := asserts.NewTestingAsserts(t, true)
......
...@@ -6,7 +6,7 @@ const Version = "0.1.0.dev" ...@@ -6,7 +6,7 @@ const Version = "0.1.0.dev"
type versionCommand byte type versionCommand byte
// Implement the Command interface by simply showing the version // 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) env.Ui().Say("Packer v%v\n", Version)
return 0 return 0
} }
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