github.com/graywolf-at-work-2/terraform-vendor@v1.4.5/internal/command/version_test.go (about)

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