Clean up temp files more thoroughly after testing. (#7815)

Our test cases spew a lot of files and directories around $TMPDIR.  Make more
thorough use of the testing package's TempDir methods to ensure these are
cleaned up.

In a few cases, this required plumbing test contexts through existing helper
code. In a couple places an explicit path was required, to work around cases
where we do global setup during a TestMain function. Those cases probably
deserve more thorough cleansing (preferably with fire), but for now I have just
worked around it to keep focused on the cleanup.
This commit is contained in:
M. J. Fromberger
2022-02-14 06:32:07 -08:00
committed by GitHub
parent 824960c565
commit 7e09c2ef43
39 changed files with 151 additions and 215 deletions

View File

@@ -60,13 +60,6 @@ func TestSetupEnv(t *testing.T) {
}
}
func tempDir(t *testing.T) string {
t.Helper()
cdir, err := os.MkdirTemp("", "test-cli")
require.NoError(t, err)
return cdir
}
// writeConfigVals writes a toml file with the given values.
// It returns an error if writing was impossible.
func writeConfigVals(dir string, vals map[string]string) error {
@@ -86,7 +79,7 @@ func TestSetupConfig(t *testing.T) {
// we pre-create two config files we can refer to in the rest of
// the test cases.
cval1 := "fubble"
conf1 := tempDir(t)
conf1 := t.TempDir()
err := writeConfigVals(conf1, map[string]string{"boo": cval1})
require.NoError(t, err)
@@ -147,11 +140,11 @@ func TestSetupUnmarshal(t *testing.T) {
// we pre-create two config files we can refer to in the rest of
// the test cases.
cval1, cval2 := "someone", "else"
conf1 := tempDir(t)
conf1 := t.TempDir()
err := writeConfigVals(conf1, map[string]string{"name": cval1})
require.NoError(t, err)
// even with some ignored fields, should be no problem
conf2 := tempDir(t)
conf2 := t.TempDir()
err = writeConfigVals(conf2, map[string]string{"name": cval2, "foo": "bar"})
require.NoError(t, err)

View File

@@ -12,7 +12,7 @@ import (
)
func TestCopyFile(t *testing.T) {
tmpfile, err := os.CreateTemp("", "example")
tmpfile, err := os.CreateTemp(t.TempDir(), "example")
if err != nil {
t.Fatal(err)
}
@@ -40,12 +40,10 @@ func TestCopyFile(t *testing.T) {
}
func TestEnsureDir(t *testing.T) {
tmp, err := os.MkdirTemp("", "ensure-dir")
require.NoError(t, err)
defer os.RemoveAll(tmp)
tmp := t.TempDir()
// Should be possible to create a new directory.
err = tmos.EnsureDir(filepath.Join(tmp, "dir"), 0755)
err := tmos.EnsureDir(filepath.Join(tmp, "dir"), 0755)
require.NoError(t, err)
require.DirExists(t, filepath.Join(tmp, "dir"))
@@ -76,11 +74,7 @@ func TestEnsureDir(t *testing.T) {
// the origin is positively a non-directory and that it is ready for copying.
// See https://github.com/tendermint/tendermint/issues/6427
func TestTrickedTruncation(t *testing.T) {
tmpDir, err := os.MkdirTemp(os.TempDir(), "pwn_truncate")
if err != nil {
t.Fatal(err)
}
defer os.Remove(tmpDir)
tmpDir := t.TempDir()
originalWALPath := filepath.Join(tmpDir, "wal")
originalWALContent := []byte("I AM BECOME DEATH, DESTROYER OF ALL WORLDS!")