mirror of
https://blitiri.com.ar/repos/chasquid
synced 2025-12-17 14:37:02 +00:00
test: Tidy up creation and removal of test directories
We have many places in our tests where we create temporary directories, which we later remove (most of the time). We have at least 3 helpers to do this, and various places where it's done ad-hoc (and the cleanup is not always present). To try to reduce the clutter, and make the tests more uniform and readable, this patch introduces two helpers in a new "testutil" package: one for creating and one for removing temporary directories. These new functions are safer, better tested, and make the tests more consistent. All the tests are updated to use them.
This commit is contained in:
@@ -8,24 +8,10 @@ import (
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"blitiri.com.ar/go/chasquid/internal/testlib"
|
||||
)
|
||||
|
||||
func mustTempDir(t *testing.T) string {
|
||||
dir, err := ioutil.TempDir("", "safeio_test")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = os.Chdir(dir)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
t.Logf("test directory: %q", dir)
|
||||
|
||||
return dir
|
||||
}
|
||||
|
||||
func testWriteFile(fname string, data []byte, perm os.FileMode, ops ...FileOp) error {
|
||||
err := WriteFile("file1", data, perm, ops...)
|
||||
if err != nil {
|
||||
@@ -56,7 +42,8 @@ func testWriteFile(fname string, data []byte, perm os.FileMode, ops ...FileOp) e
|
||||
}
|
||||
|
||||
func TestWriteFile(t *testing.T) {
|
||||
dir := mustTempDir(t)
|
||||
dir := testlib.MustTempDir(t)
|
||||
defer testlib.RemoveIfOk(t, dir)
|
||||
|
||||
// Write a new file.
|
||||
content := []byte("content 1")
|
||||
@@ -75,16 +62,11 @@ func TestWriteFile(t *testing.T) {
|
||||
if err := testWriteFile("file1", content, 0600); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
// Remove the test directory, but only if we have not failed. We want to
|
||||
// keep the failed structure for debugging.
|
||||
if !t.Failed() {
|
||||
os.RemoveAll(dir)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteFileWithOp(t *testing.T) {
|
||||
dir := mustTempDir(t)
|
||||
dir := testlib.MustTempDir(t)
|
||||
defer testlib.RemoveIfOk(t, dir)
|
||||
|
||||
var opFile string
|
||||
op := func(f string) error {
|
||||
@@ -103,16 +85,11 @@ func TestWriteFileWithOp(t *testing.T) {
|
||||
if !strings.Contains(opFile, "file1") {
|
||||
t.Errorf("operation called with suspicious file: %s", opFile)
|
||||
}
|
||||
|
||||
// Remove the test directory, but only if we have not failed. We want to
|
||||
// keep the failed structure for debugging.
|
||||
if !t.Failed() {
|
||||
os.RemoveAll(dir)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteFileWithFailingOp(t *testing.T) {
|
||||
dir := mustTempDir(t)
|
||||
dir := testlib.MustTempDir(t)
|
||||
defer testlib.RemoveIfOk(t, dir)
|
||||
|
||||
var opFile string
|
||||
opOK := func(f string) error {
|
||||
@@ -134,12 +111,6 @@ func TestWriteFileWithFailingOp(t *testing.T) {
|
||||
if _, err := os.Stat(opFile); err == nil {
|
||||
t.Errorf("temporary file was not removed after failure (%v)", opFile)
|
||||
}
|
||||
|
||||
// Remove the test directory, but only if we have not failed. We want to
|
||||
// keep the failed structure for debugging.
|
||||
if !t.Failed() {
|
||||
os.RemoveAll(dir)
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: We should test the possible failure scenarios for WriteFile, but it
|
||||
|
||||
Reference in New Issue
Block a user