fix capturing stdout with termstatus

This commit is contained in:
Michael Eischer
2025-09-14 13:51:30 +02:00
parent 91ecac8003
commit ef9930cce4
9 changed files with 11 additions and 10 deletions

View File

@@ -15,7 +15,7 @@ import (
) )
func testRunDiffOutput(gopts GlobalOptions, firstSnapshotID string, secondSnapshotID string) (string, error) { func testRunDiffOutput(gopts GlobalOptions, firstSnapshotID string, secondSnapshotID string) (string, error) {
buf, err := withCaptureStdout(func() error { buf, err := withCaptureStdout(gopts, func(gopts GlobalOptions) error {
opts := DiffOptions{ opts := DiffOptions{
ShowMetadata: false, ShowMetadata: false,
} }

View File

@@ -11,7 +11,7 @@ import (
) )
func testRunFind(t testing.TB, wantJSON bool, opts FindOptions, gopts GlobalOptions, pattern string) []byte { func testRunFind(t testing.TB, wantJSON bool, opts FindOptions, gopts GlobalOptions, pattern string) []byte {
buf, err := withCaptureStdout(func() error { buf, err := withCaptureStdout(gopts, func(gopts GlobalOptions) error {
gopts.JSON = wantJSON gopts.JSON = wantJSON
return runFind(context.TODO(), opts, gopts, []string{pattern}) return runFind(context.TODO(), opts, gopts, []string{pattern})

View File

@@ -15,7 +15,7 @@ import (
) )
func testRunKeyListOtherIDs(t testing.TB, gopts GlobalOptions) []string { func testRunKeyListOtherIDs(t testing.TB, gopts GlobalOptions) []string {
buf, err := withCaptureStdout(func() error { buf, err := withCaptureStdout(gopts, func(gopts GlobalOptions) error {
return runKeyList(context.TODO(), gopts, []string{}) return runKeyList(context.TODO(), gopts, []string{})
}) })
rtest.OK(t, err) rtest.OK(t, err)

View File

@@ -11,7 +11,7 @@ import (
) )
func testRunList(t testing.TB, gopts GlobalOptions, tpe string) restic.IDs { func testRunList(t testing.TB, gopts GlobalOptions, tpe string) restic.IDs {
buf, err := withCaptureStdout(func() error { buf, err := withCaptureStdout(gopts, func(gopts GlobalOptions) error {
return runList(context.TODO(), gopts, []string{tpe}) return runList(context.TODO(), gopts, []string{tpe})
}) })
rtest.OK(t, err) rtest.OK(t, err)

View File

@@ -13,7 +13,7 @@ import (
) )
func testRunLsWithOpts(t testing.TB, gopts GlobalOptions, opts LsOptions, args []string) []byte { func testRunLsWithOpts(t testing.TB, gopts GlobalOptions, opts LsOptions, args []string) []byte {
buf, err := withCaptureStdout(func() error { buf, err := withCaptureStdout(gopts, func(gopts GlobalOptions) error {
gopts.Quiet = true gopts.Quiet = true
return runLs(context.TODO(), opts, gopts, args) return runLs(context.TODO(), opts, gopts, args)
}) })

View File

@@ -90,7 +90,7 @@ func createPrunableRepo(t *testing.T, env *testEnvironment) {
} }
func testRunForgetJSON(t testing.TB, gopts GlobalOptions, args ...string) { func testRunForgetJSON(t testing.TB, gopts GlobalOptions, args ...string) {
buf, err := withCaptureStdout(func() error { buf, err := withCaptureStdout(gopts, func(gopts GlobalOptions) error {
gopts.JSON = true gopts.JSON = true
opts := ForgetOptions{ opts := ForgetOptions{
DryRun: true, DryRun: true,

View File

@@ -10,7 +10,7 @@ import (
) )
func testRunSnapshots(t testing.TB, gopts GlobalOptions) (newest *Snapshot, snapmap map[restic.ID]Snapshot) { func testRunSnapshots(t testing.TB, gopts GlobalOptions) (newest *Snapshot, snapmap map[restic.ID]Snapshot) {
buf, err := withCaptureStdout(func() error { buf, err := withCaptureStdout(gopts, func(gopts GlobalOptions) error {
gopts.JSON = true gopts.JSON = true
opts := SnapshotOptions{} opts := SnapshotOptions{}

View File

@@ -17,7 +17,7 @@ func Test_PrintFunctionsRespectsGlobalStdout(t *testing.T) {
func() { Print("message\n") }, func() { Print("message\n") },
func() { Printf("mes%s\n", "sage") }, func() { Printf("mes%s\n", "sage") },
} { } {
buf, _ := withCaptureStdout(func() error { buf, _ := withCaptureStdout(GlobalOptions{}, func(_ GlobalOptions) error {
p() p()
return nil return nil
}) })

View File

@@ -406,11 +406,12 @@ func withRestoreGlobalOptions(inner func() error) error {
return inner() return inner()
} }
func withCaptureStdout(inner func() error) (*bytes.Buffer, error) { func withCaptureStdout(gopts GlobalOptions, inner func(gopts GlobalOptions) error) (*bytes.Buffer, error) {
buf := bytes.NewBuffer(nil) buf := bytes.NewBuffer(nil)
err := withRestoreGlobalOptions(func() error { err := withRestoreGlobalOptions(func() error {
globalOptions.stdout = buf globalOptions.stdout = buf
return inner() gopts.stdout = buf
return inner(gopts)
}) })
return buf, err return buf, err