github.com/opentofu/opentofu@v1.7.1/internal/command/version_test.go (about) 1 // Copyright (c) The OpenTofu Authors 2 // SPDX-License-Identifier: MPL-2.0 3 // Copyright (c) 2023 HashiCorp, Inc. 4 // SPDX-License-Identifier: MPL-2.0 5 6 package command 7 8 import ( 9 "strings" 10 "testing" 11 12 "github.com/google/go-cmp/cmp" 13 "github.com/mitchellh/cli" 14 15 "github.com/opentofu/opentofu/internal/addrs" 16 "github.com/opentofu/opentofu/internal/depsfile" 17 "github.com/opentofu/opentofu/internal/getproviders" 18 ) 19 20 func TestVersionCommand_implements(t *testing.T) { 21 var _ cli.Command = &VersionCommand{} 22 } 23 24 func TestVersion(t *testing.T) { 25 td := t.TempDir() 26 defer testChdir(t, td)() 27 28 // We'll create a fixed dependency lock file in our working directory 29 // so we can verify that the version command shows the information 30 // from it. 31 locks := depsfile.NewLocks() 32 locks.SetProvider( 33 addrs.NewDefaultProvider("test2"), 34 getproviders.MustParseVersion("1.2.3"), 35 nil, 36 nil, 37 ) 38 locks.SetProvider( 39 addrs.NewDefaultProvider("test1"), 40 getproviders.MustParseVersion("7.8.9-beta.2"), 41 nil, 42 nil, 43 ) 44 45 ui := cli.NewMockUi() 46 c := &VersionCommand{ 47 Meta: Meta{ 48 Ui: ui, 49 }, 50 Version: "4.5.6", 51 VersionPrerelease: "foo", 52 Platform: getproviders.Platform{OS: "aros", Arch: "riscv64"}, 53 } 54 if err := c.replaceLockedDependencies(locks); err != nil { 55 t.Fatal(err) 56 } 57 if code := c.Run([]string{}); code != 0 { 58 t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) 59 } 60 61 actual := strings.TrimSpace(ui.OutputWriter.String()) 62 expected := "OpenTofu v4.5.6-foo\non aros_riscv64\n+ provider registry.opentofu.org/hashicorp/test1 v7.8.9-beta.2\n+ provider registry.opentofu.org/hashicorp/test2 v1.2.3" 63 if actual != expected { 64 t.Fatalf("wrong output\ngot:\n%s\nwant:\n%s", actual, expected) 65 } 66 67 } 68 69 func TestVersion_flags(t *testing.T) { 70 ui := new(cli.MockUi) 71 m := Meta{ 72 Ui: ui, 73 } 74 75 // `tofu version` 76 c := &VersionCommand{ 77 Meta: m, 78 Version: "4.5.6", 79 VersionPrerelease: "foo", 80 Platform: getproviders.Platform{OS: "aros", Arch: "riscv64"}, 81 } 82 83 if code := c.Run([]string{"-v", "-version"}); code != 0 { 84 t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) 85 } 86 87 actual := strings.TrimSpace(ui.OutputWriter.String()) 88 expected := "OpenTofu v4.5.6-foo\non aros_riscv64" 89 if actual != expected { 90 t.Fatalf("wrong output\ngot: %#v\nwant: %#v", actual, expected) 91 } 92 } 93 94 func TestVersion_json(t *testing.T) { 95 td := t.TempDir() 96 defer testChdir(t, td)() 97 98 ui := cli.NewMockUi() 99 meta := Meta{ 100 Ui: ui, 101 } 102 103 // `tofu version -json` without prerelease 104 c := &VersionCommand{ 105 Meta: meta, 106 Version: "4.5.6", 107 Platform: getproviders.Platform{OS: "aros", Arch: "riscv64"}, 108 } 109 if code := c.Run([]string{"-json"}); code != 0 { 110 t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) 111 } 112 113 actual := strings.TrimSpace(ui.OutputWriter.String()) 114 expected := strings.TrimSpace(` 115 { 116 "terraform_version": "4.5.6", 117 "platform": "aros_riscv64", 118 "provider_selections": {} 119 } 120 `) 121 if diff := cmp.Diff(expected, actual); diff != "" { 122 t.Fatalf("wrong output\n%s", diff) 123 } 124 125 // flush the output from the mock ui 126 ui.OutputWriter.Reset() 127 128 // Now we'll create a fixed dependency lock file in our working directory 129 // so we can verify that the version command shows the information 130 // from it. 131 locks := depsfile.NewLocks() 132 locks.SetProvider( 133 addrs.NewDefaultProvider("test2"), 134 getproviders.MustParseVersion("1.2.3"), 135 nil, 136 nil, 137 ) 138 locks.SetProvider( 139 addrs.NewDefaultProvider("test1"), 140 getproviders.MustParseVersion("7.8.9-beta.2"), 141 nil, 142 nil, 143 ) 144 145 // `tofu version -json` with prerelease and provider dependencies 146 c = &VersionCommand{ 147 Meta: meta, 148 Version: "4.5.6", 149 VersionPrerelease: "foo", 150 Platform: getproviders.Platform{OS: "aros", Arch: "riscv64"}, 151 } 152 if err := c.replaceLockedDependencies(locks); err != nil { 153 t.Fatal(err) 154 } 155 if code := c.Run([]string{"-json"}); code != 0 { 156 t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) 157 } 158 159 actual = strings.TrimSpace(ui.OutputWriter.String()) 160 expected = strings.TrimSpace(` 161 { 162 "terraform_version": "4.5.6-foo", 163 "platform": "aros_riscv64", 164 "provider_selections": { 165 "registry.opentofu.org/hashicorp/test1": "7.8.9-beta.2", 166 "registry.opentofu.org/hashicorp/test2": "1.2.3" 167 } 168 } 169 `) 170 if diff := cmp.Diff(expected, actual); diff != "" { 171 t.Fatalf("wrong output\n%s", diff) 172 } 173 174 }