mirror of
https://blitiri.com.ar/repos/chasquid
synced 2026-03-07 02:35:57 +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:
39
internal/testlib/testlib.go
Normal file
39
internal/testlib/testlib.go
Normal file
@@ -0,0 +1,39 @@
|
||||
// Package testlib provides common test utilities.
|
||||
package testlib
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// MustTempDir creates a temporary directory, or dies trying.
|
||||
func MustTempDir(t *testing.T) string {
|
||||
dir, err := ioutil.TempDir("", "testlib_")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = os.Chdir(dir)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
t.Logf("test directory: %q", dir)
|
||||
return dir
|
||||
}
|
||||
|
||||
// RemoveIfOk removes the given directory, but only if we have not failed. We
|
||||
// want to keep the failed directories for debugging.
|
||||
func RemoveIfOk(t *testing.T, dir string) {
|
||||
// Safeguard, to make sure we only remove test directories.
|
||||
// This should help prevent accidental deletions.
|
||||
if !strings.Contains(dir, "testlib_") {
|
||||
panic("invalid/dangerous directory")
|
||||
}
|
||||
|
||||
if !t.Failed() {
|
||||
os.RemoveAll(dir)
|
||||
}
|
||||
}
|
||||
54
internal/testlib/testlib_test.go
Normal file
54
internal/testlib/testlib_test.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package testlib
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestBasic(t *testing.T) {
|
||||
dir := MustTempDir(t)
|
||||
if err := ioutil.WriteFile(dir+"/file", nil, 0660); err != nil {
|
||||
t.Fatalf("could not create file in %s: %v", dir, err)
|
||||
}
|
||||
|
||||
wd, err := os.Getwd()
|
||||
if err != nil {
|
||||
t.Fatalf("could not get working directory: %v", err)
|
||||
}
|
||||
if wd != dir {
|
||||
t.Errorf("MustTempDir did not change directory")
|
||||
t.Errorf(" expected %q, got %q", dir, wd)
|
||||
}
|
||||
|
||||
RemoveIfOk(t, dir)
|
||||
if _, err := os.Stat(dir); !os.IsNotExist(err) {
|
||||
t.Fatalf("%s existed, should have been deleted: %v", dir, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRemoveCheck(t *testing.T) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
t.Logf("recovered: %v", r)
|
||||
} else {
|
||||
t.Fatalf("check did not panic as expected")
|
||||
}
|
||||
}()
|
||||
|
||||
RemoveIfOk(t, "/tmp/something")
|
||||
}
|
||||
|
||||
func TestLeaveDirOnError(t *testing.T) {
|
||||
myt := &testing.T{}
|
||||
dir := MustTempDir(myt)
|
||||
myt.Errorf("something bad happened")
|
||||
|
||||
RemoveIfOk(myt, dir)
|
||||
if _, err := os.Stat(dir); os.IsNotExist(err) {
|
||||
t.Fatalf("%s was removed, should have been kept", dir)
|
||||
}
|
||||
|
||||
// Remove the directory for real this time.
|
||||
RemoveIfOk(t, dir)
|
||||
}
|
||||
Reference in New Issue
Block a user