github.com/rsyabuta/packer@v1.1.4-0.20180119234903-5ef0c2280f0b/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  // Reset the input Reader than add some input to it.
    20  func writeReader(ui *BasicUi, input string) {
    21  	buffer := ui.Reader.(*bytes.Buffer)
    22  	buffer.WriteString(input)
    23  }
    24  
    25  func readErrorWriter(ui *BasicUi) (result string) {
    26  	buffer := ui.ErrorWriter.(*bytes.Buffer)
    27  	result = buffer.String()
    28  	buffer.Reset()
    29  	return
    30  }
    31  
    32  func testUi() *BasicUi {
    33  	return &BasicUi{
    34  		Reader:      new(bytes.Buffer),
    35  		Writer:      new(bytes.Buffer),
    36  		ErrorWriter: new(bytes.Buffer),
    37  	}
    38  }
    39  
    40  func TestColoredUi(t *testing.T) {
    41  	bufferUi := testUi()
    42  	ui := &ColoredUi{UiColorYellow, UiColorRed, bufferUi}
    43  
    44  	if !ui.supportsColors() {
    45  		t.Skip("skipping for ui without color support")
    46  	}
    47  
    48  	ui.Say("foo")
    49  	result := readWriter(bufferUi)
    50  	if result != "\033[1;33mfoo\033[0m\n" {
    51  		t.Fatalf("invalid output: %s", result)
    52  	}
    53  
    54  	ui.Message("foo")
    55  	result = readWriter(bufferUi)
    56  	if result != "\033[0;33mfoo\033[0m\n" {
    57  		t.Fatalf("invalid output: %s", result)
    58  	}
    59  
    60  	ui.Error("foo")
    61  	result = readWriter(bufferUi)
    62  	if result != "" {
    63  		t.Fatalf("invalid output: %s", result)
    64  	}
    65  
    66  	result = readErrorWriter(bufferUi)
    67  	if result != "\033[1;31mfoo\033[0m\n" {
    68  		t.Fatalf("invalid output: %s", result)
    69  	}
    70  }
    71  
    72  func TestColoredUi_noColorEnv(t *testing.T) {
    73  	bufferUi := testUi()
    74  	ui := &ColoredUi{UiColorYellow, UiColorRed, bufferUi}
    75  
    76  	// Set the env var to get rid of the color
    77  	oldenv := os.Getenv("PACKER_NO_COLOR")
    78  	os.Setenv("PACKER_NO_COLOR", "1")
    79  	defer os.Setenv("PACKER_NO_COLOR", oldenv)
    80  
    81  	ui.Say("foo")
    82  	result := readWriter(bufferUi)
    83  	if result != "foo\n" {
    84  		t.Fatalf("invalid output: %s", result)
    85  	}
    86  
    87  	ui.Message("foo")
    88  	result = readWriter(bufferUi)
    89  	if result != "foo\n" {
    90  		t.Fatalf("invalid output: %s", result)
    91  	}
    92  
    93  	ui.Error("foo")
    94  	result = readErrorWriter(bufferUi)
    95  	if result != "foo\n" {
    96  		t.Fatalf("invalid output: %s", result)
    97  	}
    98  }
    99  
   100  func TestTargetedUI(t *testing.T) {
   101  	bufferUi := testUi()
   102  	targettedUi := &TargetedUI{
   103  		Target: "foo",
   104  		Ui:     bufferUi,
   105  	}
   106  
   107  	var actual, expected string
   108  	targettedUi.Say("foo")
   109  	actual = readWriter(bufferUi)
   110  	expected = "==> foo: foo\n"
   111  	if actual != expected {
   112  		t.Fatalf("bad: %#v", actual)
   113  	}
   114  
   115  	targettedUi.Message("foo")
   116  	actual = readWriter(bufferUi)
   117  	expected = "    foo: foo\n"
   118  	if actual != expected {
   119  		t.Fatalf("bad: %#v", actual)
   120  	}
   121  
   122  	targettedUi.Error("bar")
   123  	actual = readErrorWriter(bufferUi)
   124  	expected = "==> foo: bar\n"
   125  	if actual != expected {
   126  		t.Fatalf("bad: %#v", actual)
   127  	}
   128  
   129  	targettedUi.Say("foo\nbar")
   130  	actual = readWriter(bufferUi)
   131  	expected = "==> foo: foo\n==> foo: bar\n"
   132  	if actual != expected {
   133  		t.Fatalf("bad: %#v", actual)
   134  	}
   135  }
   136  
   137  func TestColoredUi_ImplUi(t *testing.T) {
   138  	var raw interface{}
   139  	raw = &ColoredUi{}
   140  	if _, ok := raw.(Ui); !ok {
   141  		t.Fatalf("ColoredUi must implement Ui")
   142  	}
   143  }
   144  
   145  func TestTargetedUI_ImplUi(t *testing.T) {
   146  	var raw interface{}
   147  	raw = &TargetedUI{}
   148  	if _, ok := raw.(Ui); !ok {
   149  		t.Fatalf("TargetedUI must implement Ui")
   150  	}
   151  }
   152  
   153  func TestBasicUi_ImplUi(t *testing.T) {
   154  	var raw interface{}
   155  	raw = &BasicUi{}
   156  	if _, ok := raw.(Ui); !ok {
   157  		t.Fatalf("BasicUi must implement Ui")
   158  	}
   159  }
   160  
   161  func TestBasicUi_Error(t *testing.T) {
   162  	bufferUi := testUi()
   163  
   164  	var actual, expected string
   165  	bufferUi.Error("foo")
   166  	actual = readErrorWriter(bufferUi)
   167  	expected = "foo\n"
   168  	if actual != expected {
   169  		t.Fatalf("bad: %#v", actual)
   170  	}
   171  
   172  	bufferUi.ErrorWriter = nil
   173  	bufferUi.Error("5")
   174  	actual = readWriter(bufferUi)
   175  	expected = "5\n"
   176  	if actual != expected {
   177  		t.Fatalf("bad: %#v", actual)
   178  	}
   179  }
   180  
   181  func TestBasicUi_Say(t *testing.T) {
   182  	bufferUi := testUi()
   183  
   184  	var actual, expected string
   185  
   186  	bufferUi.Say("foo")
   187  	actual = readWriter(bufferUi)
   188  	expected = "foo\n"
   189  	if actual != expected {
   190  		t.Fatalf("bad: %#v", actual)
   191  	}
   192  
   193  	bufferUi.Say("5")
   194  	actual = readWriter(bufferUi)
   195  	expected = "5\n"
   196  	if actual != expected {
   197  		t.Fatalf("bad: %#v", actual)
   198  	}
   199  }
   200  
   201  func TestBasicUi_Ask(t *testing.T) {
   202  
   203  	var actual, expected string
   204  	var err error
   205  
   206  	var testCases = []struct {
   207  		Prompt, Input, Answer string
   208  	}{
   209  		{"[c]ontinue or [a]bort", "c\n", "c"},
   210  		{"[c]ontinue or [a]bort", "c", "c"},
   211  		// Empty input shouldn't give an error
   212  		{"Name", "Joe Bloggs\n", "Joe Bloggs"},
   213  		{"Name", "Joe Bloggs", "Joe Bloggs"},
   214  		{"Name", "\n", ""},
   215  	}
   216  
   217  	for _, testCase := range testCases {
   218  		// Because of the internal bufio we can't eaily reset the input, so create a new one each time
   219  		bufferUi := testUi()
   220  		writeReader(bufferUi, testCase.Input)
   221  
   222  		actual, err = bufferUi.Ask(testCase.Prompt)
   223  		if err != nil {
   224  			t.Fatal(err)
   225  		}
   226  
   227  		if actual != testCase.Answer {
   228  			t.Fatalf("bad answer: %#v", actual)
   229  		}
   230  
   231  		actual = readWriter(bufferUi)
   232  		expected = testCase.Prompt + " "
   233  		if actual != expected {
   234  			t.Fatalf("bad prompt: %#v", actual)
   235  		}
   236  	}
   237  
   238  }
   239  
   240  func TestMachineReadableUi_ImplUi(t *testing.T) {
   241  	var raw interface{}
   242  	raw = &MachineReadableUi{}
   243  	if _, ok := raw.(Ui); !ok {
   244  		t.Fatalf("MachineReadableUi must implement Ui")
   245  	}
   246  }
   247  
   248  func TestMachineReadableUi(t *testing.T) {
   249  	var data, expected string
   250  
   251  	buf := new(bytes.Buffer)
   252  	ui := &MachineReadableUi{Writer: buf}
   253  
   254  	// No target
   255  	ui.Machine("foo", "bar", "baz")
   256  	data = strings.SplitN(buf.String(), ",", 2)[1]
   257  	expected = ",foo,bar,baz\n"
   258  	if data != expected {
   259  		t.Fatalf("bad: %s", data)
   260  	}
   261  
   262  	// Target
   263  	buf.Reset()
   264  	ui.Machine("mitchellh,foo", "bar", "baz")
   265  	data = strings.SplitN(buf.String(), ",", 2)[1]
   266  	expected = "mitchellh,foo,bar,baz\n"
   267  	if data != expected {
   268  		t.Fatalf("bad: %s", data)
   269  	}
   270  
   271  	// Commas
   272  	buf.Reset()
   273  	ui.Machine("foo", "foo,bar")
   274  	data = strings.SplitN(buf.String(), ",", 2)[1]
   275  	expected = ",foo,foo%!(PACKER_COMMA)bar\n"
   276  	if data != expected {
   277  		t.Fatalf("bad: %s", data)
   278  	}
   279  
   280  	// New lines
   281  	buf.Reset()
   282  	ui.Machine("foo", "foo\n")
   283  	data = strings.SplitN(buf.String(), ",", 2)[1]
   284  	expected = ",foo,foo\\n\n"
   285  	if data != expected {
   286  		t.Fatalf("bad: %#v", data)
   287  	}
   288  }