Fix _user_at_host completion

Fixed _known_hosts_real user completion.
Added test for `finger'.  To execute:

    $ cd test && ./runCompletion finger.exp
This commit is contained in:
Freddy Vulto 2009-06-13 11:34:39 +02:00
parent b1d869cbf2
commit 1f63e21ff5
3 changed files with 170 additions and 18 deletions

View File

@ -1036,7 +1036,7 @@ _user_at_host() {
cur=`_get_cword` cur=`_get_cword`
if [[ $cur == *@* ]]; then if [[ $cur == *@* ]]; then
_known_hosts _known_hosts_real -h "$cur"
else else
COMPREPLY=( $( compgen -u -- "$cur" ) ) COMPREPLY=( $( compgen -u -- "$cur" ) )
fi fi
@ -1068,7 +1068,7 @@ _known_hosts()
_known_hosts_real() _known_hosts_real()
{ {
local configfile flag prefix local configfile flag prefix
local cur curd ocur user suffix aliases global_kh user_kh hosts i host local cur curd awkcur user suffix aliases global_kh user_kh hosts i host
local -a kh khd config local -a kh khd config
local IFS=$'\n' local IFS=$'\n'
@ -1083,8 +1083,6 @@ _known_hosts_real()
esac esac
done done
ocur=$cur
[[ $cur == *@* ]] && user=${cur%@*}@ && cur=${cur#*@} [[ $cur == *@* ]] && user=${cur%@*}@ && cur=${cur#*@}
kh=() kh=()
@ -1145,22 +1143,22 @@ _known_hosts_real()
# If we have known_hosts files to use # If we have known_hosts files to use
if [ ${#kh[@]} -gt 0 -o ${#khd[@]} -gt 0 -o -n "$configfile" ]; then if [ ${#kh[@]} -gt 0 -o ${#khd[@]} -gt 0 -o -n "$configfile" ]; then
# Escape slashes and dots in paths for awk # Escape slashes and dots in paths for awk
cur=${cur//\//\\\/} awkcur=${cur//\//\\\/}
cur=${cur//\./\\\.} awkcur=${awkcur//\./\\\.}
curd=$cur curd=$awkcur
if [[ "$cur" == [0-9]*.* ]]; then if [[ "$awkcur" == [0-9]*.* ]]; then
# Digits followed by a dot - just search for that # Digits followed by a dot - just search for that
cur="^$cur.*" awkcur="^$awkcur.*"
elif [[ "$cur" == [0-9]* ]]; then elif [[ "$awkcur" == [0-9]* ]]; then
# Digits followed by no dot - search for digits followed # Digits followed by no dot - search for digits followed
# by a dot # by a dot
cur="^$cur.*\." awkcur="^$awkcur.*\."
elif [ -z "$cur" ]; then elif [ -z "$awkcur" ]; then
# A blank - search for a dot or an alpha character # A blank - search for a dot or an alpha character
cur="[a-z.]" awkcur="[a-z.]"
else else
cur="^$cur" awkcur="^$awkcur"
fi fi
if [ ${#kh[@]} -gt 0 ]; then if [ ${#kh[@]} -gt 0 ]; then
@ -1169,7 +1167,7 @@ _known_hosts_real()
COMPREPLY=( $( awk 'BEGIN {FS=","} COMPREPLY=( $( awk 'BEGIN {FS=","}
/^\s*[^|\#]/ {for (i=1; i<=2; ++i) { \ /^\s*[^|\#]/ {for (i=1; i<=2; ++i) { \
gsub(" .*$", "", $i); \ gsub(" .*$", "", $i); \
if ($i ~ /'$cur'/) {print $i} \ if ($i ~ /'$awkcur'/) {print $i} \
}}' "${kh[@]}" 2>/dev/null ) ) }}' "${kh[@]}" 2>/dev/null ) )
fi fi
if [ ${#khd[@]} -gt 0 ]; then if [ ${#khd[@]} -gt 0 ]; then
@ -1178,7 +1176,7 @@ _known_hosts_real()
# dont fork any processes, because in a cluster environment, # dont fork any processes, because in a cluster environment,
# there can be hundreds of hostkeys # there can be hundreds of hostkeys
for i in "${khd[@]}" ; do for i in "${khd[@]}" ; do
if [[ "$i" == *key_22_$curd*.pub ]] && [ -r "$i" ] ; then if [[ "$i" == *key_22_$awkcurd*.pub ]] && [ -r "$i" ] ; then
host=${i/#*key_22_/} host=${i/#*key_22_/}
host=${host/%.pub/} host=${host/%.pub/}
COMPREPLY=( "${COMPREPLY[@]}" $host ) COMPREPLY=( "${COMPREPLY[@]}" $host )
@ -1188,12 +1186,12 @@ _known_hosts_real()
# append any available aliases from config files # append any available aliases from config files
if [ ${#config[@]} -gt 0 ] && [ -n "$aliases" ]; then if [ ${#config[@]} -gt 0 ] && [ -n "$aliases" ]; then
local host_aliases=$( sed -ne 's/^[ \t]*[Hh][Oo][Ss][Tt]\([Nn][Aa][Mm][Ee]\)\?['"$'\t '"']\+\([^#*?]*\)\(#.*\)\?$/\2/p' "${config[@]}" ) local host_aliases=$( sed -ne 's/^[ \t]*[Hh][Oo][Ss][Tt]\([Nn][Aa][Mm][Ee]\)\?['"$'\t '"']\+\([^#*?]*\)\(#.*\)\?$/\2/p' "${config[@]}" )
hosts=$( compgen -W "$host_aliases" -- $ocur ) hosts=$( compgen -W "$host_aliases" -- $cur )
COMPREPLY=( "${COMPREPLY[@]}" $hosts ) COMPREPLY=( "${COMPREPLY[@]}" $hosts )
fi fi
# Now add results of normal hostname completion # Now add results of normal hostname completion
COMPREPLY=( "${COMPREPLY[@]}" $( compgen -A hostname -- $ocur ) ) COMPREPLY=( "${COMPREPLY[@]}" $( compgen -A hostname -- $cur ) )
# apply suffix and prefix # apply suffix and prefix
for (( i=0; i < ${#COMPREPLY[@]}; i++ )); do for (( i=0; i < ${#COMPREPLY[@]}; i++ )); do

View File

@ -0,0 +1,6 @@
source "lib/completions/finger.exp"
# TODO: Dynamic loading of completions. After the tests have run a first time
# and real completion is installed, the tests can be run a second time.
#
# source "lib/completions/finger.exp"

View File

@ -0,0 +1,148 @@
proc setup {} {
save_env
}; # setup()
proc teardown {} {
assert_env_unmodified
}; # teardown()
setup
set test "Tab should complete usernames"
# Build string list of usernames, separated by regexp whitespace (\s+)
# Example string: user1\s+user2\s+user3
set users {}
foreach u [exec bash -c "compgen -A user"] {
# Escape special regexp characters (+) in username
regsub -all {([\+])} $u {\\\1} h
lappend users $u
}; # foreach
set users [lsort -ascii $users]
set users [join $users "\\s+"]
# Try completion
set cmd "finger "
send "$cmd\t"
set expected "^$cmd\r\n$users\r\n/@$cmd$"
expect {
-re $expected { pass "$test" }
-re /@ { unresolved "$test at prompt" }
default { unresolved "$test" }
}; # expect
sync_after_int
set test "Tab should complete partial username"
# Build string list of usernames, starting with the character of the first
# username. Separate usernames by regexp whitespace (\s+) and 'plus' (+)
# prefix. Example string: \+user1\s+\+user2\s+\+user3
set users {}
set char ""
foreach u [exec bash -c "compgen -A user"] {
if {$char == ""} {set char [string range $u 0 0]}
# Only append username if starting with $char
if {[string range $u 0 0] == "$char"} {
# Escape possible special regexp characters (+) in username
regsub -all {([\+])} $u {\\\1} u
lappend users $u
}; # if
}; # foreach
# Try completion
set cmd "finger $char"
send "$cmd\t"
if {[llength $users] == 1} {
set expected "^finger $users"
} else {
set users [lsort -ascii $users]
set users [join $users "\\s+"]
set expected "^$cmd\r\n$users\r\n/@$cmd$"
}; # if
expect {
-re $expected { pass "$test" }
-re /@ { unresolved "$test at prompt" }
default { unresolved "$test" }
}; # expect
sync_after_int
set test "Tab should complete hostnames"
# Build string list of hostnames, separated by regexp whitespace (\s+)
# Example string: host1\s+host2\s+host3
set hosts {}
foreach h [exec bash -c "compgen -A hostname"] {
# Escape special regexp characters (+) in hostname
regsub -all {([\+])} $h {\\\1} h
# Prefix hosts with username 'test@'
lappend hosts "test@$h"
}; # foreach
# Try completion
set cmd "finger test@"
send "$cmd\t"
if {[llength $hosts] == 1} {
set expected "^$cmd$hosts "
} else {
set hosts [lsort -ascii $hosts]
set hosts [join $hosts "\\s+"]
set expected "^$cmd\r\n$hosts\r\n/@$cmd$"
}; # if
expect {
-re $expected { pass "$test" }
-re /@ { unresolved "$test at prompt" }
default { unresolved "$test" }
}; # expect
sync_after_int
set test "Tab should complete partial hostname"
# Build string list of hostnames, starting with the character of the first
# host. Separate hostnames by regexp whitespace (\s+) and 'plus' (+)
# prefix. Example string: \+host1\s+\+host2\s+\+host3
set hosts {}
set char ""
foreach h [exec bash -c "compgen -A hostname"] {
if {$char == ""} {set char [string range $h 0 0]}
# Only append hostname if starting with $char
if {[string range $h 0 0] == "$char"} {
# Escape special regexp characters (+) in hostname
regsub -all {([\+])} $h {\\\1} h
# Prefix hosts with username 'test@'
lappend hosts "test@$h"
}; # if
}; # foreach
# Try completion
set cmd "finger test@$char"
send "$cmd\t"
if {[llength $hosts] == 1} {
set expected "^finger $hosts"
} else {
set hosts [lsort -ascii $hosts]
set hosts [join $hosts "\\s+"]
set expected "^$cmd\r\n$hosts\r\n/@$cmd$"
}; # if
expect {
-re $expected { pass "$test" }
-re /@ { unresolved "$test at prompt" }
default { unresolved "$test" }
}; # expect
sync_after_int
teardown