testing: use t.Cleanup in testutil.TempFile

So that it has the same behaviour as TempDir.

Also remove the now unnecessary 'defer os.Remove'
This commit is contained in:
Daniel Nephin 2020-08-14 20:06:01 -04:00
parent 8d35e37b3c
commit 3e0d63a6b7
7 changed files with 20 additions and 24 deletions

View File

@ -31,7 +31,6 @@ func TestSetFilePermissions(t *testing.T) {
}
tempFile := testutil.TempFile(t, "consul")
path := tempFile.Name()
defer os.Remove(path)
// Bad UID fails
if err := setFilePermissions(path, "%", "", ""); err == nil {

View File

@ -2,7 +2,6 @@ package write
import (
"io"
"os"
"strings"
"testing"
"time"
@ -32,7 +31,6 @@ func TestConfigWrite(t *testing.T) {
c := New(ui)
f := testutil.TempFile(t, "config-write-svc-web.hcl")
defer os.Remove(f.Name())
_, err := f.WriteString(`
Kind = "service-defaults"
Name = "web"

View File

@ -1,7 +1,6 @@
package create
import (
"os"
"strings"
"testing"
@ -146,7 +145,6 @@ func TestCommand_File(t *testing.T) {
contents := `{ "SourceName": "foo", "DestinationName": "bar", "Action": "allow" }`
f := testutil.TempFile(t, "intention-create-command-file")
defer os.Remove(f.Name())
if _, err := f.WriteString(contents); err != nil {
t.Fatalf("err: %#v", err)
}

View File

@ -4,7 +4,6 @@ import (
"bytes"
"encoding/base64"
"io"
"os"
"strconv"
"strings"
"testing"
@ -176,7 +175,6 @@ func TestKVPutCommand_File(t *testing.T) {
c := New(ui)
f := testutil.TempFile(t, "kv-put-command-file")
defer os.Remove(f.Name())
if _, err := f.WriteString("bar"); err != nil {
t.Fatalf("err: %#v", err)
}

View File

@ -172,13 +172,11 @@ func testFile(t *testing.T, suffix string) *os.File {
newName := f.Name() + "." + suffix
if err := os.Rename(f.Name(), newName); err != nil {
os.Remove(f.Name())
t.Fatalf("err: %s", err)
}
f, err := os.Create(newName)
if err != nil {
os.Remove(newName)
t.Fatalf("err: %s", err)
}

View File

@ -2,7 +2,6 @@ package validate
import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
@ -22,7 +21,6 @@ func TestValidateCommand_noTabs(t *testing.T) {
func TestValidateCommand_FailOnEmptyFile(t *testing.T) {
t.Parallel()
tmpFile := testutil.TempFile(t, "consul")
defer os.RemoveAll(tmpFile.Name())
cmd := New(cli.NewMockUi())
args := []string{tmpFile.Name()}

View File

@ -31,9 +31,10 @@ func init() {
var noCleanup = strings.ToLower(os.Getenv("TEST_NOCLEANUP")) == "true"
// TempDir creates a temporary directory within tmpdir
// with the name 'testname-name'. If the directory cannot
// be created t.Fatal is called.
// TempDir creates a temporary directory within tmpdir with the name 'testname-name'.
// If the directory cannot be created t.Fatal is called.
// The directory will be removed when the test ends. Set TEST_NOCLEANUP env var
// to prevent the directory from being removed.
func TempDir(t *testing.T, name string) string {
if t == nil {
panic("argument t must be non-nil")
@ -54,22 +55,28 @@ func TempDir(t *testing.T, name string) string {
return d
}
// TempFile creates a temporary file within tmpdir
// with the name 'testname-name'. If the file cannot
// be created t.Fatal is called. If a temporary directory
// has been created before consider storing the file
// inside this directory to avoid double cleanup.
// TempFile creates a temporary file within tmpdir with the name 'testname-name'.
// If the file cannot be created t.Fatal is called. If a temporary directory
// has been created before consider storing the file inside this directory to
// avoid double cleanup.
// The file will be removed when the test ends. Set TEST_NOCLEANUP env var
// to prevent the file from being removed.
func TempFile(t *testing.T, name string) *os.File {
if t != nil && t.Name() != "" {
name = t.Name() + "-" + name
if t == nil {
panic("argument t must be non-nil")
}
name = t.Name() + "-" + name
name = strings.Replace(name, "/", "_", -1)
f, err := ioutil.TempFile(tmpdir, name)
if err != nil {
if t == nil {
panic(err)
}
t.Fatalf("err: %s", err)
}
t.Cleanup(func() {
if noCleanup {
t.Logf("skipping cleanup because TEST_NOCLEANUP was enabled")
return
}
os.Remove(f.Name())
})
return f
}