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
+3 -9
View File
@@ -25,11 +25,7 @@ func TestSIGHUP(t *testing.T) {
})
// First, create a temporary directory and move into it
dir, err := os.MkdirTemp("", "sighup_test")
require.NoError(t, err)
t.Cleanup(func() {
_ = os.RemoveAll(dir)
})
dir := t.TempDir()
require.NoError(t, os.Chdir(dir))
// Create an AutoFile in the temporary directory
@@ -48,9 +44,7 @@ func TestSIGHUP(t *testing.T) {
require.NoError(t, os.Rename(name, name+"_old"))
// Move into a different temporary directory
otherDir, err := os.MkdirTemp("", "sighup_test_other")
require.NoError(t, err)
t.Cleanup(func() { os.RemoveAll(otherDir) })
otherDir := t.TempDir()
require.NoError(t, os.Chdir(otherDir))
// Send SIGHUP to self.
@@ -112,7 +106,7 @@ func TestAutoFileSize(t *testing.T) {
defer cancel()
// First, create an AutoFile writing to a tempfile dir
f, err := os.CreateTemp("", "sighup_test")
f, err := os.CreateTemp(t.TempDir(), "sighup_test")
require.NoError(t, err)
require.NoError(t, f.Close())
+2 -5
View File
@@ -132,11 +132,8 @@ func TestRotateFile(t *testing.T) {
}
}()
dir, err := os.MkdirTemp("", "rotate_test")
require.NoError(t, err)
defer os.RemoveAll(dir)
err = os.Chdir(dir)
require.NoError(t, err)
dir := t.TempDir()
require.NoError(t, os.Chdir(dir))
require.True(t, filepath.IsAbs(g.Head.Path))
require.True(t, filepath.IsAbs(g.Dir))
+1 -1
View File
@@ -21,7 +21,7 @@ func TestWriteFileAtomic(t *testing.T) {
perm os.FileMode = 0600
)
f, err := os.CreateTemp("/tmp", "write-atomic-test-")
f, err := os.CreateTemp(t.TempDir(), "write-atomic-test-")
if err != nil {
t.Fatal(err)
}