github.com/graywolf-at-work-2/terraform-vendor@v1.4.5/internal/command/arguments/output_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 TestParseOutput_valid(t *testing.T) { 12 testCases := map[string]struct { 13 args []string 14 want *Output 15 }{ 16 "defaults": { 17 nil, 18 &Output{ 19 Name: "", 20 ViewType: ViewHuman, 21 StatePath: "", 22 }, 23 }, 24 "json": { 25 []string{"-json"}, 26 &Output{ 27 Name: "", 28 ViewType: ViewJSON, 29 StatePath: "", 30 }, 31 }, 32 "raw": { 33 []string{"-raw", "foo"}, 34 &Output{ 35 Name: "foo", 36 ViewType: ViewRaw, 37 StatePath: "", 38 }, 39 }, 40 "state": { 41 []string{"-state=foobar.tfstate", "-raw", "foo"}, 42 &Output{ 43 Name: "foo", 44 ViewType: ViewRaw, 45 StatePath: "foobar.tfstate", 46 }, 47 }, 48 } 49 50 for name, tc := range testCases { 51 t.Run(name, func(t *testing.T) { 52 got, diags := ParseOutput(tc.args) 53 if len(diags) > 0 { 54 t.Fatalf("unexpected diags: %v", diags) 55 } 56 if *got != *tc.want { 57 t.Fatalf("unexpected result\n got: %#v\nwant: %#v", got, tc.want) 58 } 59 }) 60 } 61 } 62 63 func TestParseOutput_invalid(t *testing.T) { 64 testCases := map[string]struct { 65 args []string 66 want *Output 67 wantDiags tfdiags.Diagnostics 68 }{ 69 "unknown flag": { 70 []string{"-boop"}, 71 &Output{ 72 Name: "", 73 ViewType: ViewHuman, 74 StatePath: "", 75 }, 76 tfdiags.Diagnostics{ 77 tfdiags.Sourceless( 78 tfdiags.Error, 79 "Failed to parse command-line flags", 80 "flag provided but not defined: -boop", 81 ), 82 }, 83 }, 84 "json and raw specified": { 85 []string{"-json", "-raw"}, 86 &Output{ 87 Name: "", 88 ViewType: ViewHuman, 89 StatePath: "", 90 }, 91 tfdiags.Diagnostics{ 92 tfdiags.Sourceless( 93 tfdiags.Error, 94 "Invalid output format", 95 "The -raw and -json options are mutually-exclusive.", 96 ), 97 }, 98 }, 99 "raw with no name": { 100 []string{"-raw"}, 101 &Output{ 102 Name: "", 103 ViewType: ViewRaw, 104 StatePath: "", 105 }, 106 tfdiags.Diagnostics{ 107 tfdiags.Sourceless( 108 tfdiags.Error, 109 "Output name required", 110 "You must give the name of a single output value when using the -raw option.", 111 ), 112 }, 113 }, 114 "too many arguments": { 115 []string{"-raw", "-state=foo.tfstate", "bar", "baz"}, 116 &Output{ 117 Name: "bar", 118 ViewType: ViewRaw, 119 StatePath: "foo.tfstate", 120 }, 121 tfdiags.Diagnostics{ 122 tfdiags.Sourceless( 123 tfdiags.Error, 124 "Unexpected argument", 125 "The output command expects exactly one argument with the name of an output variable or no arguments to show all outputs.", 126 ), 127 }, 128 }, 129 } 130 131 for name, tc := range testCases { 132 t.Run(name, func(t *testing.T) { 133 got, gotDiags := ParseOutput(tc.args) 134 if *got != *tc.want { 135 t.Fatalf("unexpected result\n got: %#v\nwant: %#v", got, tc.want) 136 } 137 if !reflect.DeepEqual(gotDiags, tc.wantDiags) { 138 t.Errorf("wrong result\ngot: %s\nwant: %s", spew.Sdump(gotDiags), spew.Sdump(tc.wantDiags)) 139 } 140 }) 141 } 142 }