Files
gastown/internal/tmux/tmux_test.go
Steve Yegge 4048cdc373 fix(lint): resolve all errcheck warnings
Fix ~50 errcheck warnings across the codebase:

- Add explicit `_ =` for intentionally ignored error returns (cleanup,
  best-effort operations, etc.)
- Use `defer func() { _ = ... }()` pattern for defer statements
- Handle tmux SetEnvironment, KillSession, SendKeysRaw returns
- Handle mail router.Send returns
- Handle os.RemoveAll, os.Rename in cleanup paths
- Handle rand.Read returns for ID generation
- Handle fmt.Fprint* returns when writing to io.Writer
- Fix for-select with single case to use for-range
- Handle cobra MarkFlagRequired returns

All tests pass. Code compiles without errors.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-19 12:44:42 -08:00

212 lines
4.5 KiB
Go

package tmux
import (
"os/exec"
"strings"
"testing"
)
func hasTmux() bool {
_, err := exec.LookPath("tmux")
return err == nil
}
func TestListSessionsNoServer(t *testing.T) {
if !hasTmux() {
t.Skip("tmux not installed")
}
tm := NewTmux()
sessions, err := tm.ListSessions()
// Should not error even if no server running
if err != nil {
t.Fatalf("ListSessions: %v", err)
}
// Result may be nil or empty slice
_ = sessions
}
func TestHasSessionNoServer(t *testing.T) {
if !hasTmux() {
t.Skip("tmux not installed")
}
tm := NewTmux()
has, err := tm.HasSession("nonexistent-session-xyz")
if err != nil {
t.Fatalf("HasSession: %v", err)
}
if has {
t.Error("expected session to not exist")
}
}
func TestSessionLifecycle(t *testing.T) {
if !hasTmux() {
t.Skip("tmux not installed")
}
tm := NewTmux()
sessionName := "gt-test-session-" + t.Name()
// Clean up any existing session
_ = tm.KillSession(sessionName)
// Create session
if err := tm.NewSession(sessionName, ""); err != nil {
t.Fatalf("NewSession: %v", err)
}
defer func() { _ = tm.KillSession(sessionName) }()
// Verify exists
has, err := tm.HasSession(sessionName)
if err != nil {
t.Fatalf("HasSession: %v", err)
}
if !has {
t.Error("expected session to exist after creation")
}
// List should include it
sessions, err := tm.ListSessions()
if err != nil {
t.Fatalf("ListSessions: %v", err)
}
found := false
for _, s := range sessions {
if s == sessionName {
found = true
break
}
}
if !found {
t.Error("session not found in list")
}
// Kill session
if err := tm.KillSession(sessionName); err != nil {
t.Fatalf("KillSession: %v", err)
}
// Verify gone
has, err = tm.HasSession(sessionName)
if err != nil {
t.Fatalf("HasSession after kill: %v", err)
}
if has {
t.Error("expected session to not exist after kill")
}
}
func TestDuplicateSession(t *testing.T) {
if !hasTmux() {
t.Skip("tmux not installed")
}
tm := NewTmux()
sessionName := "gt-test-dup-" + t.Name()
// Clean up any existing session
_ = tm.KillSession(sessionName)
// Create session
if err := tm.NewSession(sessionName, ""); err != nil {
t.Fatalf("NewSession: %v", err)
}
defer func() { _ = tm.KillSession(sessionName) }()
// Try to create duplicate
err := tm.NewSession(sessionName, "")
if err != ErrSessionExists {
t.Errorf("expected ErrSessionExists, got %v", err)
}
}
func TestSendKeysAndCapture(t *testing.T) {
if !hasTmux() {
t.Skip("tmux not installed")
}
tm := NewTmux()
sessionName := "gt-test-keys-" + t.Name()
// Clean up any existing session
_ = tm.KillSession(sessionName)
// Create session
if err := tm.NewSession(sessionName, ""); err != nil {
t.Fatalf("NewSession: %v", err)
}
defer func() { _ = tm.KillSession(sessionName) }()
// Send echo command
if err := tm.SendKeys(sessionName, "echo HELLO_TEST_MARKER"); err != nil {
t.Fatalf("SendKeys: %v", err)
}
// Give it a moment to execute
// In real tests you'd wait for output, but for basic test we just capture
output, err := tm.CapturePane(sessionName, 50)
if err != nil {
t.Fatalf("CapturePane: %v", err)
}
// Should contain our marker (might not if shell is slow, but usually works)
if !strings.Contains(output, "echo HELLO_TEST_MARKER") {
t.Logf("captured output: %s", output)
// Don't fail, just note - timing issues possible
}
}
func TestGetSessionInfo(t *testing.T) {
if !hasTmux() {
t.Skip("tmux not installed")
}
tm := NewTmux()
sessionName := "gt-test-info-" + t.Name()
// Clean up any existing session
_ = tm.KillSession(sessionName)
// Create session
if err := tm.NewSession(sessionName, ""); err != nil {
t.Fatalf("NewSession: %v", err)
}
defer func() { _ = tm.KillSession(sessionName) }()
info, err := tm.GetSessionInfo(sessionName)
if err != nil {
t.Fatalf("GetSessionInfo: %v", err)
}
if info.Name != sessionName {
t.Errorf("Name = %q, want %q", info.Name, sessionName)
}
if info.Windows < 1 {
t.Errorf("Windows = %d, want >= 1", info.Windows)
}
}
func TestWrapError(t *testing.T) {
tm := NewTmux()
tests := []struct {
stderr string
want error
}{
{"no server running on /tmp/tmux-...", ErrNoServer},
{"error connecting to /tmp/tmux-...", ErrNoServer},
{"duplicate session: test", ErrSessionExists},
{"session not found: test", ErrSessionNotFound},
{"can't find session: test", ErrSessionNotFound},
}
for _, tt := range tests {
err := tm.wrapError(nil, tt.stderr, []string{"test"})
if err != tt.want {
t.Errorf("wrapError(%q) = %v, want %v", tt.stderr, err, tt.want)
}
}
}