Skip to content
Myr

light_query_test.go

This file

Path

src/tui/light_query_test.go

182 lines · 4.4 KiB · Go

Last changed by

Fix panic on a read error during the second-chance readJunegunn Choibefore adoption33a3456

History of this file

Take it

1//go:build !windows23package tui45import (6	"os"7	"strings"8	"testing"9	"time"1011	"golang.org/x/term"12)1314// Drives queryStartup against a terminal simulated by pipes, with the replies15// already queued so the exchange is deterministic.16func replyingTerminal(t *testing.T, replies string) (*LightRenderer, func() string) {17	t.Helper()18	inR, inW, err := os.Pipe()19	if err != nil {20		t.Fatal(err)21	}22	outR, outW, err := os.Pipe()23	if err != nil {24		t.Fatal(err)25	}26	if _, err := inW.WriteString(replies); err != nil {27		t.Fatal(err)28	}29	t.Cleanup(func() { inR.Close(); inW.Close(); outR.Close() })3031	r := &LightRenderer{ttyin: inR, ttyout: outW, escDelay: defaultEscDelay}32	written := func() string {33		outW.Close()34		var sb strings.Builder35		buf := make([]byte, 4096)36		for {37			n, err := outR.Read(buf)38			sb.Write(buf[:n])39			if err != nil {40				break41			}42		}43		return sb.String()44	}45	return r, written46}4748const eraseEcho = "\b \b"4950func TestQueryStartup(t *testing.T) {51	for _, tc := range []struct {52		name     string53		replies  string54		row, col int55		paste    string // "", "true" or "false"56		erase    int    // columns the echo took, and so erasures expected57	}{58		{59			name:    "mode already set",60			replies: "\x1b[5;10R\x1b[?2004;1$y\x1b[5;10R",61			row:     4, col: 9, paste: "true",62		},63		{64			name:    "mode reset",65			replies: "\x1b[5;10R\x1b[?2004;2$y\x1b[5;10R",66			row:     4, col: 9, paste: "false",67		},68		{69			name:    "mode not recognized",70			replies: "\x1b[5;10R\x1b[?2004;0$y\x1b[5;10R",71			row:     4, col: 9,72		},73		{74			// Answers the position queries but ignores DECRQM without printing75			name:    "query ignored",76			replies: "\x1b[5;10R\x1b[5;10R",77			row:     4, col: 9,78		},79		{80			// macOS Terminal.app: ends the sequence at '$' and prints the 'p'81			name:    "query echoed",82			replies: "\x1b[5;10R\x1b[5;11R",83			row:     4, col: 9, erase: 1,84		},85		{86			// A terminal that prints more of what it could not parse87			name:    "longer echo",88			replies: "\x1b[5;10R\x1b[5;13R",89			row:     4, col: 9, erase: 3,90		},91		{92			// The echo wrapped to the next line, where erasing would guess93			name:    "echo wrapped",94			replies: "\x1b[5;80R\x1b[6;1R",95			row:     4, col: 79,96		},97		{98			name:    "no reply at all",99			replies: "",100			row:     -1, col: -1,101		},102	} {103		t.Run(tc.name, func(t *testing.T) {104			r, written := replyingTerminal(t, tc.replies)105			row, col, pasteWasSet := r.queryStartup()106107			if row != tc.row || col != tc.col {108				t.Errorf("offset (%d,%d), want (%d,%d)", row, col, tc.row, tc.col)109			}110			paste := ""111			if pasteWasSet != nil {112				paste = "false"113				if *pasteWasSet {114					paste = "true"115				}116			}117			if paste != tc.paste {118				t.Errorf("pasteWasSet %q, want %q", paste, tc.paste)119			}120121			out := written()122			if got := strings.Count(out, eraseEcho); got != tc.erase {123				t.Errorf("erased %d columns, want %d (wrote %q)", got, tc.erase, out)124			}125			// The paste mode query has to sit between two position queries126			if want := "\x1b[6n\x1b[?2004$p\x1b[6n"; !strings.Contains(out, want) {127				t.Errorf("queries %q, want %q in order", out, want)128			}129		})130	}131}132133// The second-chance read has no time limit, so a key typed long after an134// unfinished sequence must not be dropped with it.135func TestLateKeyAfterUnfinishedSequence(t *testing.T) {136	inR, inW, err := os.Pipe()137	if err != nil {138		t.Fatal(err)139	}140	t.Cleanup(func() { inR.Close(); inW.Close() })141	inW.WriteString("\x1b[12")142	go func() {143		time.Sleep(2 * defaultEscDelay * time.Millisecond)144		inW.WriteString("x")145		time.Sleep(2 * defaultEscDelay * time.Millisecond)146		inW.WriteString("y") // ends the wait if x was dropped147	}()148149	r := &LightRenderer{ttyin: inR, escDelay: defaultEscDelay}150	for range 3 {151		if ev := r.GetChar(false); ev.Type == Rune {152			if ev.Char != 'x' {153				t.Errorf("got %q, want 'x'", ev.Char)154			}155			return156		}157	}158	t.Error("the late key was dropped")159}160161// A read error during the second-chance read must end fzf cleanly, not panic.162func TestSecondChanceReadError(t *testing.T) {163	inR, inW, err := os.Pipe()164	if err != nil {165		t.Fatal(err)166	}167	t.Cleanup(func() { inR.Close() })168	inW.WriteString("\x1b[12")169	go func() {170		time.Sleep(2 * defaultEscDelay * time.Millisecond)171		inW.Close()172	}()173174	out, err := os.OpenFile(os.DevNull, os.O_WRONLY, 0)175	if err != nil {176		t.Fatal(err)177	}178	r := &LightRenderer{ttyin: inR, ttyout: out, escDelay: defaultEscDelay, origState: &term.State{}}179	if ev := r.GetChar(false); ev.Type != Fatal {180		t.Errorf("got %s, want Fatal", ev.Type.String())181	}182}