From d83aa44a3a04cb159de0d7f66bdc3e4c97879b74 Mon Sep 17 00:00:00 2001 From: ivanauth Date: Mon, 4 May 2026 16:02:47 -0400 Subject: [PATCH] test: cover printBackupFileLocation stdout and path handling --- internal/cmd/backup_test.go | 42 +++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/internal/cmd/backup_test.go b/internal/cmd/backup_test.go index 0cee4581..2b2a331f 100644 --- a/internal/cmd/backup_test.go +++ b/internal/cmd/backup_test.go @@ -24,6 +24,7 @@ import ( "github.com/authzed/spicedb/pkg/tuple" "github.com/authzed/zed/internal/client" + "github.com/authzed/zed/internal/console" "github.com/authzed/zed/internal/storage" "github.com/authzed/zed/internal/zedtesting" "github.com/authzed/zed/pkg/backupformat" @@ -589,6 +590,47 @@ func TestBackupRestoreCmdFunc(t *testing.T) { require.Equal(t, "test/resource:1#reader@test/user:1", tuple.MustV1StringRelationship(rrResp.Relationship)) } +func TestPrintBackupFileLocation(t *testing.T) { + previous := console.Errorf + t.Cleanup(func() { + console.Errorf = previous + }) + + t.Run("does not print when destination is stdout", func(t *testing.T) { + var captured strings.Builder + console.Errorf = func(format string, a ...any) { + fmt.Fprintf(&captured, format, a...) + } + + printBackupFileLocation("-") + require.Empty(t, captured.String()) + }) + + t.Run("prints absolute path for relative filename", func(t *testing.T) { + var captured strings.Builder + console.Errorf = func(format string, a ...any) { + fmt.Fprintf(&captured, format, a...) + } + + printBackupFileLocation("backup.zedbackup") + + wd, err := os.Getwd() + require.NoError(t, err) + require.Equal(t, fmt.Sprintf("Backup written to %s\n", filepath.Join(wd, "backup.zedbackup")), captured.String()) + }) + + t.Run("prints absolute path as-is when already absolute", func(t *testing.T) { + var captured strings.Builder + console.Errorf = func(format string, a ...any) { + fmt.Fprintf(&captured, format, a...) + } + + abs := filepath.Join(t.TempDir(), "backup.zedbackup") + printBackupFileLocation(abs) + require.Equal(t, fmt.Sprintf("Backup written to %s\n", abs), captured.String()) + }) +} + func TestAddSizeErrInfo(t *testing.T) { tcs := []struct { name string