Exit with error if UI input scan fails (#26509)

This commit is contained in:
Bishwa Shrestha 2020-10-21 20:10:06 +02:00 committed by GitHub
parent 70c52c778c
commit c41336bc77
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 2 deletions

View File

@ -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)
}
}()
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{

View File

@ -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")
}
}