summaryrefslogtreecommitdiff
path: root/internal/tmux/tmux_test.go
blob: b18c8a3859865814a0077f5c2de9bf6614b76342 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package tmux

import (
    "errors"
    "os"
    "os/exec"
    "testing"
)

func TestInSession(t *testing.T) {
    t.Setenv("TMUX", "/tmp/tmux-123,123,0")
    if !InSession() {
        t.Fatal("expected InSession true when TMUX is set")
    }
    t.Setenv("TMUX", "")
    if InSession() {
        t.Fatal("expected InSession false when TMUX is empty")
    }
}

func TestHasBinary_UsesLookPath(t *testing.T) {
    old := lookPath
    t.Cleanup(func() { lookPath = old })
    lookPath = func(file string) (string, error) { return "/bin/tmux", nil }
    if !HasBinary() {
        t.Fatal("expected HasBinary true when lookPath succeeds")
    }
    lookPath = func(file string) (string, error) { return "", errors.New("nope") }
    if HasBinary() {
        t.Fatal("expected HasBinary false when lookPath fails")
    }
}

func TestSplitRun_AssemblesArgs(t *testing.T) {
    captured := struct{ name string; args []string }{}
    oldCmd := command
    t.Cleanup(func() { command = oldCmd })
    command = func(name string, args ...string) *exec.Cmd {
        captured.name = name
        captured.args = append([]string(nil), args...)
        // Use a benign command that exits 0
        return exec.Command("true")
    }
    opts := SplitOpts{Target: ":.", Vertical: true, Percent: 40}
    argv := []string{"/path/to/bin", "-flag", "value with spaces", "and'quote"}
    if err := SplitRun(opts, argv); err != nil {
        t.Fatalf("SplitRun error: %v", err)
    }
    if captured.name != "tmux" {
        t.Fatalf("expected tmux, got %q", captured.name)
    }
    wantFlags := map[string]bool{"split-window": true, "-v": true, "-p": true, "40": true, "-t": true, ":.": true}
    for _, a := range captured.args[:len(captured.args)-1] {
        if wantFlags[a] {
            delete(wantFlags, a)
        }
    }
    if len(wantFlags) != 0 {
        t.Fatalf("missing expected flags: %v", wantFlags)
    }
    last := captured.args[len(captured.args)-1]
    if last == "" || last == argv[0] {
        t.Fatalf("expected last arg to be joined command string, got %q", last)
    }
    _ = os.Unsetenv("TMUX")
}

func TestAvailable(t *testing.T) {
    oldLook := lookPath
    t.Cleanup(func() { lookPath = oldLook })
    // Present binary + TMUX set -> available
    lookPath = func(file string) (string, error) { return "/bin/tmux", nil }
    t.Setenv("TMUX", "/tmp/tmux-1,1,1")
    if !Available() {
        t.Fatal("expected Available true with TMUX + binary")
    }
    // No binary -> not available
    lookPath = func(file string) (string, error) { return "", errors.New("nope") }
    if Available() {
        t.Fatal("expected Available false without binary")
    }
}