Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions command/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ Options:

func (c *VersionCommand) Run(args []string) int {
var outdated bool
var latest string
var versionString bytes.Buffer
args = c.Meta.process(args)
var jsonOutput bool
Expand Down Expand Up @@ -113,13 +114,7 @@ func (c *VersionCommand) Run(args []string) int {
}
if info.Outdated {
outdated = true
if !jsonOutput {
c.Ui.Output(fmt.Sprintf(
"\nYour version of Terraform is out of date! The latest version\n"+
"is %s. You can update by downloading from https://www.terraform.io/downloads.html",
info.Latest))
}

latest = info.Latest
}
}

Expand Down Expand Up @@ -159,6 +154,12 @@ func (c *VersionCommand) Run(args []string) int {
c.Ui.Output(str)
}
}
if outdated {
c.Ui.Output(fmt.Sprintf(
"\nYour version of Terraform is out of date! The latest version\n"+
"is %s. You can update by downloading from https://www.terraform.io/downloads.html",
latest))
}

}

Expand Down
56 changes: 56 additions & 0 deletions command/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,29 @@ func TestVersion_flags(t *testing.T) {
}
}

func TestVersion_outdated(t *testing.T) {
ui := new(cli.MockUi)
m := Meta{
Ui: ui,
}

c := &VersionCommand{
Meta: m,
Version: "4.5.6",
CheckFunc: mockVersionCheckFunc(true, "4.5.7"),
}

if code := c.Run([]string{}); code != 0 {
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
}

actual := strings.TrimSpace(ui.OutputWriter.String())
expected := "Terraform v4.5.6\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"
if actual != expected {
t.Fatalf("wrong output\ngot: %#v\nwant: %#v", actual, expected)
}
}

func TestVersion_json(t *testing.T) {
fixtureDir := "testdata/providers-schema/basic"
td := tempDir(t)
Expand Down Expand Up @@ -150,3 +173,36 @@ func TestVersion_json(t *testing.T) {
}

}

func TestVersion_jsonoutdated(t *testing.T) {
ui := new(cli.MockUi)
m := Meta{
Ui: ui,
}

c := &VersionCommand{
Meta: m,
Version: "4.5.6",
CheckFunc: mockVersionCheckFunc(true, "4.5.7"),
}

if code := c.Run([]string{"-json"}); code != 0 {
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
}

actual := strings.TrimSpace(ui.OutputWriter.String())
expected := "{\n \"terraform_version\": \"4.5.6\",\n \"terraform_revision\": \"\",\n \"provider_selections\": {},\n \"terraform_outdated\": true\n}"
if actual != expected {
t.Fatalf("wrong output\ngot: %#v\nwant: %#v", actual, expected)
}
}

func mockVersionCheckFunc(outdated bool, latest string) VersionCheckFunc {
return func() (VersionCheckInfo, error) {
return VersionCheckInfo{
Outdated: outdated,
Latest: latest,
// Alerts is not used by version command
}, nil
}
}