github.com/kevinklinger/open_terraform@v1.3.6/noninternal/command/views/refresh_test.go (about)

     1  package views
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  
     7  	"github.com/kevinklinger/open_terraform/noninternal/command/arguments"
     8  	"github.com/kevinklinger/open_terraform/noninternal/lang/marks"
     9  	"github.com/kevinklinger/open_terraform/noninternal/states"
    10  	"github.com/kevinklinger/open_terraform/noninternal/terminal"
    11  	"github.com/zclconf/go-cty/cty"
    12  )
    13  
    14  // Ensure that the correct view type and in-automation settings propagate to the
    15  // Operation view.
    16  func TestRefreshHuman_operation(t *testing.T) {
    17  	streams, done := terminal.StreamsForTesting(t)
    18  	defer done(t)
    19  	v := NewRefresh(arguments.ViewHuman, NewView(streams).SetRunningInAutomation(true)).Operation()
    20  	if hv, ok := v.(*OperationHuman); !ok {
    21  		t.Fatalf("unexpected return type %t", v)
    22  	} else if hv.inAutomation != true {
    23  		t.Fatalf("unexpected inAutomation value on Operation view")
    24  	}
    25  }
    26  
    27  // Verify that Hooks includes a UI hook
    28  func TestRefreshHuman_hooks(t *testing.T) {
    29  	streams, done := terminal.StreamsForTesting(t)
    30  	defer done(t)
    31  	v := NewRefresh(arguments.ViewHuman, NewView(streams).SetRunningInAutomation(true))
    32  	hooks := v.Hooks()
    33  
    34  	var uiHook *UiHook
    35  	for _, hook := range hooks {
    36  		if ch, ok := hook.(*UiHook); ok {
    37  			uiHook = ch
    38  		}
    39  	}
    40  	if uiHook == nil {
    41  		t.Fatalf("expected Hooks to include a UiHook: %#v", hooks)
    42  	}
    43  }
    44  
    45  // Basic test coverage of Outputs, since most of its functionality is tested
    46  // elsewhere.
    47  func TestRefreshHuman_outputs(t *testing.T) {
    48  	streams, done := terminal.StreamsForTesting(t)
    49  	v := NewRefresh(arguments.ViewHuman, NewView(streams))
    50  
    51  	v.Outputs(map[string]*states.OutputValue{
    52  		"foo": {Value: cty.StringVal("secret")},
    53  	})
    54  
    55  	got := done(t).Stdout()
    56  	for _, want := range []string{"Outputs:", `foo = "secret"`} {
    57  		if !strings.Contains(got, want) {
    58  			t.Errorf("wrong result\ngot:  %q\nwant: %q", got, want)
    59  		}
    60  	}
    61  }
    62  
    63  // Outputs should do nothing if there are no outputs to render.
    64  func TestRefreshHuman_outputsEmpty(t *testing.T) {
    65  	streams, done := terminal.StreamsForTesting(t)
    66  	v := NewRefresh(arguments.ViewHuman, NewView(streams))
    67  
    68  	v.Outputs(map[string]*states.OutputValue{})
    69  
    70  	got := done(t).Stdout()
    71  	if got != "" {
    72  		t.Errorf("output should be empty, but got: %q", got)
    73  	}
    74  }
    75  
    76  // Basic test coverage of Outputs, since most of its functionality is tested
    77  // elsewhere.
    78  func TestRefreshJSON_outputs(t *testing.T) {
    79  	streams, done := terminal.StreamsForTesting(t)
    80  	v := NewRefresh(arguments.ViewJSON, NewView(streams))
    81  
    82  	v.Outputs(map[string]*states.OutputValue{
    83  		"boop_count": {Value: cty.NumberIntVal(92)},
    84  		"password":   {Value: cty.StringVal("horse-battery").Mark(marks.Sensitive), Sensitive: true},
    85  	})
    86  
    87  	want := []map[string]interface{}{
    88  		{
    89  			"@level":   "info",
    90  			"@message": "Outputs: 2",
    91  			"@module":  "terraform.ui",
    92  			"type":     "outputs",
    93  			"outputs": map[string]interface{}{
    94  				"boop_count": map[string]interface{}{
    95  					"sensitive": false,
    96  					"value":     float64(92),
    97  					"type":      "number",
    98  				},
    99  				"password": map[string]interface{}{
   100  					"sensitive": true,
   101  					"value":     "horse-battery",
   102  					"type":      "string",
   103  				},
   104  			},
   105  		},
   106  	}
   107  	testJSONViewOutputEquals(t, done(t).Stdout(), want)
   108  }