2013-03-24 17:31:18 -04:00
|
|
|
package packer
|
|
|
|
|
2013-03-24 17:47:59 -04:00
|
|
|
import (
|
2013-04-15 23:26:38 -04:00
|
|
|
"bytes"
|
2013-03-24 17:47:59 -04:00
|
|
|
"cgl.tideland.biz/asserts"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
)
|
2013-03-24 17:31:18 -04:00
|
|
|
|
2013-04-15 18:03:41 -04:00
|
|
|
func testEnvironment() *Environment {
|
2013-04-15 23:26:38 -04:00
|
|
|
config := &EnvironmentConfig{}
|
2013-04-20 20:09:54 -04:00
|
|
|
config.Ui = &ReaderWriterUi{
|
2013-04-15 23:26:38 -04:00
|
|
|
new(bytes.Buffer),
|
|
|
|
new(bytes.Buffer),
|
|
|
|
}
|
|
|
|
|
|
|
|
return NewEnvironment(config)
|
2013-04-15 18:03:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestEnvironment_Cli_CallsRun(t *testing.T) {
|
|
|
|
//_ := asserts.NewTestingAsserts(t, true)
|
|
|
|
|
|
|
|
// TODO: Test that the call to `Run` is done with
|
|
|
|
// proper arguments and such.
|
|
|
|
}
|
|
|
|
|
2013-03-24 17:47:59 -04:00
|
|
|
func TestEnvironment_DefaultCli_Empty(t *testing.T) {
|
|
|
|
assert := asserts.NewTestingAsserts(t, true)
|
|
|
|
|
2013-04-15 23:26:38 -04:00
|
|
|
defaultEnv := testEnvironment()
|
2013-03-24 17:47:59 -04:00
|
|
|
|
|
|
|
assert.Equal(defaultEnv.Cli([]string{}), 1, "CLI with no args")
|
|
|
|
}
|
|
|
|
|
2013-03-24 19:35:33 -04:00
|
|
|
func TestEnvironment_DefaultCli_Help(t *testing.T) {
|
|
|
|
assert := asserts.NewTestingAsserts(t, true)
|
|
|
|
|
2013-04-15 23:26:38 -04:00
|
|
|
defaultEnv := testEnvironment()
|
2013-03-24 19:35:33 -04:00
|
|
|
|
|
|
|
// Test the basic version options
|
|
|
|
assert.Equal(defaultEnv.Cli([]string{"--help"}), 1, "--help should print")
|
|
|
|
assert.Equal(defaultEnv.Cli([]string{"-h"}), 1, "--help should print")
|
|
|
|
}
|
|
|
|
|
2013-03-24 17:47:59 -04:00
|
|
|
func TestEnvironment_DefaultCli_Version(t *testing.T) {
|
|
|
|
assert := asserts.NewTestingAsserts(t, true)
|
|
|
|
|
2013-04-15 23:26:38 -04:00
|
|
|
defaultEnv := testEnvironment()
|
2013-03-24 17:47:59 -04:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
|
2013-04-15 23:26:38 -04:00
|
|
|
defaultEnv := NewEnvironment(nil)
|
2013-03-24 17:47:59 -04:00
|
|
|
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")
|
|
|
|
}
|
2013-03-24 19:28:35 -04:00
|
|
|
|
|
|
|
func TestEnvironment_PrintHelp(t *testing.T) {
|
|
|
|
// Just call the function and verify that no panics occur
|
2013-04-15 23:26:38 -04:00
|
|
|
testEnvironment().PrintHelp()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEnvironment_SettingUi(t *testing.T) {
|
|
|
|
assert := asserts.NewTestingAsserts(t, true)
|
|
|
|
|
|
|
|
ui := &ReaderWriterUi{new(bytes.Buffer), new(bytes.Buffer)}
|
|
|
|
|
|
|
|
config := &EnvironmentConfig{}
|
2013-04-20 20:08:43 -04:00
|
|
|
config.Ui = ui
|
2013-04-15 23:26:38 -04:00
|
|
|
|
|
|
|
env := NewEnvironment(config)
|
|
|
|
|
|
|
|
assert.Equal(env.Ui(), ui, "UIs should be equal")
|
2013-03-24 19:28:35 -04:00
|
|
|
}
|