[Bash-completion-commits] [SCM] bash-completion branch, master, updated. 31f03cfb82cfe528d987f8c3d6375da5306b4973

Freddy Vulto fvulto at gmail.com
Mon Aug 24 20:38:43 UTC 2009


The following commit has been merged in the master branch:
commit 31f03cfb82cfe528d987f8c3d6375da5306b4973
Author: Freddy Vulto <fvulto at gmail.com>
Date:   Sun Aug 23 09:38:19 2009 +0200

    Fix for known_host files containing * character
    Quote sed output ("\1") retrieving "GlobalKnownHostsFile/UserKnownHostsFile" to
    prevent bash globbing of special characters, e.g. '*'.
    
    Added unit test "Config file containing star (*) should work".
    Added test library function `assert_bash_list()'.
    To run unit tests:
    
        cd test && ./runUnit _known_hosts_real.exp

diff --git a/bash_completion b/bash_completion
index 96217d7..806dacf 100644
--- a/bash_completion
+++ b/bash_completion
@@ -1110,12 +1110,12 @@ _known_hosts_real()
 	if [ ${#config[@]} -gt 0 ]; then
 		local OIFS=$IFS IFS=$'\n'
 		# expand path (if present) to global known hosts file
-		global_kh=($( sed -ne 's/^[ \t]*[Gg][Ll][Oo][Bb][Aa][Ll][Kk][Nn][Oo][Ww][Nn][Hh][Oo][Ss][Tt][Ss][Ff][Ii][Ll][Ee]['"$'\t '"']*\(.*\)$/\1/p' "${config[@]}" ))
+		global_kh=($( sed -ne 's/^[ \t]*[Gg][Ll][Oo][Bb][Aa][Ll][Kk][Nn][Oo][Ww][Nn][Hh][Oo][Ss][Tt][Ss][Ff][Ii][Ll][Ee]['"$'\t '"']*\(.*\)$/"\1"/p' "${config[@]}" ))
 		for (( i=0; i < ${#global_kh[@]}; i++ )); do
 			global_kh[i]=$(echo "${global_kh[i]//\"/}")
 		done
 		# expand path (if present) to user known hosts file
-		user_kh=($( sed -ne 's/^[ \t]*[Uu][Ss][Ee][Rr][Kk][Nn][Oo][Ww][Nn][Hh][Oo][Ss][Tt][Ss][Ff][Ii][Ll][Ee]['"$'\t '"']*\(.*\)$/\1/p' "${config[@]}" ))
+		user_kh=($( sed -ne 's/^[ \t]*[Uu][Ss][Ee][Rr][Kk][Nn][Oo][Ww][Nn][Hh][Oo][Ss][Tt][Ss][Ff][Ii][Ll][Ee]['"$'\t '"']*\(.*\)$/"\1"/p' "${config[@]}" ))
 		for (( i=0; i < ${#user_kh[@]}; i++ )); do
 			user_kh[i]=$(echo "${user_kh[i]//\"/}")
 		done
diff --git a/test/fixtures/_known_hosts_real/known*hosts b/test/fixtures/_known_hosts_real/known*hosts
new file mode 100644
index 0000000..5db1143
--- /dev/null
+++ b/test/fixtures/_known_hosts_real/known*hosts
@@ -0,0 +1,2 @@
+star*d
+tra,10.0.0.0 key
diff --git a/test/fixtures/_known_hosts_real/known*hosts2 b/test/fixtures/_known_hosts_real/known*hosts2
new file mode 100644
index 0000000..3cabce9
--- /dev/null
+++ b/test/fixtures/_known_hosts_real/known*hosts2
@@ -0,0 +1 @@
+star*d2
diff --git a/test/fixtures/_known_hosts_real/star*conf b/test/fixtures/_known_hosts_real/star*conf
new file mode 100644
index 0000000..a20af35
--- /dev/null
+++ b/test/fixtures/_known_hosts_real/star*conf
@@ -0,0 +1,2 @@
+UserKnownHostsFile fixtures/_known_hosts_real/known*hosts
+UserKnownHostsFile "fixtures/_known_hosts_real/known*hosts2"
diff --git a/test/lib/library.exp b/test/lib/library.exp
index b663d3f..436c70c 100644
--- a/test/lib/library.exp
+++ b/test/lib/library.exp
@@ -52,6 +52,29 @@ proc assert_bash_type {command} {
 }; # assert_bash_type()
 
 
+# Make sure the expected list is returned by executing the specified command.
+# @param list $expected
+# @param string $cmd  Command given to generate items
+# @param string $test  (optional) Test titel.  Default is "$cmd<TAB> should show completions"
+# @param string $prompt  (optional) Bash prompt.  Default is "/@"
+# @param integer $size  (optional) Chunk size.  Default is 20.
+# @result boolean  True if successful, False if not
+proc assert_bash_list {expected cmd {test ""} {prompt /@} {size 20}} {
+    if {$test == ""} {set test "$cmd should show expected output"}
+    send "$cmd\r\n"
+    expect -ex "$cmd\r\n"
+
+    if {[match_items $expected $test]} {
+        expect {
+            -re $prompt { pass "$test" }
+            -re eof { unresolved "eof" }
+        }; # expect
+    } else {
+        fail "$test"
+    }; # if
+}; # assert_bash_list()
+
+
 # Make sure the expected items are returned by TAB-completing the specified
 # command.
 # @param list $expected
@@ -328,7 +351,7 @@ proc match_items {items test {size 20}} {
         for {set j 0} {$j < $size && $i + $j < [llength $items]} {incr j} {
             set item "[lindex $items [expr {$i + $j}]]"
                 # Escape special regexp characters
-            regsub -all {([\[\]\(\)\.\\\+])} $item {\\\1} item
+            regsub -all {([\[\]\(\)\.\\\+\*])} $item {\\\1} item
             append expected $item
             if {[llength $items] > 1} {append expected {\s+}};
         }; # for
diff --git a/test/unit/_known_hosts_real.exp b/test/unit/_known_hosts_real.exp
index 94fc67e..4c95998 100644
--- a/test/unit/_known_hosts_real.exp
+++ b/test/unit/_known_hosts_real.exp
@@ -57,7 +57,6 @@ 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
@@ -101,6 +100,17 @@ sync_after_int
 assert_bash_exec "unset -v COMP_KNOWN_HOSTS_WITH_HOSTFILE"
 
 
+sync_after_int
+
+
+set test "Config file containing star (*) should work"
+set hosts [get_hosts]
+    # Hosts `star*d', `tra' and `10.0.0.0' are defined in ./fixtures/_known_hosts_real/known*hosts
+    # Hosts `star*d2' is defined in ./fixtures/_known_hosts_real/known*hosts2
+lappend hosts star*d star*d2 tra 10.0.0.0
+set cmd {_known_hosts_real -aF 'fixtures/_known_hosts_real/star*conf' ''; echo_array COMPREPLY}
+assert_bash_list $hosts $cmd $test
+
 
 sync_after_int
 

-- 
bash-completion



More information about the Bash-completion-commits mailing list