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

     1  package arguments
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/davecgh/go-spew/spew"
     8  	"github.com/hashicorp/terraform/internal/tfdiags"
     9  )
    10  
    11  func TestParseShow_valid(t *testing.T) {
    12  	testCases := map[string]struct {
    13  		args []string
    14  		want *Show
    15  	}{
    16  		"defaults": {
    17  			nil,
    18  			&Show{
    19  				Path:     "",
    20  				ViewType: ViewHuman,
    21  			},
    22  		},
    23  		"json": {
    24  			[]string{"-json"},
    25  			&Show{
    26  				Path:     "",
    27  				ViewType: ViewJSON,
    28  			},
    29  		},
    30  		"path": {
    31  			[]string{"-json", "foo"},
    32  			&Show{
    33  				Path:     "foo",
    34  				ViewType: ViewJSON,
    35  			},
    36  		},
    37  	}
    38  
    39  	for name, tc := range testCases {
    40  		t.Run(name, func(t *testing.T) {
    41  			got, diags := ParseShow(tc.args)
    42  			if len(diags) > 0 {
    43  				t.Fatalf("unexpected diags: %v", diags)
    44  			}
    45  			if *got != *tc.want {
    46  				t.Fatalf("unexpected result\n got: %#v\nwant: %#v", got, tc.want)
    47  			}
    48  		})
    49  	}
    50  }
    51  
    52  func TestParseShow_invalid(t *testing.T) {
    53  	testCases := map[string]struct {
    54  		args      []string
    55  		want      *Show
    56  		wantDiags tfdiags.Diagnostics
    57  	}{
    58  		"unknown flag": {
    59  			[]string{"-boop"},
    60  			&Show{
    61  				Path:     "",
    62  				ViewType: ViewHuman,
    63  			},
    64  			tfdiags.Diagnostics{
    65  				tfdiags.Sourceless(
    66  					tfdiags.Error,
    67  					"Failed to parse command-line flags",
    68  					"flag provided but not defined: -boop",
    69  				),
    70  			},
    71  		},
    72  		"too many arguments": {
    73  			[]string{"-json", "bar", "baz"},
    74  			&Show{
    75  				Path:     "bar",
    76  				ViewType: ViewJSON,
    77  			},
    78  			tfdiags.Diagnostics{
    79  				tfdiags.Sourceless(
    80  					tfdiags.Error,
    81  					"Too many command line arguments",
    82  					"Expected at most one positional argument.",
    83  				),
    84  			},
    85  		},
    86  	}
    87  
    88  	for name, tc := range testCases {
    89  		t.Run(name, func(t *testing.T) {
    90  			got, gotDiags := ParseShow(tc.args)
    91  			if *got != *tc.want {
    92  				t.Fatalf("unexpected result\n got: %#v\nwant: %#v", got, tc.want)
    93  			}
    94  			if !reflect.DeepEqual(gotDiags, tc.wantDiags) {
    95  				t.Errorf("wrong result\ngot: %s\nwant: %s", spew.Sdump(gotDiags), spew.Sdump(tc.wantDiags))
    96  			}
    97  		})
    98  	}
    99  }