packer-cn/packer/ui_test.go

156 lines
3.4 KiB
Go
Raw Normal View History

2013-03-24 17:31:18 -04:00
package packer
import (
"bytes"
"cgl.tideland.biz/asserts"
2013-08-11 22:05:07 -04:00
"strings"
2013-03-24 17:47:59 -04:00
"testing"
2013-03-24 17:31:18 -04:00
)
func testUi() *BasicUi {
return &BasicUi{
Reader: new(bytes.Buffer),
Writer: new(bytes.Buffer),
2013-04-20 21:55:02 -04:00
}
}
2013-03-24 17:31:18 -04:00
2013-06-03 16:35:43 -04:00
func TestColoredUi(t *testing.T) {
bufferUi := testUi()
2013-06-12 13:41:58 -04:00
ui := &ColoredUi{UiColorYellow, UiColorRed, bufferUi}
2013-06-03 16:35:43 -04:00
ui.Say("foo")
2013-06-12 13:41:58 -04:00
result := readWriter(bufferUi)
if result != "\033[1;33;40mfoo\033[0m\n" {
t.Fatalf("invalid output: %s", result)
}
ui.Message("foo")
result = readWriter(bufferUi)
if result != "\033[0;33;40mfoo\033[0m\n" {
t.Fatalf("invalid output: %s", result)
}
ui.Error("foo")
result = readWriter(bufferUi)
if result != "\033[1;31;40mfoo\033[0m\n" {
t.Fatalf("invalid output: %s", result)
}
2013-06-03 16:35:43 -04:00
}
func TestTargettedUi(t *testing.T) {
2013-05-21 16:20:51 -04:00
assert := asserts.NewTestingAsserts(t, true)
bufferUi := testUi()
targettedUi := &TargettedUi{
Target: "foo",
Ui: bufferUi,
}
2013-05-21 16:20:51 -04:00
targettedUi.Say("foo")
assert.Equal(readWriter(bufferUi), "==> foo: foo\n", "should have prefix")
2013-05-21 16:20:51 -04:00
targettedUi.Message("foo")
assert.Equal(readWriter(bufferUi), " foo: foo\n", "should have prefix")
2013-06-03 14:30:38 -04:00
targettedUi.Error("bar")
assert.Equal(readWriter(bufferUi), "==> foo: bar\n", "should have prefix")
targettedUi.Say("foo\nbar")
assert.Equal(readWriter(bufferUi), "==> foo: foo\n==> foo: bar\n", "should multiline")
2013-05-21 16:20:51 -04:00
}
2013-06-03 16:35:43 -04:00
func TestColoredUi_ImplUi(t *testing.T) {
var raw interface{}
raw = &ColoredUi{}
if _, ok := raw.(Ui); !ok {
t.Fatalf("ColoredUi must implement Ui")
}
}
func TestTargettedUi_ImplUi(t *testing.T) {
2013-05-21 16:20:51 -04:00
var raw interface{}
raw = &TargettedUi{}
2013-05-21 16:20:51 -04:00
if _, ok := raw.(Ui); !ok {
t.Fatalf("TargettedUi must implement Ui")
2013-05-21 16:20:51 -04:00
}
}
func TestBasicUi_ImplUi(t *testing.T) {
2013-05-21 16:20:51 -04:00
var raw interface{}
raw = &BasicUi{}
2013-05-21 16:20:51 -04:00
if _, ok := raw.(Ui); !ok {
t.Fatalf("BasicUi must implement Ui")
2013-05-21 16:20:51 -04:00
}
}
func TestBasicUi_Error(t *testing.T) {
2013-05-08 18:12:48 -04:00
assert := asserts.NewTestingAsserts(t, true)
bufferUi := testUi()
bufferUi.Error("foo")
assert.Equal(readWriter(bufferUi), "foo\n", "basic output")
2013-05-08 18:12:48 -04:00
bufferUi.Error("5")
assert.Equal(readWriter(bufferUi), "5\n", "formatting")
2013-05-08 18:12:48 -04:00
}
func TestBasicUi_Say(t *testing.T) {
2013-03-24 17:31:18 -04:00
assert := asserts.NewTestingAsserts(t, true)
2013-04-20 21:55:02 -04:00
bufferUi := testUi()
2013-03-24 17:31:18 -04:00
bufferUi.Say("foo")
assert.Equal(readWriter(bufferUi), "foo\n", "basic output")
2013-03-24 17:31:18 -04:00
bufferUi.Say("5")
assert.Equal(readWriter(bufferUi), "5\n", "formatting")
2013-03-24 17:31:18 -04:00
}
2013-08-11 22:05:07 -04:00
func TestMachineReadableUi_ImplUi(t *testing.T) {
var raw interface{}
raw = &MachineReadableUi{}
if _, ok := raw.(Ui); !ok {
t.Fatalf("MachineReadableUi must implement Ui")
}
}
func TestMachineReadableUi(t *testing.T) {
var data, expected string
buf := new(bytes.Buffer)
ui := &MachineReadableUi{Writer: buf}
ui.Machine("foo", "bar", "baz")
data = strings.SplitN(buf.String(), ",", 2)[1]
expected = ",foo,bar,baz"
if data != expected {
t.Fatalf("bad: %s", data)
}
buf.Reset()
ui.Machine("mitchellh,foo", "bar", "baz")
data = strings.SplitN(buf.String(), ",", 2)[1]
expected = "mitchellh,foo,bar,baz"
if data != expected {
t.Fatalf("bad: %s", data)
}
buf.Reset()
ui.Machine("foo", "foo,bar")
data = strings.SplitN(buf.String(), ",", 2)[1]
expected = ",foo,foo%!(PACKER_COMMA)bar"
if data != expected {
t.Fatalf("bad: %s", data)
}
}
2013-03-24 17:31:18 -04:00
// This reads the output from the bytes.Buffer in our test object
// and then resets the buffer.
func readWriter(ui *BasicUi) (result string) {
2013-03-24 17:31:18 -04:00
buffer := ui.Writer.(*bytes.Buffer)
result = buffer.String()
buffer.Reset()
return
}