Added setting COMP_KNOWN_HOSTS_WITH_HOSTFILE

`_known_hosts_real' will add hosts from HOSTFILE (compgen -A hostname), unless
`COMP_KNOWN_HOSTS_WITH_HOSTFILE' is set to an empty value.

To run the unit tests:

   $ cd test && ./runUnit _known_hosts_real.exp
master
Freddy Vulto 2009-08-12 22:20:56 +02:00
parent 57565c1ce2
commit 2e87bd4c01
9 changed files with 162 additions and 32 deletions

View File

@ -143,6 +143,9 @@ bash-completion (1.x)
* Patched _known_hosts() to support multiple {Global,User}KnownHosts in SSH
config files, thanks to Thomas Nilsson (Alioth: #311595) (Debian: #524190)
* Fix leaking $i from info, man and python completions.
* Added setting COMP_KNOWN_HOSTS_WITH_HOSTFILE. _known_hosts_real() will add
hosts from HOSTFILE, unless COMP_KNOWN_HOSTS_WITH_HOSTFILE is set to an
empty value (Alioth: #311821)
-- David Paleino <d.paleino@gmail.com> Thu, 18 Jun 2009 13:12:36 +0200

View File

@ -1214,16 +1214,16 @@ _known_hosts_real()
fi
fi
# Now add results of normal hostname completion
COMPREPLY=( "${COMPREPLY[@]}" $( compgen -A hostname -- $cur ) )
# apply suffix and prefix
for (( i=0; i < ${#COMPREPLY[@]}; i++ )); do
COMPREPLY[i]=$prefix$user${COMPREPLY[i]}$suffix
done
elif [ -z "$configfile" ]; then
# Just do normal hostname completion
COMPREPLY=( $( compgen -A hostname -S "$suffix" -- $cur ) )
fi
# Add results of normal hostname completion, unless `COMP_KNOWN_HOSTS_WITH_HOSTFILE'
# is set to an empty value.
if [ -n "${COMP_KNOWN_HOSTS_WITH_HOSTFILE-1}" ]; then
COMPREPLY=( "${COMPREPLY[@]}" $( compgen -A hostname -P "$prefix$user" -S "$suffix" -- $cur ) )
fi
return 0

34
doc/bash_completion.txt Normal file
View File

@ -0,0 +1,34 @@
Bash completion
===============
Environment variables
---------------------
*COMP_CONFIGURE_HINTS*::
If set and not null, `configure` completion will return the entire option
string (e.g. `--this-option=DESCRIPTION`) so one can see what kind of data
is required and then simply delete the descriptive text and add one's own
data. If unset or null (default), `configure` completion will strip
everything after the '=' when returning completions.
*COMP_CVS_REMOTE*::
If set and not null, `cvs commit` completion will try to complete on
remotely checked-out files. This requires passwordless access to the
remote repository. Default is unset.
*COMP_KNOWN_HOSTS_WITH_HOSTFILE*::
If set and not null (default), known_hosts completion will complement
hostnames from ssh's known_hosts_files with hostnames taken from the file
specified by the HOSTFILE shell variable (compgen -A hostname). If null,
known_hosts completion will omit hostnames from HOSTFILE. Omitting
hostnames from HOSTFILE is useful if HOSTFILE contains many entries for
local web development or ad-blocking.
*COMP_TAR_INTERNAL_PATHS*::
If set and not null *before* sourcing bash_completion, `tar` completion
will do correct path completion for tar file contents. If unset or null,
`tar' completion will do correct completion for paths to tar files. See
also README.

File diff suppressed because one or more lines are too long

View File

@ -12,7 +12,7 @@ common Linux/UNIX commands, reducing the amount of typing sysadmins
and programmers need to do on a daily basis.
// include::intro.txt[]
// include::bash_completion.txt[]
include::bash_completion.txt[]
include::styleguide.txt[]
include::testing.txt[]

View File

@ -1,4 +1,4 @@
#!/bin/bash -eu
[ -d html~ ] || mkdir html~
a2x -D html~ -d book -f xhtml main.txt
a2x -D html~ -d book -f xhtml --unsafe main.txt

View File

@ -72,7 +72,7 @@ Maintenance
Adding a completion test
~~~~~~~~~~~~~~~~~~~~~~~~
You can add script/generate to add a test.
You can run `cd test && ./generate` to add a test.
Fixing a completion test

View File

@ -20,4 +20,12 @@ export INPUTRC=$TESTDIR/config/inputrc
# Ensure enough columns so expect doesn't have to care about line breaks
stty columns 150
# Make sure default settings are in effect
unset -v \
COMP_CONFIGURE_HINTS \
COMP_CVS_REMOTE \
COMP_KNOWN_HOSTS_WITH_HOSTFILE \
COMP_TAR_INTERNAL_PATHS
# Load bash testsuite helper functions
. lib/library.sh

View File

@ -32,6 +32,32 @@ expect {
sync_after_int
set test "Hosts should have username prefix and colon suffix"
set hosts [get_hosts]
# Hosts `gee', `hus' and `jar' are defined in ./fixtures/_known_hosts_real/config
# Hosts `doo' and `ike' are defined in ./fixtures/_known_hosts_real/known_hosts
lappend hosts doo gee hus ike jar
set hosts [lsort -ascii $hosts]
set expected {}
foreach host $hosts {
lappend expected "user@$host:"
}; # foreach
set expected [join $expected "\\s+"]
# Call _known_hosts
set cmd {_known_hosts_real -acF fixtures/_known_hosts_real/config 'user@'; echo_array COMPREPLY}
send "$cmd\r"
expect -ex "$cmd\r\n"
expect {
-re "^$expected\r\n/@$" { pass "$test" }
-re /@ { unresolved "$test at prompt" }
default { unresolved "$test" }
}; # expect
sync_after_int
set test "Config file containing space should work"
set hosts [get_hosts]
# Hosts `gee' and `hus' are defined in ./fixtures/_known_hosts_real/spaced conf
@ -51,6 +77,31 @@ expect {
}; # expect
sync_after_int
set test "Empty COMP_KNOWN_HOSTS_WITH_HOSTFILE should omit HOSTFILE"
assert_bash_exec "COMP_KNOWN_HOSTS_WITH_HOSTFILE="
set hosts {}
# Hosts `gee', `hus' and `jar' are defined in ./fixtures/_known_hosts_real/config
# Hosts `doo' and `ike' are defined in ./fixtures/_known_hosts_real/known_hosts
lappend hosts doo gee hus ike jar
set hosts [lsort -ascii $hosts]
set hosts [join $hosts "\\s+"]
# Call _known_hosts
set cmd {_known_hosts_real -aF fixtures/_known_hosts_real/config ''; echo_array COMPREPLY}
send "$cmd\r"
expect -ex "$cmd\r\n"
expect {
-re "^$hosts\r\n/@$" { pass "$test" }
-re /@ { unresolved "$test at prompt" }
default { unresolved "$test" }
}; # expect
sync_after_int
assert_bash_exec "unset -v COMP_KNOWN_HOSTS_WITH_HOSTFILE"
sync_after_int