|
| 1 | +#!/bin/bash |
| 2 | +# Tests for find-controlling-tty.sh |
| 3 | +# |
| 4 | +# The function walks up the process tree using `ps`. We override `ps` as a |
| 5 | +# shell function so the tests don't depend on the actual process tree. |
| 6 | + |
| 7 | +set -uo pipefail |
| 8 | + |
| 9 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/../scripts" && pwd)" |
| 10 | +source "$SCRIPT_DIR/find-controlling-tty.sh" |
| 11 | + |
| 12 | +PASSED=0 |
| 13 | +FAILED=0 |
| 14 | + |
| 15 | +assert_eq() { |
| 16 | + local test_name="$1" |
| 17 | + local expected="$2" |
| 18 | + local actual="$3" |
| 19 | + if [[ $expected == "$actual" ]]; then |
| 20 | + echo " ✓ $test_name" |
| 21 | + PASSED=$((PASSED + 1)) |
| 22 | + else |
| 23 | + echo " ✗ $test_name" |
| 24 | + echo " expected: $expected" |
| 25 | + echo " actual: $actual" |
| 26 | + FAILED=$((FAILED + 1)) |
| 27 | + fi |
| 28 | +} |
| 29 | + |
| 30 | +echo "=== find-controlling-tty.sh ===" |
| 31 | + |
| 32 | +echo "" |
| 33 | +echo "--- Emits the parent's tty when it has one ---" |
| 34 | +ps() { |
| 35 | + case "$*" in |
| 36 | + "-o tty= -p $PPID") echo "ttys003" ;; |
| 37 | + "-o ppid= -p $PPID") echo "1" ;; |
| 38 | + *) command ps "$@" ;; |
| 39 | + esac |
| 40 | +} |
| 41 | +result=$(find_candidate_ttys | tr '\n' ' ' | sed 's/ $//') |
| 42 | +assert_eq "parent tty appears as /dev/<name>" "/dev/ttys003" "$result" |
| 43 | +unset -f ps |
| 44 | + |
| 45 | +echo "" |
| 46 | +echo "--- Skips ancestors with no controlling tty, keeps walking ---" |
| 47 | +ps() { |
| 48 | + case "$*" in |
| 49 | + "-o tty= -p $PPID") echo "??" ;; |
| 50 | + "-o tty= -p 77001") echo "??" ;; |
| 51 | + "-o tty= -p 77002") echo "ttys004" ;; |
| 52 | + "-o ppid= -p $PPID") echo "77001" ;; |
| 53 | + "-o ppid= -p 77001") echo "77002" ;; |
| 54 | + "-o ppid= -p 77002") echo "1" ;; |
| 55 | + *) command ps "$@" ;; |
| 56 | + esac |
| 57 | +} |
| 58 | +result=$(find_candidate_ttys | tr '\n' ' ' | sed 's/ $//') |
| 59 | +assert_eq "?? entries are skipped, real tty emitted" "/dev/ttys004" "$result" |
| 60 | +unset -f ps |
| 61 | + |
| 62 | +echo "" |
| 63 | +echo "--- Emits multiple ancestors if more than one has a tty ---" |
| 64 | +# A nested case (e.g. tmux): both inner and outer ancestors have ttys. |
| 65 | +ps() { |
| 66 | + case "$*" in |
| 67 | + "-o tty= -p $PPID") echo "ttys001" ;; |
| 68 | + "-o tty= -p 88001") echo "ttys002" ;; |
| 69 | + "-o ppid= -p $PPID") echo "88001" ;; |
| 70 | + "-o ppid= -p 88001") echo "1" ;; |
| 71 | + *) command ps "$@" ;; |
| 72 | + esac |
| 73 | +} |
| 74 | +result=$(find_candidate_ttys | tr '\n' ' ' | sed 's/ $//') |
| 75 | +assert_eq "both ancestor ttys are emitted in order" "/dev/ttys001 /dev/ttys002" "$result" |
| 76 | +unset -f ps |
| 77 | + |
| 78 | +echo "" |
| 79 | +echo "--- Emits nothing when no ancestor has a tty ---" |
| 80 | +ps() { |
| 81 | + case "$*" in |
| 82 | + "-o tty="*) echo "??" ;; |
| 83 | + "-o ppid="*) echo "1" ;; |
| 84 | + *) command ps "$@" ;; |
| 85 | + esac |
| 86 | +} |
| 87 | +result=$(find_candidate_ttys) |
| 88 | +assert_eq "no output when every ancestor reports ??" "" "$result" |
| 89 | +unset -f ps |
| 90 | + |
| 91 | +echo "" |
| 92 | +echo "--- Honors depth limit on unbounded process chains ---" |
| 93 | +ps() { |
| 94 | + case "$*" in |
| 95 | + "-o tty="*) echo "??" ;; |
| 96 | + "-o ppid="*) echo "9999" ;; |
| 97 | + *) command ps "$@" ;; |
| 98 | + esac |
| 99 | +} |
| 100 | +# Should terminate (not hang) even though every ppid points to a non-init pid. |
| 101 | +start_secs=$SECONDS |
| 102 | +find_candidate_ttys > /dev/null |
| 103 | +elapsed=$((SECONDS - start_secs)) |
| 104 | +if (( elapsed <= 2 )); then |
| 105 | + echo " ✓ depth-limited walk terminates promptly" |
| 106 | + PASSED=$((PASSED + 1)) |
| 107 | +else |
| 108 | + echo " ✗ depth-limited walk took too long (${elapsed}s)" |
| 109 | + FAILED=$((FAILED + 1)) |
| 110 | +fi |
| 111 | +unset -f ps |
| 112 | + |
| 113 | +echo "" |
| 114 | +echo "=== Results: $PASSED passed, $FAILED failed ===" |
| 115 | + |
| 116 | +if (( FAILED > 0 )); then |
| 117 | + exit 1 |
| 118 | +fi |
0 commit comments