From ee86dc87fde85e3fe5622e2d524852dcca09fe75 Mon Sep 17 00:00:00 2001 From: Matthew Hooker Date: Tue, 20 Dec 2016 18:10:58 -0800 Subject: [PATCH] fix broken test --- .../common/step_type_boot_command.go | 30 +++++++++---------- .../common/step_type_boot_command_test.go | 10 +++++-- 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/builder/virtualbox/common/step_type_boot_command.go b/builder/virtualbox/common/step_type_boot_command.go index 0d13fffb8..aa12df00f 100644 --- a/builder/virtualbox/common/step_type_boot_command.go +++ b/builder/virtualbox/common/step_type_boot_command.go @@ -108,24 +108,24 @@ func (s *StepTypeBootCommand) Run(state multistep.StateBag) multistep.StepAction func (*StepTypeBootCommand) Cleanup(multistep.StateBag) {} // Gather scancodes to send to console efficiently. -func gathercodes(codes []string) []string { - result := make([]string, 0, len(codes)) - begin := 0 - end := 0 - for pos, code := range codes { - if strings.HasPrefix(code, "wait") { - if pos > begin { - result = append(result, strings.Join(codes[begin:end+1], " ")) - } - result = append(result, code) - begin = pos + 1 +func gathercodes(codes []string) (gathered []string) { + working := []string{} + pushWorking := func() { + if len(working) > 0 { + gathered = append(gathered, strings.Join(working, " ")) + working = []string{} } - end = pos } - if end > begin { - result = append(result, strings.Join(codes[begin:end+1], " ")) + for _, code := range codes { + if strings.HasPrefix(code, "wait") { + pushWorking() + gathered = append(gathered, code) + } else { + working = append(working, code) + } } - return result + pushWorking() + return } func scancodes(message string) []string { diff --git a/builder/virtualbox/common/step_type_boot_command_test.go b/builder/virtualbox/common/step_type_boot_command_test.go index bfb7b1729..53b33bda5 100644 --- a/builder/virtualbox/common/step_type_boot_command_test.go +++ b/builder/virtualbox/common/step_type_boot_command_test.go @@ -11,6 +11,8 @@ func TestStepTypeBootCommand_gather(t *testing.T) { {"02", "82", "03", "83"}, {"wait5", "wait1", "wait10"}, {"wait5", "02", "82", "03", "83", "wait1", "wait10"}, + {"wait1"}, + {"01"}, } expected := [][]string{ @@ -18,12 +20,14 @@ func TestStepTypeBootCommand_gather(t *testing.T) { {"02 82 03 83"}, {"wait5", "wait1", "wait10"}, {"wait5", "02 82 03 83", "wait1", "wait10"}, + {"wait1"}, + {"01"}, } for i, data := range input { - if !reflect.DeepEqual(gathercodes(data), expected[i]) { - t.Fatalf("%#v did not equal expected %#v", data, expected[i]) + gathered := gathercodes(data) + if !reflect.DeepEqual(gathered, expected[i]) { + t.Fatalf("%#v did not equal expected %#v", gathered, expected[i]) } } - }