branch: externals/tramp-hlo
commit 8893112d4064e1b01344f0fa05d773fdf908ae56
Author: Joe Sadusk <[email protected]>
Commit: Joe Sadusk <[email protected]>

    Handle special characters
---
 tramp-hlo-tests.el | 85 +++++++++++++++++++++++++++++-------------------------
 tramp-hlo.el       | 17 +++++------
 2 files changed, 53 insertions(+), 49 deletions(-)

diff --git a/tramp-hlo-tests.el b/tramp-hlo-tests.el
index 9bb162eacd..0dcda0eb4d 100644
--- a/tramp-hlo-tests.el
+++ b/tramp-hlo-tests.el
@@ -103,12 +103,18 @@ The result must be equal."
            (setq received (apply fun args))
            (should (string-match-p "tramp-hlo been here" captured-messages)))
          ;; Compare results.
+         ;(message "%s %s\n%s\n%s\n" fun args expected received)
          (should (equal expected received)))
 
       ;; Cleanup.
       (remove-function
        (symbol-function hlo-fun) #'tramp-hlo--test-advice-function))))
 
+;; See tramp-tests.el for more examples.
+(defconst tramp-hlo--special-names
+  '(" foo\tbar baz\t" "&foo&bar&baz&" "$foo$bar$$baz$" "'foo\"bar'baz\"")
+  "File names with special characters.")
+
 (ert-deftest tramp-hlo-test-dir-locals--all-files ()
   "Test `dir-locals--all-files'."
   (skip-unless (tramp-hlo--test-enabled))
@@ -126,10 +132,8 @@ The result must be equal."
     (let ((default-directory tmpdir))
       (tramp-hlo--run-test 'dir-locals--all-files "./")))
 
-  ;; Try directory with special characters.  See tramp-tests.el for
-  ;; more examples.
-  (dolist (prefix '(" foo\tbar baz\t" "&foo&bar&baz&"
-                   "$foo$bar$$baz$" "'foo\"bar'baz\""))
+  ;; Try directories with special characters.
+  (dolist (prefix tramp-hlo--special-names)
     (ert-with-temp-directory tmpdir
       :prefix (expand-file-name prefix ert-remote-temporary-file-directory)
       (make-empty-file (expand-file-name dir-locals-file tmpdir))
@@ -172,22 +176,23 @@ The result must be equal."
     (let ((default-directory (file-name-concat tmpdir "foo" "bar" "baz")))
       (tramp-hlo--run-test 'dir-locals-find-file "./"))
 
-    ;; With space in directory name
-    (make-directory (file-name-concat tmpdir "foo" "bar bar") 'parents)
+    ;; Try directories with special characters.
+    (dolist (bar tramp-hlo--special-names)
+      (make-directory (file-name-concat tmpdir "foo" bar) 'parents)
 
-    ;; Use absolute directory.
-    (tramp-hlo--run-test
-     'dir-locals-find-file (file-name-concat tmpdir "foo" "bar bar" "baz"))
+      ;; Use absolute directory.
+      (tramp-hlo--run-test
+       'dir-locals-find-file (file-name-concat tmpdir "foo" bar "baz"))
 
-    ;; Subdirectory that doesn't exist yet
-    (tramp-hlo--run-test
-     'dir-locals-find-file
-     (file-name-concat tmpdir "foo" "bar bar" "baz" "blah" "bloo"))
+      ;; Subdirectory that doesn't exist yet
+      (tramp-hlo--run-test
+       'dir-locals-find-file
+       (file-name-concat tmpdir "foo" bar "baz" "blah" "bloo"))
 
-    ;; Use relative directory
-    (let ((default-directory (file-name-concat tmpdir "foo" "bar bar" "baz")))
-      (tramp-hlo--run-test 'dir-locals-find-file "./")
-      (tramp-hlo--run-test 'dir-locals-find-file "./blah/bleh"))))
+      ;; Use relative directory
+      (let ((default-directory (file-name-concat tmpdir "foo" bar "baz")))
+       (tramp-hlo--run-test 'dir-locals-find-file "./")
+       (tramp-hlo--run-test 'dir-locals-find-file "./blah/bleh")))))
 
 (ert-deftest tramp-hlo-test-locate-dominating-file ()
   "Test `locate-dominating-file'."
@@ -220,31 +225,31 @@ The result must be equal."
       (tramp-hlo--run-test
        'locate-dominating-file "./blah/bleh" dir-locals-file))
 
-    ;; Directory name with space
-    (make-directory (file-name-concat tmpdir "foo" "bar bar") 'parents)
-    ;; Use absolute directory.  Search for regular file and directory.
-    (tramp-hlo--run-test
-     'locate-dominating-file
-     (file-name-concat tmpdir "foo" "bar bar" "baz") dir-locals-file)
-    (tramp-hlo--run-test
-     'locate-dominating-file
-     (file-name-concat tmpdir "foo" "bar bar" "baz") "foo")
+    ;; Try directories with special characters.
+    (dolist (bar tramp-hlo--special-names)
+      (make-directory (file-name-concat tmpdir "foo" bar) 'parents)
+      ;; Use absolute directory.  Search for regular file and directory.
+      (tramp-hlo--run-test
+       'locate-dominating-file
+       (file-name-concat tmpdir "foo" bar "baz") dir-locals-file)
+      (tramp-hlo--run-test
+       'locate-dominating-file (file-name-concat tmpdir "foo" bar "baz") "foo")
 
-    ;; Use subdirectory that doesn't exist yet
-    (tramp-hlo--run-test
-     'locate-dominating-file
-     (file-name-concat tmpdir "foo" "bar bar" "baz" "blah" "bleh")
-     dir-locals-file)
-    (tramp-hlo--run-test
-     'locate-dominating-file
-     (file-name-concat tmpdir "foo" "bar bar" "baz" "blah" "bleh") "foo")
-    ;; Use relative directory.
-    (let ((default-directory (file-name-concat tmpdir "foo" "bar bar" "baz")))
-      (tramp-hlo--run-test 'locate-dominating-file "./" dir-locals-file)
-      (tramp-hlo--run-test 'locate-dominating-file "./" "foo")
-      (tramp-hlo--run-test 'locate-dominating-file "./blah/bleh" "foo")
+      ;; Use subdirectory that doesn't exist yet
+      (tramp-hlo--run-test
+       'locate-dominating-file
+       (file-name-concat tmpdir "foo" bar "baz" "blah" "bleh")
+       dir-locals-file)
       (tramp-hlo--run-test
-       'locate-dominating-file "./blah/bleh" dir-locals-file))))
+       'locate-dominating-file
+       (file-name-concat tmpdir "foo" bar "blah" "bleh") "foo")
+      ;; Use relative directory.
+      (let ((default-directory (file-name-concat tmpdir "foo" bar "baz")))
+       (tramp-hlo--run-test 'locate-dominating-file "./" dir-locals-file)
+       (tramp-hlo--run-test 'locate-dominating-file "./" "foo")
+       (tramp-hlo--run-test 'locate-dominating-file "./blah/bleh" "foo")
+       (tramp-hlo--run-test
+        'locate-dominating-file "./blah/bleh" dir-locals-file)))))
 
 (provide 'tramp-hlo-tests)
 
diff --git a/tramp-hlo.el b/tramp-hlo.el
index faa1e7e47c..2b70b9e69c 100644
--- a/tramp-hlo.el
+++ b/tramp-hlo.el
@@ -83,14 +83,14 @@ characters need to be doubled.")
 FILE=$1
 shift
 NAMES=$@
-TEST=\"$(dirname $FILE )\"
+TEST=\"$(dirname \"$FILE\" )\"
 echo \\(
 FOUND=\"\"
 while [ ! -z \"$TEST\" ] && [ -z \"$FOUND\" ]; do
     if [ -d \"$TEST\" ]; then
         for NAME in $NAMES; do
             if [ -e \"$TEST/$NAME\" ]; then
-                echo \"\\\"$TEST/$NAME\\\"\"
+                %k \"$TEST/$NAME\"
                 FOUND=1
             fi
         done
@@ -158,7 +158,7 @@ else
 
     # Start the plist with the real filename
     echo \"(\"
-    echo \":file \\\"$FILE\\\" \"
+    printf \":file \"; %k \"$FILE\"; printf \"\\n\"
 
     # Walk up the directory structure looking for the search files
     FOUND=\"\"
@@ -184,7 +184,7 @@ else
 
     # Add found files to the plist
     if [ ! -z \"$FOUND\" ]; then
-        echo \":locals  (\\\"$DOMINATING_DIR/\\\" $FOUND )\"
+        printf \":locals (\"; %k \"$DOMINATING_DIR/\"; echo \" $FOUND)\"
     fi
 
     # Test cached dirs for updated mtime
@@ -271,7 +271,7 @@ Stop at the first parent directory matched, and return the 
directory. Return nil
 if not found."
   (tramp-maybe-send-script vec tramp-hlo-locate-dominating-file-multi-script
                            "locate_dominating_file_multi")
-  (let* ((localfile (tramp-file-name-localname vec))
+  (let* ((localfile (tramp-shell-quote-argument (tramp-file-name-localname 
vec)))
          (quoted-names (mapcar #'tramp-shell-quote-argument names))
          (quoted-names-str (string-join quoted-names " "))
          (command (format
@@ -349,8 +349,8 @@ This function returns a plist with the fields:
            (cache-dirs-string (string-join cache-dirs-quoted " "))
            (command (format
     "dir_locals_find_file_cache_update %s \".dir-locals.el .dir-locals2.el\" 
%s"
-    (tramp-shell-quote-argument (tramp-file-local-name file))
-    cache-dirs-string)))
+           (tramp-shell-quote-argument (tramp-file-local-name file))
+          cache-dirs-string)))
       (tramp-send-command-and-read vec command))))
 
 (defun tramp-hlo-dir-locals-find-file (file)
@@ -374,8 +374,7 @@ This function returns either:
     least one `dir-locals-file' in the case of no valid cache
     entry."
   (let* ((file (if (file-name-absolute-p file)
-                   file
-                 (file-name-concat default-directory file)))
+                   file (file-name-concat default-directory file)))
          (file-connection (file-remote-p file))
          (cache-update (tramp-hlo-dir-locals-find-file-cache-update
                         file dir-locals-directory-cache))

Reply via email to