all: use T.TempDir to create temporary test directory (#8280)

This commit replaces `os.MkdirTemp` with `t.TempDir` in tests. The
directory created by `t.TempDir` is automatically removed when the test
and all its subtests complete.

Prior to this commit, temporary directory created using `os.MkdirTemp`
needs to be removed manually by calling `os.RemoveAll`, which is omitted
in some tests. The error handling boilerplate e.g.
	defer func() {
		if err := os.RemoveAll(dir); err != nil {
			t.Fatal(err)
		}
	}
is also tedious, but `t.TempDir` handles this for us nicely.

Reference: https://pkg.go.dev/testing#T.TempDir
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
This commit is contained in:
Eng Zer Jun
2022-04-15 07:44:06 +04:00
committed by GitHub
parent 0537b9546f
commit bc27aa12cd
22 changed files with 91 additions and 225 deletions
+4 -3
View File
@@ -89,7 +89,7 @@ func createEmptyFileInfo(t *testing.T, name string, fs fs.Filesystem) protocol.F
// Sets up a folder and model, but makes sure the services aren't actually running.
func setupSendReceiveFolder(t testing.TB, files ...protocol.FileInfo) (*testModel, *sendReceiveFolder, context.CancelFunc) {
w, fcfg, wCancel := tmpDefaultWrapper()
w, fcfg, wCancel := tmpDefaultWrapper(t)
// Initialise model and stop immediately.
model := setupModel(t, w)
model.cancel()
@@ -972,8 +972,7 @@ func TestDeleteBehindSymlink(t *testing.T) {
defer cleanupSRFolder(f, m, wcfgCancel)
ffs := f.Filesystem(nil)
destDir := createTmpDir()
defer os.RemoveAll(destDir)
destDir := t.TempDir()
destFs := fs.NewFilesystem(fs.FilesystemTypeBasic, destDir)
link := "link"
@@ -1228,6 +1227,8 @@ func TestPullTempFileCaseConflict(t *testing.T) {
cs := <-copyChan
if _, err := cs.tempFile(); err != nil {
t.Error(err)
} else {
cs.finalClose()
}
}
+22 -24
View File
@@ -2475,7 +2475,7 @@ func TestIssue2571(t *testing.T) {
t.Skip("Scanning symlinks isn't supported on windows")
}
w, fcfg, wCancel := tmpDefaultWrapper()
w, fcfg, wCancel := tmpDefaultWrapper(t)
defer wCancel()
testFs := fcfg.Filesystem(nil)
defer os.RemoveAll(testFs.URI())
@@ -2514,7 +2514,7 @@ func TestIssue4573(t *testing.T) {
t.Skip("Can't make the dir inaccessible on windows")
}
w, fcfg, wCancel := tmpDefaultWrapper()
w, fcfg, wCancel := tmpDefaultWrapper(t)
defer wCancel()
testFs := fcfg.Filesystem(nil)
defer os.RemoveAll(testFs.URI())
@@ -2544,7 +2544,7 @@ func TestIssue4573(t *testing.T) {
// TestInternalScan checks whether various fs operations are correctly represented
// in the db after scanning.
func TestInternalScan(t *testing.T) {
w, fcfg, wCancel := tmpDefaultWrapper()
w, fcfg, wCancel := tmpDefaultWrapper(t)
defer wCancel()
testFs := fcfg.Filesystem(nil)
defer os.RemoveAll(testFs.URI())
@@ -2605,7 +2605,7 @@ func TestInternalScan(t *testing.T) {
func TestCustomMarkerName(t *testing.T) {
testOs := &fatalOs{t}
fcfg := testFolderConfigTmp()
fcfg := testFolderConfig(t.TempDir())
fcfg.ID = "default"
fcfg.RescanIntervalS = 1
fcfg.MarkerName = "myfile"
@@ -2762,9 +2762,7 @@ func TestVersionRestore(t *testing.T) {
// In each file, we write the filename as the content
// We verify that the content matches at the expected filenames
// after the restore operation.
dir, err := os.MkdirTemp("", "")
must(t, err)
defer os.RemoveAll(dir)
dir := t.TempDir()
fcfg := newFolderConfiguration(defaultCfgWrapper, "default", "default", fs.FilesystemTypeBasic, dir)
fcfg.Versioning.Type = "simple"
@@ -3181,7 +3179,7 @@ func TestConnCloseOnRestart(t *testing.T) {
protocol.CloseTimeout = oldCloseTimeout
}()
w, fcfg, wCancel := tmpDefaultWrapper()
w, fcfg, wCancel := tmpDefaultWrapper(t)
defer wCancel()
m := setupModel(t, w)
defer cleanupModelAndRemoveDir(m, fcfg.Filesystem(nil).URI())
@@ -3218,7 +3216,7 @@ func TestConnCloseOnRestart(t *testing.T) {
}
func TestModTimeWindow(t *testing.T) {
w, fcfg, wCancel := tmpDefaultWrapper()
w, fcfg, wCancel := tmpDefaultWrapper(t)
defer wCancel()
tfs := fcfg.Filesystem(nil)
fcfg.RawModTimeWindowS = 2
@@ -3347,7 +3345,7 @@ func TestNewLimitedRequestResponse(t *testing.T) {
}
func TestSummaryPausedNoError(t *testing.T) {
wcfg, fcfg, wcfgCancel := tmpDefaultWrapper()
wcfg, fcfg, wcfgCancel := tmpDefaultWrapper(t)
defer wcfgCancel()
pauseFolder(t, wcfg, fcfg.ID, true)
m := setupModel(t, wcfg)
@@ -3360,7 +3358,7 @@ func TestSummaryPausedNoError(t *testing.T) {
}
func TestFolderAPIErrors(t *testing.T) {
wcfg, fcfg, wcfgCancel := tmpDefaultWrapper()
wcfg, fcfg, wcfgCancel := tmpDefaultWrapper(t)
defer wcfgCancel()
pauseFolder(t, wcfg, fcfg.ID, true)
m := setupModel(t, wcfg)
@@ -3392,7 +3390,7 @@ func TestFolderAPIErrors(t *testing.T) {
}
func TestRenameSequenceOrder(t *testing.T) {
wcfg, fcfg, wcfgCancel := tmpDefaultWrapper()
wcfg, fcfg, wcfgCancel := tmpDefaultWrapper(t)
defer wcfgCancel()
m := setupModel(t, wcfg)
defer cleanupModel(m)
@@ -3463,7 +3461,7 @@ func TestRenameSequenceOrder(t *testing.T) {
}
func TestRenameSameFile(t *testing.T) {
wcfg, fcfg, wcfgCancel := tmpDefaultWrapper()
wcfg, fcfg, wcfgCancel := tmpDefaultWrapper(t)
defer wcfgCancel()
m := setupModel(t, wcfg)
defer cleanupModel(m)
@@ -3514,7 +3512,7 @@ func TestRenameSameFile(t *testing.T) {
}
func TestRenameEmptyFile(t *testing.T) {
wcfg, fcfg, wcfgCancel := tmpDefaultWrapper()
wcfg, fcfg, wcfgCancel := tmpDefaultWrapper(t)
defer wcfgCancel()
m := setupModel(t, wcfg)
defer cleanupModel(m)
@@ -3591,7 +3589,7 @@ func TestRenameEmptyFile(t *testing.T) {
}
func TestBlockListMap(t *testing.T) {
wcfg, fcfg, wcfgCancel := tmpDefaultWrapper()
wcfg, fcfg, wcfgCancel := tmpDefaultWrapper(t)
defer wcfgCancel()
m := setupModel(t, wcfg)
defer cleanupModel(m)
@@ -3659,7 +3657,7 @@ func TestBlockListMap(t *testing.T) {
}
func TestScanRenameCaseOnly(t *testing.T) {
wcfg, fcfg, wcfgCancel := tmpDefaultWrapper()
wcfg, fcfg, wcfgCancel := tmpDefaultWrapper(t)
defer wcfgCancel()
m := setupModel(t, wcfg)
defer cleanupModel(m)
@@ -3712,7 +3710,7 @@ func TestScanRenameCaseOnly(t *testing.T) {
func TestClusterConfigOnFolderAdd(t *testing.T) {
testConfigChangeTriggersClusterConfigs(t, false, true, nil, func(wrapper config.Wrapper) {
fcfg := testFolderConfigTmp()
fcfg := testFolderConfig(t.TempDir())
fcfg.ID = "second"
fcfg.Label = "second"
fcfg.Devices = []config.FolderDeviceConfiguration{{
@@ -3822,7 +3820,7 @@ func TestScanDeletedROChangedOnSR(t *testing.T) {
func testConfigChangeTriggersClusterConfigs(t *testing.T, expectFirst, expectSecond bool, pre func(config.Wrapper), fn func(config.Wrapper)) {
t.Helper()
wcfg, _, wcfgCancel := tmpDefaultWrapper()
wcfg, _, wcfgCancel := tmpDefaultWrapper(t)
defer wcfgCancel()
m := setupModel(t, wcfg)
defer cleanupModel(m)
@@ -3884,7 +3882,7 @@ func testConfigChangeTriggersClusterConfigs(t *testing.T, expectFirst, expectSec
// That then causes these files to be considered as needed, while they are not.
// https://github.com/syncthing/syncthing/issues/6961
func TestIssue6961(t *testing.T) {
wcfg, fcfg, wcfgCancel := tmpDefaultWrapper()
wcfg, fcfg, wcfgCancel := tmpDefaultWrapper(t)
defer wcfgCancel()
tfs := fcfg.Filesystem(nil)
waiter, err := wcfg.Modify(func(cfg *config.Configuration) {
@@ -3971,7 +3969,7 @@ func TestCompletionEmptyGlobal(t *testing.T) {
}
func TestNeedMetaAfterIndexReset(t *testing.T) {
w, fcfg, wCancel := tmpDefaultWrapper()
w, fcfg, wCancel := tmpDefaultWrapper(t)
defer wCancel()
addDevice2(t, w, fcfg)
m := setupModel(t, w)
@@ -4014,7 +4012,7 @@ func TestCcCheckEncryption(t *testing.T) {
t.Skip("skipping on short testing - generating encryption tokens is slow")
}
w, fcfg, wCancel := tmpDefaultWrapper()
w, fcfg, wCancel := tmpDefaultWrapper(t)
defer wCancel()
m := setupModel(t, w)
m.cancel()
@@ -4155,7 +4153,7 @@ func TestCcCheckEncryption(t *testing.T) {
func TestCCFolderNotRunning(t *testing.T) {
// Create the folder, but don't start it.
w, fcfg, wCancel := tmpDefaultWrapper()
w, fcfg, wCancel := tmpDefaultWrapper(t)
defer wCancel()
tfs := fcfg.Filesystem(nil)
m := newModel(t, w, myID, "syncthing", "dev", nil)
@@ -4183,7 +4181,7 @@ func TestCCFolderNotRunning(t *testing.T) {
}
func TestPendingFolder(t *testing.T) {
w, _, wCancel := tmpDefaultWrapper()
w, _, wCancel := tmpDefaultWrapper(t)
defer wCancel()
m := setupModel(t, w)
defer cleanupModel(m)
@@ -4263,7 +4261,7 @@ func TestDeletedNotLocallyChangedReceiveEncrypted(t *testing.T) {
}
func deletedNotLocallyChanged(t *testing.T, ft config.FolderType) {
w, fcfg, wCancel := tmpDefaultWrapper()
w, fcfg, wCancel := tmpDefaultWrapper(t)
tfs := fcfg.Filesystem(nil)
fcfg.Type = ft
setFolder(t, w, fcfg)
+6 -10
View File
@@ -221,7 +221,7 @@ func TestRequestVersioningSymlinkAttack(t *testing.T) {
// Sets up a folder with trashcan versioning and tries to use a
// deleted symlink to escape
w, fcfg, wCancel := tmpDefaultWrapper()
w, fcfg, wCancel := tmpDefaultWrapper(t)
defer wCancel()
defer func() {
os.RemoveAll(fcfg.Filesystem(nil).URI())
@@ -235,11 +235,7 @@ func TestRequestVersioningSymlinkAttack(t *testing.T) {
// Create a temporary directory that we will use as target to see if
// we can escape to it
tmpdir, err := os.MkdirTemp("", "syncthing-test")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpdir)
tmpdir := t.TempDir()
// We listen for incoming index updates and trigger when we see one for
// the expected test file.
@@ -300,7 +296,7 @@ func TestPullInvalidIgnoredSR(t *testing.T) {
func pullInvalidIgnored(t *testing.T, ft config.FolderType) {
w, wCancel := createTmpWrapper(defaultCfgWrapper.RawCopy())
defer wCancel()
fcfg := testFolderConfigTmp()
fcfg := testFolderConfig(t.TempDir())
fss := fcfg.Filesystem(nil)
fcfg.Type = ft
setFolder(t, w, fcfg)
@@ -1029,7 +1025,7 @@ func TestNeedFolderFiles(t *testing.T) {
// propagated upon un-ignoring.
// https://github.com/syncthing/syncthing/issues/6038
func TestIgnoreDeleteUnignore(t *testing.T) {
w, fcfg, wCancel := tmpDefaultWrapper()
w, fcfg, wCancel := tmpDefaultWrapper(t)
defer wCancel()
m := setupModel(t, w)
fss := fcfg.Filesystem(nil)
@@ -1273,7 +1269,7 @@ func TestRequestIndexSenderPause(t *testing.T) {
}
func TestRequestIndexSenderClusterConfigBeforeStart(t *testing.T) {
w, fcfg, wCancel := tmpDefaultWrapper()
w, fcfg, wCancel := tmpDefaultWrapper(t)
defer wCancel()
tfs := fcfg.Filesystem(nil)
dir1 := "foo"
@@ -1340,7 +1336,7 @@ func TestRequestReceiveEncrypted(t *testing.T) {
t.Skip("skipping on short testing - scrypt is too slow")
}
w, fcfg, wCancel := tmpDefaultWrapper()
w, fcfg, wCancel := tmpDefaultWrapper(t)
defer wCancel()
tfs := fcfg.Filesystem(nil)
fcfg.Type = config.FolderTypeReceiveEncrypted
+1 -2
View File
@@ -17,8 +17,7 @@ import (
// Test creating temporary file inside read-only directory
func TestReadOnlyDir(t *testing.T) {
// Create a read only directory, clean it up afterwards.
tmpDir := createTmpDir()
defer os.RemoveAll(tmpDir)
tmpDir := t.TempDir()
if err := os.Chmod(tmpDir, 0555); err != nil {
t.Fatal(err)
}
+3 -16
View File
@@ -86,20 +86,15 @@ func createTmpWrapper(cfg config.Configuration) (config.Wrapper, context.CancelF
return wrapper, cancel
}
func tmpDefaultWrapper() (config.Wrapper, config.FolderConfiguration, context.CancelFunc) {
func tmpDefaultWrapper(t testing.TB) (config.Wrapper, config.FolderConfiguration, context.CancelFunc) {
w, cancel := createTmpWrapper(defaultCfgWrapper.RawCopy())
fcfg := testFolderConfigTmp()
fcfg := testFolderConfig(t.TempDir())
_, _ = w.Modify(func(cfg *config.Configuration) {
cfg.SetFolder(fcfg)
})
return w, fcfg, cancel
}
func testFolderConfigTmp() config.FolderConfiguration {
tmpDir := createTmpDir()
return testFolderConfig(tmpDir)
}
func testFolderConfig(path string) config.FolderConfiguration {
cfg := newFolderConfiguration(defaultCfgWrapper, "default", "default", fs.FilesystemTypeBasic, path)
cfg.FSWatcherEnabled = false
@@ -116,7 +111,7 @@ func testFolderConfigFake() config.FolderConfiguration {
func setupModelWithConnection(t testing.TB) (*testModel, *fakeConnection, config.FolderConfiguration, context.CancelFunc) {
t.Helper()
w, fcfg, cancel := tmpDefaultWrapper()
w, fcfg, cancel := tmpDefaultWrapper(t)
m, fc := setupModelWithConnectionFromWrapper(t, w)
return m, fc, fcfg, cancel
}
@@ -213,14 +208,6 @@ func cleanupModelAndRemoveDir(m *testModel, dir string) {
os.RemoveAll(dir)
}
func createTmpDir() string {
tmpDir, err := os.MkdirTemp("", "syncthing_testFolder-")
if err != nil {
panic("Failed to create temporary testing dir")
}
return tmpDir
}
type alwaysChangedKey struct {
fs fs.Filesystem
name string
+4 -9
View File
@@ -7,7 +7,6 @@
package model
import (
"os"
"runtime"
"testing"
@@ -15,8 +14,7 @@ import (
)
func TestInWriteableDir(t *testing.T) {
dir := createTmpDir()
defer os.RemoveAll(dir)
dir := t.TempDir()
fs := fs.NewFilesystem(fs.FilesystemTypeBasic, dir)
@@ -77,8 +75,7 @@ func TestOSWindowsRemove(t *testing.T) {
return
}
dir := createTmpDir()
defer os.RemoveAll(dir)
dir := t.TempDir()
fs := fs.NewFilesystem(fs.FilesystemTypeBasic, dir)
defer fs.Chmod("testdata/windows/ro/readonlynew", 0700)
@@ -115,8 +112,7 @@ func TestOSWindowsRemoveAll(t *testing.T) {
return
}
dir := createTmpDir()
defer os.RemoveAll(dir)
dir := t.TempDir()
fs := fs.NewFilesystem(fs.FilesystemTypeBasic, dir)
defer fs.Chmod("testdata/windows/ro/readonlynew", 0700)
@@ -148,8 +144,7 @@ func TestInWritableDirWindowsRename(t *testing.T) {
return
}
dir := createTmpDir()
defer os.RemoveAll(dir)
dir := t.TempDir()
fs := fs.NewFilesystem(fs.FilesystemTypeBasic, dir)
defer fs.Chmod("testdata/windows/ro/readonlynew", 0700)