@@ -23,34 +23,63 @@ if [ ${RESULT} = 1 ]; then
2323 exit 1
2424fi
2525
26+ # tear down the tmux session on any exit, so a timeout failure does not
27+ # leave a stale session that breaks the next run with "duplicate session"
28+ trap ' tmux kill-session -t test 2>/dev/null || true' EXIT
29+
30+ # Wait until the remote shell produces some output (i.e. a prompt), so the
31+ # SSH session is known to be up before keys are sent to it. CI runners can
32+ # take several seconds to get through key exchange and login.
33+ wait_for_session () {
34+ for _ in $( seq 1 10) ; do
35+ if tmux capture-pane -p -t test | grep -q ' [^[:space:]]' ; then
36+ return 0
37+ fi
38+ sleep 1
39+ done
40+ echo " Timed out waiting for SSH session output"
41+ tmux capture-pane -p -t test
42+ return 1
43+ }
44+
45+ # Ask the remote shell for its size and poll the pane until a line of the
46+ # form "<columns> <rows>" shows up. The result is left in SIZE_LINE.
47+ get_size_line () {
48+ SIZE_LINE=" "
49+ for _ in $( seq 1 10) ; do
50+ # Re-send the query each pass in case the shell was not yet ready to
51+ # read input on an earlier pass; tail -n 1 below tolerates the extra
52+ # numeric line a repeat can produce.
53+ tmux send-keys -t test ' echo;echo $COLUMNS $LINES;echo'
54+ tmux send-keys -t test ' ENTER'
55+ sleep 1
56+ SIZE_LINE=$( tmux capture-pane -p -t test | tr -d ' \r' | \
57+ grep -E ' ^[0-9]+[[:space:]]+[0-9]+[[:space:]]*$' | tail -n 1)
58+ if [ -n " $SIZE_LINE " ]; then
59+ return 0
60+ fi
61+ done
62+ echo " Timed out waiting for terminal size output"
63+ tmux capture-pane -p -t test
64+ return 1
65+ }
66+
2667echo " Creating tmux session at $PWD with command :"
2768echo " tmux new-session -d -s test \" $TEST_CLIENT -q -t -u $USER -i $PRIVATE_KEY -j $PUBLIC_KEY -h \" $1 \" -p \" $2 \"\" "
2869tmux new-session -d -s test " $TEST_CLIENT -q -t -u $USER -i $PRIVATE_KEY -j $PUBLIC_KEY -h \" $1 \" -p \" $2 \" "
2970echo " Result of tmux new-session = $? "
3071
31- # give the command a second to establish SSH connection
32- sleep 1
72+ wait_for_session || exit 1
3373
3474COL=` tmux display -p -t test ' #{pane_width}' `
3575ROW=` tmux display -p -t test ' #{pane_height}' `
3676echo " tmux 'test' session has COL = ${COL} and ROW = ${ROW} "
3777
3878# get the terminals columns and lines
39- tmux send-keys -t test ' echo;echo $COLUMNS $LINES;echo'
40- tmux send-keys -t test ' ENTER'
41-
42- # give the command a second to run
43- sleep 1
44-
45- tmux capture-pane -t test
46- RESULT=$( tmux show-buffer | grep ' ^[0-9]* [0-9]*$' )
47- tmux show-buffer
79+ get_size_line || exit 1
80+ echo " Captured terminal size line: '$SIZE_LINE '"
4881
49- echo " $RESULT "
50- echo " "
51- echo " "
52- ROW_FOUND=$( echo " $RESULT " | sed -e ' s/[0-9]* \([0-9]*\)/\1/' )
53- COL_FOUND=$( echo " $RESULT " | sed -e ' s/\([0-9]*\) [0-9]*/\1/' )
82+ read -r COL_FOUND ROW_FOUND <<< " $SIZE_LINE"
5483
5584if [ " $COL " != " $COL_FOUND " ]; then
5685 echo " Col found was $COL_FOUND which does not match expected $COL "
@@ -80,22 +109,13 @@ echo "Starting another session with a smaller window size"
80109echo " tmux new-session -d -x 50 -y 10 -s test \" $TEST_CLIENT -q -t -u $USER -i $PRIVATE_KEY -j $PUBLIC_KEY -h \" $1 \" -p \" $2 \"\" "
81110tmux new-session -d -x 50 -y 10 -s test " $TEST_CLIENT -q -t -u $USER -i $PRIVATE_KEY -j $PUBLIC_KEY -h \" $1 \" -p \" $2 \" "
82111
83- # give the command a second to establish SSH connection
84- sleep 1
112+ wait_for_session || exit 1
85113
86114echo " Sending keys to tmux session for displaying column/rows"
87- tmux send-keys -t test ' echo;echo $COLUMNS $LINES;echo'
88- tmux send-keys -t test ' ENTER'
89- tmux capture-pane -t test
90- RESULT=$( tmux show-buffer | grep ' ^[0-9]* [0-9]*$' )
91-
92- ROW_FOUND=$( echo " $RESULT " | sed -e ' s/[0-9]* \([0-9]*\)/\1/' )
93- COL_FOUND=$( echo " $RESULT " | sed -e ' s/\([0-9]*\) [0-9]*/\1/' )
94-
95- # remove any newlines, tabs, or returns
96- ROW_FOUND=$( tr -d ' \n\t\r ' <<< " $ROW_FOUND" )
97- COL_FOUND=$( tr -d ' \n\t\r ' <<< " $COL_FOUND" )
115+ get_size_line || exit 1
116+ echo " Captured terminal size line: '$SIZE_LINE '"
98117
118+ read -r COL_FOUND ROW_FOUND <<< " $SIZE_LINE"
99119
100120if [ " 50" != " $COL_FOUND " ]; then
101121 echo " Col found was $COL_FOUND which does not match expected 50"
0 commit comments