|
|
|
|
@ -3,6 +3,7 @@ package packer
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"cgl.tideland.biz/asserts"
|
|
|
|
|
"fmt"
|
|
|
|
|
"os"
|
|
|
|
|
"strings"
|
|
|
|
|
"testing"
|
|
|
|
|
@ -58,10 +59,12 @@ func TestEnvironment_Builder(t *testing.T) {
|
|
|
|
|
builders["foo"] = builder
|
|
|
|
|
|
|
|
|
|
config := DefaultEnvironmentConfig()
|
|
|
|
|
config.BuilderFunc = func(n string) Builder { return builders[n] }
|
|
|
|
|
config.BuilderFunc = func(n string) (Builder, error) { return builders[n], nil }
|
|
|
|
|
|
|
|
|
|
env, _ := NewEnvironment(config)
|
|
|
|
|
assert.Equal(env.Builder("foo"), builder, "should return correct builder")
|
|
|
|
|
returnedBuilder, err := env.Builder("foo")
|
|
|
|
|
assert.Nil(err, "should be no error")
|
|
|
|
|
assert.Equal(returnedBuilder, builder, "should return correct builder")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestEnvironment_Cli_CallsRun(t *testing.T) {
|
|
|
|
|
@ -73,10 +76,12 @@ func TestEnvironment_Cli_CallsRun(t *testing.T) {
|
|
|
|
|
|
|
|
|
|
config := &EnvironmentConfig{}
|
|
|
|
|
config.Commands = []string{"foo"}
|
|
|
|
|
config.CommandFunc = func(n string) Command { return commands[n] }
|
|
|
|
|
config.CommandFunc = func(n string) (Command, error) { return commands[n], nil }
|
|
|
|
|
|
|
|
|
|
env, _ := NewEnvironment(config)
|
|
|
|
|
assert.Equal(env.Cli([]string{"foo", "bar", "baz"}), 0, "runs foo command")
|
|
|
|
|
exitCode, err := env.Cli([]string{"foo", "bar", "baz"})
|
|
|
|
|
assert.Nil(err, "should be no error")
|
|
|
|
|
assert.Equal(exitCode, 0, "runs foo command")
|
|
|
|
|
assert.True(command.runCalled, "run should've been called")
|
|
|
|
|
assert.Equal(command.runEnv, env, "should've ran with env")
|
|
|
|
|
assert.Equal(command.runArgs, []string{"bar", "baz"}, "should have right args")
|
|
|
|
|
@ -87,7 +92,8 @@ func TestEnvironment_DefaultCli_Empty(t *testing.T) {
|
|
|
|
|
|
|
|
|
|
defaultEnv := testEnvironment()
|
|
|
|
|
|
|
|
|
|
assert.Equal(defaultEnv.Cli([]string{}), 1, "CLI with no args")
|
|
|
|
|
exitCode, _ := defaultEnv.Cli([]string{})
|
|
|
|
|
assert.Equal(exitCode, 1, "CLI with no args")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestEnvironment_DefaultCli_Help(t *testing.T) {
|
|
|
|
|
@ -104,11 +110,13 @@ func TestEnvironment_DefaultCli_Help(t *testing.T) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Test "--help"
|
|
|
|
|
assert.Equal(defaultEnv.Cli([]string{"--help"}), 1, "--help should print")
|
|
|
|
|
exitCode, _ := defaultEnv.Cli([]string{"--help"})
|
|
|
|
|
assert.Equal(exitCode, 1, "--help should print")
|
|
|
|
|
testOutput()
|
|
|
|
|
|
|
|
|
|
// Test "-h"
|
|
|
|
|
assert.Equal(defaultEnv.Cli([]string{"-h"}), 1, "--help should print")
|
|
|
|
|
exitCode, _ = defaultEnv.Cli([]string{"--help"})
|
|
|
|
|
assert.Equal(exitCode, 1, "--help should print")
|
|
|
|
|
testOutput()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -117,17 +125,20 @@ func TestEnvironment_DefaultCli_Version(t *testing.T) {
|
|
|
|
|
|
|
|
|
|
defaultEnv := testEnvironment()
|
|
|
|
|
|
|
|
|
|
// 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")
|
|
|
|
|
versionCommands := []string{"version", "--version", "-v"}
|
|
|
|
|
for _, command := range versionCommands {
|
|
|
|
|
exitCode, _ := defaultEnv.Cli([]string{command})
|
|
|
|
|
assert.Equal(exitCode, 0, fmt.Sprintf("%s should work", command))
|
|
|
|
|
|
|
|
|
|
// 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 the --version and -v can appear anywhere
|
|
|
|
|
exitCode, _ = defaultEnv.Cli([]string{"bad", command})
|
|
|
|
|
|
|
|
|
|
// Test that "version" can't appear anywhere
|
|
|
|
|
assert.Equal(defaultEnv.Cli([]string{"bad", "version"}), 1, "version should NOT work anywhere")
|
|
|
|
|
if command != "version" {
|
|
|
|
|
assert.Equal(exitCode, 0, fmt.Sprintf("%s should work anywhere", command))
|
|
|
|
|
} else {
|
|
|
|
|
assert.Equal(exitCode, 1, fmt.Sprintf("%s should NOT work anywhere", command))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestEnvironment_SettingUi(t *testing.T) {
|
|
|
|
|
|