mirror of https://github.com/status-im/consul.git
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:
parent
d68edcecf4
commit
16217fe9b9
|
@ -31,7 +31,6 @@ func TestSetFilePermissions(t *testing.T) {
|
||||||
}
|
}
|
||||||
tempFile := testutil.TempFile(t, "consul")
|
tempFile := testutil.TempFile(t, "consul")
|
||||||
path := tempFile.Name()
|
path := tempFile.Name()
|
||||||
defer os.Remove(path)
|
|
||||||
|
|
||||||
// Bad UID fails
|
// Bad UID fails
|
||||||
if err := setFilePermissions(path, "%", "", ""); err == nil {
|
if err := setFilePermissions(path, "%", "", ""); err == nil {
|
||||||
|
|
|
@ -2,7 +2,6 @@ package write
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
"os"
|
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
@ -32,7 +31,6 @@ func TestConfigWrite(t *testing.T) {
|
||||||
c := New(ui)
|
c := New(ui)
|
||||||
|
|
||||||
f := testutil.TempFile(t, "config-write-svc-web.hcl")
|
f := testutil.TempFile(t, "config-write-svc-web.hcl")
|
||||||
defer os.Remove(f.Name())
|
|
||||||
_, err := f.WriteString(`
|
_, err := f.WriteString(`
|
||||||
Kind = "service-defaults"
|
Kind = "service-defaults"
|
||||||
Name = "web"
|
Name = "web"
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package create
|
package create
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
@ -146,7 +145,6 @@ func TestCommand_File(t *testing.T) {
|
||||||
|
|
||||||
contents := `{ "SourceName": "foo", "DestinationName": "bar", "Action": "allow" }`
|
contents := `{ "SourceName": "foo", "DestinationName": "bar", "Action": "allow" }`
|
||||||
f := testutil.TempFile(t, "intention-create-command-file")
|
f := testutil.TempFile(t, "intention-create-command-file")
|
||||||
defer os.Remove(f.Name())
|
|
||||||
if _, err := f.WriteString(contents); err != nil {
|
if _, err := f.WriteString(contents); err != nil {
|
||||||
t.Fatalf("err: %#v", err)
|
t.Fatalf("err: %#v", err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,6 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
@ -176,7 +175,6 @@ func TestKVPutCommand_File(t *testing.T) {
|
||||||
c := New(ui)
|
c := New(ui)
|
||||||
|
|
||||||
f := testutil.TempFile(t, "kv-put-command-file")
|
f := testutil.TempFile(t, "kv-put-command-file")
|
||||||
defer os.Remove(f.Name())
|
|
||||||
if _, err := f.WriteString("bar"); err != nil {
|
if _, err := f.WriteString("bar"); err != nil {
|
||||||
t.Fatalf("err: %#v", err)
|
t.Fatalf("err: %#v", err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -172,13 +172,11 @@ func testFile(t *testing.T, suffix string) *os.File {
|
||||||
|
|
||||||
newName := f.Name() + "." + suffix
|
newName := f.Name() + "." + suffix
|
||||||
if err := os.Rename(f.Name(), newName); err != nil {
|
if err := os.Rename(f.Name(), newName); err != nil {
|
||||||
os.Remove(f.Name())
|
|
||||||
t.Fatalf("err: %s", err)
|
t.Fatalf("err: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
f, err := os.Create(newName)
|
f, err := os.Create(newName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
os.Remove(newName)
|
|
||||||
t.Fatalf("err: %s", err)
|
t.Fatalf("err: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,6 @@ package validate
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
@ -22,7 +21,6 @@ func TestValidateCommand_noTabs(t *testing.T) {
|
||||||
func TestValidateCommand_FailOnEmptyFile(t *testing.T) {
|
func TestValidateCommand_FailOnEmptyFile(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
tmpFile := testutil.TempFile(t, "consul")
|
tmpFile := testutil.TempFile(t, "consul")
|
||||||
defer os.RemoveAll(tmpFile.Name())
|
|
||||||
|
|
||||||
cmd := New(cli.NewMockUi())
|
cmd := New(cli.NewMockUi())
|
||||||
args := []string{tmpFile.Name()}
|
args := []string{tmpFile.Name()}
|
||||||
|
|
|
@ -31,9 +31,10 @@ func init() {
|
||||||
|
|
||||||
var noCleanup = strings.ToLower(os.Getenv("TEST_NOCLEANUP")) == "true"
|
var noCleanup = strings.ToLower(os.Getenv("TEST_NOCLEANUP")) == "true"
|
||||||
|
|
||||||
// TempDir creates a temporary directory within tmpdir
|
// TempDir creates a temporary directory within tmpdir with the name 'testname-name'.
|
||||||
// with the name 'testname-name'. If the directory cannot
|
// If the directory cannot be created t.Fatal is called.
|
||||||
// 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 {
|
func TempDir(t *testing.T, name string) string {
|
||||||
if t == nil {
|
if t == nil {
|
||||||
panic("argument t must be non-nil")
|
panic("argument t must be non-nil")
|
||||||
|
@ -54,22 +55,28 @@ func TempDir(t *testing.T, name string) string {
|
||||||
return d
|
return d
|
||||||
}
|
}
|
||||||
|
|
||||||
// TempFile creates a temporary file within tmpdir
|
// TempFile creates a temporary file within tmpdir with the name 'testname-name'.
|
||||||
// with the name 'testname-name'. If the file cannot
|
// If the file cannot be created t.Fatal is called. If a temporary directory
|
||||||
// be created t.Fatal is called. If a temporary directory
|
// has been created before consider storing the file inside this directory to
|
||||||
// has been created before consider storing the file
|
// avoid double cleanup.
|
||||||
// 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 {
|
func TempFile(t *testing.T, name string) *os.File {
|
||||||
if t != nil && t.Name() != "" {
|
if t == nil {
|
||||||
name = t.Name() + "-" + name
|
panic("argument t must be non-nil")
|
||||||
}
|
}
|
||||||
|
name = t.Name() + "-" + name
|
||||||
name = strings.Replace(name, "/", "_", -1)
|
name = strings.Replace(name, "/", "_", -1)
|
||||||
f, err := ioutil.TempFile(tmpdir, name)
|
f, err := ioutil.TempFile(tmpdir, name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if t == nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
t.Fatalf("err: %s", 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
|
return f
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue