2018-05-27 12:51:42 +02:00
|
|
|
package termstatus
|
|
|
|
|
|
2023-02-11 14:51:58 +01:00
|
|
|
import (
|
2023-05-05 11:10:02 +02:00
|
|
|
"bytes"
|
|
|
|
|
"context"
|
2025-09-14 19:21:51 +02:00
|
|
|
"errors"
|
2023-05-05 11:10:02 +02:00
|
|
|
"fmt"
|
|
|
|
|
"io"
|
2023-02-11 14:51:58 +01:00
|
|
|
"testing"
|
|
|
|
|
|
2025-09-07 13:49:26 +02:00
|
|
|
"github.com/restic/restic/internal/terminal"
|
2023-02-11 14:51:58 +01:00
|
|
|
rtest "github.com/restic/restic/internal/test"
|
|
|
|
|
)
|
|
|
|
|
|
2023-05-05 11:10:02 +02:00
|
|
|
func TestSetStatus(t *testing.T) {
|
|
|
|
|
var buf bytes.Buffer
|
2025-09-18 22:17:21 +02:00
|
|
|
term := New(nil, &buf, io.Discard, false)
|
2023-05-05 11:10:02 +02:00
|
|
|
|
|
|
|
|
term.canUpdateStatus = true
|
|
|
|
|
term.fd = ^uintptr(0)
|
2025-09-07 13:49:26 +02:00
|
|
|
term.clearCurrentLine = terminal.PosixClearCurrentLine
|
|
|
|
|
term.moveCursorUp = terminal.PosixMoveCursorUp
|
2023-05-05 11:10:02 +02:00
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
go term.Run(ctx)
|
|
|
|
|
|
|
|
|
|
const (
|
2025-09-07 13:49:26 +02:00
|
|
|
cl = terminal.PosixControlClearLine
|
|
|
|
|
home = terminal.PosixControlMoveCursorHome
|
|
|
|
|
up = terminal.PosixControlMoveCursorUp
|
2023-05-05 11:10:02 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
term.SetStatus([]string{"first"})
|
2025-02-28 20:00:40 +00:00
|
|
|
exp := home + cl + "first" + home
|
2023-05-05 11:10:02 +02:00
|
|
|
|
2024-07-06 11:27:35 +02:00
|
|
|
term.SetStatus([]string{""})
|
2025-02-28 20:00:40 +00:00
|
|
|
exp += home + cl + "" + home
|
2024-07-06 11:27:35 +02:00
|
|
|
|
|
|
|
|
term.SetStatus([]string{})
|
2025-02-28 20:00:40 +00:00
|
|
|
exp += home + cl + "" + home
|
2024-07-06 11:27:35 +02:00
|
|
|
|
|
|
|
|
// already empty status
|
|
|
|
|
term.SetStatus([]string{})
|
|
|
|
|
|
2023-05-05 11:10:02 +02:00
|
|
|
term.SetStatus([]string{"foo", "bar", "baz"})
|
2025-02-28 20:00:40 +00:00
|
|
|
exp += home + cl + "foo\n" + home + cl + "bar\n" +
|
|
|
|
|
home + cl + "baz" + home + up + up
|
2023-05-05 11:10:02 +02:00
|
|
|
|
|
|
|
|
term.SetStatus([]string{"quux", "needs\nquote"})
|
2025-02-28 20:00:40 +00:00
|
|
|
exp += home + cl + "quux\n" +
|
|
|
|
|
home + cl + "\"needs\\nquote\"\n" +
|
|
|
|
|
home + cl + home + up + up // Clear third line
|
2023-05-05 11:10:02 +02:00
|
|
|
|
|
|
|
|
cancel()
|
2025-02-28 20:00:40 +00:00
|
|
|
exp += home + cl + "\n" + home + cl + home + up // Status cleared
|
2023-05-05 11:10:02 +02:00
|
|
|
|
|
|
|
|
<-term.closed
|
|
|
|
|
rtest.Equals(t, exp, buf.String())
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-01 18:21:08 +02:00
|
|
|
func TestSanitizeLines(t *testing.T) {
|
|
|
|
|
var tests = []struct {
|
|
|
|
|
input []string
|
|
|
|
|
width int
|
|
|
|
|
output []string
|
|
|
|
|
}{
|
|
|
|
|
{[]string{""}, 80, []string{""}},
|
|
|
|
|
{[]string{"too long test line"}, 10, []string{"too long"}},
|
|
|
|
|
{[]string{"too long test line", "text"}, 10, []string{"too long\n", "text"}},
|
|
|
|
|
{[]string{"too long test line", "second long test line"}, 10, []string{"too long\n", "second l"}},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, test := range tests {
|
2023-05-05 11:10:02 +02:00
|
|
|
t.Run(fmt.Sprintf("%s %d", test.input, test.width), func(t *testing.T) {
|
2023-05-01 18:21:08 +02:00
|
|
|
out := sanitizeLines(test.input, test.width)
|
2023-05-05 11:10:02 +02:00
|
|
|
rtest.Equals(t, test.output, out)
|
2023-05-01 18:21:08 +02:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-09-14 19:21:51 +02:00
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
}
|