Skip to content
Myr

tty_test.go

This file

Path

tty_test.go

190 lines · 4.7 KiB · Go

Last changed by

fix(tty): fall back to default dimensions when terminal size is 0x0 (#1836)adam maguedbefore adoption96d69d2

History of this file

Take it

1package tea23import (4	"bytes"5	"testing"6)78type windowSizeModel struct {9	windowSize WindowSizeMsg10	quit       bool11}1213func (m *windowSizeModel) Init() Cmd {14	return nil15}1617func (m *windowSizeModel) Update(msg Msg) (Model, Cmd) {18	switch msg := msg.(type) {19	case WindowSizeMsg:20		m.windowSize = msg21		m.quit = true22		return m, Quit23	}24	return m, nil25}2627func (m *windowSizeModel) View() View {28	return NewView("")29}3031func TestFallbackDimensions(t *testing.T) {32	t.Run("default fallback to 80x24", func(t *testing.T) {33		t.Setenv("COLUMNS", "")34		t.Setenv("LINES", "")3536		p := NewProgram(nil)37		w, h := p.fallbackDimensions(0, 0)38		if w != defaultWidth || h != defaultHeight {39			t.Errorf("expected %dx%d, got %dx%d", defaultWidth, defaultHeight, w, h)40		}4142		w, h = p.fallbackDimensions(-10, -5)43		if w != defaultWidth || h != defaultHeight {44			t.Errorf("expected %dx%d, got %dx%d", defaultWidth, defaultHeight, w, h)45		}46	})4748	t.Run("preserve positive dimensions", func(t *testing.T) {49		p := NewProgram(nil)50		w, h := p.fallbackDimensions(120, 40)51		if w != 120 || h != 40 {52			t.Errorf("expected 120x40, got %dx%d", w, h)53		}54	})5556	t.Run("mixed non-positive dimensions", func(t *testing.T) {57		t.Setenv("COLUMNS", "")58		t.Setenv("LINES", "")5960		p := NewProgram(nil)61		w, h := p.fallbackDimensions(0, 50)62		if w != defaultWidth || h != 50 {63			t.Errorf("expected %dx50, got %dx%d", defaultWidth, w, h)64		}6566		w, h = p.fallbackDimensions(100, 0)67		if w != 100 || h != defaultHeight {68			t.Errorf("expected 100x%d, got %dx%d", defaultHeight, w, h)69		}70	})7172	t.Run("fallback to COLUMNS and LINES from os env", func(t *testing.T) {73		t.Setenv("COLUMNS", "132")74		t.Setenv("LINES", "43")7576		p := NewProgram(nil)77		w, h := p.fallbackDimensions(0, 0)78		if w != 132 || h != 43 {79			t.Errorf("expected 132x43, got %dx%d", w, h)80		}81	})8283	t.Run("fallback to COLUMNS and LINES from program environment", func(t *testing.T) {84		t.Setenv("COLUMNS", "200")85		t.Setenv("LINES", "100")8687		p := NewProgram(nil, WithEnvironment([]string{"COLUMNS=150", "LINES=60"}))88		w, h := p.fallbackDimensions(0, 0)89		if w != 150 || h != 60 {90			t.Errorf("expected 150x60, got %dx%d", w, h)91		}92	})9394	t.Run("program environment without COLUMNS and LINES falls back to 80x24 skipping os env", func(t *testing.T) {95		t.Setenv("COLUMNS", "200")96		t.Setenv("LINES", "100")9798		p := NewProgram(nil, WithEnvironment([]string{"OTHER_VAR=1"}))99		w, h := p.fallbackDimensions(0, 0)100		if w != defaultWidth || h != defaultHeight {101			t.Errorf("expected %dx%d, got %dx%d", defaultWidth, defaultHeight, w, h)102		}103	})104105	t.Run("invalid or non-positive COLUMNS and LINES fall back to defaults", func(t *testing.T) {106		t.Setenv("COLUMNS", "invalid")107		t.Setenv("LINES", "-10")108109		p := NewProgram(nil)110		w, h := p.fallbackDimensions(0, 0)111		if w != defaultWidth || h != defaultHeight {112			t.Errorf("expected %dx%d, got %dx%d", defaultWidth, defaultHeight, w, h)113		}114115		t.Setenv("COLUMNS", "0")116		t.Setenv("LINES", "0")117118		w, h = p.fallbackDimensions(0, 0)119		if w != defaultWidth || h != defaultHeight {120			t.Errorf("expected %dx%d, got %dx%d", defaultWidth, defaultHeight, w, h)121		}122	})123}124125func TestProgramNonTTYDimensions(t *testing.T) {126	t.Run("non-tty output does not receive fallback dimensions", func(t *testing.T) {127		t.Setenv("COLUMNS", "110")128		t.Setenv("LINES", "35")129130		var buf bytes.Buffer131		var in bytes.Buffer132		m := &windowSizeModel{}133134		p := NewProgram(m,135			WithInput(&in),136			WithOutput(&buf),137		)138139		if _, err := p.Run(); err != nil {140			t.Fatalf("unexpected error: %v", err)141		}142143		if m.windowSize.Width != 0 || m.windowSize.Height != 0 {144			t.Errorf("expected WindowSizeMsg 0x0 for non-TTY output, got %dx%d",145				m.windowSize.Width, m.windowSize.Height)146		}147	})148149	t.Run("non-tty output with program environment does not receive fallback dimensions", func(t *testing.T) {150		var buf bytes.Buffer151		var in bytes.Buffer152		m := &windowSizeModel{}153154		p := NewProgram(m,155			WithInput(&in),156			WithOutput(&buf),157			WithEnvironment([]string{"COLUMNS=150", "LINES=60"}),158		)159160		if _, err := p.Run(); err != nil {161			t.Fatalf("unexpected error: %v", err)162		}163164		if m.windowSize.Width != 0 || m.windowSize.Height != 0 {165			t.Errorf("expected WindowSizeMsg 0x0 for non-TTY output, got %dx%d",166				m.windowSize.Width, m.windowSize.Height)167		}168	})169170	t.Run("non-tty output preserves WithWindowSize when specified", func(t *testing.T) {171		var buf bytes.Buffer172		var in bytes.Buffer173		m := &windowSizeModel{}174175		p := NewProgram(m,176			WithInput(&in),177			WithOutput(&buf),178			WithWindowSize(120, 50),179		)180181		if _, err := p.Run(); err != nil {182			t.Fatalf("unexpected error: %v", err)183		}184185		if m.windowSize.Width != 120 || m.windowSize.Height != 50 {186			t.Errorf("expected WindowSizeMsg 120x50, got %dx%d",187				m.windowSize.Width, m.windowSize.Height)188		}189	})190}