github.com/graywolf-at-work-2/terraform-vendor@v1.4.5/internal/command/arguments/refresh_test.go (about) 1 package arguments 2 3 import ( 4 "strings" 5 "testing" 6 7 "github.com/google/go-cmp/cmp" 8 "github.com/hashicorp/terraform/internal/addrs" 9 ) 10 11 func TestParseRefresh_basicValid(t *testing.T) { 12 testCases := map[string]struct { 13 args []string 14 want *Refresh 15 }{ 16 "defaults": { 17 nil, 18 &Refresh{ 19 InputEnabled: true, 20 ViewType: ViewHuman, 21 }, 22 }, 23 "input=false": { 24 []string{"-input=false"}, 25 &Refresh{ 26 InputEnabled: false, 27 ViewType: ViewHuman, 28 }, 29 }, 30 "JSON view disables input": { 31 []string{"-json"}, 32 &Refresh{ 33 InputEnabled: false, 34 ViewType: ViewJSON, 35 }, 36 }, 37 } 38 39 for name, tc := range testCases { 40 t.Run(name, func(t *testing.T) { 41 got, diags := ParseRefresh(tc.args) 42 if len(diags) > 0 { 43 t.Fatalf("unexpected diags: %v", diags) 44 } 45 // Ignore the extended arguments for simplicity 46 got.State = nil 47 got.Operation = nil 48 got.Vars = nil 49 if *got != *tc.want { 50 t.Fatalf("unexpected result\n got: %#v\nwant: %#v", got, tc.want) 51 } 52 }) 53 } 54 } 55 56 func TestParseRefresh_invalid(t *testing.T) { 57 got, diags := ParseRefresh([]string{"-frob"}) 58 if len(diags) == 0 { 59 t.Fatal("expected diags but got none") 60 } 61 if got, want := diags.Err().Error(), "flag provided but not defined"; !strings.Contains(got, want) { 62 t.Fatalf("wrong diags\n got: %s\nwant: %s", got, want) 63 } 64 if got.ViewType != ViewHuman { 65 t.Fatalf("wrong view type, got %#v, want %#v", got.ViewType, ViewHuman) 66 } 67 } 68 69 func TestParseRefresh_tooManyArguments(t *testing.T) { 70 got, diags := ParseRefresh([]string{"saved.tfplan"}) 71 if len(diags) == 0 { 72 t.Fatal("expected diags but got none") 73 } 74 if got, want := diags.Err().Error(), "Too many command line arguments"; !strings.Contains(got, want) { 75 t.Fatalf("wrong diags\n got: %s\nwant: %s", got, want) 76 } 77 if got.ViewType != ViewHuman { 78 t.Fatalf("wrong view type, got %#v, want %#v", got.ViewType, ViewHuman) 79 } 80 } 81 82 func TestParseRefresh_targets(t *testing.T) { 83 foobarbaz, _ := addrs.ParseTargetStr("foo_bar.baz") 84 boop, _ := addrs.ParseTargetStr("module.boop") 85 testCases := map[string]struct { 86 args []string 87 want []addrs.Targetable 88 wantErr string 89 }{ 90 "no targets by default": { 91 args: nil, 92 want: nil, 93 }, 94 "one target": { 95 args: []string{"-target=foo_bar.baz"}, 96 want: []addrs.Targetable{foobarbaz.Subject}, 97 }, 98 "two targets": { 99 args: []string{"-target=foo_bar.baz", "-target", "module.boop"}, 100 want: []addrs.Targetable{foobarbaz.Subject, boop.Subject}, 101 }, 102 "invalid traversal": { 103 args: []string{"-target=foo."}, 104 want: nil, 105 wantErr: "Dot must be followed by attribute name", 106 }, 107 "invalid target": { 108 args: []string{"-target=data[0].foo"}, 109 want: nil, 110 wantErr: "A data source name is required", 111 }, 112 } 113 114 for name, tc := range testCases { 115 t.Run(name, func(t *testing.T) { 116 got, diags := ParseRefresh(tc.args) 117 if len(diags) > 0 { 118 if tc.wantErr == "" { 119 t.Fatalf("unexpected diags: %v", diags) 120 } else if got := diags.Err().Error(); !strings.Contains(got, tc.wantErr) { 121 t.Fatalf("wrong diags\n got: %s\nwant: %s", got, tc.wantErr) 122 } 123 } 124 if !cmp.Equal(got.Operation.Targets, tc.want) { 125 t.Fatalf("unexpected result\n%s", cmp.Diff(got.Operation.Targets, tc.want)) 126 } 127 }) 128 } 129 } 130 131 func TestParseRefresh_vars(t *testing.T) { 132 testCases := map[string]struct { 133 args []string 134 want []FlagNameValue 135 }{ 136 "no var flags by default": { 137 args: nil, 138 want: nil, 139 }, 140 "one var": { 141 args: []string{"-var", "foo=bar"}, 142 want: []FlagNameValue{ 143 {Name: "-var", Value: "foo=bar"}, 144 }, 145 }, 146 "one var-file": { 147 args: []string{"-var-file", "cool.tfvars"}, 148 want: []FlagNameValue{ 149 {Name: "-var-file", Value: "cool.tfvars"}, 150 }, 151 }, 152 "ordering preserved": { 153 args: []string{ 154 "-var", "foo=bar", 155 "-var-file", "cool.tfvars", 156 "-var", "boop=beep", 157 }, 158 want: []FlagNameValue{ 159 {Name: "-var", Value: "foo=bar"}, 160 {Name: "-var-file", Value: "cool.tfvars"}, 161 {Name: "-var", Value: "boop=beep"}, 162 }, 163 }, 164 } 165 166 for name, tc := range testCases { 167 t.Run(name, func(t *testing.T) { 168 got, diags := ParseRefresh(tc.args) 169 if len(diags) > 0 { 170 t.Fatalf("unexpected diags: %v", diags) 171 } 172 if vars := got.Vars.All(); !cmp.Equal(vars, tc.want) { 173 t.Fatalf("unexpected result\n%s", cmp.Diff(vars, tc.want)) 174 } 175 if got, want := got.Vars.Empty(), len(tc.want) == 0; got != want { 176 t.Fatalf("expected Empty() to return %t, but was %t", want, got) 177 } 178 }) 179 } 180 }