From e59553ef0284158fca4c8a94e36c5fffbe52e0ae Mon Sep 17 00:00:00 2001 From: Katy Moe Date: Wed, 12 Aug 2020 21:22:39 +0100 Subject: [PATCH] test outdated version output --- command/version_test.go | 56 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/command/version_test.go b/command/version_test.go index 36bb1692c4..5ac7dcc507 100644 --- a/command/version_test.go +++ b/command/version_test.go @@ -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) @@ -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 + } +}