test: use `T.TempDir` to create temporary test directory (#718)
The directory created by `T.TempDir` is automatically removed when the test and all its subtests complete. Reference: https://pkg.go.dev/testing#T.TempDir Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
This commit is contained in:
parent
d6534ea7e8
commit
841a702e34
|
@ -4,7 +4,6 @@ import (
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
@ -221,9 +220,7 @@ func TestResponsive(t *testing.T) {
|
||||||
defer seeder.Close()
|
defer seeder.Close()
|
||||||
seederTorrent, _, _ := seeder.AddTorrentSpec(TorrentSpecFromMetaInfo(mi))
|
seederTorrent, _, _ := seeder.AddTorrentSpec(TorrentSpecFromMetaInfo(mi))
|
||||||
seederTorrent.VerifyData()
|
seederTorrent.VerifyData()
|
||||||
leecherDataDir, err := ioutil.TempDir("", "")
|
leecherDataDir := t.TempDir()
|
||||||
require.Nil(t, err)
|
|
||||||
defer os.RemoveAll(leecherDataDir)
|
|
||||||
cfg = TestingConfig(t)
|
cfg = TestingConfig(t)
|
||||||
cfg.DataDir = leecherDataDir
|
cfg.DataDir = leecherDataDir
|
||||||
leecher, err := NewClient(cfg)
|
leecher, err := NewClient(cfg)
|
||||||
|
@ -264,9 +261,7 @@ func TestTorrentDroppedDuringResponsiveRead(t *testing.T) {
|
||||||
defer seeder.Close()
|
defer seeder.Close()
|
||||||
seederTorrent, _, _ := seeder.AddTorrentSpec(TorrentSpecFromMetaInfo(mi))
|
seederTorrent, _, _ := seeder.AddTorrentSpec(TorrentSpecFromMetaInfo(mi))
|
||||||
seederTorrent.VerifyData()
|
seederTorrent.VerifyData()
|
||||||
leecherDataDir, err := ioutil.TempDir("", "")
|
leecherDataDir := t.TempDir()
|
||||||
require.Nil(t, err)
|
|
||||||
defer os.RemoveAll(leecherDataDir)
|
|
||||||
cfg = TestingConfig(t)
|
cfg = TestingConfig(t)
|
||||||
cfg.DataDir = leecherDataDir
|
cfg.DataDir = leecherDataDir
|
||||||
leecher, err := NewClient(cfg)
|
leecher, err := NewClient(cfg)
|
||||||
|
@ -359,9 +354,7 @@ func writeTorrentData(ts *storage.Torrent, info metainfo.Info, b []byte) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func testAddTorrentPriorPieceCompletion(t *testing.T, alreadyCompleted bool, csf func(*filecache.Cache) storage.ClientImpl) {
|
func testAddTorrentPriorPieceCompletion(t *testing.T, alreadyCompleted bool, csf func(*filecache.Cache) storage.ClientImpl) {
|
||||||
fileCacheDir, err := ioutil.TempDir("", "")
|
fileCacheDir := t.TempDir()
|
||||||
require.NoError(t, err)
|
|
||||||
defer os.RemoveAll(fileCacheDir)
|
|
||||||
fileCache, err := filecache.NewCache(fileCacheDir)
|
fileCache, err := filecache.NewCache(fileCacheDir)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
greetingDataTempDir, greetingMetainfo := testutil.GreetingTestTorrent()
|
greetingDataTempDir, greetingMetainfo := testutil.GreetingTestTorrent()
|
||||||
|
@ -456,9 +449,7 @@ func testDownloadCancel(t *testing.T, ps testDownloadCancelParams) {
|
||||||
defer testutil.ExportStatusWriter(seeder, "s", t)()
|
defer testutil.ExportStatusWriter(seeder, "s", t)()
|
||||||
seederTorrent, _, _ := seeder.AddTorrentSpec(TorrentSpecFromMetaInfo(mi))
|
seederTorrent, _, _ := seeder.AddTorrentSpec(TorrentSpecFromMetaInfo(mi))
|
||||||
seederTorrent.VerifyData()
|
seederTorrent.VerifyData()
|
||||||
leecherDataDir, err := ioutil.TempDir("", "")
|
leecherDataDir := t.TempDir()
|
||||||
require.NoError(t, err)
|
|
||||||
defer os.RemoveAll(leecherDataDir)
|
|
||||||
fc, err := filecache.NewCache(leecherDataDir)
|
fc, err := filecache.NewCache(leecherDataDir)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
if ps.SetLeecherStorageCapacity {
|
if ps.SetLeecherStorageCapacity {
|
||||||
|
|
|
@ -62,11 +62,8 @@ func (tl *testLayout) Destroy() error {
|
||||||
return os.RemoveAll(tl.BaseDir)
|
return os.RemoveAll(tl.BaseDir)
|
||||||
}
|
}
|
||||||
|
|
||||||
func newGreetingLayout() (tl testLayout, err error) {
|
func newGreetingLayout(t *testing.T) (tl testLayout, err error) {
|
||||||
tl.BaseDir, err = ioutil.TempDir("", "torrentfs")
|
tl.BaseDir = t.TempDir()
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
tl.Completed = filepath.Join(tl.BaseDir, "completed")
|
tl.Completed = filepath.Join(tl.BaseDir, "completed")
|
||||||
os.Mkdir(tl.Completed, 0o777)
|
os.Mkdir(tl.Completed, 0o777)
|
||||||
tl.MountDir = filepath.Join(tl.BaseDir, "mnt")
|
tl.MountDir = filepath.Join(tl.BaseDir, "mnt")
|
||||||
|
@ -79,7 +76,7 @@ func newGreetingLayout() (tl testLayout, err error) {
|
||||||
// Unmount without first killing the FUSE connection while there are FUSE
|
// Unmount without first killing the FUSE connection while there are FUSE
|
||||||
// operations blocked inside the filesystem code.
|
// operations blocked inside the filesystem code.
|
||||||
func TestUnmountWedged(t *testing.T) {
|
func TestUnmountWedged(t *testing.T) {
|
||||||
layout, err := newGreetingLayout()
|
layout, err := newGreetingLayout(t)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
defer func() {
|
defer func() {
|
||||||
err := layout.Destroy()
|
err := layout.Destroy()
|
||||||
|
@ -161,7 +158,7 @@ func TestUnmountWedged(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDownloadOnDemand(t *testing.T) {
|
func TestDownloadOnDemand(t *testing.T) {
|
||||||
layout, err := newGreetingLayout()
|
layout, err := newGreetingLayout(t)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
defer layout.Destroy()
|
defer layout.Destroy()
|
||||||
cfg := torrent.NewDefaultClientConfig()
|
cfg := torrent.NewDefaultClientConfig()
|
||||||
|
|
|
@ -1,8 +1,6 @@
|
||||||
package torrent
|
package torrent
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/anacrolix/log"
|
"github.com/anacrolix/log"
|
||||||
|
@ -13,9 +11,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestHashPieceAfterStorageClosed(t *testing.T) {
|
func TestHashPieceAfterStorageClosed(t *testing.T) {
|
||||||
td, err := ioutil.TempDir("", "")
|
td := t.TempDir()
|
||||||
require.NoError(t, err)
|
|
||||||
defer os.RemoveAll(td)
|
|
||||||
tt := &Torrent{
|
tt := &Torrent{
|
||||||
storageOpener: storage.NewClient(storage.NewFile(td)),
|
storageOpener: storage.NewClient(storage.NewFile(td)),
|
||||||
logger: log.Default,
|
logger: log.Default,
|
||||||
|
|
|
@ -85,9 +85,7 @@ func touchFile(path string) (err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBuildFromFilePathOrder(t *testing.T) {
|
func TestBuildFromFilePathOrder(t *testing.T) {
|
||||||
td, err := ioutil.TempDir("", "anacrolix")
|
td := t.TempDir()
|
||||||
require.NoError(t, err)
|
|
||||||
defer os.RemoveAll(td)
|
|
||||||
require.NoError(t, touchFile(filepath.Join(td, "b")))
|
require.NoError(t, touchFile(filepath.Join(td, "b")))
|
||||||
require.NoError(t, touchFile(filepath.Join(td, "a")))
|
require.NoError(t, touchFile(filepath.Join(td, "a")))
|
||||||
info := Info{
|
info := Info{
|
||||||
|
|
|
@ -1,8 +1,6 @@
|
||||||
package storage
|
package storage
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
@ -12,9 +10,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestBoltPieceCompletion(t *testing.T) {
|
func TestBoltPieceCompletion(t *testing.T) {
|
||||||
td, err := ioutil.TempDir("", "")
|
td := t.TempDir()
|
||||||
require.NoError(t, err)
|
|
||||||
defer os.RemoveAll(td)
|
|
||||||
|
|
||||||
pc, err := NewBoltPieceCompletion(td)
|
pc, err := NewBoltPieceCompletion(td)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
|
@ -3,7 +3,6 @@ package storage
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
|
@ -16,9 +15,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestShortFile(t *testing.T) {
|
func TestShortFile(t *testing.T) {
|
||||||
td, err := ioutil.TempDir("", "")
|
td := t.TempDir()
|
||||||
require.NoError(t, err)
|
|
||||||
defer os.RemoveAll(td)
|
|
||||||
s := NewFile(td)
|
s := NewFile(td)
|
||||||
info := &metainfo.Info{
|
info := &metainfo.Info{
|
||||||
Name: "a",
|
Name: "a",
|
||||||
|
|
|
@ -1,8 +1,6 @@
|
||||||
package storage
|
package storage
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/anacrolix/missinggo/v2/resource"
|
"github.com/anacrolix/missinggo/v2/resource"
|
||||||
|
@ -33,22 +31,15 @@ func testIssue95(t *testing.T, c ClientImpl) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestIssue95File(t *testing.T) {
|
func TestIssue95File(t *testing.T) {
|
||||||
td, err := ioutil.TempDir("", "")
|
td := t.TempDir()
|
||||||
require.NoError(t, err)
|
|
||||||
defer os.RemoveAll(td)
|
|
||||||
testIssue95(t, NewFile(td))
|
testIssue95(t, NewFile(td))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestIssue95MMap(t *testing.T) {
|
func TestIssue95MMap(t *testing.T) {
|
||||||
td, err := ioutil.TempDir("", "")
|
td := t.TempDir()
|
||||||
require.NoError(t, err)
|
|
||||||
defer os.RemoveAll(td)
|
|
||||||
testIssue95(t, NewMMap(td))
|
testIssue95(t, NewMMap(td))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestIssue95ResourcePieces(t *testing.T) {
|
func TestIssue95ResourcePieces(t *testing.T) {
|
||||||
td, err := ioutil.TempDir("", "")
|
|
||||||
require.NoError(t, err)
|
|
||||||
defer os.RemoveAll(td)
|
|
||||||
testIssue95(t, NewResourcePieces(resource.OSFileProvider{}))
|
testIssue95(t, NewResourcePieces(resource.OSFileProvider{}))
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,6 @@
|
||||||
package storage
|
package storage
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
@ -11,9 +9,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func testMarkedCompleteMissingOnRead(t *testing.T, csf func(string) ClientImplCloser) {
|
func testMarkedCompleteMissingOnRead(t *testing.T, csf func(string) ClientImplCloser) {
|
||||||
td, err := ioutil.TempDir("", "")
|
td := t.TempDir()
|
||||||
require.NoError(t, err)
|
|
||||||
defer os.RemoveAll(td)
|
|
||||||
cs := NewClient(csf(td))
|
cs := NewClient(csf(td))
|
||||||
info := &metainfo.Info{
|
info := &metainfo.Info{
|
||||||
PieceLength: 1,
|
PieceLength: 1,
|
||||||
|
|
|
@ -397,9 +397,7 @@ func TestSeedAfterDownloading(t *testing.T) {
|
||||||
|
|
||||||
cfg = torrent.TestingConfig(t)
|
cfg = torrent.TestingConfig(t)
|
||||||
cfg.Seed = true
|
cfg.Seed = true
|
||||||
cfg.DataDir, err = ioutil.TempDir("", "")
|
cfg.DataDir = t.TempDir()
|
||||||
require.NoError(t, err)
|
|
||||||
defer os.RemoveAll(cfg.DataDir)
|
|
||||||
leecher, err := torrent.NewClient(cfg)
|
leecher, err := torrent.NewClient(cfg)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
defer leecher.Close()
|
defer leecher.Close()
|
||||||
|
@ -407,9 +405,7 @@ func TestSeedAfterDownloading(t *testing.T) {
|
||||||
|
|
||||||
cfg = torrent.TestingConfig(t)
|
cfg = torrent.TestingConfig(t)
|
||||||
cfg.Seed = false
|
cfg.Seed = false
|
||||||
cfg.DataDir, err = ioutil.TempDir("", "")
|
cfg.DataDir = t.TempDir()
|
||||||
require.NoError(t, err)
|
|
||||||
defer os.RemoveAll(cfg.DataDir)
|
|
||||||
leecherLeecher, _ := torrent.NewClient(cfg)
|
leecherLeecher, _ := torrent.NewClient(cfg)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
defer leecherLeecher.Close()
|
defer leecherLeecher.Close()
|
||||||
|
|
|
@ -1,19 +1,13 @@
|
||||||
package dirwatch
|
package dirwatch
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestDirwatch(t *testing.T) {
|
func TestDirwatch(t *testing.T) {
|
||||||
tempDirName, err := ioutil.TempDir("", "")
|
tempDirName := t.TempDir()
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
defer os.RemoveAll(tempDirName)
|
|
||||||
t.Logf("tempdir: %q", tempDirName)
|
t.Logf("tempdir: %q", tempDirName)
|
||||||
dw, err := New(tempDirName)
|
dw, err := New(tempDirName)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
Loading…
Reference in New Issue