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 11:44:06 +08:00 committed by GitHub
parent 0537b9546f
commit bc27aa12cd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
22 changed files with 91 additions and 225 deletions

View File

@ -17,14 +17,17 @@ import (
func TestRotatedFile(t *testing.T) { func TestRotatedFile(t *testing.T) {
// Verify that log rotation happens. // Verify that log rotation happens.
dir, err := os.MkdirTemp("", "syncthing") dir := t.TempDir()
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
open := func(name string) (io.WriteCloser, error) { open := func(name string) (io.WriteCloser, error) {
return os.Create(name) f, err := os.Create(name)
t.Cleanup(func() {
if f != nil {
_ = f.Close()
}
})
return f, err
} }
logName := filepath.Join(dir, "log.txt") logName := filepath.Join(dir, "log.txt")

View File

@ -1146,11 +1146,7 @@ func TestBrowse(t *testing.T) {
pathSep := string(os.PathSeparator) pathSep := string(os.PathSeparator)
tmpDir, err := os.MkdirTemp("", "syncthing") tmpDir := t.TempDir()
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpDir)
if err := os.Mkdir(filepath.Join(tmpDir, "dir"), 0755); err != nil { if err := os.Mkdir(filepath.Join(tmpDir, "dir"), 0755); err != nil {
t.Fatal(err) t.Fatal(err)

View File

@ -504,13 +504,10 @@ func TestFolderPath(t *testing.T) {
} }
func TestFolderCheckPath(t *testing.T) { func TestFolderCheckPath(t *testing.T) {
n, err := os.MkdirTemp("", "") n := t.TempDir()
if err != nil {
t.Fatal(err)
}
testFs := fs.NewFilesystem(fs.FilesystemTypeBasic, n) testFs := fs.NewFilesystem(fs.FilesystemTypeBasic, n)
err = os.MkdirAll(filepath.Join(n, "dir", ".stfolder"), os.FileMode(0777)) err := os.MkdirAll(filepath.Join(n, "dir", ".stfolder"), os.FileMode(0777))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -602,11 +599,7 @@ func TestWindowsLineEndings(t *testing.T) {
t.Skip("Windows specific") t.Skip("Windows specific")
} }
dir, err := os.MkdirTemp("", "syncthing-test") dir := t.TempDir()
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
path := filepath.Join(dir, "config.xml") path := filepath.Join(dir, "config.xml")
os.Remove(path) os.Remove(path)

View File

@ -20,17 +20,13 @@ import (
func setup(t *testing.T) (*BasicFilesystem, string) { func setup(t *testing.T) (*BasicFilesystem, string) {
t.Helper() t.Helper()
dir, err := os.MkdirTemp("", "") dir := t.TempDir()
if err != nil {
t.Fatal(err)
}
return newBasicFilesystem(dir), dir return newBasicFilesystem(dir), dir
} }
func TestChmodFile(t *testing.T) { func TestChmodFile(t *testing.T) {
fs, dir := setup(t) fs, dir := setup(t)
path := filepath.Join(dir, "file") path := filepath.Join(dir, "file")
defer os.RemoveAll(dir)
defer os.Chmod(path, 0666) defer os.Chmod(path, 0666)
@ -71,7 +67,6 @@ func TestChownFile(t *testing.T) {
fs, dir := setup(t) fs, dir := setup(t)
path := filepath.Join(dir, "file") path := filepath.Join(dir, "file")
defer os.RemoveAll(dir)
defer os.Chmod(path, 0666) defer os.Chmod(path, 0666)
@ -108,7 +103,6 @@ func TestChownFile(t *testing.T) {
func TestChmodDir(t *testing.T) { func TestChmodDir(t *testing.T) {
fs, dir := setup(t) fs, dir := setup(t)
path := filepath.Join(dir, "dir") path := filepath.Join(dir, "dir")
defer os.RemoveAll(dir)
mode := os.FileMode(0755) mode := os.FileMode(0755)
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
@ -141,7 +135,6 @@ func TestChmodDir(t *testing.T) {
func TestChtimes(t *testing.T) { func TestChtimes(t *testing.T) {
fs, dir := setup(t) fs, dir := setup(t)
path := filepath.Join(dir, "file") path := filepath.Join(dir, "file")
defer os.RemoveAll(dir)
fd, err := os.Create(path) fd, err := os.Create(path)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
@ -166,7 +159,6 @@ func TestChtimes(t *testing.T) {
func TestCreate(t *testing.T) { func TestCreate(t *testing.T) {
fs, dir := setup(t) fs, dir := setup(t)
path := filepath.Join(dir, "file") path := filepath.Join(dir, "file")
defer os.RemoveAll(dir)
if _, err := os.Stat(path); err == nil { if _, err := os.Stat(path); err == nil {
t.Errorf("exists?") t.Errorf("exists?")
@ -190,7 +182,6 @@ func TestCreateSymlink(t *testing.T) {
fs, dir := setup(t) fs, dir := setup(t)
path := filepath.Join(dir, "file") path := filepath.Join(dir, "file")
defer os.RemoveAll(dir)
if err := fs.CreateSymlink("blah", "file"); err != nil { if err := fs.CreateSymlink("blah", "file"); err != nil {
t.Error(err) t.Error(err)
@ -215,7 +206,6 @@ func TestCreateSymlink(t *testing.T) {
func TestDirNames(t *testing.T) { func TestDirNames(t *testing.T) {
fs, dir := setup(t) fs, dir := setup(t)
defer os.RemoveAll(dir)
// Case differences // Case differences
testCases := []string{ testCases := []string{
@ -244,8 +234,7 @@ func TestDirNames(t *testing.T) {
func TestNames(t *testing.T) { func TestNames(t *testing.T) {
// Tests that all names are without the root directory. // Tests that all names are without the root directory.
fs, dir := setup(t) fs, _ := setup(t)
defer os.RemoveAll(dir)
expected := "file" expected := "file"
fd, err := fs.Create(expected) fd, err := fs.Create(expected)
@ -284,8 +273,7 @@ func TestNames(t *testing.T) {
func TestGlob(t *testing.T) { func TestGlob(t *testing.T) {
// Tests that all names are without the root directory. // Tests that all names are without the root directory.
fs, dir := setup(t) fs, _ := setup(t)
defer os.RemoveAll(dir)
for _, dirToCreate := range []string{ for _, dirToCreate := range []string{
filepath.Join("a", "test", "b"), filepath.Join("a", "test", "b"),
@ -344,8 +332,7 @@ func TestGlob(t *testing.T) {
} }
func TestUsage(t *testing.T) { func TestUsage(t *testing.T) {
fs, dir := setup(t) fs, _ := setup(t)
defer os.RemoveAll(dir)
usage, err := fs.Usage(".") usage, err := fs.Usage(".")
if err != nil { if err != nil {
if runtime.GOOS == "netbsd" || runtime.GOOS == "openbsd" || runtime.GOOS == "solaris" { if runtime.GOOS == "netbsd" || runtime.GOOS == "openbsd" || runtime.GOOS == "solaris" {
@ -577,18 +564,15 @@ func TestRel(t *testing.T) {
func TestBasicWalkSkipSymlink(t *testing.T) { func TestBasicWalkSkipSymlink(t *testing.T) {
_, dir := setup(t) _, dir := setup(t)
defer os.RemoveAll(dir)
testWalkSkipSymlink(t, FilesystemTypeBasic, dir) testWalkSkipSymlink(t, FilesystemTypeBasic, dir)
} }
func TestWalkTraverseDirJunct(t *testing.T) { func TestWalkTraverseDirJunct(t *testing.T) {
_, dir := setup(t) _, dir := setup(t)
defer os.RemoveAll(dir)
testWalkTraverseDirJunct(t, FilesystemTypeBasic, dir) testWalkTraverseDirJunct(t, FilesystemTypeBasic, dir)
} }
func TestWalkInfiniteRecursion(t *testing.T) { func TestWalkInfiniteRecursion(t *testing.T) {
_, dir := setup(t) _, dir := setup(t)
defer os.RemoveAll(dir)
testWalkInfiniteRecursion(t, FilesystemTypeBasic, dir) testWalkInfiniteRecursion(t, FilesystemTypeBasic, dir)
} }

View File

@ -49,7 +49,6 @@ func TestResolveWindows83(t *testing.T) {
dir = fs.resolveWin83(dir) dir = fs.resolveWin83(dir)
fs = newBasicFilesystem(dir) fs = newBasicFilesystem(dir)
} }
defer os.RemoveAll(dir)
shortAbs, _ := fs.rooted("LFDATA~1") shortAbs, _ := fs.rooted("LFDATA~1")
long := "LFDataTool" long := "LFDataTool"
@ -80,7 +79,6 @@ func TestIsWindows83(t *testing.T) {
dir = fs.resolveWin83(dir) dir = fs.resolveWin83(dir)
fs = newBasicFilesystem(dir) fs = newBasicFilesystem(dir)
} }
defer os.RemoveAll(dir)
tempTop, _ := fs.rooted(TempName("baz")) tempTop, _ := fs.rooted(TempName("baz"))
tempBelow, _ := fs.rooted(filepath.Join("foo", "bar", TempName("baz"))) tempBelow, _ := fs.rooted(filepath.Join("foo", "bar", TempName("baz")))

View File

@ -9,7 +9,6 @@ package fs
import ( import (
"errors" "errors"
"fmt" "fmt"
"os"
"path/filepath" "path/filepath"
"runtime" "runtime"
"sort" "sort"
@ -28,8 +27,7 @@ func TestRealCase(t *testing.T) {
testRealCase(t, newFakeFilesystem(t.Name()+"?insens=true")) testRealCase(t, newFakeFilesystem(t.Name()+"?insens=true"))
}) })
t.Run("actual", func(t *testing.T) { t.Run("actual", func(t *testing.T) {
fsys, tmpDir := setup(t) fsys, _ := setup(t)
defer os.RemoveAll(tmpDir)
testRealCase(t, fsys) testRealCase(t, fsys)
}) })
} }
@ -83,8 +81,7 @@ func TestRealCaseSensitive(t *testing.T) {
testRealCaseSensitive(t, newFakeFilesystem(t.Name())) testRealCaseSensitive(t, newFakeFilesystem(t.Name()))
}) })
t.Run("actual", func(t *testing.T) { t.Run("actual", func(t *testing.T) {
fsys, tmpDir := setup(t) fsys, _ := setup(t)
defer os.RemoveAll(tmpDir)
testRealCaseSensitive(t, fsys) testRealCaseSensitive(t, fsys)
}) })
} }
@ -124,8 +121,7 @@ func TestCaseFSStat(t *testing.T) {
testCaseFSStat(t, newFakeFilesystem(t.Name()+"?insens=true")) testCaseFSStat(t, newFakeFilesystem(t.Name()+"?insens=true"))
}) })
t.Run("actual", func(t *testing.T) { t.Run("actual", func(t *testing.T) {
fsys, tmpDir := setup(t) fsys, _ := setup(t)
defer os.RemoveAll(tmpDir)
testCaseFSStat(t, fsys) testCaseFSStat(t, fsys)
}) })
} }

View File

@ -204,7 +204,6 @@ func TestFakeFSCaseSensitive(t *testing.T) {
} }
testDir, sensitive := createTestDir(t) testDir, sensitive := createTestDir(t)
defer removeTestDir(t, testDir)
if sensitive { if sensitive {
filesystems = append(filesystems, testFS{runtime.GOOS, newBasicFilesystem(testDir)}) filesystems = append(filesystems, testFS{runtime.GOOS, newBasicFilesystem(testDir)})
} }
@ -240,7 +239,6 @@ func TestFakeFSCaseInsensitive(t *testing.T) {
} }
testDir, sensitive := createTestDir(t) testDir, sensitive := createTestDir(t)
defer removeTestDir(t, testDir)
if !sensitive { if !sensitive {
filesystems = append(filesystems, testFS{runtime.GOOS, newBasicFilesystem(testDir)}) filesystems = append(filesystems, testFS{runtime.GOOS, newBasicFilesystem(testDir)})
} }
@ -251,10 +249,7 @@ func TestFakeFSCaseInsensitive(t *testing.T) {
func createTestDir(t *testing.T) (string, bool) { func createTestDir(t *testing.T) (string, bool) {
t.Helper() t.Helper()
testDir, err := os.MkdirTemp("", "") testDir := t.TempDir()
if err != nil {
t.Fatalf("could not create temporary dir for testing: %s", err)
}
if fd, err := os.Create(filepath.Join(testDir, ".stfolder")); err != nil { if fd, err := os.Create(filepath.Join(testDir, ".stfolder")); err != nil {
t.Fatalf("could not create .stfolder: %s", err) t.Fatalf("could not create .stfolder: %s", err)
@ -273,14 +268,6 @@ func createTestDir(t *testing.T) (string, bool) {
return testDir, sensitive return testDir, sensitive
} }
func removeTestDir(t *testing.T, testDir string) {
t.Helper()
if err := os.RemoveAll(testDir); err != nil {
t.Fatalf("could not remove test directory: %s", err)
}
}
func runTests(t *testing.T, tests []test, filesystems []testFS) { func runTests(t *testing.T, tests []test, filesystems []testFS) {
for _, filesystem := range filesystems { for _, filesystem := range filesystems {
for _, test := range tests { for _, test := range tests {

View File

@ -82,11 +82,7 @@ func TestMtimeFS(t *testing.T) {
} }
func TestMtimeFSWalk(t *testing.T) { func TestMtimeFSWalk(t *testing.T) {
dir, err := os.MkdirTemp("", "") dir := t.TempDir()
if err != nil {
t.Fatal(err)
}
defer func() { _ = os.RemoveAll(dir) }()
mtimefs, walkFs := newMtimeFSWithWalk(dir, make(mapStore)) mtimefs, walkFs := newMtimeFSWithWalk(dir, make(mapStore))
underlying := mtimefs.Filesystem underlying := mtimefs.Filesystem
@ -136,11 +132,7 @@ func TestMtimeFSWalk(t *testing.T) {
} }
func TestMtimeFSOpen(t *testing.T) { func TestMtimeFSOpen(t *testing.T) {
dir, err := os.MkdirTemp("", "") dir := t.TempDir()
if err != nil {
t.Fatal(err)
}
defer func() { _ = os.RemoveAll(dir) }()
mtimefs := newMtimeFS(dir, make(mapStore)) mtimefs := newMtimeFS(dir, make(mapStore))
underlying := mtimefs.Filesystem underlying := mtimefs.Filesystem
@ -177,6 +169,8 @@ func TestMtimeFSOpen(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
defer fd.Close()
info, err := fd.Stat() info, err := fd.Stat()
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)

View File

@ -231,10 +231,7 @@ func TestCaseSensitivity(t *testing.T) {
} }
func TestCaching(t *testing.T) { func TestCaching(t *testing.T) {
dir, err := os.MkdirTemp("", "") dir := t.TempDir()
if err != nil {
t.Fatal(err)
}
fs := fs.NewFilesystem(fs.FilesystemTypeBasic, dir) fs := fs.NewFilesystem(fs.FilesystemTypeBasic, dir)
@ -424,10 +421,7 @@ flamingo
*.crow *.crow
` `
// Caches per file, hence write the patterns to a file. // Caches per file, hence write the patterns to a file.
dir, err := os.MkdirTemp("", "") dir := b.TempDir()
if err != nil {
b.Fatal(err)
}
fs := fs.NewFilesystem(fs.FilesystemTypeBasic, dir) fs := fs.NewFilesystem(fs.FilesystemTypeBasic, dir)
@ -465,10 +459,7 @@ flamingo
} }
func TestCacheReload(t *testing.T) { func TestCacheReload(t *testing.T) {
dir, err := os.MkdirTemp("", "") dir := t.TempDir()
if err != nil {
t.Fatal(err)
}
fs := fs.NewFilesystem(fs.FilesystemTypeBasic, dir) fs := fs.NewFilesystem(fs.FilesystemTypeBasic, dir)
@ -988,12 +979,7 @@ func TestIssue4689(t *testing.T) {
} }
func TestIssue4901(t *testing.T) { func TestIssue4901(t *testing.T) {
dir, err := os.MkdirTemp("", "") dir := t.TempDir()
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
stignore := ` stignore := `
#include unicorn-lazor-death #include unicorn-lazor-death
@ -1023,7 +1009,7 @@ func TestIssue4901(t *testing.T) {
t.Fatalf(err.Error()) t.Fatalf(err.Error())
} }
err = pats.Load(".stignore") err := pats.Load(".stignore")
if err != nil { if err != nil {
t.Fatalf("unexpected error: %s", err.Error()) t.Fatalf("unexpected error: %s", err.Error())
} }
@ -1200,11 +1186,7 @@ func TestWindowsLineEndings(t *testing.T) {
lines := "foo\nbar\nbaz\n" lines := "foo\nbar\nbaz\n"
dir, err := os.MkdirTemp("", "syncthing-test") dir := t.TempDir()
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
ffs := fs.NewFilesystem(fs.FilesystemTypeBasic, dir) ffs := fs.NewFilesystem(fs.FilesystemTypeBasic, dir)
m := New(ffs) m := New(ffs)

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. // 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) { 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. // Initialise model and stop immediately.
model := setupModel(t, w) model := setupModel(t, w)
model.cancel() model.cancel()
@ -972,8 +972,7 @@ func TestDeleteBehindSymlink(t *testing.T) {
defer cleanupSRFolder(f, m, wcfgCancel) defer cleanupSRFolder(f, m, wcfgCancel)
ffs := f.Filesystem(nil) ffs := f.Filesystem(nil)
destDir := createTmpDir() destDir := t.TempDir()
defer os.RemoveAll(destDir)
destFs := fs.NewFilesystem(fs.FilesystemTypeBasic, destDir) destFs := fs.NewFilesystem(fs.FilesystemTypeBasic, destDir)
link := "link" link := "link"
@ -1228,6 +1227,8 @@ func TestPullTempFileCaseConflict(t *testing.T) {
cs := <-copyChan cs := <-copyChan
if _, err := cs.tempFile(); err != nil { if _, err := cs.tempFile(); err != nil {
t.Error(err) t.Error(err)
} else {
cs.finalClose()
} }
} }

View File

@ -2475,7 +2475,7 @@ func TestIssue2571(t *testing.T) {
t.Skip("Scanning symlinks isn't supported on windows") t.Skip("Scanning symlinks isn't supported on windows")
} }
w, fcfg, wCancel := tmpDefaultWrapper() w, fcfg, wCancel := tmpDefaultWrapper(t)
defer wCancel() defer wCancel()
testFs := fcfg.Filesystem(nil) testFs := fcfg.Filesystem(nil)
defer os.RemoveAll(testFs.URI()) defer os.RemoveAll(testFs.URI())
@ -2514,7 +2514,7 @@ func TestIssue4573(t *testing.T) {
t.Skip("Can't make the dir inaccessible on windows") t.Skip("Can't make the dir inaccessible on windows")
} }
w, fcfg, wCancel := tmpDefaultWrapper() w, fcfg, wCancel := tmpDefaultWrapper(t)
defer wCancel() defer wCancel()
testFs := fcfg.Filesystem(nil) testFs := fcfg.Filesystem(nil)
defer os.RemoveAll(testFs.URI()) defer os.RemoveAll(testFs.URI())
@ -2544,7 +2544,7 @@ func TestIssue4573(t *testing.T) {
// TestInternalScan checks whether various fs operations are correctly represented // TestInternalScan checks whether various fs operations are correctly represented
// in the db after scanning. // in the db after scanning.
func TestInternalScan(t *testing.T) { func TestInternalScan(t *testing.T) {
w, fcfg, wCancel := tmpDefaultWrapper() w, fcfg, wCancel := tmpDefaultWrapper(t)
defer wCancel() defer wCancel()
testFs := fcfg.Filesystem(nil) testFs := fcfg.Filesystem(nil)
defer os.RemoveAll(testFs.URI()) defer os.RemoveAll(testFs.URI())
@ -2605,7 +2605,7 @@ func TestInternalScan(t *testing.T) {
func TestCustomMarkerName(t *testing.T) { func TestCustomMarkerName(t *testing.T) {
testOs := &fatalOs{t} testOs := &fatalOs{t}
fcfg := testFolderConfigTmp() fcfg := testFolderConfig(t.TempDir())
fcfg.ID = "default" fcfg.ID = "default"
fcfg.RescanIntervalS = 1 fcfg.RescanIntervalS = 1
fcfg.MarkerName = "myfile" fcfg.MarkerName = "myfile"
@ -2762,9 +2762,7 @@ func TestVersionRestore(t *testing.T) {
// In each file, we write the filename as the content // In each file, we write the filename as the content
// We verify that the content matches at the expected filenames // We verify that the content matches at the expected filenames
// after the restore operation. // after the restore operation.
dir, err := os.MkdirTemp("", "") dir := t.TempDir()
must(t, err)
defer os.RemoveAll(dir)
fcfg := newFolderConfiguration(defaultCfgWrapper, "default", "default", fs.FilesystemTypeBasic, dir) fcfg := newFolderConfiguration(defaultCfgWrapper, "default", "default", fs.FilesystemTypeBasic, dir)
fcfg.Versioning.Type = "simple" fcfg.Versioning.Type = "simple"
@ -3181,7 +3179,7 @@ func TestConnCloseOnRestart(t *testing.T) {
protocol.CloseTimeout = oldCloseTimeout protocol.CloseTimeout = oldCloseTimeout
}() }()
w, fcfg, wCancel := tmpDefaultWrapper() w, fcfg, wCancel := tmpDefaultWrapper(t)
defer wCancel() defer wCancel()
m := setupModel(t, w) m := setupModel(t, w)
defer cleanupModelAndRemoveDir(m, fcfg.Filesystem(nil).URI()) defer cleanupModelAndRemoveDir(m, fcfg.Filesystem(nil).URI())
@ -3218,7 +3216,7 @@ func TestConnCloseOnRestart(t *testing.T) {
} }
func TestModTimeWindow(t *testing.T) { func TestModTimeWindow(t *testing.T) {
w, fcfg, wCancel := tmpDefaultWrapper() w, fcfg, wCancel := tmpDefaultWrapper(t)
defer wCancel() defer wCancel()
tfs := fcfg.Filesystem(nil) tfs := fcfg.Filesystem(nil)
fcfg.RawModTimeWindowS = 2 fcfg.RawModTimeWindowS = 2
@ -3347,7 +3345,7 @@ func TestNewLimitedRequestResponse(t *testing.T) {
} }
func TestSummaryPausedNoError(t *testing.T) { func TestSummaryPausedNoError(t *testing.T) {
wcfg, fcfg, wcfgCancel := tmpDefaultWrapper() wcfg, fcfg, wcfgCancel := tmpDefaultWrapper(t)
defer wcfgCancel() defer wcfgCancel()
pauseFolder(t, wcfg, fcfg.ID, true) pauseFolder(t, wcfg, fcfg.ID, true)
m := setupModel(t, wcfg) m := setupModel(t, wcfg)
@ -3360,7 +3358,7 @@ func TestSummaryPausedNoError(t *testing.T) {
} }
func TestFolderAPIErrors(t *testing.T) { func TestFolderAPIErrors(t *testing.T) {
wcfg, fcfg, wcfgCancel := tmpDefaultWrapper() wcfg, fcfg, wcfgCancel := tmpDefaultWrapper(t)
defer wcfgCancel() defer wcfgCancel()
pauseFolder(t, wcfg, fcfg.ID, true) pauseFolder(t, wcfg, fcfg.ID, true)
m := setupModel(t, wcfg) m := setupModel(t, wcfg)
@ -3392,7 +3390,7 @@ func TestFolderAPIErrors(t *testing.T) {
} }
func TestRenameSequenceOrder(t *testing.T) { func TestRenameSequenceOrder(t *testing.T) {
wcfg, fcfg, wcfgCancel := tmpDefaultWrapper() wcfg, fcfg, wcfgCancel := tmpDefaultWrapper(t)
defer wcfgCancel() defer wcfgCancel()
m := setupModel(t, wcfg) m := setupModel(t, wcfg)
defer cleanupModel(m) defer cleanupModel(m)
@ -3463,7 +3461,7 @@ func TestRenameSequenceOrder(t *testing.T) {
} }
func TestRenameSameFile(t *testing.T) { func TestRenameSameFile(t *testing.T) {
wcfg, fcfg, wcfgCancel := tmpDefaultWrapper() wcfg, fcfg, wcfgCancel := tmpDefaultWrapper(t)
defer wcfgCancel() defer wcfgCancel()
m := setupModel(t, wcfg) m := setupModel(t, wcfg)
defer cleanupModel(m) defer cleanupModel(m)
@ -3514,7 +3512,7 @@ func TestRenameSameFile(t *testing.T) {
} }
func TestRenameEmptyFile(t *testing.T) { func TestRenameEmptyFile(t *testing.T) {
wcfg, fcfg, wcfgCancel := tmpDefaultWrapper() wcfg, fcfg, wcfgCancel := tmpDefaultWrapper(t)
defer wcfgCancel() defer wcfgCancel()
m := setupModel(t, wcfg) m := setupModel(t, wcfg)
defer cleanupModel(m) defer cleanupModel(m)
@ -3591,7 +3589,7 @@ func TestRenameEmptyFile(t *testing.T) {
} }
func TestBlockListMap(t *testing.T) { func TestBlockListMap(t *testing.T) {
wcfg, fcfg, wcfgCancel := tmpDefaultWrapper() wcfg, fcfg, wcfgCancel := tmpDefaultWrapper(t)
defer wcfgCancel() defer wcfgCancel()
m := setupModel(t, wcfg) m := setupModel(t, wcfg)
defer cleanupModel(m) defer cleanupModel(m)
@ -3659,7 +3657,7 @@ func TestBlockListMap(t *testing.T) {
} }
func TestScanRenameCaseOnly(t *testing.T) { func TestScanRenameCaseOnly(t *testing.T) {
wcfg, fcfg, wcfgCancel := tmpDefaultWrapper() wcfg, fcfg, wcfgCancel := tmpDefaultWrapper(t)
defer wcfgCancel() defer wcfgCancel()
m := setupModel(t, wcfg) m := setupModel(t, wcfg)
defer cleanupModel(m) defer cleanupModel(m)
@ -3712,7 +3710,7 @@ func TestScanRenameCaseOnly(t *testing.T) {
func TestClusterConfigOnFolderAdd(t *testing.T) { func TestClusterConfigOnFolderAdd(t *testing.T) {
testConfigChangeTriggersClusterConfigs(t, false, true, nil, func(wrapper config.Wrapper) { testConfigChangeTriggersClusterConfigs(t, false, true, nil, func(wrapper config.Wrapper) {
fcfg := testFolderConfigTmp() fcfg := testFolderConfig(t.TempDir())
fcfg.ID = "second" fcfg.ID = "second"
fcfg.Label = "second" fcfg.Label = "second"
fcfg.Devices = []config.FolderDeviceConfiguration{{ 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)) { func testConfigChangeTriggersClusterConfigs(t *testing.T, expectFirst, expectSecond bool, pre func(config.Wrapper), fn func(config.Wrapper)) {
t.Helper() t.Helper()
wcfg, _, wcfgCancel := tmpDefaultWrapper() wcfg, _, wcfgCancel := tmpDefaultWrapper(t)
defer wcfgCancel() defer wcfgCancel()
m := setupModel(t, wcfg) m := setupModel(t, wcfg)
defer cleanupModel(m) 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. // That then causes these files to be considered as needed, while they are not.
// https://github.com/syncthing/syncthing/issues/6961 // https://github.com/syncthing/syncthing/issues/6961
func TestIssue6961(t *testing.T) { func TestIssue6961(t *testing.T) {
wcfg, fcfg, wcfgCancel := tmpDefaultWrapper() wcfg, fcfg, wcfgCancel := tmpDefaultWrapper(t)
defer wcfgCancel() defer wcfgCancel()
tfs := fcfg.Filesystem(nil) tfs := fcfg.Filesystem(nil)
waiter, err := wcfg.Modify(func(cfg *config.Configuration) { waiter, err := wcfg.Modify(func(cfg *config.Configuration) {
@ -3971,7 +3969,7 @@ func TestCompletionEmptyGlobal(t *testing.T) {
} }
func TestNeedMetaAfterIndexReset(t *testing.T) { func TestNeedMetaAfterIndexReset(t *testing.T) {
w, fcfg, wCancel := tmpDefaultWrapper() w, fcfg, wCancel := tmpDefaultWrapper(t)
defer wCancel() defer wCancel()
addDevice2(t, w, fcfg) addDevice2(t, w, fcfg)
m := setupModel(t, w) m := setupModel(t, w)
@ -4014,7 +4012,7 @@ func TestCcCheckEncryption(t *testing.T) {
t.Skip("skipping on short testing - generating encryption tokens is slow") t.Skip("skipping on short testing - generating encryption tokens is slow")
} }
w, fcfg, wCancel := tmpDefaultWrapper() w, fcfg, wCancel := tmpDefaultWrapper(t)
defer wCancel() defer wCancel()
m := setupModel(t, w) m := setupModel(t, w)
m.cancel() m.cancel()
@ -4155,7 +4153,7 @@ func TestCcCheckEncryption(t *testing.T) {
func TestCCFolderNotRunning(t *testing.T) { func TestCCFolderNotRunning(t *testing.T) {
// Create the folder, but don't start it. // Create the folder, but don't start it.
w, fcfg, wCancel := tmpDefaultWrapper() w, fcfg, wCancel := tmpDefaultWrapper(t)
defer wCancel() defer wCancel()
tfs := fcfg.Filesystem(nil) tfs := fcfg.Filesystem(nil)
m := newModel(t, w, myID, "syncthing", "dev", nil) m := newModel(t, w, myID, "syncthing", "dev", nil)
@ -4183,7 +4181,7 @@ func TestCCFolderNotRunning(t *testing.T) {
} }
func TestPendingFolder(t *testing.T) { func TestPendingFolder(t *testing.T) {
w, _, wCancel := tmpDefaultWrapper() w, _, wCancel := tmpDefaultWrapper(t)
defer wCancel() defer wCancel()
m := setupModel(t, w) m := setupModel(t, w)
defer cleanupModel(m) defer cleanupModel(m)
@ -4263,7 +4261,7 @@ func TestDeletedNotLocallyChangedReceiveEncrypted(t *testing.T) {
} }
func deletedNotLocallyChanged(t *testing.T, ft config.FolderType) { func deletedNotLocallyChanged(t *testing.T, ft config.FolderType) {
w, fcfg, wCancel := tmpDefaultWrapper() w, fcfg, wCancel := tmpDefaultWrapper(t)
tfs := fcfg.Filesystem(nil) tfs := fcfg.Filesystem(nil)
fcfg.Type = ft fcfg.Type = ft
setFolder(t, w, fcfg) setFolder(t, w, fcfg)

View File

@ -221,7 +221,7 @@ func TestRequestVersioningSymlinkAttack(t *testing.T) {
// Sets up a folder with trashcan versioning and tries to use a // Sets up a folder with trashcan versioning and tries to use a
// deleted symlink to escape // deleted symlink to escape
w, fcfg, wCancel := tmpDefaultWrapper() w, fcfg, wCancel := tmpDefaultWrapper(t)
defer wCancel() defer wCancel()
defer func() { defer func() {
os.RemoveAll(fcfg.Filesystem(nil).URI()) 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 // Create a temporary directory that we will use as target to see if
// we can escape to it // we can escape to it
tmpdir, err := os.MkdirTemp("", "syncthing-test") tmpdir := t.TempDir()
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpdir)
// We listen for incoming index updates and trigger when we see one for // We listen for incoming index updates and trigger when we see one for
// the expected test file. // the expected test file.
@ -300,7 +296,7 @@ func TestPullInvalidIgnoredSR(t *testing.T) {
func pullInvalidIgnored(t *testing.T, ft config.FolderType) { func pullInvalidIgnored(t *testing.T, ft config.FolderType) {
w, wCancel := createTmpWrapper(defaultCfgWrapper.RawCopy()) w, wCancel := createTmpWrapper(defaultCfgWrapper.RawCopy())
defer wCancel() defer wCancel()
fcfg := testFolderConfigTmp() fcfg := testFolderConfig(t.TempDir())
fss := fcfg.Filesystem(nil) fss := fcfg.Filesystem(nil)
fcfg.Type = ft fcfg.Type = ft
setFolder(t, w, fcfg) setFolder(t, w, fcfg)
@ -1029,7 +1025,7 @@ func TestNeedFolderFiles(t *testing.T) {
// propagated upon un-ignoring. // propagated upon un-ignoring.
// https://github.com/syncthing/syncthing/issues/6038 // https://github.com/syncthing/syncthing/issues/6038
func TestIgnoreDeleteUnignore(t *testing.T) { func TestIgnoreDeleteUnignore(t *testing.T) {
w, fcfg, wCancel := tmpDefaultWrapper() w, fcfg, wCancel := tmpDefaultWrapper(t)
defer wCancel() defer wCancel()
m := setupModel(t, w) m := setupModel(t, w)
fss := fcfg.Filesystem(nil) fss := fcfg.Filesystem(nil)
@ -1273,7 +1269,7 @@ func TestRequestIndexSenderPause(t *testing.T) {
} }
func TestRequestIndexSenderClusterConfigBeforeStart(t *testing.T) { func TestRequestIndexSenderClusterConfigBeforeStart(t *testing.T) {
w, fcfg, wCancel := tmpDefaultWrapper() w, fcfg, wCancel := tmpDefaultWrapper(t)
defer wCancel() defer wCancel()
tfs := fcfg.Filesystem(nil) tfs := fcfg.Filesystem(nil)
dir1 := "foo" dir1 := "foo"
@ -1340,7 +1336,7 @@ func TestRequestReceiveEncrypted(t *testing.T) {
t.Skip("skipping on short testing - scrypt is too slow") t.Skip("skipping on short testing - scrypt is too slow")
} }
w, fcfg, wCancel := tmpDefaultWrapper() w, fcfg, wCancel := tmpDefaultWrapper(t)
defer wCancel() defer wCancel()
tfs := fcfg.Filesystem(nil) tfs := fcfg.Filesystem(nil)
fcfg.Type = config.FolderTypeReceiveEncrypted fcfg.Type = config.FolderTypeReceiveEncrypted

View File

@ -17,8 +17,7 @@ import (
// Test creating temporary file inside read-only directory // Test creating temporary file inside read-only directory
func TestReadOnlyDir(t *testing.T) { func TestReadOnlyDir(t *testing.T) {
// Create a read only directory, clean it up afterwards. // Create a read only directory, clean it up afterwards.
tmpDir := createTmpDir() tmpDir := t.TempDir()
defer os.RemoveAll(tmpDir)
if err := os.Chmod(tmpDir, 0555); err != nil { if err := os.Chmod(tmpDir, 0555); err != nil {
t.Fatal(err) t.Fatal(err)
} }

View File

@ -86,20 +86,15 @@ func createTmpWrapper(cfg config.Configuration) (config.Wrapper, context.CancelF
return wrapper, cancel 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()) w, cancel := createTmpWrapper(defaultCfgWrapper.RawCopy())
fcfg := testFolderConfigTmp() fcfg := testFolderConfig(t.TempDir())
_, _ = w.Modify(func(cfg *config.Configuration) { _, _ = w.Modify(func(cfg *config.Configuration) {
cfg.SetFolder(fcfg) cfg.SetFolder(fcfg)
}) })
return w, fcfg, cancel return w, fcfg, cancel
} }
func testFolderConfigTmp() config.FolderConfiguration {
tmpDir := createTmpDir()
return testFolderConfig(tmpDir)
}
func testFolderConfig(path string) config.FolderConfiguration { func testFolderConfig(path string) config.FolderConfiguration {
cfg := newFolderConfiguration(defaultCfgWrapper, "default", "default", fs.FilesystemTypeBasic, path) cfg := newFolderConfiguration(defaultCfgWrapper, "default", "default", fs.FilesystemTypeBasic, path)
cfg.FSWatcherEnabled = false cfg.FSWatcherEnabled = false
@ -116,7 +111,7 @@ func testFolderConfigFake() config.FolderConfiguration {
func setupModelWithConnection(t testing.TB) (*testModel, *fakeConnection, config.FolderConfiguration, context.CancelFunc) { func setupModelWithConnection(t testing.TB) (*testModel, *fakeConnection, config.FolderConfiguration, context.CancelFunc) {
t.Helper() t.Helper()
w, fcfg, cancel := tmpDefaultWrapper() w, fcfg, cancel := tmpDefaultWrapper(t)
m, fc := setupModelWithConnectionFromWrapper(t, w) m, fc := setupModelWithConnectionFromWrapper(t, w)
return m, fc, fcfg, cancel return m, fc, fcfg, cancel
} }
@ -213,14 +208,6 @@ func cleanupModelAndRemoveDir(m *testModel, dir string) {
os.RemoveAll(dir) 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 { type alwaysChangedKey struct {
fs fs.Filesystem fs fs.Filesystem
name string name string

View File

@ -7,7 +7,6 @@
package model package model
import ( import (
"os"
"runtime" "runtime"
"testing" "testing"
@ -15,8 +14,7 @@ import (
) )
func TestInWriteableDir(t *testing.T) { func TestInWriteableDir(t *testing.T) {
dir := createTmpDir() dir := t.TempDir()
defer os.RemoveAll(dir)
fs := fs.NewFilesystem(fs.FilesystemTypeBasic, dir) fs := fs.NewFilesystem(fs.FilesystemTypeBasic, dir)
@ -77,8 +75,7 @@ func TestOSWindowsRemove(t *testing.T) {
return return
} }
dir := createTmpDir() dir := t.TempDir()
defer os.RemoveAll(dir)
fs := fs.NewFilesystem(fs.FilesystemTypeBasic, dir) fs := fs.NewFilesystem(fs.FilesystemTypeBasic, dir)
defer fs.Chmod("testdata/windows/ro/readonlynew", 0700) defer fs.Chmod("testdata/windows/ro/readonlynew", 0700)
@ -115,8 +112,7 @@ func TestOSWindowsRemoveAll(t *testing.T) {
return return
} }
dir := createTmpDir() dir := t.TempDir()
defer os.RemoveAll(dir)
fs := fs.NewFilesystem(fs.FilesystemTypeBasic, dir) fs := fs.NewFilesystem(fs.FilesystemTypeBasic, dir)
defer fs.Chmod("testdata/windows/ro/readonlynew", 0700) defer fs.Chmod("testdata/windows/ro/readonlynew", 0700)
@ -148,8 +144,7 @@ func TestInWritableDirWindowsRename(t *testing.T) {
return return
} }
dir := createTmpDir() dir := t.TempDir()
defer os.RemoveAll(dir)
fs := fs.NewFilesystem(fs.FilesystemTypeBasic, dir) fs := fs.NewFilesystem(fs.FilesystemTypeBasic, dir)
defer fs.Chmod("testdata/windows/ro/readonlynew", 0700) defer fs.Chmod("testdata/windows/ro/readonlynew", 0700)

View File

@ -61,14 +61,10 @@ func TestCreateAtomicReplaceReadOnly(t *testing.T) {
func testCreateAtomicReplace(t *testing.T, oldPerms os.FileMode) { func testCreateAtomicReplace(t *testing.T, oldPerms os.FileMode) {
t.Helper() t.Helper()
testdir, err := os.MkdirTemp("", "syncthing") testdir := t.TempDir()
if err != nil {
t.Fatal(err)
}
testfile := filepath.Join(testdir, "testfile") testfile := filepath.Join(testdir, "testfile")
os.RemoveAll(testdir) os.RemoveAll(testdir)
defer os.RemoveAll(testdir)
if err := os.Mkdir(testdir, 0755); err != nil { if err := os.Mkdir(testdir, 0755); err != nil {
t.Fatal(err) t.Fatal(err)

View File

@ -81,15 +81,7 @@ func TestIsDeleted(t *testing.T) {
} }
func TestRenameOrCopy(t *testing.T) { func TestRenameOrCopy(t *testing.T) {
mustTempDir := func() string { sameFs := fs.NewFilesystem(fs.FilesystemTypeBasic, t.TempDir())
t.Helper()
tmpDir, err := os.MkdirTemp("", "")
if err != nil {
t.Fatal(err)
}
return tmpDir
}
sameFs := fs.NewFilesystem(fs.FilesystemTypeBasic, mustTempDir())
tests := []struct { tests := []struct {
src fs.Filesystem src fs.Filesystem
dst fs.Filesystem dst fs.Filesystem
@ -101,13 +93,13 @@ func TestRenameOrCopy(t *testing.T) {
file: "file", file: "file",
}, },
{ {
src: fs.NewFilesystem(fs.FilesystemTypeBasic, mustTempDir()), src: fs.NewFilesystem(fs.FilesystemTypeBasic, t.TempDir()),
dst: fs.NewFilesystem(fs.FilesystemTypeBasic, mustTempDir()), dst: fs.NewFilesystem(fs.FilesystemTypeBasic, t.TempDir()),
file: "file", file: "file",
}, },
{ {
src: fs.NewFilesystem(fs.FilesystemTypeFake, `fake://fake/?files=1&seed=42`), src: fs.NewFilesystem(fs.FilesystemTypeFake, `fake://fake/?files=1&seed=42`),
dst: fs.NewFilesystem(fs.FilesystemTypeBasic, mustTempDir()), dst: fs.NewFilesystem(fs.FilesystemTypeBasic, t.TempDir()),
file: osutil.NativeFilename(`05/7a/4d52f284145b9fe8`), file: osutil.NativeFilename(`05/7a/4d52f284145b9fe8`),
}, },
} }
@ -147,6 +139,10 @@ func TestRenameOrCopy(t *testing.T) {
if fd, err := test.dst.Open("new"); err != nil { if fd, err := test.dst.Open("new"); err != nil {
t.Fatal(err) t.Fatal(err)
} else { } else {
t.Cleanup(func() {
_ = fd.Close()
})
if buf, err := io.ReadAll(fd); err != nil { if buf, err := io.ReadAll(fd); err != nil {
t.Fatal(err) t.Fatal(err)
} else if string(buf) != content { } else if string(buf) != content {

View File

@ -17,15 +17,11 @@ import (
) )
func TestTraversesSymlink(t *testing.T) { func TestTraversesSymlink(t *testing.T) {
tmpDir, err := os.MkdirTemp(".", ".test-TraversesSymlink-") tmpDir := t.TempDir()
if err != nil {
panic("Failed to create temporary testing dir")
}
defer os.RemoveAll(tmpDir)
testFs := fs.NewFilesystem(fs.FilesystemTypeBasic, tmpDir) testFs := fs.NewFilesystem(fs.FilesystemTypeBasic, tmpDir)
testFs.MkdirAll("a/b/c", 0755) testFs.MkdirAll("a/b/c", 0755)
if err = fs.DebugSymlinkForTestsOnly(testFs, testFs, filepath.Join("a", "b"), filepath.Join("a", "l")); err != nil { if err := fs.DebugSymlinkForTestsOnly(testFs, testFs, filepath.Join("a", "b"), filepath.Join("a", "l")); err != nil {
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
t.Skip("Symlinks aren't working") t.Skip("Symlinks aren't working")
} }
@ -70,15 +66,11 @@ func TestTraversesSymlink(t *testing.T) {
} }
func TestIssue4875(t *testing.T) { func TestIssue4875(t *testing.T) {
tmpDir, err := os.MkdirTemp("", ".test-Issue4875-") tmpDir := t.TempDir()
if err != nil {
panic("Failed to create temporary testing dir")
}
defer os.RemoveAll(tmpDir)
testFs := fs.NewFilesystem(fs.FilesystemTypeBasic, tmpDir) testFs := fs.NewFilesystem(fs.FilesystemTypeBasic, tmpDir)
testFs.MkdirAll(filepath.Join("a", "b", "c"), 0755) testFs.MkdirAll(filepath.Join("a", "b", "c"), 0755)
if err = fs.DebugSymlinkForTestsOnly(testFs, testFs, filepath.Join("a", "b"), filepath.Join("a", "l")); err != nil { if err := fs.DebugSymlinkForTestsOnly(testFs, testFs, filepath.Join("a", "b"), filepath.Join("a", "l")); err != nil {
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
t.Skip("Symlinks aren't working") t.Skip("Symlinks aren't working")
} }

View File

@ -379,11 +379,7 @@ func TestWalkSymlinkWindows(t *testing.T) {
func TestWalkRootSymlink(t *testing.T) { func TestWalkRootSymlink(t *testing.T) {
// Create a folder with a symlink in it // Create a folder with a symlink in it
tmp, err := os.MkdirTemp("", "") tmp := t.TempDir()
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmp)
testFs := fs.NewFilesystem(testFsType, tmp) testFs := fs.NewFilesystem(testFsType, tmp)
link := "link" link := "link"
@ -712,11 +708,7 @@ func TestStopWalk(t *testing.T) {
} }
func TestIssue4799(t *testing.T) { func TestIssue4799(t *testing.T) {
tmp, err := os.MkdirTemp("", "") tmp := t.TempDir()
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmp)
fs := fs.NewFilesystem(testFsType, tmp) fs := fs.NewFilesystem(testFsType, tmp)
@ -774,11 +766,7 @@ func TestRecurseInclude(t *testing.T) {
} }
func TestIssue4841(t *testing.T) { func TestIssue4841(t *testing.T) {
tmp, err := os.MkdirTemp("", "") tmp := t.TempDir()
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmp)
fs := fs.NewFilesystem(testFsType, tmp) fs := fs.NewFilesystem(testFsType, tmp)

View File

@ -8,7 +8,6 @@ package versioner
import ( import (
"math" "math"
"os"
"path/filepath" "path/filepath"
"testing" "testing"
"time" "time"
@ -55,11 +54,7 @@ func TestSimpleVersioningVersionCount(t *testing.T) {
t.Skip("Test takes some time, skipping.") t.Skip("Test takes some time, skipping.")
} }
dir, err := os.MkdirTemp("", "") dir := t.TempDir()
//defer os.RemoveAll(dir)
if err != nil {
t.Error(err)
}
cfg := config.FolderConfiguration{ cfg := config.FolderConfiguration{
FilesystemType: fs.FilesystemTypeBasic, FilesystemType: fs.FilesystemTypeBasic,

View File

@ -133,10 +133,7 @@ func TestCreateVersionPath(t *testing.T) {
) )
// Create a test dir and file // Create a test dir and file
tmpDir, err := os.MkdirTemp("", "") tmpDir := t.TempDir()
if err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(tmpDir, archiveFile), []byte("sup"), 0644); err != nil { if err := os.WriteFile(filepath.Join(tmpDir, archiveFile), []byte("sup"), 0644); err != nil {
t.Fatal(err) t.Fatal(err)
} }

View File

@ -8,7 +8,6 @@ package versioner
import ( import (
"io" "io"
"os"
"testing" "testing"
"time" "time"
@ -20,15 +19,9 @@ func TestTrashcanArchiveRestoreSwitcharoo(t *testing.T) {
// This tests that trashcan versioner restoration correctly archives existing file, because trashcan versioner // This tests that trashcan versioner restoration correctly archives existing file, because trashcan versioner
// files are untagged, archiving existing file to replace with a restored version technically should collide in // files are untagged, archiving existing file to replace with a restored version technically should collide in
// in names. // in names.
tmpDir1, err := os.MkdirTemp("", "") tmpDir1 := t.TempDir()
if err != nil {
t.Fatal(err)
}
tmpDir2, err := os.MkdirTemp("", "") tmpDir2 := t.TempDir()
if err != nil {
t.Fatal(err)
}
cfg := config.FolderConfiguration{ cfg := config.FolderConfiguration{
FilesystemType: fs.FilesystemTypeBasic, FilesystemType: fs.FilesystemTypeBasic,