From 89d1bbc7437b19c7bca778b97bc704f56c5ed52a Mon Sep 17 00:00:00 2001 From: Julio Lopez <1953782+julio-lopez@users.noreply.github.com> Date: Tue, 1 Jul 2025 12:05:50 -0700 Subject: [PATCH] chore(general): minor cleanups (#4704) - use `slices.Clone` - remove stale `.gometalinter.json` - unexport `maintenance.dropDeletedContents` - rename `fetchIndexBlob` - use `require` in `TestTimeFuncWiring` --- .gometalinter.json | 9 ----- internal/repotesting/repotesting_test.go | 46 +++++++---------------- repo/content/committed_content_index.go | 10 ++--- repo/maintenance/drop_deleted_contents.go | 4 +- repo/maintenance/maintenance_run.go | 2 +- snapshot/manifest.go | 3 +- 6 files changed, 23 insertions(+), 51 deletions(-) delete mode 100644 .gometalinter.json diff --git a/.gometalinter.json b/.gometalinter.json deleted file mode 100644 index dea60f4af..000000000 --- a/.gometalinter.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "Disable": ["maligned","gas","gosec"], - "Exclude": [ - "playground/", - ".+\\.pb\\.go", - ".+_test\\.go" - ], - "Deadline": "120s" -} \ No newline at end of file diff --git a/internal/repotesting/repotesting_test.go b/internal/repotesting/repotesting_test.go index 53e6a25e8..38d4f6936 100644 --- a/internal/repotesting/repotesting_test.go +++ b/internal/repotesting/repotesting_test.go @@ -24,9 +24,7 @@ func TestTimeFuncWiring(t *testing.T) { // Re open with injected time rep, err := repo.Open(ctx, env.RepositoryWriter.ConfigFilename(), env.Password, &repo.Options{TimeNowFunc: ft.NowFunc()}) - if err != nil { - t.Fatal("Failed to open repo:", err) - } + require.NoError(t, err, "failed to open test repository") r0 := testutil.EnsureType[repo.DirectRepository](t, rep) @@ -34,30 +32,22 @@ func TestTimeFuncWiring(t *testing.T) { require.NoError(t, err) // verify wiring for the repo layer - if got, want := env.RepositoryWriter.Time(), ft.NowFunc()(); !got.Equal(want) { - t.Errorf("times don't match, got %v, want %v", got, want) - } + got, want := env.RepositoryWriter.Time(), ft.NowFunc()() + require.WithinDuration(t, want, got, 0, "times do not match") - if want, got := ft.Advance(10*time.Minute), env.RepositoryWriter.Time(); !got.Equal(want) { - t.Errorf("times don't match, got %v, want %v", got, want) - } + want, got = ft.Advance(10*time.Minute), env.RepositoryWriter.Time() + require.WithinDuration(t, want, got, 0, "times do not match") // verify wiring for the content layer nt := ft.Advance(20 * time.Second) cid, err := env.RepositoryWriter.ContentManager().WriteContent(ctx, gather.FromSlice([]byte("foo")), "", content.NoCompression) - if err != nil { - t.Fatal("failed to write content:", err) - } + require.NoError(t, err, "failed to write content") info, err := env.RepositoryWriter.ContentInfo(ctx, cid) - if err != nil { - t.Fatal("failed to get content info for", cid, err) - } - if got, want := info.Timestamp(), nt; !got.Equal(want) { - t.Errorf("content time does not match, got %v, want %v", got, want) - } + require.NoErrorf(t, err, "failed to get content info for %s", cid) + require.WithinDuration(t, nt, info.Timestamp(), 0, "content time does not match") // verify wiring for the manifest layer nt = ft.Advance(3 * time.Minute) @@ -65,18 +55,12 @@ func TestTimeFuncWiring(t *testing.T) { labels := map[string]string{"l1": "v1", "l2": "v2", "type": "my-manifest"} mid, err := env.RepositoryWriter.PutManifest(ctx, labels, "manifest content") - if err != nil { - t.Fatal("failed to put manifest:", err) - } + require.NoError(t, err, "failed to put manifest") meta, err := env.RepositoryWriter.GetManifest(ctx, mid, nil) - if err != nil { - t.Fatal("failed to get manifest metadata:", err) - } - if got, want := meta.ModTime, nt; !got.Equal(want) { - t.Errorf("manifest time does not match, got %v, want %v", got, want) - } + require.NoError(t, err, "failed to get manifest metadata") + require.WithinDuration(t, nt, meta.ModTime, 0, "manifest modification time does not match") const defaultPermissions = 0o777 @@ -89,11 +73,7 @@ func TestTimeFuncWiring(t *testing.T) { policyTree := policy.BuildTree(nil, policy.DefaultPolicy) s1, err := u.Upload(ctx, sourceDir, policyTree, snapshot.SourceInfo{}) - if err != nil { - t.Fatal("failed to create snapshot:", err) - } - if got, want := nt, s1.StartTime.ToTime(); !got.Equal(want) { - t.Fatalf("snapshot time does not match, got: %v, want: %v", got, want) - } + require.NoError(t, err, "failed to create snapshot") + require.WithinDuration(t, nt, s1.StartTime.ToTime(), 0, "snapshot time does not match") } diff --git a/repo/content/committed_content_index.go b/repo/content/committed_content_index.go index 75b52e85b..d479852ba 100644 --- a/repo/content/committed_content_index.go +++ b/repo/content/committed_content_index.go @@ -42,8 +42,8 @@ type committedContentIndex struct { v1PerContentOverhead func() int formatProvider format.Provider - // fetchOne loads one index blob - fetchOne func(ctx context.Context, blobID blob.ID, output *gather.WriteBuffer) error + // fetchIndexBlob retrieves one index blob from storage + fetchIndexBlob func(ctx context.Context, blobID blob.ID, output *gather.WriteBuffer) error log logging.Logger } @@ -321,7 +321,7 @@ func (c *committedContentIndex) fetchIndexBlobs(ctx context.Context, isPermissiv for indexBlobID := range ch { data.Reset() - if err := c.fetchOne(ctx, indexBlobID, &data); err != nil { + if err := c.fetchIndexBlob(ctx, indexBlobID, &data); err != nil { if isPermissiveCacheLoading { c.log.Errorf("skipping bad read of index blob %v", indexBlobID) continue @@ -371,7 +371,7 @@ func newCommittedContentIndex(caching *CachingOptions, v1PerContentOverhead func() int, formatProvider format.Provider, permissiveCacheLoading bool, - fetchOne func(ctx context.Context, blobID blob.ID, output *gather.WriteBuffer) error, + fetchIndexBlob func(ctx context.Context, blobID blob.ID, output *gather.WriteBuffer) error, log logging.Logger, minSweepAge time.Duration, ) *committedContentIndex { @@ -393,7 +393,7 @@ func newCommittedContentIndex(caching *CachingOptions, inUse: map[blob.ID]index.Index{}, v1PerContentOverhead: v1PerContentOverhead, formatProvider: formatProvider, - fetchOne: fetchOne, + fetchIndexBlob: fetchIndexBlob, log: log, } } diff --git a/repo/maintenance/drop_deleted_contents.go b/repo/maintenance/drop_deleted_contents.go index 9bb26ae98..3ed8d4cac 100644 --- a/repo/maintenance/drop_deleted_contents.go +++ b/repo/maintenance/drop_deleted_contents.go @@ -8,8 +8,8 @@ "github.com/kopia/kopia/repo/content/indexblob" ) -// DropDeletedContents rewrites indexes while dropping deleted contents above certain age. -func DropDeletedContents(ctx context.Context, rep repo.DirectRepositoryWriter, dropDeletedBefore time.Time, safety SafetyParameters) error { +// dropDeletedContents rewrites indexes while dropping deleted contents above certain age. +func dropDeletedContents(ctx context.Context, rep repo.DirectRepositoryWriter, dropDeletedBefore time.Time, safety SafetyParameters) error { log(ctx).Infof("Dropping contents deleted before %v", dropDeletedBefore) //nolint:wrapcheck diff --git a/repo/maintenance/maintenance_run.go b/repo/maintenance/maintenance_run.go index f2638d677..0a3168c67 100644 --- a/repo/maintenance/maintenance_run.go +++ b/repo/maintenance/maintenance_run.go @@ -418,7 +418,7 @@ func runTaskDropDeletedContentsFull(ctx context.Context, runParams RunParameters log(ctx).Infof("Found safe time to drop indexes: %v", safeDropTime) return ReportRun(ctx, runParams.rep, TaskDropDeletedContentsFull, s, func() error { - return DropDeletedContents(ctx, runParams.rep, safeDropTime, safety) + return dropDeletedContents(ctx, runParams.rep, safeDropTime, safety) }) } diff --git a/snapshot/manifest.go b/snapshot/manifest.go index 5a8d44bab..cb7aff2f1 100644 --- a/snapshot/manifest.go +++ b/snapshot/manifest.go @@ -3,6 +3,7 @@ import ( "context" "encoding/json" + "slices" "sort" "strconv" @@ -238,7 +239,7 @@ func GroupBySource(manifests []*Manifest) [][]*Manifest { // SortByTime returns a slice of manifests sorted by start time. func SortByTime(manifests []*Manifest, reverse bool) []*Manifest { - result := append([]*Manifest(nil), manifests...) + result := slices.Clone(manifests) sort.Slice(result, func(i, j int) bool { return result[i].StartTime > result[j].StartTime == reverse })