mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-28 18:01:01 -06:00
fedd315275
This commit replaces `ioutil.TempDir` with `t.TempDir` in tests. The directory created by `t.TempDir` is automatically removed when the test and all its subtests complete. Prior to this commit, temporary directory created using `ioutil.TempDir` needs to be removed manually by calling `os.RemoveAll`, which is omitted in some tests. The error handling boilerplate e.g. defer func() { if err := os.RemoveAll(dir); err != nil { t.Fatal(err) } } is also tedious, but `t.TempDir` handles this for us nicely. Reference: https://pkg.go.dev/testing#T.TempDir Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
113 lines
2.5 KiB
Go
113 lines
2.5 KiB
Go
package command
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/mitchellh/cli"
|
|
)
|
|
|
|
func TestGet(t *testing.T) {
|
|
wd := tempWorkingDirFixture(t, "get")
|
|
defer testChdir(t, 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)
|
|
defer testChdir(t, 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")
|
|
defer testChdir(t, 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")
|
|
defer testChdir(t, 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)
|
|
}
|
|
}
|