2018-05-27 10:51:42 +00:00
|
|
|
package termstatus
|
|
|
|
|
2023-02-11 13:51:58 +00:00
|
|
|
import (
|
2023-05-05 09:10:02 +00:00
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2023-02-11 13:51:58 +00:00
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
rtest "github.com/restic/restic/internal/test"
|
|
|
|
)
|
|
|
|
|
2023-05-05 09:10:02 +00:00
|
|
|
func TestSetStatus(t *testing.T) {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
term := New(&buf, io.Discard, false)
|
|
|
|
|
|
|
|
term.canUpdateStatus = true
|
|
|
|
term.fd = ^uintptr(0)
|
|
|
|
term.clearCurrentLine = posixClearCurrentLine
|
|
|
|
term.moveCursorUp = posixMoveCursorUp
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
go term.Run(ctx)
|
|
|
|
|
|
|
|
const (
|
|
|
|
clear = posixControlClearLine
|
|
|
|
home = posixControlMoveCursorHome
|
|
|
|
up = posixControlMoveCursorUp
|
|
|
|
)
|
|
|
|
|
|
|
|
term.SetStatus([]string{"first"})
|
|
|
|
exp := home + clear + "first" + home
|
|
|
|
|
2024-07-06 09:27:35 +00:00
|
|
|
term.SetStatus([]string{""})
|
|
|
|
exp += home + clear + "" + home
|
|
|
|
|
|
|
|
term.SetStatus([]string{})
|
|
|
|
exp += home + clear + "" + home
|
|
|
|
|
|
|
|
// already empty status
|
|
|
|
term.SetStatus([]string{})
|
|
|
|
|
2023-05-05 09:10:02 +00:00
|
|
|
term.SetStatus([]string{"foo", "bar", "baz"})
|
|
|
|
exp += home + clear + "foo\n" + home + clear + "bar\n" +
|
|
|
|
home + clear + "baz" + home + up + up
|
|
|
|
|
|
|
|
term.SetStatus([]string{"quux", "needs\nquote"})
|
|
|
|
exp += home + clear + "quux\n" +
|
|
|
|
home + clear + "\"needs\\nquote\"\n" +
|
2024-01-21 20:16:33 +00:00
|
|
|
home + clear + home + up + up // Clear third line
|
2023-05-05 09:10:02 +00:00
|
|
|
|
|
|
|
cancel()
|
2024-01-21 20:16:33 +00:00
|
|
|
exp += home + clear + "\n" + home + clear + home + up // Status cleared
|
2023-05-05 09:10:02 +00:00
|
|
|
|
|
|
|
<-term.closed
|
|
|
|
rtest.Equals(t, exp, buf.String())
|
|
|
|
}
|
|
|
|
|
2023-02-11 13:51:58 +00:00
|
|
|
func TestQuote(t *testing.T) {
|
|
|
|
for _, c := range []struct {
|
|
|
|
in string
|
|
|
|
needQuote bool
|
|
|
|
}{
|
|
|
|
{"foo.bar/baz", false},
|
|
|
|
{"föó_bàŕ-bãẑ", false},
|
|
|
|
{" foo ", false},
|
|
|
|
{"foo bar", false},
|
|
|
|
{"foo\nbar", true},
|
|
|
|
{"foo\rbar", true},
|
|
|
|
{"foo\abar", true},
|
|
|
|
{"\xff", true},
|
|
|
|
{`c:\foo\bar`, false},
|
|
|
|
// Issue #2260: terminal control characters.
|
|
|
|
{"\x1bm_red_is_beautiful", true},
|
|
|
|
} {
|
|
|
|
if c.needQuote {
|
|
|
|
rtest.Equals(t, strconv.Quote(c.in), Quote(c.in))
|
|
|
|
} else {
|
|
|
|
rtest.Equals(t, c.in, Quote(c.in))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-05-27 10:51:42 +00:00
|
|
|
|
|
|
|
func TestTruncate(t *testing.T) {
|
|
|
|
var tests = []struct {
|
|
|
|
input string
|
2020-11-02 11:06:21 +00:00
|
|
|
width int
|
2018-05-27 10:51:42 +00:00
|
|
|
output string
|
|
|
|
}{
|
|
|
|
{"", 80, ""},
|
|
|
|
{"", 0, ""},
|
|
|
|
{"", -1, ""},
|
|
|
|
{"foo", 80, "foo"},
|
|
|
|
{"foo", 4, "foo"},
|
|
|
|
{"foo", 3, "foo"},
|
|
|
|
{"foo", 2, "fo"},
|
|
|
|
{"foo", 1, "f"},
|
|
|
|
{"foo", 0, ""},
|
|
|
|
{"foo", -1, ""},
|
2020-11-02 11:06:21 +00:00
|
|
|
{"Löwen", 4, "Löwe"},
|
2021-08-29 12:54:15 +00:00
|
|
|
{"あああああ/data", 7, "あああ"},
|
|
|
|
{"あああああ/data", 10, "あああああ"},
|
|
|
|
{"あああああ/data", 11, "あああああ/"},
|
2018-05-27 10:51:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run("", func(t *testing.T) {
|
2021-08-29 12:54:15 +00:00
|
|
|
out := Truncate(test.input, test.width)
|
2018-05-27 10:51:42 +00:00
|
|
|
if out != test.output {
|
2020-11-02 11:06:21 +00:00
|
|
|
t.Fatalf("wrong output for input %v, width %d: want %q, got %q",
|
|
|
|
test.input, test.width, test.output, out)
|
2018-05-27 10:51:42 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-08-29 12:54:15 +00:00
|
|
|
|
|
|
|
func benchmarkTruncate(b *testing.B, s string, w int) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
Truncate(s, w)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkTruncateASCII(b *testing.B) {
|
|
|
|
s := "This is an ASCII-only status message...\r\n"
|
|
|
|
benchmarkTruncate(b, s, len(s)-1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkTruncateUnicode(b *testing.B) {
|
|
|
|
s := "Hello World or Καλημέρα κόσμε or こんにちは 世界"
|
|
|
|
w := 0
|
2023-04-14 09:13:39 +00:00
|
|
|
for i := 0; i < len(s); {
|
2021-08-29 12:54:15 +00:00
|
|
|
w++
|
2023-04-14 09:13:39 +00:00
|
|
|
wide, utfsize := wideRune(s[i:])
|
|
|
|
if wide {
|
2021-08-29 12:54:15 +00:00
|
|
|
w++
|
|
|
|
}
|
2023-04-14 09:13:39 +00:00
|
|
|
i += int(utfsize)
|
2021-08-29 12:54:15 +00:00
|
|
|
}
|
2023-04-14 09:13:39 +00:00
|
|
|
b.ResetTimer()
|
|
|
|
|
2021-08-29 12:54:15 +00:00
|
|
|
benchmarkTruncate(b, s, w-1)
|
|
|
|
}
|
2023-05-01 16:21:08 +00: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 09:10:02 +00:00
|
|
|
t.Run(fmt.Sprintf("%s %d", test.input, test.width), func(t *testing.T) {
|
2023-05-01 16:21:08 +00:00
|
|
|
out := sanitizeLines(test.input, test.width)
|
2023-05-05 09:10:02 +00:00
|
|
|
rtest.Equals(t, test.output, out)
|
2023-05-01 16:21:08 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|