From c41336bc776c5a654287944672e93893bc07fc28 Mon Sep 17 00:00:00 2001 From: Bishwa Shrestha Date: Wed, 21 Oct 2020 20:10:06 +0200 Subject: [PATCH] Exit with error if UI input scan fails (#26509) --- command/ui_input.go | 10 ++++++++-- command/ui_input_test.go | 20 ++++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/command/ui_input.go b/command/ui_input.go index 962fa76de..9f3410778 100644 --- a/command/ui_input.go +++ b/command/ui_input.go @@ -39,6 +39,7 @@ type UIInput struct { listening int32 result chan string + err chan string interrupted bool l sync.Mutex @@ -140,12 +141,16 @@ func (i *UIInput) Input(ctx context.Context, opts *terraform.InputOpts) (string, } if err != nil { log.Printf("[ERR] UIInput scan err: %s", err) + i.err <- string(err.Error()) + } else { + i.result <- strings.TrimRightFunc(line, unicode.IsSpace) } - - i.result <- strings.TrimRightFunc(line, unicode.IsSpace) }() select { + case err := <-i.err: + return "", errors.New(err) + case line := <-i.result: fmt.Fprint(w, "\n") @@ -174,6 +179,7 @@ func (i *UIInput) Input(ctx context.Context, opts *terraform.InputOpts) (string, func (i *UIInput) init() { i.result = make(chan string) + i.err = make(chan string) if i.Colorize == nil { i.Colorize = &colorstring.Colorize{ diff --git a/command/ui_input_test.go b/command/ui_input_test.go index 23539a132..7bda3962d 100644 --- a/command/ui_input_test.go +++ b/command/ui_input_test.go @@ -97,3 +97,23 @@ func TestUIInputInput_spaces(t *testing.T) { t.Fatalf("unexpected input: %s", v) } } + +func TestUIInputInput_Error(t *testing.T) { + i := &UIInput{ + Reader: bytes.NewBuffer(nil), + Writer: bytes.NewBuffer(nil), + } + + v, err := i.Input(context.Background(), &terraform.InputOpts{}) + if err == nil { + t.Fatalf("Error is not 'nil'") + } + + if err.Error() != "EOF" { + t.Fatalf("unexpected error: %v", err) + } + + if v != "" { + t.Fatalf("input must be empty") + } +}