github.com/askholme/packer@v0.7.2-0.20140924152349-70d9566a6852/packer/ui_test.go (about) 1 package packer 2 3 import ( 4 "bytes" 5 "os" 6 "strings" 7 "testing" 8 ) 9 10 // This reads the output from the bytes.Buffer in our test object 11 // and then resets the buffer. 12 func readWriter(ui *BasicUi) (result string) { 13 buffer := ui.Writer.(*bytes.Buffer) 14 result = buffer.String() 15 buffer.Reset() 16 return 17 } 18 19 func readErrorWriter(ui *BasicUi) (result string) { 20 buffer := ui.ErrorWriter.(*bytes.Buffer) 21 result = buffer.String() 22 buffer.Reset() 23 return 24 } 25 26 func testUi() *BasicUi { 27 return &BasicUi{ 28 Reader: new(bytes.Buffer), 29 Writer: new(bytes.Buffer), 30 ErrorWriter: new(bytes.Buffer), 31 } 32 } 33 34 func TestColoredUi(t *testing.T) { 35 bufferUi := testUi() 36 ui := &ColoredUi{UiColorYellow, UiColorRed, bufferUi} 37 38 ui.Say("foo") 39 result := readWriter(bufferUi) 40 if result != "\033[1;33mfoo\033[0m\n" { 41 t.Fatalf("invalid output: %s", result) 42 } 43 44 ui.Message("foo") 45 result = readWriter(bufferUi) 46 if result != "\033[0;33mfoo\033[0m\n" { 47 t.Fatalf("invalid output: %s", result) 48 } 49 50 ui.Error("foo") 51 result = readWriter(bufferUi) 52 if result != "" { 53 t.Fatalf("invalid output: %s", result) 54 } 55 56 result = readErrorWriter(bufferUi) 57 if result != "\033[1;31mfoo\033[0m\n" { 58 t.Fatalf("invalid output: %s", result) 59 } 60 } 61 62 func TestColoredUi_noColorEnv(t *testing.T) { 63 bufferUi := testUi() 64 ui := &ColoredUi{UiColorYellow, UiColorRed, bufferUi} 65 66 // Set the env var to get rid of the color 67 oldenv := os.Getenv("PACKER_NO_COLOR") 68 os.Setenv("PACKER_NO_COLOR", "1") 69 defer os.Setenv("PACKER_NO_COLOR", oldenv) 70 71 ui.Say("foo") 72 result := readWriter(bufferUi) 73 if result != "foo\n" { 74 t.Fatalf("invalid output: %s", result) 75 } 76 77 ui.Message("foo") 78 result = readWriter(bufferUi) 79 if result != "foo\n" { 80 t.Fatalf("invalid output: %s", result) 81 } 82 83 ui.Error("foo") 84 result = readErrorWriter(bufferUi) 85 if result != "foo\n" { 86 t.Fatalf("invalid output: %s", result) 87 } 88 } 89 90 func TestTargettedUi(t *testing.T) { 91 bufferUi := testUi() 92 targettedUi := &TargettedUi{ 93 Target: "foo", 94 Ui: bufferUi, 95 } 96 97 var actual, expected string 98 targettedUi.Say("foo") 99 actual = readWriter(bufferUi) 100 expected = "==> foo: foo\n" 101 if actual != expected { 102 t.Fatalf("bad: %#v", actual) 103 } 104 105 targettedUi.Message("foo") 106 actual = readWriter(bufferUi) 107 expected = " foo: foo\n" 108 if actual != expected { 109 t.Fatalf("bad: %#v", actual) 110 } 111 112 targettedUi.Error("bar") 113 actual = readErrorWriter(bufferUi) 114 expected = "==> foo: bar\n" 115 if actual != expected { 116 t.Fatalf("bad: %#v", actual) 117 } 118 119 targettedUi.Say("foo\nbar") 120 actual = readWriter(bufferUi) 121 expected = "==> foo: foo\n==> foo: bar\n" 122 if actual != expected { 123 t.Fatalf("bad: %#v", actual) 124 } 125 } 126 127 func TestColoredUi_ImplUi(t *testing.T) { 128 var raw interface{} 129 raw = &ColoredUi{} 130 if _, ok := raw.(Ui); !ok { 131 t.Fatalf("ColoredUi must implement Ui") 132 } 133 } 134 135 func TestTargettedUi_ImplUi(t *testing.T) { 136 var raw interface{} 137 raw = &TargettedUi{} 138 if _, ok := raw.(Ui); !ok { 139 t.Fatalf("TargettedUi must implement Ui") 140 } 141 } 142 143 func TestBasicUi_ImplUi(t *testing.T) { 144 var raw interface{} 145 raw = &BasicUi{} 146 if _, ok := raw.(Ui); !ok { 147 t.Fatalf("BasicUi must implement Ui") 148 } 149 } 150 151 func TestBasicUi_Error(t *testing.T) { 152 bufferUi := testUi() 153 154 var actual, expected string 155 bufferUi.Error("foo") 156 actual = readErrorWriter(bufferUi) 157 expected = "foo\n" 158 if actual != expected { 159 t.Fatalf("bad: %#v", actual) 160 } 161 162 bufferUi.ErrorWriter = nil 163 bufferUi.Error("5") 164 actual = readWriter(bufferUi) 165 expected = "5\n" 166 if actual != expected { 167 t.Fatalf("bad: %#v", actual) 168 } 169 } 170 171 func TestBasicUi_Say(t *testing.T) { 172 bufferUi := testUi() 173 174 var actual, expected string 175 176 bufferUi.Say("foo") 177 actual = readWriter(bufferUi) 178 expected = "foo\n" 179 if actual != expected { 180 t.Fatalf("bad: %#v", actual) 181 } 182 183 bufferUi.Say("5") 184 actual = readWriter(bufferUi) 185 expected = "5\n" 186 if actual != expected { 187 t.Fatalf("bad: %#v", actual) 188 } 189 } 190 191 func TestMachineReadableUi_ImplUi(t *testing.T) { 192 var raw interface{} 193 raw = &MachineReadableUi{} 194 if _, ok := raw.(Ui); !ok { 195 t.Fatalf("MachineReadableUi must implement Ui") 196 } 197 } 198 199 func TestMachineReadableUi(t *testing.T) { 200 var data, expected string 201 202 buf := new(bytes.Buffer) 203 ui := &MachineReadableUi{Writer: buf} 204 205 // No target 206 ui.Machine("foo", "bar", "baz") 207 data = strings.SplitN(buf.String(), ",", 2)[1] 208 expected = ",foo,bar,baz\n" 209 if data != expected { 210 t.Fatalf("bad: %s", data) 211 } 212 213 // Target 214 buf.Reset() 215 ui.Machine("mitchellh,foo", "bar", "baz") 216 data = strings.SplitN(buf.String(), ",", 2)[1] 217 expected = "mitchellh,foo,bar,baz\n" 218 if data != expected { 219 t.Fatalf("bad: %s", data) 220 } 221 222 // Commas 223 buf.Reset() 224 ui.Machine("foo", "foo,bar") 225 data = strings.SplitN(buf.String(), ",", 2)[1] 226 expected = ",foo,foo%!(PACKER_COMMA)bar\n" 227 if data != expected { 228 t.Fatalf("bad: %s", data) 229 } 230 231 // New lines 232 buf.Reset() 233 ui.Machine("foo", "foo\n") 234 data = strings.SplitN(buf.String(), ",", 2)[1] 235 expected = ",foo,foo\\n\n" 236 if data != expected { 237 t.Fatalf("bad: %#v", data) 238 } 239 }