github.com/graywolf-at-work-2/terraform-vendor@v1.4.5/internal/command/ui_input_test.go (about)

     1  package command
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"fmt"
     7  	"io"
     8  	"sync/atomic"
     9  	"testing"
    10  	"time"
    11  
    12  	"github.com/hashicorp/terraform/internal/terraform"
    13  )
    14  
    15  func TestUIInput_impl(t *testing.T) {
    16  	var _ terraform.UIInput = new(UIInput)
    17  }
    18  
    19  func TestUIInputInput(t *testing.T) {
    20  	i := &UIInput{
    21  		Reader: bytes.NewBufferString("foo\n"),
    22  		Writer: bytes.NewBuffer(nil),
    23  	}
    24  
    25  	v, err := i.Input(context.Background(), &terraform.InputOpts{})
    26  	if err != nil {
    27  		t.Fatalf("unexpected error: %v", err)
    28  	}
    29  
    30  	if v != "foo" {
    31  		t.Fatalf("unexpected input: %s", v)
    32  	}
    33  }
    34  
    35  func TestUIInputInput_canceled(t *testing.T) {
    36  	r, w := io.Pipe()
    37  	i := &UIInput{
    38  		Reader: r,
    39  		Writer: bytes.NewBuffer(nil),
    40  	}
    41  
    42  	// Make a context that can be canceled.
    43  	ctx, cancel := context.WithCancel(context.Background())
    44  
    45  	go func() {
    46  		// Cancel the context after 2 seconds.
    47  		time.Sleep(2 * time.Second)
    48  		cancel()
    49  	}()
    50  
    51  	// Get input until the context is canceled.
    52  	v, err := i.Input(ctx, &terraform.InputOpts{})
    53  	if err != context.Canceled {
    54  		t.Fatalf("expected a context.Canceled error, got: %v", err)
    55  	}
    56  
    57  	// As the context was canceled v should be empty.
    58  	if v != "" {
    59  		t.Fatalf("unexpected input: %s", v)
    60  	}
    61  
    62  	// As the context was canceled we should still be listening.
    63  	listening := atomic.LoadInt32(&i.listening)
    64  	if listening != 1 {
    65  		t.Fatalf("expected listening to be 1, got: %d", listening)
    66  	}
    67  
    68  	go func() {
    69  		// Fake input is given after 1 second.
    70  		time.Sleep(time.Second)
    71  		fmt.Fprint(w, "foo\n")
    72  		w.Close()
    73  	}()
    74  
    75  	v, err = i.Input(context.Background(), &terraform.InputOpts{})
    76  	if err != nil {
    77  		t.Fatalf("unexpected error: %v", err)
    78  	}
    79  
    80  	if v != "foo" {
    81  		t.Fatalf("unexpected input: %s", v)
    82  	}
    83  }
    84  
    85  func TestUIInputInput_spaces(t *testing.T) {
    86  	i := &UIInput{
    87  		Reader: bytes.NewBufferString("foo bar\n"),
    88  		Writer: bytes.NewBuffer(nil),
    89  	}
    90  
    91  	v, err := i.Input(context.Background(), &terraform.InputOpts{})
    92  	if err != nil {
    93  		t.Fatalf("unexpected error: %v", err)
    94  	}
    95  
    96  	if v != "foo bar" {
    97  		t.Fatalf("unexpected input: %s", v)
    98  	}
    99  }
   100  
   101  func TestUIInputInput_Error(t *testing.T) {
   102  	i := &UIInput{
   103  		Reader: bytes.NewBuffer(nil),
   104  		Writer: bytes.NewBuffer(nil),
   105  	}
   106  
   107  	v, err := i.Input(context.Background(), &terraform.InputOpts{})
   108  	if err == nil {
   109  		t.Fatalf("Error is not 'nil'")
   110  	}
   111  
   112  	if err.Error() != "EOF" {
   113  		t.Fatalf("unexpected error: %v", err)
   114  	}
   115  
   116  	if v != "" {
   117  		t.Fatalf("input must be empty")
   118  	}
   119  }