mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-25 08:21:07 -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>
95 lines
2.2 KiB
Go
95 lines
2.2 KiB
Go
package command
|
|
|
|
import (
|
|
"bytes"
|
|
"io/ioutil"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/mitchellh/cli"
|
|
)
|
|
|
|
func TestStatePull(t *testing.T) {
|
|
// Create a temporary working directory that is empty
|
|
td := t.TempDir()
|
|
testCopyDir(t, testFixturePath("state-pull-backend"), td)
|
|
defer testChdir(t, td)()
|
|
|
|
expected, err := ioutil.ReadFile("local-state.tfstate")
|
|
if err != nil {
|
|
t.Fatalf("error reading state: %v", err)
|
|
}
|
|
|
|
p := testProvider()
|
|
ui := new(cli.MockUi)
|
|
c := &StatePullCommand{
|
|
Meta: Meta{
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
Ui: ui,
|
|
},
|
|
}
|
|
|
|
args := []string{}
|
|
if code := c.Run(args); code != 0 {
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
}
|
|
|
|
actual := ui.OutputWriter.Bytes()
|
|
if bytes.Equal(actual, expected) {
|
|
t.Fatalf("expected:\n%s\n\nto include: %q", actual, expected)
|
|
}
|
|
}
|
|
|
|
func TestStatePull_noState(t *testing.T) {
|
|
testCwd(t)
|
|
|
|
p := testProvider()
|
|
ui := cli.NewMockUi()
|
|
c := &StatePullCommand{
|
|
Meta: Meta{
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
Ui: ui,
|
|
},
|
|
}
|
|
|
|
args := []string{}
|
|
if code := c.Run(args); code != 0 {
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
}
|
|
|
|
actual := ui.OutputWriter.String()
|
|
if actual != "" {
|
|
t.Fatalf("bad: %s", actual)
|
|
}
|
|
}
|
|
|
|
func TestStatePull_checkRequiredVersion(t *testing.T) {
|
|
// Create a temporary working directory that is empty
|
|
td := t.TempDir()
|
|
testCopyDir(t, testFixturePath("command-check-required-version"), td)
|
|
defer testChdir(t, td)()
|
|
|
|
p := testProvider()
|
|
ui := cli.NewMockUi()
|
|
c := &StatePullCommand{
|
|
Meta: Meta{
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
Ui: ui,
|
|
},
|
|
}
|
|
|
|
args := []string{}
|
|
if code := c.Run(args); code != 1 {
|
|
t.Fatalf("got exit status %d; want 1\nstderr:\n%s\n\nstdout:\n%s", code, ui.ErrorWriter.String(), ui.OutputWriter.String())
|
|
}
|
|
|
|
// Required version diags are correct
|
|
errStr := ui.ErrorWriter.String()
|
|
if !strings.Contains(errStr, `required_version = "~> 0.9.0"`) {
|
|
t.Fatalf("output should point to unmet version constraint, but is:\n\n%s", errStr)
|
|
}
|
|
if strings.Contains(errStr, `required_version = ">= 0.13.0"`) {
|
|
t.Fatalf("output should not point to met version constraint, but is:\n\n%s", errStr)
|
|
}
|
|
}
|