From c2dc3c780eaaa25bc98ad502c8ba95c08d69ef6f Mon Sep 17 00:00:00 2001 From: folbrich Date: Sun, 8 Feb 2026 10:08:32 +0100 Subject: [PATCH] Replace ioutil.TempDir() in tests with testing.Tempdir() --- assemble_test.go | 24 ++++-------------------- cmd/desync/cache_test.go | 7 ++----- cmd/desync/chop_test.go | 6 ++---- cmd/desync/extract_test.go | 19 +++++-------------- cmd/desync/indexserver_test.go | 7 ++----- cmd/desync/prune_test.go | 8 ++------ cmd/desync/tar_test.go | 14 ++++---------- cmd/desync/untar_test.go | 13 ++++--------- cmd/desync/verify_test.go | 6 ++---- index_test.go | 12 ++---------- mount-index_linux_test.go | 6 +----- selfseed_test.go | 6 +----- tar_test.go | 12 ++++-------- 13 files changed, 35 insertions(+), 105 deletions(-) diff --git a/assemble_test.go b/assemble_test.go index 025b8b6..6392d81 100644 --- a/assemble_test.go +++ b/assemble_test.go @@ -49,11 +49,7 @@ func TestExtract(t *testing.T) { } // Chop up the input file into a (temporary) local store - store, err := ioutil.TempDir("", "store") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(store) + store := t.TempDir() s, err := NewLocalStore(store, StoreOptions{}) if err != nil { @@ -64,11 +60,7 @@ func TestExtract(t *testing.T) { } // Make a blank store - used to test a case where no chunk *should* be requested - blankstore, err := ioutil.TempDir("", "blankstore") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(blankstore) + blankstore := t.TempDir() bs, err := NewLocalStore(blankstore, StoreOptions{}) if err != nil { t.Fatal(err) @@ -160,11 +152,7 @@ func TestSeed(t *testing.T) { rand.Read(rand2) // Setup a temporary store - store, err := ioutil.TempDir("", "store") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(store) + store := t.TempDir() s, err := NewLocalStore(store, StoreOptions{}) if err != nil { @@ -295,11 +283,7 @@ func TestSeed(t *testing.T) { // be kept in-place and the self-seed must not cause any re-writes. func TestSelfSeedInPlace(t *testing.T) { // Setup a temporary store - store, err := ioutil.TempDir("", "store") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(store) + store := t.TempDir() s, err := NewLocalStore(store, StoreOptions{}) if err != nil { diff --git a/cmd/desync/cache_test.go b/cmd/desync/cache_test.go index 305f688..d1ee7e6 100644 --- a/cmd/desync/cache_test.go +++ b/cmd/desync/cache_test.go @@ -3,7 +3,6 @@ package main import ( "context" "io/ioutil" - "os" "testing" "github.com/stretchr/testify/require" @@ -22,9 +21,7 @@ func TestCacheCommand(t *testing.T) { []string{"--store", "testdata/blob1.store", "--store", "testdata/blob2.store", "testdata/blob1.caibx", "testdata/blob2.caibx"}}, } { t.Run(test.name, func(t *testing.T) { - cache, err := ioutil.TempDir("", "") - require.NoError(t, err) - defer os.RemoveAll(cache) + cache := t.TempDir() cmd := newCacheCommand(context.Background()) cmd.SetArgs(append(test.args, "-c", cache)) @@ -32,7 +29,7 @@ func TestCacheCommand(t *testing.T) { // Redirect the command's output to turn off the progressbar and run it stderr = ioutil.Discard cmd.SetOutput(ioutil.Discard) - _, err = cmd.ExecuteC() + _, err := cmd.ExecuteC() require.NoError(t, err) // If the file was split right, we'll have chunks in the dir now diff --git a/cmd/desync/chop_test.go b/cmd/desync/chop_test.go index 5529fb1..fb927d6 100644 --- a/cmd/desync/chop_test.go +++ b/cmd/desync/chop_test.go @@ -20,9 +20,7 @@ func TestChopCommand(t *testing.T) { {"chop with ignore", []string{"--ignore", "testdata/blob2.caibx", "testdata/blob1.caibx", "testdata/blob1"}}, } { - store, err := ioutil.TempDir("", "") - require.NoError(t, err) - defer os.RemoveAll(store) + store := t.TempDir() args := []string{"-s", store} args = append(args, test.args...) @@ -33,7 +31,7 @@ func TestChopCommand(t *testing.T) { // Redirect the command's output to turn off the progressbar and run it stderr = ioutil.Discard cmd.SetOutput(ioutil.Discard) - _, err = cmd.ExecuteC() + _, err := cmd.ExecuteC() require.NoError(t, err) // If the file was split right, we'll have chunks in the dir now diff --git a/cmd/desync/extract_test.go b/cmd/desync/extract_test.go index f8f078e..a1801ff 100644 --- a/cmd/desync/extract_test.go +++ b/cmd/desync/extract_test.go @@ -5,7 +5,6 @@ import ( "io/ioutil" "net/http" "net/http/httptest" - "os" "path/filepath" "testing" @@ -18,9 +17,7 @@ func TestExtractCommand(t *testing.T) { require.NoError(t, err) // Now prepare several files used to extract into - outDir, err := ioutil.TempDir("", "") - require.NoError(t, err) - defer os.RemoveAll(outDir) + outDir := t.TempDir() out1 := filepath.Join(outDir, "out1") // Doesn't exit out2 := filepath.Join(outDir, "out2") // Exists, but different content require.NoError(t, ioutil.WriteFile(out2, []byte{0, 1, 2, 3}, 0644)) @@ -28,9 +25,7 @@ func TestExtractCommand(t *testing.T) { require.NoError(t, ioutil.WriteFile(out3, expected, 0644)) // Make a cache dir - cacheDir, err := ioutil.TempDir("", "") - require.NoError(t, err) - defer os.RemoveAll(cacheDir) + cacheDir := t.TempDir() for _, test := range []struct { name string @@ -106,9 +101,7 @@ func TestExtractCommand(t *testing.T) { } func TestExtractWithFailover(t *testing.T) { - outDir, err := ioutil.TempDir("", "") - require.NoError(t, err) - defer os.RemoveAll(outDir) + outDir := t.TempDir() out := filepath.Join(outDir, "out") // Start a server that'll always fail @@ -124,14 +117,12 @@ func TestExtractWithFailover(t *testing.T) { // Redirect the command's output and run it stderr = ioutil.Discard cmd.SetOutput(ioutil.Discard) - _, err = cmd.ExecuteC() + _, err := cmd.ExecuteC() require.NoError(t, err) } func TestExtractWithInvalidSeeds(t *testing.T) { - outDir, err := ioutil.TempDir("", "") - require.NoError(t, err) - defer os.RemoveAll(outDir) + outDir := t.TempDir() out := filepath.Join(outDir, "out") for _, test := range []struct { diff --git a/cmd/desync/indexserver_test.go b/cmd/desync/indexserver_test.go index 652d006..0e7d672 100644 --- a/cmd/desync/indexserver_test.go +++ b/cmd/desync/indexserver_test.go @@ -6,7 +6,6 @@ import ( "io/ioutil" "net" "net/http" - "os" "strings" "testing" "time" @@ -47,9 +46,7 @@ func TestIndexServerReadCommand(t *testing.T) { func TestIndexServerWriteCommand(t *testing.T) { // Create an empty store to be used for writing - store, err := ioutil.TempDir("", "") - require.NoError(t, err) - defer os.RemoveAll(store) + store := t.TempDir() // Start a read-write server addr, cancel := startIndexServer(t, "-s", store, "-w") @@ -60,7 +57,7 @@ func TestIndexServerWriteCommand(t *testing.T) { makeCmd := newMakeCommand(context.Background()) makeCmd.SetArgs([]string{fmt.Sprintf("http://%s/new.caibx", addr), "testdata/blob1"}) makeCmd.SetOutput(ioutil.Discard) - _, err = makeCmd.ExecuteC() + _, err := makeCmd.ExecuteC() require.NoError(t, err) // The index server should not accept arbitrary (non-index) files. diff --git a/cmd/desync/prune_test.go b/cmd/desync/prune_test.go index 4f22e0f..850ce86 100644 --- a/cmd/desync/prune_test.go +++ b/cmd/desync/prune_test.go @@ -2,8 +2,6 @@ package main import ( "context" - "io/ioutil" - "os" "testing" "github.com/stretchr/testify/require" @@ -11,14 +9,12 @@ import ( func TestPruneCommand(t *testing.T) { // Create a blank store - store, err := ioutil.TempDir("", "") - require.NoError(t, err) - defer os.RemoveAll(store) + store := t.TempDir() // Run a "chop" command to populate the store chopCmd := newChopCommand(context.Background()) chopCmd.SetArgs([]string{"-s", store, "testdata/blob1.caibx", "testdata/blob1"}) - _, err = chopCmd.ExecuteC() + _, err := chopCmd.ExecuteC() require.NoError(t, err) // Now prune the store. Using a different index that doesn't have the exact same chunks diff --git a/cmd/desync/tar_test.go b/cmd/desync/tar_test.go index 174f312..6daeb2b 100644 --- a/cmd/desync/tar_test.go +++ b/cmd/desync/tar_test.go @@ -4,8 +4,6 @@ package main import ( "context" - "io/ioutil" - "os" "path/filepath" "testing" @@ -14,28 +12,24 @@ import ( func TestTarCommandArchive(t *testing.T) { // Create an output dir - out, err := ioutil.TempDir("", "") - require.NoError(t, err) - defer os.RemoveAll(out) + out := t.TempDir() archive := filepath.Join(out, "tree.catar") // Run "tar" command to build the catar archive cmd := newTarCommand(context.Background()) cmd.SetArgs([]string{archive, "testdata/tree"}) - _, err = cmd.ExecuteC() + _, err := cmd.ExecuteC() require.NoError(t, err) } func TestTarCommandIndex(t *testing.T) { // Create an output dir to function as chunk store and to hold the caidx - out, err := ioutil.TempDir("", "") - require.NoError(t, err) - defer os.RemoveAll(out) + out := t.TempDir() index := filepath.Join(out, "tree.caidx") // Run "tar" command to build a caidx index and store the chunks cmd := newTarCommand(context.Background()) cmd.SetArgs([]string{"-s", out, "-i", index, "testdata/tree"}) - _, err = cmd.ExecuteC() + _, err := cmd.ExecuteC() require.NoError(t, err) } diff --git a/cmd/desync/untar_test.go b/cmd/desync/untar_test.go index 64cbff0..a7b8662 100644 --- a/cmd/desync/untar_test.go +++ b/cmd/desync/untar_test.go @@ -6,7 +6,6 @@ package main import ( "context" "fmt" - "io/ioutil" "os" "path" "testing" @@ -16,27 +15,23 @@ import ( func TestUntarCommandArchive(t *testing.T) { // Create an output dir to extract into - out, err := ioutil.TempDir("", "") - require.NoError(t, err) - defer os.RemoveAll(out) + out := t.TempDir() // Run "untar" command to unpack an archive cmd := newUntarCommand(context.Background()) cmd.SetArgs([]string{"--no-same-owner", "--no-same-permissions", "testdata/tree.catar", out}) - _, err = cmd.ExecuteC() + _, err := cmd.ExecuteC() require.NoError(t, err) } func TestUntarCommandIndex(t *testing.T) { // Create an output dir to extract into - out, err := ioutil.TempDir("", "") - require.NoError(t, err) - defer os.RemoveAll(out) + out := t.TempDir() // Run "untar" to extract from a caidx index cmd := newUntarCommand(context.Background()) cmd.SetArgs([]string{"-s", "testdata/tree.store", "-i", "--no-same-owner", "--no-same-permissions", "testdata/tree.caidx", out}) - _, err = cmd.ExecuteC() + _, err := cmd.ExecuteC() require.NoError(t, err) } diff --git a/cmd/desync/verify_test.go b/cmd/desync/verify_test.go index 71cdb76..8f929c1 100644 --- a/cmd/desync/verify_test.go +++ b/cmd/desync/verify_test.go @@ -13,14 +13,12 @@ import ( func TestVerifyCommand(t *testing.T) { // Create a blank store - store, err := ioutil.TempDir("", "") - require.NoError(t, err) - defer os.RemoveAll(store) + store := t.TempDir() // Run a "chop" command to populate the store chopCmd := newChopCommand(context.Background()) chopCmd.SetArgs([]string{"-s", store, "testdata/blob1.caibx", "testdata/blob1"}) - _, err = chopCmd.ExecuteC() + _, err := chopCmd.ExecuteC() require.NoError(t, err) // Place an invalid chunk in the store diff --git a/index_test.go b/index_test.go index 4348c11..60fddc3 100644 --- a/index_test.go +++ b/index_test.go @@ -82,11 +82,7 @@ func TestIndexChunking(t *testing.T) { } // Make a temp local store - dir, err := ioutil.TempDir("", "chunktest") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(dir) // clean up + dir := t.TempDir() s, err := NewLocalStore(dir, StoreOptions{}) if err != nil { t.Fatal(err) @@ -174,11 +170,7 @@ func splitBlob(b *testing.B) { } // Make a temp local store - dir, err := ioutil.TempDir("", "chunktest") - if err != nil { - b.Fatal(err) - } - defer os.RemoveAll(dir) // clean up + dir := b.TempDir() s, err := NewLocalStore(dir, StoreOptions{}) if err != nil { b.Fatal(err) diff --git a/mount-index_linux_test.go b/mount-index_linux_test.go index 230c4ca..7354322 100644 --- a/mount-index_linux_test.go +++ b/mount-index_linux_test.go @@ -14,11 +14,7 @@ import ( func TestMountIndex(t *testing.T) { // Create the mount point - mnt, err := ioutil.TempDir("", "mount-index-store") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(mnt) + mnt := t.TempDir() // Define the store s, err := NewLocalStore("testdata/blob1.store", StoreOptions{}) diff --git a/selfseed_test.go b/selfseed_test.go index 6753b85..5d521c3 100644 --- a/selfseed_test.go +++ b/selfseed_test.go @@ -11,11 +11,7 @@ import ( func TestSelfSeed(t *testing.T) { // Setup a temporary store - store, err := ioutil.TempDir("", "store") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(store) + store := t.TempDir() s, err := NewLocalStore(store, StoreOptions{}) if err != nil { diff --git a/tar_test.go b/tar_test.go index dbd49d5..7991b11 100644 --- a/tar_test.go +++ b/tar_test.go @@ -15,11 +15,7 @@ import ( func TestTar(t *testing.T) { // First make a tempdir and create a few dirs and files in it - base, err := ioutil.TempDir("", "desync-test") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(base) + base := t.TempDir() dirs := []string{ "dir1/sub11", @@ -28,7 +24,7 @@ func TestTar(t *testing.T) { "dir2/sub22", } for _, d := range dirs { - if err = os.MkdirAll(filepath.Join(base, d), 0755); err != nil { + if err := os.MkdirAll(filepath.Join(base, d), 0755); err != nil { t.Fatal() } } @@ -41,14 +37,14 @@ func TestTar(t *testing.T) { ioutil.WriteFile(filepath.Join(base, name), []byte(fmt.Sprintf("filecontent%d", i)), 0644) } - if err = os.Symlink("dir1", filepath.Join(base, "symlink")); err != nil { + if err := os.Symlink("dir1", filepath.Join(base, "symlink")); err != nil { t.Fatal(err) } // Encode it all into a buffer fs := NewLocalFS(base, LocalFSOptions{}) b := new(bytes.Buffer) - if err = Tar(context.Background(), b, fs); err != nil { + if err := Tar(context.Background(), b, fs); err != nil { t.Fatal(err) }