diff --git a/internal/archiver/archiver_test.go b/internal/archiver/archiver_test.go index d0ce9c827..99aafea3d 100644 --- a/internal/archiver/archiver_test.go +++ b/internal/archiver/archiver_test.go @@ -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. diff --git a/internal/archiver/file_saver_test.go b/internal/archiver/file_saver_test.go index 91d2aa11b..e71bf6cec 100644 --- a/internal/archiver/file_saver_test.go +++ b/internal/archiver/file_saver_test.go @@ -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() {} diff --git a/internal/archiver/scanner_test.go b/internal/archiver/scanner_test.go index c2ec8fdb8..71898f402 100644 --- a/internal/archiver/scanner_test.go +++ b/internal/archiver/scanner_test.go @@ -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) diff --git a/internal/archiver/testing_test.go b/internal/archiver/testing_test.go index 0805bd5b2..1c5450ccd 100644 --- a/internal/archiver/testing_test.go +++ b/internal/archiver/testing_test.go @@ -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) diff --git a/internal/backend/azure/azure_test.go b/internal/backend/azure/azure_test.go index f755b21d2..149b58257 100644 --- a/internal/backend/azure/azure_test.go +++ b/internal/backend/azure/azure_test.go @@ -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") diff --git a/internal/backend/rest/rest_test.go b/internal/backend/rest/rest_test.go index 109259b92..b0073d5e5 100644 --- a/internal/backend/rest/rest_test.go +++ b/internal/backend/rest/rest_test.go @@ -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") diff --git a/internal/backend/rest/rest_unix_test.go b/internal/backend/rest/rest_unix_test.go index 2c565f8da..d22b6b00e 100644 --- a/internal/backend/rest/rest_unix_test.go +++ b/internal/backend/rest/rest_unix_test.go @@ -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"))) diff --git a/internal/dump/common_test.go b/internal/dump/common_test.go index 042d41f90..53cf17490 100644 --- a/internal/dump/common_test.go +++ b/internal/dump/common_test.go @@ -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{}) diff --git a/internal/fuse/fuse_test.go b/internal/fuse/fuse_test.go index 9e9afbcaf..aed188dcd 100644 --- a/internal/fuse/fuse_test.go +++ b/internal/fuse/fuse_test.go @@ -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) diff --git a/internal/repository/repository_internal_test.go b/internal/repository/repository_internal_test.go index e5e0c4b5a..60e2c0ceb 100644 --- a/internal/repository/repository_internal_test.go +++ b/internal/repository/repository_internal_test.go @@ -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 diff --git a/internal/restorer/restorer_test.go b/internal/restorer/restorer_test.go index 29b1d770a..fea41f3d2 100644 --- a/internal/restorer/restorer_test.go +++ b/internal/restorer/restorer_test.go @@ -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) diff --git a/internal/restorer/restorer_unix_test.go b/internal/restorer/restorer_unix_test.go index 6b1388af4..641b89aaf 100644 --- a/internal/restorer/restorer_unix_test.go +++ b/internal/restorer/restorer_unix_test.go @@ -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()) diff --git a/internal/walker/rewriter_test.go b/internal/walker/rewriter_test.go index 7cb34c9d0..ca7f5deef 100644 --- a/internal/walker/rewriter_test.go +++ b/internal/walker/rewriter_test.go @@ -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{}) diff --git a/internal/walker/walker_test.go b/internal/walker/walker_test.go index 6f8a5529a..d0c5c59b0 100644 --- a/internal/walker/walker_test.go +++ b/internal/walker/walker_test.go @@ -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{