github.com/terramate-io/tf@v0.0.0-20230830114523-fce866b4dfcd/command/version_test.go (about) 1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: MPL-2.0 3 4 package command 5 6 import ( 7 "strings" 8 "testing" 9 10 "github.com/google/go-cmp/cmp" 11 "github.com/mitchellh/cli" 12 "github.com/terramate-io/tf/addrs" 13 "github.com/terramate-io/tf/depsfile" 14 "github.com/terramate-io/tf/getproviders" 15 ) 16 17 func TestVersionCommand_implements(t *testing.T) { 18 var _ cli.Command = &VersionCommand{} 19 } 20 21 func TestVersion(t *testing.T) { 22 td := t.TempDir() 23 defer testChdir(t, td)() 24 25 // We'll create a fixed dependency lock file in our working directory 26 // so we can verify that the version command shows the information 27 // from it. 28 locks := depsfile.NewLocks() 29 locks.SetProvider( 30 addrs.NewDefaultProvider("test2"), 31 getproviders.MustParseVersion("1.2.3"), 32 nil, 33 nil, 34 ) 35 locks.SetProvider( 36 addrs.NewDefaultProvider("test1"), 37 getproviders.MustParseVersion("7.8.9-beta.2"), 38 nil, 39 nil, 40 ) 41 42 ui := cli.NewMockUi() 43 c := &VersionCommand{ 44 Meta: Meta{ 45 Ui: ui, 46 }, 47 Version: "4.5.6", 48 VersionPrerelease: "foo", 49 Platform: getproviders.Platform{OS: "aros", Arch: "riscv64"}, 50 } 51 if err := c.replaceLockedDependencies(locks); err != nil { 52 t.Fatal(err) 53 } 54 if code := c.Run([]string{}); code != 0 { 55 t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) 56 } 57 58 actual := strings.TrimSpace(ui.OutputWriter.String()) 59 expected := "Terraform v4.5.6-foo\non aros_riscv64\n+ provider registry.terraform.io/hashicorp/test1 v7.8.9-beta.2\n+ provider registry.terraform.io/hashicorp/test2 v1.2.3" 60 if actual != expected { 61 t.Fatalf("wrong output\ngot:\n%s\nwant:\n%s", actual, expected) 62 } 63 64 } 65 66 func TestVersion_flags(t *testing.T) { 67 ui := new(cli.MockUi) 68 m := Meta{ 69 Ui: ui, 70 } 71 72 // `terraform version` 73 c := &VersionCommand{ 74 Meta: m, 75 Version: "4.5.6", 76 VersionPrerelease: "foo", 77 Platform: getproviders.Platform{OS: "aros", Arch: "riscv64"}, 78 } 79 80 if code := c.Run([]string{"-v", "-version"}); code != 0 { 81 t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) 82 } 83 84 actual := strings.TrimSpace(ui.OutputWriter.String()) 85 expected := "Terraform v4.5.6-foo\non aros_riscv64" 86 if actual != expected { 87 t.Fatalf("wrong output\ngot: %#v\nwant: %#v", actual, expected) 88 } 89 } 90 91 func TestVersion_outdated(t *testing.T) { 92 ui := new(cli.MockUi) 93 m := Meta{ 94 Ui: ui, 95 } 96 97 c := &VersionCommand{ 98 Meta: m, 99 Version: "4.5.6", 100 CheckFunc: mockVersionCheckFunc(true, "4.5.7"), 101 Platform: getproviders.Platform{OS: "aros", Arch: "riscv64"}, 102 } 103 104 if code := c.Run([]string{}); code != 0 { 105 t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) 106 } 107 108 actual := strings.TrimSpace(ui.OutputWriter.String()) 109 expected := "Terraform v4.5.6\non aros_riscv64\n\nYour version of Terraform is out of date! The latest version\nis 4.5.7. You can update by downloading from https://www.terraform.io/downloads.html" 110 if actual != expected { 111 t.Fatalf("wrong output\ngot: %#v\nwant: %#v", actual, expected) 112 } 113 } 114 115 func TestVersion_json(t *testing.T) { 116 td := t.TempDir() 117 defer testChdir(t, td)() 118 119 ui := cli.NewMockUi() 120 meta := Meta{ 121 Ui: ui, 122 } 123 124 // `terraform version -json` without prerelease 125 c := &VersionCommand{ 126 Meta: meta, 127 Version: "4.5.6", 128 Platform: getproviders.Platform{OS: "aros", Arch: "riscv64"}, 129 } 130 if code := c.Run([]string{"-json"}); code != 0 { 131 t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) 132 } 133 134 actual := strings.TrimSpace(ui.OutputWriter.String()) 135 expected := strings.TrimSpace(` 136 { 137 "terraform_version": "4.5.6", 138 "platform": "aros_riscv64", 139 "provider_selections": {}, 140 "terraform_outdated": false 141 } 142 `) 143 if diff := cmp.Diff(expected, actual); diff != "" { 144 t.Fatalf("wrong output\n%s", diff) 145 } 146 147 // flush the output from the mock ui 148 ui.OutputWriter.Reset() 149 150 // Now we'll create a fixed dependency lock file in our working directory 151 // so we can verify that the version command shows the information 152 // from it. 153 locks := depsfile.NewLocks() 154 locks.SetProvider( 155 addrs.NewDefaultProvider("test2"), 156 getproviders.MustParseVersion("1.2.3"), 157 nil, 158 nil, 159 ) 160 locks.SetProvider( 161 addrs.NewDefaultProvider("test1"), 162 getproviders.MustParseVersion("7.8.9-beta.2"), 163 nil, 164 nil, 165 ) 166 167 // `terraform version -json` with prerelease and provider dependencies 168 c = &VersionCommand{ 169 Meta: meta, 170 Version: "4.5.6", 171 VersionPrerelease: "foo", 172 Platform: getproviders.Platform{OS: "aros", Arch: "riscv64"}, 173 } 174 if err := c.replaceLockedDependencies(locks); err != nil { 175 t.Fatal(err) 176 } 177 if code := c.Run([]string{"-json"}); code != 0 { 178 t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) 179 } 180 181 actual = strings.TrimSpace(ui.OutputWriter.String()) 182 expected = strings.TrimSpace(` 183 { 184 "terraform_version": "4.5.6-foo", 185 "platform": "aros_riscv64", 186 "provider_selections": { 187 "registry.terraform.io/hashicorp/test1": "7.8.9-beta.2", 188 "registry.terraform.io/hashicorp/test2": "1.2.3" 189 }, 190 "terraform_outdated": false 191 } 192 `) 193 if diff := cmp.Diff(expected, actual); diff != "" { 194 t.Fatalf("wrong output\n%s", diff) 195 } 196 197 } 198 199 func TestVersion_jsonoutdated(t *testing.T) { 200 ui := new(cli.MockUi) 201 m := Meta{ 202 Ui: ui, 203 } 204 205 c := &VersionCommand{ 206 Meta: m, 207 Version: "4.5.6", 208 CheckFunc: mockVersionCheckFunc(true, "4.5.7"), 209 Platform: getproviders.Platform{OS: "aros", Arch: "riscv64"}, 210 } 211 212 if code := c.Run([]string{"-json"}); code != 0 { 213 t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) 214 } 215 216 actual := strings.TrimSpace(ui.OutputWriter.String()) 217 expected := "{\n \"terraform_version\": \"4.5.6\",\n \"platform\": \"aros_riscv64\",\n \"provider_selections\": {},\n \"terraform_outdated\": true\n}" 218 if actual != expected { 219 t.Fatalf("wrong output\ngot: %#v\nwant: %#v", actual, expected) 220 } 221 } 222 223 func mockVersionCheckFunc(outdated bool, latest string) VersionCheckFunc { 224 return func() (VersionCheckInfo, error) { 225 return VersionCheckInfo{ 226 Outdated: outdated, 227 Latest: latest, 228 // Alerts is not used by version command 229 }, nil 230 } 231 }