From 10fdb914df657048a8ed04c9c745785efe418e70 Mon Sep 17 00:00:00 2001 From: greatroar <61184462+greatroar@users.noreply.github.com> Date: Sat, 1 Jun 2024 15:15:06 +0200 Subject: [PATCH] cmd: Return error in readPassword The returned error was always nil. Replaced Wrap by WithStack because the function name was stale. --- cmd/restic/global.go | 2 +- cmd/restic/global_test.go | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/cmd/restic/global.go b/cmd/restic/global.go index 6920caa8d..cc24b74c2 100644 --- a/cmd/restic/global.go +++ b/cmd/restic/global.go @@ -280,7 +280,7 @@ func readPassword(in io.Reader) (password string, err error) { sc := bufio.NewScanner(in) sc.Scan() - return sc.Text(), errors.Wrap(err, "Scan") + return sc.Text(), errors.WithStack(sc.Err()) } // readPasswordTerminal reads the password from the given reader which must be a diff --git a/cmd/restic/global_test.go b/cmd/restic/global_test.go index 4f5c29e9a..b43fdd1f3 100644 --- a/cmd/restic/global_test.go +++ b/cmd/restic/global_test.go @@ -5,6 +5,7 @@ import ( "path/filepath" "testing" + "github.com/restic/restic/internal/errors" rtest "github.com/restic/restic/internal/test" ) @@ -22,6 +23,16 @@ func Test_PrintFunctionsRespectsGlobalStdout(t *testing.T) { } } +type errorReader struct{ err error } + +func (r *errorReader) Read([]byte) (int, error) { return 0, r.err } + +func TestReadPassword(t *testing.T) { + want := errors.New("foo") + _, err := readPassword(&errorReader{want}) + rtest.Assert(t, errors.Is(err, want), "wrong error %v", err) +} + func TestReadRepo(t *testing.T) { tempDir := rtest.TempDir(t)