github.com/terramate-io/tf@v0.0.0-20230830114523-fce866b4dfcd/command/views/refresh_test.go (about)

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