From 04c85920772c182e49f15635ec2d88d62175aafb Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 11 Aug 2013 23:18:14 -0700 Subject: [PATCH] packer: replace new lines with literal \n --- packer/ui.go | 1 + packer/ui_test.go | 18 +++++++++++++++--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/packer/ui.go b/packer/ui.go index 8abaffa55..8a869aab6 100644 --- a/packer/ui.go +++ b/packer/ui.go @@ -270,6 +270,7 @@ func (u *MachineReadableUi) Machine(category string, args ...string) { // Prepare the args for i, v := range args { args[i] = strings.Replace(v, ",", "%!(PACKER_COMMA)", -1) + args[i] = strings.Replace(args[i], "\n", "\\n", -1) } argsString := strings.Join(args, ",") diff --git a/packer/ui_test.go b/packer/ui_test.go index a9ce5dc26..87091f189 100644 --- a/packer/ui_test.go +++ b/packer/ui_test.go @@ -121,28 +121,40 @@ func TestMachineReadableUi(t *testing.T) { buf := new(bytes.Buffer) ui := &MachineReadableUi{Writer: buf} + // No target ui.Machine("foo", "bar", "baz") data = strings.SplitN(buf.String(), ",", 2)[1] - expected = ",foo,bar,baz" + expected = ",foo,bar,baz\n" if data != expected { t.Fatalf("bad: %s", data) } + // Target buf.Reset() ui.Machine("mitchellh,foo", "bar", "baz") data = strings.SplitN(buf.String(), ",", 2)[1] - expected = "mitchellh,foo,bar,baz" + expected = "mitchellh,foo,bar,baz\n" if data != expected { t.Fatalf("bad: %s", data) } + // Commas buf.Reset() ui.Machine("foo", "foo,bar") data = strings.SplitN(buf.String(), ",", 2)[1] - expected = ",foo,foo%!(PACKER_COMMA)bar" + expected = ",foo,foo%!(PACKER_COMMA)bar\n" if data != expected { t.Fatalf("bad: %s", data) } + + // New lines + buf.Reset() + ui.Machine("foo", "foo\n") + data = strings.SplitN(buf.String(), ",", 2)[1] + expected = ",foo,foo\\n\n" + if data != expected { + t.Fatalf("bad: %#v", data) + } } // This reads the output from the bytes.Buffer in our test object