opentofu/internal/command/workdir/plugin_dirs_test.go
Eng Zer Jun fedd315275
test: use T.TempDir to create temporary test directory (#30803)
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>
2022-04-08 17:34:16 +01:00

56 lines
1.2 KiB
Go

package workdir
import (
"path/filepath"
"testing"
"github.com/google/go-cmp/cmp"
)
func TestDirForcedPluginDirs(t *testing.T) {
tmpDir := t.TempDir()
dir := NewDir(tmpDir)
// We'll use the default convention of a data dir nested inside the
// working directory, so we don't need to override anything on "dir".
want := []string(nil)
got, err := dir.ForcedPluginDirs()
if err != nil {
t.Fatal(err)
}
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("wrong initial settings\n%s", diff)
}
fakeDir1 := filepath.Join(tmpDir, "boop1")
fakeDir2 := filepath.Join(tmpDir, "boop2")
err = dir.SetForcedPluginDirs([]string{fakeDir1, fakeDir2})
if err != nil {
t.Fatal(err)
}
want = []string{fakeDir1, fakeDir2}
got, err = dir.ForcedPluginDirs()
if err != nil {
t.Fatal(err)
}
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("wrong updated settings\n%s", diff)
}
err = dir.SetForcedPluginDirs(nil)
if err != nil {
t.Fatal(err)
}
want = nil
got, err = dir.ForcedPluginDirs()
if err != nil {
t.Fatal(err)
}
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("wrong final settings, after reverting back to defaults\n%s", diff)
}
}