terraform/internal/command/get_test.go
Sarah French 7199fbd2bb
Update use of testChdir to standard library's t.Chdir, remove testChdir function from codebase (#37334)
* Replace use of `testChdir` with `t.Chdir`

* Update tests to use temporary directories with copied content, instead of using directories in the repo directly.

* Remove stacks copy of testChdir function

This has been replaced with t.Chdir from the standard library

* Replace remaining simple usage of testChdir

* Update guidance for using `tempWorkingDir`

* Replace use of testChdir in a function reused in a single test

* Update comments to no longer recommend using testChdir

* Remove testChdir function!
2025-07-16 16:04:10 +01:00

115 lines
2.5 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package command
import (
"strings"
"testing"
"github.com/hashicorp/cli"
)
func TestGet(t *testing.T) {
wd := tempWorkingDirFixture(t, "get")
t.Chdir(wd.RootModuleDir())
ui := cli.NewMockUi()
c := &GetCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(testProvider()),
Ui: ui,
WorkingDir: wd,
},
}
args := []string{}
if code := c.Run(args); code != 0 {
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
}
output := ui.OutputWriter.String()
if !strings.Contains(output, "- foo in") {
t.Fatalf("doesn't look like get: %s", output)
}
}
func TestGet_multipleArgs(t *testing.T) {
wd := tempWorkingDir(t)
t.Chdir(wd.RootModuleDir())
ui := cli.NewMockUi()
c := &GetCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(testProvider()),
Ui: ui,
WorkingDir: wd,
},
}
args := []string{
"bad",
"bad",
}
if code := c.Run(args); code != 1 {
t.Fatalf("bad: \n%s", ui.OutputWriter.String())
}
}
func TestGet_update(t *testing.T) {
wd := tempWorkingDirFixture(t, "get")
t.Chdir(wd.RootModuleDir())
ui := cli.NewMockUi()
c := &GetCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(testProvider()),
Ui: ui,
WorkingDir: wd,
},
}
args := []string{
"-update",
}
if code := c.Run(args); code != 0 {
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
}
output := ui.OutputWriter.String()
if !strings.Contains(output, `- foo in`) {
t.Fatalf("doesn't look like get: %s", output)
}
}
func TestGet_cancel(t *testing.T) {
// This test runs `terraform get` as if SIGINT (or similar on other
// platforms) were sent to it, testing that it is interruptible.
wd := tempWorkingDirFixture(t, "init-registry-module")
t.Chdir(wd.RootModuleDir())
// Our shutdown channel is pre-closed so init will exit as soon as it
// starts a cancelable portion of the process.
shutdownCh := make(chan struct{})
close(shutdownCh)
ui := cli.NewMockUi()
c := &GetCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(testProvider()),
Ui: ui,
WorkingDir: wd,
ShutdownCh: shutdownCh,
},
}
args := []string{}
if code := c.Run(args); code == 0 {
t.Fatalf("succeeded; wanted error\n%s", ui.OutputWriter.String())
}
if got, want := ui.ErrorWriter.String(), `Module installation was canceled by an interrupt signal`; !strings.Contains(got, want) {
t.Fatalf("wrong error message\nshould contain: %s\ngot:\n%s", want, got)
}
}