aboutsummaryrefslogtreecommitdiff
path: root/tests/tmux-util/fake-tmux
blob: f6217bb429668b53bf893566c50dfb20d399fe77 (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
#!/bin/sh
# Fake tmux for testing tmux-util.
#
# State file: $FAKE_TMUX_DIR/sessions.txt
#   One line per session, space-separated: <name> <attached> <pids_csv> [last_activity_epoch]
#   pids_csv is a comma-separated list of pane PIDs (or '-' for none)
#
# 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

# Helper: read state file, ignoring blank lines
read_state() {
    [ -f "$STATE" ] || return 0
    grep -v '^[[:space:]]*$' "$STATE" || true
}

case "$cmd" in
    list-sessions)
        # Format string is ignored; we always emit "<name> <attached>" because
        # that's the only format tmux-util uses against list-sessions.
        read_state | while IFS=' ' read -r name attached pids _rest; do
            [ -n "$name" ] || continue
            echo "$name $attached"
        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
        ;;
    has-session)
        session=""
        while [ "$#" -gt 0 ]; do
            case "$1" in
                -t) shift; session="$1"; shift ;;
                *)  shift ;;
            esac
        done
        while IFS=' ' read -r name attached pids _rest; do
            if [ "$name" = "$session" ]; then
                exit 0
            fi
        done < "$STATE"
        exit 1
        ;;
    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 name attached pids rest; do
            [ -n "$name" ] || continue
            if [ "$name" != "$session" ]; then
                if [ -n "$rest" ]; then
                    printf '%s %s %s %s\n' "$name" "$attached" "$pids" "$rest" >> "$tmp"
                else
                    printf '%s %s %s\n' "$name" "$attached" "$pids" >> "$tmp"
                fi
            fi
        done < "$STATE"
        mv "$tmp" "$STATE"
        ;;
    *)
        echo "fake-tmux: unknown command '$cmd'" >&2
        exit 1
        ;;
esac