From 91c524c7ec6397f0b21dca4677502c7dbfe7815c Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 24 Mar 2013 14:47:59 -0700 Subject: [PATCH] More environment testing --- Makefile | 3 +++ packer/environment.go | 2 +- packer/environment_test.go | 42 ++++++++++++++++++++++++++++++++++++++ packer/ui_test.go | 4 ++-- packer/version.go | 2 +- 5 files changed, 49 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index c0f378dd3..1361e2777 100644 --- a/Makefile +++ b/Makefile @@ -3,6 +3,9 @@ all: go get -d ./... go build -a -o bin/packer +format: + go fmt ./... + test: go test ./... diff --git a/packer/environment.go b/packer/environment.go index b732a83ea..c48c10381 100644 --- a/packer/environment.go +++ b/packer/environment.go @@ -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 } diff --git a/packer/environment_test.go b/packer/environment_test.go index 345d69bda..48ac7f704 100644 --- a/packer/environment_test.go +++ b/packer/environment_test.go @@ -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") +} diff --git a/packer/ui_test.go b/packer/ui_test.go index 57fa4492a..ccc0460d3 100644 --- a/packer/ui_test.go +++ b/packer/ui_test.go @@ -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) diff --git a/packer/version.go b/packer/version.go index 394834a8a..4536aad80 100644 --- a/packer/version.go +++ b/packer/version.go @@ -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 }