Apply go fix -testingcontext ./...

This commit is contained in:
Michael Eischer
2026-07-22 22:27:15 +02:00
parent 58f92708c2
commit 14e523a1d9
14 changed files with 48 additions and 102 deletions
+13 -26
View File
@@ -132,8 +132,7 @@ func TestArchiverSaveFile(t *testing.T) {
for _, testfile := range tests {
t.Run("", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ctx := t.Context()
tempdir, repo := prepareTempdirRepoSrc(t, TestDir{"file": testfile})
node, stats := saveFile(t, repo, filepath.Join(tempdir, "file"), fs.Track{FS: fs.NewLocal()})
@@ -165,8 +164,7 @@ func TestArchiverSaveFileReaderFS(t *testing.T) {
for _, test := range tests {
t.Run("", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ctx := t.Context()
repo := repository.TestRepository(t)
@@ -206,8 +204,7 @@ func TestArchiverSave(t *testing.T) {
for _, testfile := range tests {
t.Run("", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ctx := t.Context()
tempdir, repo := prepareTempdirRepoSrc(t, TestDir{"file": testfile})
@@ -276,8 +273,7 @@ func TestArchiverSaveReaderFS(t *testing.T) {
for _, test := range tests {
t.Run("", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ctx := t.Context()
repo := repository.TestRepository(t)
@@ -1461,8 +1457,7 @@ func TestArchiverSnapshot(t *testing.T) {
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ctx := t.Context()
tempdir, repo := prepareTempdirRepoSrc(t, test.src)
@@ -1682,8 +1677,7 @@ func TestArchiverSnapshotSelect(t *testing.T) {
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ctx := t.Context()
tempdir, repo := prepareTempdirRepoSrc(t, test.src)
@@ -1790,8 +1784,7 @@ func TestArchiverExplicitBackupTarget(t *testing.T) {
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ctx := t.Context()
tempdir, repo := prepareTempdirRepoSrc(t, test.src)
@@ -1956,8 +1949,7 @@ func TestArchiverParent(t *testing.T) {
for _, test := range tests {
t.Run("", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ctx := t.Context()
tempdir, repo := prepareTempdirRepoSrc(t, test.src)
@@ -2144,8 +2136,7 @@ func TestArchiverErrorReporting(t *testing.T) {
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ctx := t.Context()
tempdir, repo := prepareTempdirRepoSrc(t, test.src)
@@ -2370,8 +2361,7 @@ func TestArchiverAbortEarlyOnError(t *testing.T) {
for _, test := range tests {
t.Run("", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ctx := t.Context()
tempdir, repo := prepareTempdirRepoSrc(t, test.src)
@@ -2618,8 +2608,7 @@ func TestRacyFileTypeSwap(t *testing.T) {
resetFIOnRead: true,
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ctx := t.Context()
_ = repo.WithBlobUploader(ctx, func(ctx context.Context, uploader restic.BlobSaverWithAsync) error {
wg, ctx := errgroup.WithContext(ctx)
@@ -2717,8 +2706,7 @@ func TestIrregularFile(t *testing.T) {
overrideErr: fmt.Errorf(`unsupported file type "irregular"`),
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ctx := t.Context()
arch := New(repo, fs.Track{FS: override}, Options{})
_, excluded, err := arch.save(ctx, "/", tempfile, nil, false)
@@ -2764,8 +2752,7 @@ func TestDisappearedFile(t *testing.T) {
back := rtest.Chdir(t, tempdir)
defer back()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ctx := t.Context()
// depending on the underlying FS implementation a missing file may be detected by OpenFile or
// the subsequent file.Stat() call. Thus test both cases.
+1 -2
View File
@@ -46,8 +46,7 @@ func startFileSaver(ctx context.Context, t testing.TB, _ fs.FS) (*fileSaver, *mo
}
func TestFileSaver(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ctx := t.Context()
startFn := func() {}
completeReadingFn := func() {}
+2 -4
View File
@@ -78,8 +78,7 @@ func TestScanner(t *testing.T) {
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ctx := t.Context()
tempdir := rtest.TempDir(t)
TestCreateFiles(t, tempdir, test.src)
@@ -213,8 +212,7 @@ func TestScannerError(t *testing.T) {
t.Skipf("skip on windows")
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ctx := t.Context()
tempdir := rtest.TempDir(t)
TestCreateFiles(t, tempdir, test.src)
+1 -3
View File
@@ -1,7 +1,6 @@
package archiver
import (
"context"
"fmt"
"os"
"path/filepath"
@@ -449,8 +448,7 @@ func TestTestEnsureSnapshot(t *testing.T) {
for _, test := range tests {
t.Run("", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ctx := t.Context()
tempdir := rtest.TempDir(t)
+3 -7
View File
@@ -2,7 +2,6 @@ package azure_test
import (
"bytes"
"context"
"fmt"
"io"
"os"
@@ -100,8 +99,7 @@ func TestBackendAzureAccountToken(t *testing.T) {
}
}
ctx, cancel := context.WithCancel(context.TODO())
defer cancel()
ctx := t.Context()
cfg, err := azure.ParseConfig(os.Getenv("RESTIC_TEST_AZURE_REPOSITORY"))
if err != nil {
@@ -143,8 +141,7 @@ func TestBackendAzureContainerToken(t *testing.T) {
}
}
ctx, cancel := context.WithCancel(context.TODO())
defer cancel()
ctx := t.Context()
cfg, err := azure.ParseConfig(os.Getenv("RESTIC_TEST_AZURE_REPOSITORY"))
if err != nil {
@@ -171,8 +168,7 @@ func TestUploadLargeFile(t *testing.T) {
return
}
ctx, cancel := context.WithCancel(context.TODO())
defer cancel()
ctx := t.Context()
if os.Getenv("RESTIC_TEST_AZURE_REPOSITORY") == "" {
t.Skipf("environment variables not available")
+2 -4
View File
@@ -170,8 +170,7 @@ func TestBackendREST(t *testing.T) {
}
}()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ctx := t.Context()
dir := rtest.TempDir(t)
serverURL, cleanup := runRESTServer(ctx, t, dir, ":0")
@@ -195,8 +194,7 @@ func TestBackendRESTExternalServer(t *testing.T) {
}
func BenchmarkBackendREST(t *testing.B) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ctx := t.Context()
dir := rtest.TempDir(t)
serverURL, cleanup := runRESTServer(ctx, t, dir, ":0")
+1 -3
View File
@@ -3,7 +3,6 @@
package rest_test
import (
"context"
"fmt"
"path"
"testing"
@@ -18,8 +17,7 @@ func TestBackendRESTWithUnixSocket(t *testing.T) {
}
}()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ctx := t.Context()
dir := rtest.TempDir(t)
serverURL, cleanup := runRESTServer(ctx, t, path.Join(dir, "data"), fmt.Sprintf("unix:%s", path.Join(dir, "sock")))
+1 -3
View File
@@ -2,7 +2,6 @@ package dump
import (
"bytes"
"context"
"testing"
"github.com/restic/restic/internal/archiver"
@@ -80,8 +79,7 @@ func WriteTest(t *testing.T, format string, cd CheckDump) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ctx := t.Context()
tmpdir, repo, be := prepareTempdirRepoSrc(t, tt.args)
arch := archiver.New(repo, fs.Track{FS: fs.NewLocal()}, archiver.Options{})
+1 -2
View File
@@ -69,8 +69,7 @@ func loadTree(t testing.TB, repo restic.Loader, id restic.ID) data.TreeNodeItera
func TestFuseFile(t *testing.T) {
repo := repository.TestRepository(t)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ctx := t.Context()
timestamp, err := time.Parse(time.RFC3339, "2017-01-24T10:42:56+01:00")
rtest.OK(t, err)
@@ -300,8 +300,7 @@ func testStreamPack(t *testing.T, version uint) {
for _, test := range tests {
t.Run("", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ctx := t.Context()
gotBlobs := make(map[restic.ID]int)
@@ -379,8 +378,7 @@ func testStreamPack(t *testing.T, version uint) {
for _, test := range tests {
t.Run("", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ctx := t.Context()
handleBlob := func(blob restic.BlobHandle, buf []byte, err error) error {
return err
+13 -26
View File
@@ -390,8 +390,7 @@ func TestRestorer(t *testing.T) {
return nil
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ctx := t.Context()
countRestoredFiles, err := res.RestoreTo(ctx, tempdir)
if err != nil {
@@ -488,8 +487,7 @@ func TestRestorerRelative(t *testing.T) {
return nil
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ctx := t.Context()
countRestoredFiles, err := res.RestoreTo(ctx, "restore")
if err != nil {
@@ -743,8 +741,7 @@ func TestRestorerTraverseTree(t *testing.T) {
res.SelectFilter = test.Select
tempdir := rtest.TempDir(t)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ctx := t.Context()
// make sure we're creating a new subdir of the tempdir
target := filepath.Join(tempdir, "target")
@@ -834,8 +831,7 @@ func TestRestorerConsistentTimestampsAndPermissions(t *testing.T) {
}
tempdir := rtest.TempDir(t)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ctx := t.Context()
_, err := res.RestoreTo(ctx, tempdir)
rtest.OK(t, err)
@@ -872,8 +868,7 @@ func TestVerifyCancel(t *testing.T) {
res := NewRestorer(repo, sn, Options{})
tempdir := rtest.TempDir(t)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ctx := t.Context()
countRestoredFiles, err := res.RestoreTo(ctx, tempdir)
rtest.OK(t, err)
err = os.WriteFile(filepath.Join(tempdir, "foo"), []byte("bar"), 0644)
@@ -913,8 +908,7 @@ func TestRestorerSparseFiles(t *testing.T) {
res := NewRestorer(repo, sn, Options{Sparse: true})
tempdir := rtest.TempDir(t)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ctx := t.Context()
_, err = res.RestoreTo(ctx, tempdir)
rtest.OK(t, err)
@@ -1217,8 +1211,7 @@ func TestRestoreModified(t *testing.T) {
repo := repository.TestRepository(t)
tempdir := filepath.Join(rtest.TempDir(t), "target")
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ctx := t.Context()
for _, snapshot := range snapshots {
sn, id := saveSnapshot(t, repo, snapshot, noopGetGenericAttributes)
@@ -1245,8 +1238,7 @@ func TestRestoreIfChanged(t *testing.T) {
repo := repository.TestRepository(t)
tempdir := filepath.Join(rtest.TempDir(t), "target")
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ctx := t.Context()
sn, id := saveSnapshot(t, repo, snapshot, noopGetGenericAttributes)
t.Logf("snapshot saved as %v", id.Str())
@@ -1302,8 +1294,7 @@ func TestRestoreDryRun(t *testing.T) {
repo := repository.TestRepository(t)
tempdir := filepath.Join(rtest.TempDir(t), "target")
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ctx := t.Context()
sn, id := saveSnapshot(t, repo, snapshot, noopGetGenericAttributes)
t.Logf("snapshot saved as %v", id.Str())
@@ -1326,8 +1317,7 @@ func TestRestoreDryRunDelete(t *testing.T) {
repo := repository.TestRepository(t)
tempdir := filepath.Join(rtest.TempDir(t), "target")
tempfile := filepath.Join(tempdir, "existing")
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ctx := t.Context()
rtest.OK(t, os.Mkdir(tempdir, 0o755))
f, err := os.Create(tempfile)
@@ -1452,8 +1442,7 @@ func TestRestoreDelete(t *testing.T) {
for _, test := range tests {
t.Run("", func(t *testing.T) {
res := NewRestorer(repo, sn, Options{})
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ctx := t.Context()
_, err := res.RestoreTo(ctx, tempdir)
rtest.OK(t, err)
@@ -1490,8 +1479,7 @@ func TestRestoreToFile(t *testing.T) {
// create a file in the place of the target directory
rtest.OK(t, os.WriteFile(tempdir, []byte{}, 0o700))
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ctx := t.Context()
sn, _ := saveSnapshot(t, repo, snapshot, noopGetGenericAttributes)
res := NewRestorer(repo, sn, Options{})
@@ -1524,8 +1512,7 @@ func TestRestorerLongPath(t *testing.T) {
rtest.OK(t, err)
res := NewRestorer(repo, sn, Options{})
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ctx := t.Context()
countRestoredFiles, err := res.RestoreTo(ctx, tmp)
rtest.OK(t, err)
+2 -4
View File
@@ -32,8 +32,7 @@ func TestRestorerRestoreEmptyHardlinkedFields(t *testing.T) {
res := NewRestorer(repo, sn, Options{})
tempdir := rtest.TempDir(t)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ctx := t.Context()
_, err := res.RestoreTo(ctx, tempdir)
rtest.OK(t, err)
@@ -115,8 +114,7 @@ func TestRestorePermissions(t *testing.T) {
repo := repository.TestRepository(t)
tempdir := filepath.Join(rtest.TempDir(t), "target")
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ctx := t.Context()
sn, id := saveSnapshot(t, repo, snapshot, noopGetGenericAttributes)
t.Logf("snapshot saved as %v", id.Str())
+5 -11
View File
@@ -1,7 +1,6 @@
package walker
import (
"context"
"slices"
"testing"
@@ -253,8 +252,7 @@ func TestRewriter(t *testing.T) {
expRepo, expRoot := BuildTreeMap(test.newTree)
modrepo := data.TestWritableTreeMap{TestTreeMap: repo}
ctx, cancel := context.WithCancel(context.TODO())
defer cancel()
ctx := t.Context()
rewriter, last := test.check(t)
newRoot, err := rewriter.RewriteTree(ctx, modrepo, modrepo, "/", root)
@@ -294,8 +292,7 @@ func TestSnapshotSizeQuery(t *testing.T) {
expRepo, expRoot := BuildTreeMap(newTree)
modrepo := data.TestWritableTreeMap{TestTreeMap: repo}
ctx, cancel := context.WithCancel(context.TODO())
defer cancel()
ctx := t.Context()
rewriteNode := func(node *data.Node, path string) *data.Node {
if path == "/bar" {
@@ -383,8 +380,7 @@ func TestRewriterKeepEmptyDirectory(t *testing.T) {
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ctx := t.Context()
repo, root := BuildTreeMap(TestTree{"empty": TestTree{}})
modrepo := data.TestWritableTreeMap{TestTreeMap: repo}
@@ -403,8 +399,7 @@ func TestRewriterFailOnUnknownFields(t *testing.T) {
id := restic.Hash(node)
tm.TestTreeMap[id] = node
ctx, cancel := context.WithCancel(context.TODO())
defer cancel()
ctx := t.Context()
rewriter := NewTreeRewriter(RewriteOpts{
RewriteNode: func(node *data.Node, path string) *data.Node {
@@ -435,8 +430,7 @@ func TestRewriterTreeLoadError(t *testing.T) {
tm := data.TestWritableTreeMap{TestTreeMap: data.TestTreeMap{}}
id := restic.NewRandomID()
ctx, cancel := context.WithCancel(context.TODO())
defer cancel()
ctx := t.Context()
// also check that load error by default cause the operation to fail
rewriter := NewTreeRewriter(RewriteOpts{})
+1 -3
View File
@@ -1,7 +1,6 @@
package walker
import (
"context"
"fmt"
"sort"
"testing"
@@ -461,8 +460,7 @@ func TestWalker(t *testing.T) {
repo, root := BuildTreeMap(test.tree)
for _, check := range test.checks {
t.Run("", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.TODO())
defer cancel()
ctx := t.Context()
fn, leaveDir, last := check(t)
err := Walk(ctx, repo, root, WalkVisitor{