From 0bbfb072afac77f4e2d26b0ab426e8d75e10ed95 Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Fri, 5 Jun 2026 14:48:44 +0200 Subject: [PATCH] ui/progress: unexport NoopPrinter and add New* function --- cmd/restic/cmd_check_test.go | 4 ++-- cmd/restic/cmd_init_integration_test.go | 4 ++-- cmd/restic/cmd_key_integration_test.go | 4 ++-- internal/repository/prune_internal_test.go | 4 ++-- internal/repository/prune_test.go | 8 +++---- internal/repository/repack_test.go | 2 +- internal/repository/repair_index_test.go | 2 +- internal/repository/repair_pack_test.go | 4 ++-- internal/restorer/restorer_test.go | 6 ++--- internal/restorer/restorer_unix_test.go | 3 ++- internal/ui/backup/progress_test.go | 4 ++-- internal/ui/progress/printer.go | 27 +++++++++++++--------- internal/ui/restore/progress_test.go | 4 ++-- 13 files changed, 41 insertions(+), 35 deletions(-) diff --git a/cmd/restic/cmd_check_test.go b/cmd/restic/cmd_check_test.go index 58b50dae3..db05051f7 100644 --- a/cmd/restic/cmd_check_test.go +++ b/cmd/restic/cmd_check_test.go @@ -204,7 +204,7 @@ func TestPrepareCheckCache(t *testing.T) { rtest.OK(t, err) } gopts := global.Options{CacheDir: tmpDirBase} - cleanup := prepareCheckCache(testCase.opts, &gopts, &progress.NoopPrinter{}) + cleanup := prepareCheckCache(testCase.opts, &gopts, progress.NewNoopPrinter()) files, err := os.ReadDir(tmpDirBase) rtest.OK(t, err) @@ -234,7 +234,7 @@ func TestPrepareCheckCache(t *testing.T) { func TestPrepareDefaultCheckCache(t *testing.T) { gopts := global.Options{CacheDir: ""} - cleanup := prepareCheckCache(CheckOptions{}, &gopts, &progress.NoopPrinter{}) + cleanup := prepareCheckCache(CheckOptions{}, &gopts, progress.NewNoopPrinter()) _, err := os.ReadDir(gopts.CacheDir) rtest.OK(t, err) diff --git a/cmd/restic/cmd_init_integration_test.go b/cmd/restic/cmd_init_integration_test.go index ef08eabe5..5d8a8c64c 100644 --- a/cmd/restic/cmd_init_integration_test.go +++ b/cmd/restic/cmd_init_integration_test.go @@ -57,14 +57,14 @@ func TestInitCopyChunkerParams(t *testing.T) { var repo *repository.Repository err = withTermStatus(t, env.gopts, func(ctx context.Context, gopts global.Options) error { - repo, err = global.OpenRepository(ctx, gopts, &progress.NoopPrinter{}) + repo, err = global.OpenRepository(ctx, gopts, progress.NewNoopPrinter()) return err }) rtest.OK(t, err) var otherRepo *repository.Repository err = withTermStatus(t, env2.gopts, func(ctx context.Context, gopts global.Options) error { - otherRepo, err = global.OpenRepository(ctx, gopts, &progress.NoopPrinter{}) + otherRepo, err = global.OpenRepository(ctx, gopts, progress.NewNoopPrinter()) return err }) rtest.OK(t, err) diff --git a/cmd/restic/cmd_key_integration_test.go b/cmd/restic/cmd_key_integration_test.go index abc5d96c7..35e949020 100644 --- a/cmd/restic/cmd_key_integration_test.go +++ b/cmd/restic/cmd_key_integration_test.go @@ -63,7 +63,7 @@ func testRunKeyAddNewKeyUserHost(t testing.TB, gopts global.Options) { rtest.OK(t, err) _ = withTermStatus(t, gopts, func(ctx context.Context, gopts global.Options) error { - repo, err := global.OpenRepository(ctx, gopts, &progress.NoopPrinter{}) + repo, err := global.OpenRepository(ctx, gopts, progress.NewNoopPrinter()) rtest.OK(t, err) key, err := repository.SearchKey(ctx, repo, testKeyNewPassword, 2, "") rtest.OK(t, err) @@ -105,7 +105,7 @@ func testRunKeyPasswdUserHost(t testing.TB, newPassword string, gopts global.Opt gopts.Password = testKeyNewPassword _ = withTermStatus(t, gopts, func(ctx context.Context, gopts global.Options) error { - repo, err := global.OpenRepository(ctx, gopts, &progress.NoopPrinter{}) + repo, err := global.OpenRepository(ctx, gopts, progress.NewNoopPrinter()) rtest.OK(t, err) key, err := repository.SearchKey(ctx, repo, testKeyNewPassword, 1, "") rtest.OK(t, err) diff --git a/internal/repository/prune_internal_test.go b/internal/repository/prune_internal_test.go index 640ab061b..94fcdad41 100644 --- a/internal/repository/prune_internal_test.go +++ b/internal/repository/prune_internal_test.go @@ -70,10 +70,10 @@ func TestPruneMaxUnusedDuplicate(t *testing.T) { usedBlobs.Insert(blob) } return nil - }, &progress.NoopPrinter{}) + }, progress.NewNoopPrinter()) rtest.OK(t, err) - rtest.OK(t, plan.Execute(context.TODO(), &progress.NoopPrinter{})) + rtest.OK(t, plan.Execute(context.TODO(), progress.NewNoopPrinter())) rsize := plan.Stats().Size remainingUnusedSize := rsize.Duplicate + rsize.Unused - rsize.Remove - rsize.Repackrm diff --git a/internal/repository/prune_test.go b/internal/repository/prune_test.go index fc3c4dbbe..609935aa0 100644 --- a/internal/repository/prune_test.go +++ b/internal/repository/prune_test.go @@ -41,10 +41,10 @@ func testPrune(t *testing.T, opts repository.PruneOptions, errOnUnused bool) { usedBlobs.Insert(blob) } return nil - }, &progress.NoopPrinter{}) + }, progress.NewNoopPrinter()) rtest.OK(t, err) - rtest.OK(t, plan.Execute(context.TODO(), &progress.NoopPrinter{})) + rtest.OK(t, plan.Execute(context.TODO(), progress.NewNoopPrinter())) repo = repository.TestOpenBackend(t, be) repository.TestCheckRepo(t, repo) @@ -165,9 +165,9 @@ func TestPruneSmall(t *testing.T) { usedBlobs.Insert(blob) } return nil - }, &progress.NoopPrinter{}) + }, progress.NewNoopPrinter()) rtest.OK(t, err) - rtest.OK(t, plan.Execute(context.TODO(), &progress.NoopPrinter{})) + rtest.OK(t, plan.Execute(context.TODO(), progress.NewNoopPrinter())) stats := plan.Stats() rtest.Equals(t, stats.Size.Used/blobSize, uint64(numBlobsCreated), fmt.Sprintf("total size of blobs should be %d but is %d", diff --git a/internal/repository/repack_test.go b/internal/repository/repack_test.go index fc7d86900..c490759ba 100644 --- a/internal/repository/repack_test.go +++ b/internal/repository/repack_test.go @@ -162,7 +162,7 @@ func repack(t *testing.T, repo restic.Repository, be backend.Backend, packs rest func rebuildAndReloadIndex(t *testing.T, repo *repository.Repository) { rtest.OK(t, repository.RepairIndex(context.TODO(), repo, repository.RepairIndexOptions{ ReadAllPacks: true, - }, &progress.NoopPrinter{})) + }, progress.NewNoopPrinter())) rtest.OK(t, repo.LoadIndex(context.TODO(), nil)) } diff --git a/internal/repository/repair_index_test.go b/internal/repository/repair_index_test.go index c6b095696..3a39f4fa5 100644 --- a/internal/repository/repair_index_test.go +++ b/internal/repository/repair_index_test.go @@ -33,7 +33,7 @@ func testRebuildIndex(t *testing.T, readAllPacks bool, damage func(t *testing.T, repo = repository.TestOpenBackend(t, be) rtest.OK(t, repository.RepairIndex(context.TODO(), repo, repository.RepairIndexOptions{ ReadAllPacks: readAllPacks, - }, &progress.NoopPrinter{})) + }, progress.NewNoopPrinter())) repository.TestCheckRepo(t, repo) } diff --git a/internal/repository/repair_pack_test.go b/internal/repository/repair_pack_test.go index 54f0ca02a..558889e0d 100644 --- a/internal/repository/repair_pack_test.go +++ b/internal/repository/repair_pack_test.go @@ -106,7 +106,7 @@ func testRepairBrokenPack(t *testing.T, version uint) { buf, err := backendtest.LoadAll(context.TODO(), be, h) rtest.OK(t, err) rtest.OK(t, be.Remove(context.TODO(), h)) - rtest.OK(t, repository.RepairIndex(context.TODO(), repo, repository.RepairIndexOptions{}, &progress.NoopPrinter{})) + rtest.OK(t, repository.RepairIndex(context.TODO(), repo, repository.RepairIndexOptions{}, progress.NewNoopPrinter())) rtest.OK(t, be.Save(context.TODO(), h, backend.NewByteReader(buf, be.Hasher()))) @@ -130,7 +130,7 @@ func testRepairBrokenPack(t *testing.T, version uint) { toRepair, damagedBlobs := test.damage(t, random, repo, be, packsBefore) - rtest.OK(t, repository.RepairPacks(context.TODO(), repo, toRepair, &progress.NoopPrinter{})) + rtest.OK(t, repository.RepairPacks(context.TODO(), repo, toRepair, progress.NewNoopPrinter())) // reload index rtest.OK(t, repo.LoadIndex(context.TODO(), nil)) diff --git a/internal/restorer/restorer_test.go b/internal/restorer/restorer_test.go index 337a99918..25e4fc071 100644 --- a/internal/restorer/restorer_test.go +++ b/internal/restorer/restorer_test.go @@ -989,7 +989,7 @@ func TestRestorerSparseOverwrite(t *testing.T) { type printerMock struct { s restoreui.State - progress.NoopPrinter + progress.Printer } func (p *printerMock) Update(_ restoreui.State, _ time.Duration) { @@ -1102,7 +1102,7 @@ func TestRestorerOverwriteBehavior(t *testing.T) { for _, test := range tests { t.Run("", func(t *testing.T) { - mock := &printerMock{} + mock := &printerMock{Printer: progress.NewNoopPrinter()} progress := restoreui.NewProgress(mock, 0) tempdir := saveSnapshotsAndOverwrite(t, baseSnapshot, overwriteSnapshot, Options{}, Options{Overwrite: test.Overwrite, Progress: progress}) @@ -1154,7 +1154,7 @@ func TestRestorerOverwritePartial(t *testing.T) { }, } - mock := &printerMock{} + mock := &printerMock{Printer: progress.NewNoopPrinter()} progress := restoreui.NewProgress(mock, 0) saveSnapshotsAndOverwrite(t, baseSnapshot, overwriteSnapshot, Options{}, Options{Overwrite: OverwriteAlways, Progress: progress}) progress.Finish() diff --git a/internal/restorer/restorer_unix_test.go b/internal/restorer/restorer_unix_test.go index a7912ea03..cad4976b4 100644 --- a/internal/restorer/restorer_unix_test.go +++ b/internal/restorer/restorer_unix_test.go @@ -13,6 +13,7 @@ import ( "github.com/restic/restic/internal/repository" rtest "github.com/restic/restic/internal/test" + "github.com/restic/restic/internal/ui/progress" restoreui "github.com/restic/restic/internal/ui/restore" ) @@ -87,7 +88,7 @@ func testRestorerProgressBar(t *testing.T, dryRun bool) { }, }, noopGetGenericAttributes) - mock := &printerMock{} + mock := &printerMock{Printer: progress.NewNoopPrinter()} progress := restoreui.NewProgress(mock, 0) res := NewRestorer(repo, sn, Options{Progress: progress, DryRun: dryRun}) diff --git a/internal/ui/backup/progress_test.go b/internal/ui/backup/progress_test.go index 7b53f2116..0afd4e85d 100644 --- a/internal/ui/backup/progress_test.go +++ b/internal/ui/backup/progress_test.go @@ -13,7 +13,7 @@ import ( type mockPrinter struct { sync.Mutex - progress.NoopPrinter + progress.Printer dirUnchanged, fileNew bool id restic.ID } @@ -48,7 +48,7 @@ func (p *mockPrinter) Reset() {} func TestProgress(t *testing.T) { t.Parallel() - prnt := &mockPrinter{} + prnt := &mockPrinter{Printer: progress.NewNoopPrinter()} prog := NewProgress(prnt, time.Millisecond) prog.StartFile("foo") diff --git a/internal/ui/progress/printer.go b/internal/ui/progress/printer.go index 5bf9d5017..f9b37808e 100644 --- a/internal/ui/progress/printer.go +++ b/internal/ui/progress/printer.go @@ -32,27 +32,32 @@ type Printer interface { VV(msg string, args ...interface{}) } -// NoopPrinter discards all messages -type NoopPrinter struct{} +// noopPrinter discards all messages. +type noopPrinter struct{} -var _ Printer = (*NoopPrinter)(nil) +var _ Printer = (*noopPrinter)(nil) -func (*NoopPrinter) NewCounter(_ string) *Counter { +// NewNoopPrinter returns a Printer that discards all messages. +func NewNoopPrinter() Printer { + return &noopPrinter{} +} + +func (*noopPrinter) NewCounter(_ string) *Counter { return nil } -func (*NoopPrinter) NewCounterTerminalOnly(_ string) *Counter { +func (*noopPrinter) NewCounterTerminalOnly(_ string) *Counter { return nil } -func (*NoopPrinter) E(_ string, _ ...interface{}) {} +func (*noopPrinter) E(_ string, _ ...interface{}) {} -func (*NoopPrinter) S(_ string, _ ...interface{}) {} +func (*noopPrinter) S(_ string, _ ...interface{}) {} -func (*NoopPrinter) PT(_ string, _ ...interface{}) {} +func (*noopPrinter) PT(_ string, _ ...interface{}) {} -func (*NoopPrinter) P(_ string, _ ...interface{}) {} +func (*noopPrinter) P(_ string, _ ...interface{}) {} -func (*NoopPrinter) V(_ string, _ ...interface{}) {} +func (*noopPrinter) V(_ string, _ ...interface{}) {} -func (*NoopPrinter) VV(_ string, _ ...interface{}) {} +func (*noopPrinter) VV(_ string, _ ...interface{}) {} diff --git a/internal/ui/restore/progress_test.go b/internal/ui/restore/progress_test.go index 4c59d11ee..85fe8f553 100644 --- a/internal/ui/restore/progress_test.go +++ b/internal/ui/restore/progress_test.go @@ -37,7 +37,7 @@ type mockPrinter struct { trace printerTrace items itemTrace errors errorTrace - progress.NoopPrinter + progress.Printer } const mockFinishDuration = 42 * time.Second @@ -57,7 +57,7 @@ func (p *mockPrinter) Finish(progress State, _ time.Duration) { } func testProgress(fn func(progress *Progress) bool) (printerTrace, itemTrace, errorTrace) { - printer := &mockPrinter{} + printer := &mockPrinter{Printer: progress.NewNoopPrinter()} progress := NewProgress(printer, 0) final := fn(progress) progress.update(0, final)