From f867e65bcd83451277273113d69d29af1d951e7b Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Sat, 30 Jan 2021 20:43:53 +0100 Subject: [PATCH] Fix issues reported by staticcheck --- cmd/restic/global_test.go | 2 +- cmd/restic/integration_test.go | 39 +++++++++++++++++++++++------- internal/backend/rclone/backend.go | 3 +-- 3 files changed, 32 insertions(+), 12 deletions(-) diff --git a/cmd/restic/global_test.go b/cmd/restic/global_test.go index 2a0b05ceb..fee5294b5 100644 --- a/cmd/restic/global_test.go +++ b/cmd/restic/global_test.go @@ -54,7 +54,7 @@ func TestReadRepo(t *testing.T) { var opts3 GlobalOptions opts3.RepositoryFile = foo + "-invalid" - repo, err = ReadRepo(opts3) + _, err = ReadRepo(opts3) if err == nil { t.Fatal("must not read repository path from invalid file path") } diff --git a/cmd/restic/integration_test.go b/cmd/restic/integration_test.go index b32d2dc39..7d198d336 100644 --- a/cmd/restic/integration_test.go +++ b/cmd/restic/integration_test.go @@ -166,7 +166,7 @@ func testRunDiffOutput(gopts GlobalOptions, firstSnapshotID string, secondSnapsh ShowMetadata: false, } err := runDiff(opts, gopts, []string{firstSnapshotID, secondSnapshotID}) - return string(buf.Bytes()), err + return buf.String(), err } func testRunRebuildIndex(t testing.TB, gopts GlobalOptions) { @@ -657,7 +657,11 @@ func TestBackupTags(t *testing.T) { testRunBackup(t, "", []string{env.testdata}, opts, env.gopts) testRunCheck(t, env.gopts) newest, _ := testRunSnapshots(t, env.gopts) - rtest.Assert(t, newest != nil, "expected a new backup, got nil") + + if newest == nil { + t.Fatal("expected a backup, got nil") + } + rtest.Assert(t, len(newest.Tags) == 0, "expected no tags, got %v", newest.Tags) parent := newest @@ -666,7 +670,11 @@ func TestBackupTags(t *testing.T) { testRunBackup(t, "", []string{env.testdata}, opts, env.gopts) testRunCheck(t, env.gopts) newest, _ = testRunSnapshots(t, env.gopts) - rtest.Assert(t, newest != nil, "expected a new backup, got nil") + + if newest == nil { + t.Fatal("expected a backup, got nil") + } + rtest.Assert(t, len(newest.Tags) == 1 && newest.Tags[0] == "NL", "expected one NL tag, got %v", newest.Tags) // Tagged backup should have untagged backup as parent. @@ -833,7 +841,10 @@ func TestTag(t *testing.T) { testRunBackup(t, "", []string{env.testdata}, BackupOptions{}, env.gopts) testRunCheck(t, env.gopts) newest, _ := testRunSnapshots(t, env.gopts) - rtest.Assert(t, newest != nil, "expected a new backup, got nil") + if newest == nil { + t.Fatal("expected a new backup, got nil") + } + rtest.Assert(t, len(newest.Tags) == 0, "expected no tags, got %v", newest.Tags) rtest.Assert(t, newest.Original == nil, @@ -843,7 +854,9 @@ func TestTag(t *testing.T) { testRunTag(t, TagOptions{SetTags: restic.TagLists{[]string{"NL"}}}, env.gopts) testRunCheck(t, env.gopts) newest, _ = testRunSnapshots(t, env.gopts) - rtest.Assert(t, newest != nil, "expected a new backup, got nil") + if newest == nil { + t.Fatal("expected a backup, got nil") + } rtest.Assert(t, len(newest.Tags) == 1 && newest.Tags[0] == "NL", "set failed, expected one NL tag, got %v", newest.Tags) rtest.Assert(t, newest.Original != nil, "expected original snapshot id, got nil") @@ -853,7 +866,9 @@ func TestTag(t *testing.T) { testRunTag(t, TagOptions{AddTags: restic.TagLists{[]string{"CH"}}}, env.gopts) testRunCheck(t, env.gopts) newest, _ = testRunSnapshots(t, env.gopts) - rtest.Assert(t, newest != nil, "expected a new backup, got nil") + if newest == nil { + t.Fatal("expected a backup, got nil") + } rtest.Assert(t, len(newest.Tags) == 2 && newest.Tags[0] == "NL" && newest.Tags[1] == "CH", "add failed, expected CH,NL tags, got %v", newest.Tags) rtest.Assert(t, newest.Original != nil, "expected original snapshot id, got nil") @@ -863,7 +878,9 @@ func TestTag(t *testing.T) { testRunTag(t, TagOptions{RemoveTags: restic.TagLists{[]string{"NL"}}}, env.gopts) testRunCheck(t, env.gopts) newest, _ = testRunSnapshots(t, env.gopts) - rtest.Assert(t, newest != nil, "expected a new backup, got nil") + if newest == nil { + t.Fatal("expected a backup, got nil") + } rtest.Assert(t, len(newest.Tags) == 1 && newest.Tags[0] == "CH", "remove failed, expected one CH tag, got %v", newest.Tags) rtest.Assert(t, newest.Original != nil, "expected original snapshot id, got nil") @@ -874,7 +891,9 @@ func TestTag(t *testing.T) { testRunTag(t, TagOptions{RemoveTags: restic.TagLists{[]string{"CH", "US", "RU"}}}, env.gopts) testRunCheck(t, env.gopts) newest, _ = testRunSnapshots(t, env.gopts) - rtest.Assert(t, newest != nil, "expected a new backup, got nil") + if newest == nil { + t.Fatal("expected a backup, got nil") + } rtest.Assert(t, len(newest.Tags) == 0, "expected no tags, got %v", newest.Tags) rtest.Assert(t, newest.Original != nil, "expected original snapshot id, got nil") @@ -885,7 +904,9 @@ func TestTag(t *testing.T) { testRunTag(t, TagOptions{SetTags: restic.TagLists{[]string{""}}}, env.gopts) testRunCheck(t, env.gopts) newest, _ = testRunSnapshots(t, env.gopts) - rtest.Assert(t, newest != nil, "expected a new backup, got nil") + if newest == nil { + t.Fatal("expected a backup, got nil") + } rtest.Assert(t, len(newest.Tags) == 0, "expected no tags, got %v", newest.Tags) rtest.Assert(t, newest.Original != nil, "expected original snapshot id, got nil") diff --git a/internal/backend/rclone/backend.go b/internal/backend/rclone/backend.go index 25169fd58..9aa36c9d3 100644 --- a/internal/backend/rclone/backend.go +++ b/internal/backend/rclone/backend.go @@ -228,12 +228,11 @@ func newBackend(cfg Config, lim limiter.Limiter) (*Backend, error) { // rclone is able to accept HTTP requests. url := fmt.Sprintf("http://localhost/file-%d", rand.Uint64()) - req, err := http.NewRequest(http.MethodGet, url, nil) + req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) if err != nil { return nil, err } req.Header.Set("Accept", rest.ContentTypeV2) - req.Cancel = ctx.Done() res, err := ctxhttp.Do(ctx, client, req) if err != nil {