aboutsummaryrefslogtreecommitdiff
path: root/tests/tmux-util/fake-tmux
blob: 163ea24a4580c936815e0b446101a2560f5a0f57 (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/bin/bash
# Fake tmux for testing tmux-util.
#
# State file: $FAKE_TMUX_DIR/sessions.txt
#   One line per session, space-separated:
#     <name> <attached> <pids_csv> [<activity_epoch> [<windows> [<cwd>]]]
#   pids_csv is a comma-separated list of pane PIDs (or '-' for none)
#   cwd cannot contain spaces in test data.
#
# Log file: $FAKE_TMUX_DIR/calls.log
#   Each invocation appended as a single line: tmux <args>

: "${FAKE_TMUX_DIR:?FAKE_TMUX_DIR must be set}"

STATE="$FAKE_TMUX_DIR/sessions.txt"
LOG="$FAKE_TMUX_DIR/calls.log"

# Log every invocation
printf 'tmux %s\n' "$*" >> "$LOG"

cmd="$1"
shift

read_state() {
    [ -f "$STATE" ] || return 0
    grep -v '^[[:space:]]*$' "$STATE" || true
}

# Render a tmux format string against one session's fields.
# Args: format, name, attached, activity, windows, cwd
render_format() {
    local out="$1" name="$2" attached="$3" activity="$4" windows="$5" cwd="$6"
    out="${out//\#\{session_name\}/$name}"
    out="${out//\#\{session_attached\}/$attached}"
    out="${out//\#\{session_activity\}/$activity}"
    out="${out//\#\{session_windows\}/$windows}"
    out="${out//\#\{pane_current_path\}/$cwd}"
    echo "$out"
}

case "$cmd" in
    list-sessions)
        fmt=""
        while [ "$#" -gt 0 ]; do
            case "$1" in
                -F) shift; fmt="${1:-}"; shift ;;
                *)  shift ;;
            esac
        done
        [ -n "$fmt" ] || fmt='#{session_name} #{session_attached}'
        read_state | while IFS=' ' read -r name attached pids activity windows cwd; do
            [ -n "$name" ] || continue
            render_format "$fmt" "$name" "$attached" "${activity:-0}" "${windows:-1}" "${cwd:-/tmp}"
        done
        ;;
    list-panes)
        session=""
        while [ "$#" -gt 0 ]; do
            case "$1" in
                -t) shift; session="$1"; shift ;;
                -F) shift; [ "$#" -gt 0 ] && shift ;;
                -s) shift ;;
                *)  shift ;;
            esac
        done
        read_state | while IFS=' ' read -r name attached pids _rest; do
            if [ "$name" = "$session" ]; then
                [ "$pids" = "-" ] || echo "$pids" | tr ',' '\n'
                exit 0
            fi
        done
        ;;
    display)
        fmt=""
        target=""
        while [ "$#" -gt 0 ]; do
            case "$1" in
                -p) shift ;;
                -t) shift; target="$1"; shift ;;
                *) fmt="$1"; shift ;;
            esac
        done
        while IFS=' ' read -r name attached pids activity windows cwd; do
            [ -n "$name" ] || continue
            if [ "$name" = "$target" ]; then
                render_format "$fmt" "$name" "$attached" "${activity:-0}" "${windows:-1}" "${cwd:-/tmp}"
                exit 0
            fi
        done < "$STATE"
        exit 1
        ;;
    has-session)
        session=""
        while [ "$#" -gt 0 ]; do
            case "$1" in
                -t) shift; session="$1"; shift ;;
                *)  shift ;;
            esac
        done
        # tmux accepts a `=name` form to force exact match; strip the prefix.
        session="${session#=}"
        while IFS=' ' read -r name attached pids _rest; do
            if [ "$name" = "$session" ]; then
                exit 0
            fi
        done < "$STATE"
        exit 1
        ;;
    new-session)
        detached=0
        name=""
        cwd=""
        while [ "$#" -gt 0 ]; do
            case "$1" in
                -s) shift; name="$1"; shift ;;
                -c) shift; cwd="$1"; shift ;;
                -d) detached=1; shift ;;
                *)  shift ;;
            esac
        done
        attached=1
        [ "$detached" -eq 1 ] && attached=0
        printf '%s %s - 0 1 %s\n' "$name" "$attached" "${cwd:-/tmp}" >> "$STATE"
        ;;
    attach-session|switch-client)
        # No state mutation needed — the call log already records intent.
        ;;
    kill-session)
        session=""
        while [ "$#" -gt 0 ]; do
            case "$1" in
                -t) shift; session="$1"; shift ;;
                *)  shift ;;
            esac
        done
        tmp="$STATE.tmp"
        : > "$tmp"
        while IFS= read -r line; do
            [ -n "$line" ] || continue
            first="${line%% *}"
            if [ "$first" != "$session" ]; then
                printf '%s\n' "$line" >> "$tmp"
            fi
        done < "$STATE"
        mv "$tmp" "$STATE"
        ;;
    *)
        echo "fake-tmux: unknown command '$cmd'" >&2
        exit 1
        ;;
esac