Skip to content

Commit

Permalink
test: use T.TempDir to create temporary test directory
Browse files Browse the repository at this point in the history
The directory created by `T.TempDir` is automatically removed when the
test and all its subtests complete.

Reference: https://pkg.go.dev/testing#T.TempDir
Signed-off-by: Eng Zer Jun <[email protected]>
  • Loading branch information
Juneezee committed Feb 18, 2022
1 parent 7714964 commit deede3e
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 51 deletions.
11 changes: 1 addition & 10 deletions protokube/pkg/gossip/dns/hosts/hosts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,16 +112,7 @@ func TestRecoversFromBadNesting(t *testing.T) {
}

func runTest(t *testing.T, in string, expected string) {
dir, err := os.MkdirTemp("", "")
if err != nil {
t.Fatalf("error creating temp dir: %v", err)
}
defer func() {
err := os.RemoveAll(dir)
if err != nil {
t.Errorf("failed to remove temp dir %q: %v", dir, err)
}
}()
dir := t.TempDir()

p := filepath.Join(dir, "hosts")
namesToAddresses := map[string][]string{
Expand Down
12 changes: 1 addition & 11 deletions upup/pkg/fi/cloudup/awstasks/render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,7 @@ type renderTest struct {
}

func doRenderTests(t *testing.T, method string, cases []*renderTest) {
outdir, err := os.MkdirTemp("", "kops-render-")
if err != nil {
t.Errorf("failed to create local render directory: %s", err)
t.FailNow()
}
defer func() {
err := os.RemoveAll(outdir)
if err != nil {
t.Errorf("failed to remove temp dir %q: %v", outdir, err)
}
}()
outdir := t.TempDir()

for i, c := range cases {
var filename string
Expand Down
11 changes: 1 addition & 10 deletions upup/pkg/fi/files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,7 @@ func TestWriteFile(t *testing.T) {
// Clear the umask so an unusual umask doesn't break our test (for directory mode)
syscall.Umask(0)

tempDir, err := os.MkdirTemp("", "fitest")
if err != nil {
t.Fatalf("error creating temp dir: %v", err)
}
defer func() {
err := os.RemoveAll(tempDir)
if err != nil {
t.Errorf("failed to remove temp dir %q: %v", tempDir, err)
}
}()
tempDir := t.TempDir()

tests := []struct {
path string
Expand Down
22 changes: 2 additions & 20 deletions util/pkg/vfs/fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,7 @@ import (
)

func TestCreateFile(t *testing.T) {
TempDir, err := os.MkdirTemp("", "test")
if err != nil {
t.Fatalf("error creating temp dir: %v", err)
}
defer func() {
err := os.RemoveAll(TempDir)
if err != nil {
t.Errorf("failed to remove temp dir %q: %v", TempDir, err)
}
}()
TempDir := t.TempDir()

tests := []struct {
path string
Expand Down Expand Up @@ -70,16 +61,7 @@ func TestCreateFile(t *testing.T) {
}

func TestWriteTo(t *testing.T) {
TempDir, err := os.MkdirTemp("", "test")
if err != nil {
t.Fatalf("error creating temp dir: %v", err)
}
defer func() {
err := os.RemoveAll(TempDir)
if err != nil {
t.Errorf("failed to remove temp dir %q: %v", TempDir, err)
}
}()
TempDir := t.TempDir()

tests := []struct {
path string
Expand Down

0 comments on commit deede3e

Please sign in to comment.