* test/lisp/tramp-tests.el (tramp-test21-file-links): Special code for smb.
[emacs.git] / test / lisp / net / tramp-tests.el
blobbfdc30178046b7a0e60485f82d83edd873190144
1 ;;; tramp-tests.el --- Tests of remote file access -*- lexical-binding:t -*-
3 ;; Copyright (C) 2013-2017 Free Software Foundation, Inc.
5 ;; Author: Michael Albinus <michael.albinus@gmx.de>
7 ;; This program is free software: you can redistribute it and/or
8 ;; modify it under the terms of the GNU General Public License as
9 ;; published by the Free Software Foundation, either version 3 of the
10 ;; License, or (at your option) any later version.
12 ;; This program is distributed in the hope that it will be useful, but
13 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 ;; General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with this program. If not, see `https://www.gnu.org/licenses/'.
20 ;;; Commentary:
22 ;; The tests require a recent ert.el from Emacs 24.4.
24 ;; Some of the tests require access to a remote host files. Since
25 ;; this could be problematic, a mock-up connection method "mock" is
26 ;; used. Emulating a remote connection, it simply calls "sh -i".
27 ;; Tramp's file name handlers still run, so this test is sufficient
28 ;; except for connection establishing.
30 ;; If you want to test a real Tramp connection, set
31 ;; $REMOTE_TEMPORARY_FILE_DIRECTORY to a suitable value in order to
32 ;; overwrite the default value. If you want to skip tests accessing a
33 ;; remote host, set this environment variable to "/dev/null" or
34 ;; whatever is appropriate on your system.
36 ;; A whole test run can be performed calling the command `tramp-test-all'.
38 ;;; Code:
40 (require 'dired)
41 (require 'ert)
42 (require 'tramp)
43 (require 'vc)
44 (require 'vc-bzr)
45 (require 'vc-git)
46 (require 'vc-hg)
48 (declare-function tramp-find-executable "tramp-sh")
49 (declare-function tramp-get-remote-path "tramp-sh")
50 (declare-function tramp-get-remote-stat "tramp-sh")
51 (declare-function tramp-get-remote-perl "tramp-sh")
52 (defvar auto-save-file-name-transforms)
53 (defvar tramp-copy-size-limit)
54 (defvar tramp-persistency-file-name)
55 (defvar tramp-remote-process-environment)
56 ;; Suppress nasty messages.
57 (fset 'shell-command-sentinel 'ignore)
59 ;; There is no default value on w32 systems, which could work out of the box.
60 (defconst tramp-test-temporary-file-directory
61 (cond
62 ((getenv "REMOTE_TEMPORARY_FILE_DIRECTORY"))
63 ((eq system-type 'windows-nt) null-device)
64 (t (add-to-list
65 'tramp-methods
66 '("mock"
67 (tramp-login-program "sh")
68 (tramp-login-args (("-i")))
69 (tramp-remote-shell "/bin/sh")
70 (tramp-remote-shell-args ("-c"))
71 (tramp-connection-timeout 10)))
72 (add-to-list
73 'tramp-default-host-alist
74 `("\\`mock\\'" nil ,(system-name)))
75 ;; Emacs' Makefile sets $HOME to a nonexistent value. Needed in
76 ;; batch mode only, therefore.
77 (unless (and (null noninteractive) (file-directory-p "~/"))
78 (setenv "HOME" temporary-file-directory))
79 (format "/mock::%s" temporary-file-directory)))
80 "Temporary directory for Tramp tests.")
82 (setq password-cache-expiry nil
83 tramp-verbose 0
84 tramp-cache-read-persistent-data t ;; For auth-sources.
85 tramp-copy-size-limit nil
86 tramp-message-show-message nil
87 tramp-persistency-file-name nil)
89 ;; This should happen on hydra only.
90 (when (getenv "EMACS_HYDRA_CI")
91 (add-to-list 'tramp-remote-path 'tramp-own-remote-path))
93 (defvar tramp--test-expensive-test
94 (null
95 (string-equal (getenv "SELECTOR") "(quote (not (tag :expensive-test)))"))
96 "Whether expensive tests are run.")
98 (defvar tramp--test-enabled-checked nil
99 "Cached result of `tramp--test-enabled'.
100 If the function did run, the value is a cons cell, the `cdr'
101 being the result.")
103 (defun tramp--test-enabled ()
104 "Whether remote file access is enabled."
105 (unless (consp tramp--test-enabled-checked)
106 (setq
107 tramp--test-enabled-checked
108 (cons
109 t (ignore-errors
110 (and
111 (file-remote-p tramp-test-temporary-file-directory)
112 (file-directory-p tramp-test-temporary-file-directory)
113 (file-writable-p tramp-test-temporary-file-directory))))))
115 (when (cdr tramp--test-enabled-checked)
116 ;; Cleanup connection.
117 (ignore-errors
118 (tramp-cleanup-connection
119 (tramp-dissect-file-name tramp-test-temporary-file-directory)
120 nil 'keep-password)))
122 ;; Return result.
123 (cdr tramp--test-enabled-checked))
125 (defun tramp--test-make-temp-name (&optional local quoted)
126 "Return a temporary file name for test.
127 If LOCAL is non-nil, a local file name is returned.
128 If QUOTED is non-nil, the local part of the file name is quoted.
129 The temporary file is not created."
130 (funcall
131 (if quoted 'tramp-compat-file-name-quote 'identity)
132 (expand-file-name
133 (make-temp-name "tramp-test")
134 (if local temporary-file-directory tramp-test-temporary-file-directory))))
136 ;; Don't print messages in nested `tramp--test-instrument-test-case' calls.
137 (defvar tramp--test-instrument-test-case-p nil
138 "Whether `tramp--test-instrument-test-case' run.
139 This shall used dynamically bound only.")
141 (defmacro tramp--test-instrument-test-case (verbose &rest body)
142 "Run BODY with `tramp-verbose' equal VERBOSE.
143 Print the the content of the Tramp debug buffer, if BODY does not
144 eval properly in `should' or `should-not'. `should-error' is not
145 handled properly. BODY shall not contain a timeout."
146 (declare (indent 1) (debug (natnump body)))
147 `(let ((tramp-verbose (max (or ,verbose 0) (or tramp-verbose 0)))
148 (tramp-message-show-message t)
149 (tramp-debug-on-error t)
150 (debug-ignored-errors
151 (cons "^make-symbolic-link not supported$" debug-ignored-errors))
152 inhibit-message)
153 (unwind-protect
154 (let ((tramp--test-instrument-test-case-p t)) ,@body)
155 ;; Unwind forms.
156 (when (and (null tramp--test-instrument-test-case-p) (> tramp-verbose 3))
157 (with-parsed-tramp-file-name tramp-test-temporary-file-directory nil
158 (with-current-buffer (tramp-get-connection-buffer v)
159 (message "%s" (buffer-string)))
160 (with-current-buffer (tramp-get-debug-buffer v)
161 (message "%s" (buffer-string))))))))
163 (defsubst tramp--test-message (fmt-string &rest arguments)
164 "Emit a message into ERT *Messages*."
165 (tramp--test-instrument-test-case 0
166 (apply
167 'tramp-message
168 (tramp-dissect-file-name tramp-test-temporary-file-directory) 0
169 fmt-string arguments)))
171 (defsubst tramp--test-backtrace ()
172 "Dump a backtrace into ERT *Messages*."
173 (tramp--test-instrument-test-case 10
174 (tramp-backtrace
175 (tramp-dissect-file-name tramp-test-temporary-file-directory))))
177 (ert-deftest tramp-test00-availability ()
178 "Test availability of Tramp functions."
179 :expected-result (if (tramp--test-enabled) :passed :failed)
180 (tramp--test-message
181 "Remote directory: `%s'" tramp-test-temporary-file-directory)
182 (should (ignore-errors
183 (and
184 (file-remote-p tramp-test-temporary-file-directory)
185 (file-directory-p tramp-test-temporary-file-directory)
186 (file-writable-p tramp-test-temporary-file-directory)))))
188 (ert-deftest tramp-test01-file-name-syntax ()
189 "Check remote file name syntax."
190 ;; Simple cases.
191 (should (tramp-tramp-file-p "/method::"))
192 (should (tramp-tramp-file-p "/method:host:"))
193 (should (tramp-tramp-file-p "/method:user@:"))
194 (should (tramp-tramp-file-p "/method:user@host:"))
195 (should (tramp-tramp-file-p "/method:user@email@host:"))
197 ;; Using a port.
198 (should (tramp-tramp-file-p "/method:host#1234:"))
199 (should (tramp-tramp-file-p "/method:user@host#1234:"))
201 ;; Using an IPv4 address.
202 (should (tramp-tramp-file-p "/method:1.2.3.4:"))
203 (should (tramp-tramp-file-p "/method:user@1.2.3.4:"))
205 ;; Using an IPv6 address.
206 (should (tramp-tramp-file-p "/method:[::1]:"))
207 (should (tramp-tramp-file-p "/method:user@[::1]:"))
209 ;; Local file name part.
210 (should (tramp-tramp-file-p "/method:::"))
211 (should (tramp-tramp-file-p "/method::/:"))
212 (should (tramp-tramp-file-p "/method::/path/to/file"))
213 (should (tramp-tramp-file-p "/method::/:/path/to/file"))
214 (should (tramp-tramp-file-p "/method::file"))
215 (should (tramp-tramp-file-p "/method::/:file"))
217 ;; Multihop.
218 (should (tramp-tramp-file-p "/method1:|method2::"))
219 (should (tramp-tramp-file-p "/method1:host1|method2:host2:"))
220 (should (tramp-tramp-file-p "/method1:user1@host1|method2:user2@host2:"))
221 (should (tramp-tramp-file-p
222 "/method1:user1@host1|method2:user2@host2|method3:user3@host3:"))
224 ;; No strings.
225 (should-not (tramp-tramp-file-p nil))
226 (should-not (tramp-tramp-file-p 'symbol))
227 ;; Ange-ftp syntax.
228 (should-not (tramp-tramp-file-p "/host:"))
229 (should-not (tramp-tramp-file-p "/user@host:"))
230 (should-not (tramp-tramp-file-p "/1.2.3.4:"))
231 (should-not (tramp-tramp-file-p "/[]:"))
232 (should-not (tramp-tramp-file-p "/[::1]:"))
233 (should-not (tramp-tramp-file-p "/host:/:"))
234 (should-not (tramp-tramp-file-p "/host1|host2:"))
235 (should-not (tramp-tramp-file-p "/user1@host1|user2@host2:"))
236 ;; Quote with "/:" suppresses file name handlers.
237 (should-not (tramp-tramp-file-p "/::"))
238 (should-not (tramp-tramp-file-p "/:@:"))
239 (should-not (tramp-tramp-file-p "/:[]:"))
240 ;; Methods shall be at least two characters on MS Windows, except
241 ;; the default method.
242 (let ((system-type 'windows-nt))
243 (should-not (tramp-tramp-file-p "/c:/path/to/file"))
244 (should-not (tramp-tramp-file-p "/c::/path/to/file"))
245 (should (tramp-tramp-file-p "/-::/path/to/file")))
246 (let ((system-type 'gnu/linux))
247 (should (tramp-tramp-file-p "/-:h:/path/to/file"))
248 (should (tramp-tramp-file-p "/m::/path/to/file"))))
250 (ert-deftest tramp-test01-file-name-syntax-simplified ()
251 "Check simplified file name syntax."
252 :tags '(:expensive-test)
253 (let ((syntax tramp-syntax))
254 (unwind-protect
255 (progn
256 (tramp-change-syntax 'simplified)
257 ;; Simple cases.
258 (should (tramp-tramp-file-p "/host:"))
259 (should (tramp-tramp-file-p "/user@:"))
260 (should (tramp-tramp-file-p "/user@host:"))
261 (should (tramp-tramp-file-p "/user@email@host:"))
263 ;; Using a port.
264 (should (tramp-tramp-file-p "/host#1234:"))
265 (should (tramp-tramp-file-p "/user@host#1234:"))
267 ;; Using an IPv4 address.
268 (should (tramp-tramp-file-p "/1.2.3.4:"))
269 (should (tramp-tramp-file-p "/user@1.2.3.4:"))
271 ;; Using an IPv6 address.
272 (should (tramp-tramp-file-p "/[::1]:"))
273 (should (tramp-tramp-file-p "/user@[::1]:"))
275 ;; Local file name part.
276 (should (tramp-tramp-file-p "/host::"))
277 (should (tramp-tramp-file-p "/host:/:"))
278 (should (tramp-tramp-file-p "/host:/path/to/file"))
279 (should (tramp-tramp-file-p "/host:/:/path/to/file"))
280 (should (tramp-tramp-file-p "/host:file"))
281 (should (tramp-tramp-file-p "/host:/:file"))
283 ;; Multihop.
284 (should (tramp-tramp-file-p "/host1|host2:"))
285 (should (tramp-tramp-file-p "/user1@host1|user2@host2:"))
286 (should (tramp-tramp-file-p "/user1@host1|user2@host2|user3@host3:"))
288 ;; No strings.
289 (should-not (tramp-tramp-file-p nil))
290 (should-not (tramp-tramp-file-p 'symbol))
291 ;; Quote with "/:" suppresses file name handlers.
292 (should-not (tramp-tramp-file-p "/::"))
293 (should-not (tramp-tramp-file-p "/:@:"))
294 (should-not (tramp-tramp-file-p "/:[]:")))
296 ;; Exit.
297 (tramp-change-syntax syntax))))
299 (ert-deftest tramp-test01-file-name-syntax-separate ()
300 "Check separate file name syntax."
301 :tags '(:expensive-test)
302 (let ((syntax tramp-syntax))
303 (unwind-protect
304 (progn
305 (tramp-change-syntax 'separate)
306 ;; Simple cases.
307 (should (tramp-tramp-file-p "/[method/]"))
308 (should (tramp-tramp-file-p "/[method/host]"))
309 (should (tramp-tramp-file-p "/[method/user@]"))
310 (should (tramp-tramp-file-p "/[method/user@host]"))
311 (should (tramp-tramp-file-p "/[method/user@email@host]"))
313 ;; Using a port.
314 (should (tramp-tramp-file-p "/[method/host#1234]"))
315 (should (tramp-tramp-file-p "/[method/user@host#1234]"))
317 ;; Using an IPv4 address.
318 (should (tramp-tramp-file-p "/[method/1.2.3.4]"))
319 (should (tramp-tramp-file-p "/[method/user@1.2.3.4]"))
321 ;; Using an IPv6 address.
322 (should (tramp-tramp-file-p "/[method/::1]"))
323 (should (tramp-tramp-file-p "/[method/user@::1]"))
325 ;; Local file name part.
326 (should (tramp-tramp-file-p "/[method/]"))
327 (should (tramp-tramp-file-p "/[method/]/:"))
328 (should (tramp-tramp-file-p "/[method/]/path/to/file"))
329 (should (tramp-tramp-file-p "/[method/]/:/path/to/file"))
330 (should (tramp-tramp-file-p "/[method/]file"))
331 (should (tramp-tramp-file-p "/[method/]/:file"))
333 ;; Multihop.
334 (should (tramp-tramp-file-p "/[method1/|method2/]"))
335 (should (tramp-tramp-file-p "/[method1/host1|method2/host2]"))
336 (should
337 (tramp-tramp-file-p
338 "/[method1/user1@host1|method2/user2@host2]"))
339 (should
340 (tramp-tramp-file-p
341 "/[method1/user1@host1|method2/user2@host2|method3/user3@host3]"))
343 ;; No strings.
344 (should-not (tramp-tramp-file-p nil))
345 (should-not (tramp-tramp-file-p 'symbol))
346 ;; Ange-ftp syntax.
347 (should-not (tramp-tramp-file-p "/host:"))
348 (should-not (tramp-tramp-file-p "/user@host:"))
349 (should-not (tramp-tramp-file-p "/1.2.3.4:"))
350 (should-not (tramp-tramp-file-p "/host:/:"))
351 (should-not (tramp-tramp-file-p "/host1|host2:"))
352 (should-not (tramp-tramp-file-p "/user1@host1|user2@host2:"))
353 ;; Quote with "/:" suppresses file name handlers.
354 (should-not (tramp-tramp-file-p "/:[]")))
356 ;; Exit.
357 (tramp-change-syntax syntax))))
359 (ert-deftest tramp-test02-file-name-dissect ()
360 "Check remote file name components."
361 (let ((tramp-default-method "default-method")
362 (tramp-default-user "default-user")
363 (tramp-default-host "default-host"))
364 ;; Expand `tramp-default-user' and `tramp-default-host'.
365 (should (string-equal
366 (file-remote-p "/method::")
367 (format "/%s:%s@%s:" "method" "default-user" "default-host")))
368 (should (string-equal (file-remote-p "/method::" 'method) "method"))
369 (should (string-equal (file-remote-p "/method::" 'user) "default-user"))
370 (should (string-equal (file-remote-p "/method::" 'host) "default-host"))
371 (should (string-equal (file-remote-p "/method::" 'localname) ""))
372 (should (string-equal (file-remote-p "/method::" 'hop) nil))
374 ;; Expand `tramp-default-method' and `tramp-default-user'.
375 (should (string-equal
376 (file-remote-p "/-:host:")
377 (format "/%s:%s@%s:" "default-method" "default-user" "host")))
378 (should (string-equal (file-remote-p "/-:host:" 'method) "default-method"))
379 (should (string-equal (file-remote-p "/-:host:" 'user) "default-user"))
380 (should (string-equal (file-remote-p "/-:host:" 'host) "host"))
381 (should (string-equal (file-remote-p "/-:host:" 'localname) ""))
382 (should (string-equal (file-remote-p "/-:host:" 'hop) nil))
384 ;; Expand `tramp-default-method' and `tramp-default-host'.
385 (should (string-equal
386 (file-remote-p "/-:user@:")
387 (format "/%s:%s@%s:" "default-method" "user" "default-host")))
388 (should (string-equal (file-remote-p "/-:user@:" 'method) "default-method"))
389 (should (string-equal (file-remote-p "/-:user@:" 'user) "user"))
390 (should (string-equal (file-remote-p "/-:user@:" 'host) "default-host"))
391 (should (string-equal (file-remote-p "/-:user@:" 'localname) ""))
392 (should (string-equal (file-remote-p "/-:user@:" 'hop) nil))
394 ;; Expand `tramp-default-method'.
395 (should (string-equal
396 (file-remote-p "/-:user@host:")
397 (format "/%s:%s@%s:" "default-method" "user" "host")))
398 (should (string-equal
399 (file-remote-p "/-:user@host:" 'method) "default-method"))
400 (should (string-equal (file-remote-p "/-:user@host:" 'user) "user"))
401 (should (string-equal (file-remote-p "/-:user@host:" 'host) "host"))
402 (should (string-equal (file-remote-p "/-:user@host:" 'localname) ""))
403 (should (string-equal (file-remote-p "/-:user@host:" 'hop) nil))
405 ;; Expand `tramp-default-user'.
406 (should (string-equal
407 (file-remote-p "/method:host:")
408 (format "/%s:%s@%s:" "method" "default-user" "host")))
409 (should (string-equal (file-remote-p "/method:host:" 'method) "method"))
410 (should (string-equal (file-remote-p "/method:host:" 'user) "default-user"))
411 (should (string-equal (file-remote-p "/method:host:" 'host) "host"))
412 (should (string-equal (file-remote-p "/method:host:" 'localname) ""))
413 (should (string-equal (file-remote-p "/method:host:" 'hop) nil))
415 ;; Expand `tramp-default-host'.
416 (should (string-equal
417 (file-remote-p "/method:user@:")
418 (format "/%s:%s@%s:" "method" "user" "default-host")))
419 (should (string-equal (file-remote-p "/method:user@:" 'method) "method"))
420 (should (string-equal (file-remote-p "/method:user@:" 'user) "user"))
421 (should (string-equal (file-remote-p "/method:user@:" 'host)
422 "default-host"))
423 (should (string-equal (file-remote-p "/method:user@:" 'localname) ""))
424 (should (string-equal (file-remote-p "/method:user@:" 'hop) nil))
426 ;; No expansion.
427 (should (string-equal
428 (file-remote-p "/method:user@host:")
429 (format "/%s:%s@%s:" "method" "user" "host")))
430 (should (string-equal
431 (file-remote-p "/method:user@host:" 'method) "method"))
432 (should (string-equal (file-remote-p "/method:user@host:" 'user) "user"))
433 (should (string-equal (file-remote-p "/method:user@host:" 'host) "host"))
434 (should (string-equal (file-remote-p "/method:user@host:" 'localname) ""))
435 (should (string-equal (file-remote-p "/method:user@host:" 'hop) nil))
437 ;; No expansion.
438 (should (string-equal
439 (file-remote-p "/method:user@email@host:")
440 (format "/%s:%s@%s:" "method" "user@email" "host")))
441 (should (string-equal
442 (file-remote-p "/method:user@email@host:" 'method) "method"))
443 (should (string-equal
444 (file-remote-p "/method:user@email@host:" 'user) "user@email"))
445 (should (string-equal
446 (file-remote-p "/method:user@email@host:" 'host) "host"))
447 (should (string-equal
448 (file-remote-p "/method:user@email@host:" 'localname) ""))
449 (should (string-equal
450 (file-remote-p "/method:user@email@host:" 'hop) nil))
452 ;; Expand `tramp-default-method' and `tramp-default-user'.
453 (should (string-equal
454 (file-remote-p "/-:host#1234:")
455 (format "/%s:%s@%s:" "default-method" "default-user" "host#1234")))
456 (should (string-equal
457 (file-remote-p "/-:host#1234:" 'method) "default-method"))
458 (should (string-equal (file-remote-p "/-:host#1234:" 'user) "default-user"))
459 (should (string-equal (file-remote-p "/-:host#1234:" 'host) "host#1234"))
460 (should (string-equal (file-remote-p "/-:host#1234:" 'localname) ""))
461 (should (string-equal (file-remote-p "/-:host#1234:" 'hop) nil))
463 ;; Expand `tramp-default-method'.
464 (should (string-equal
465 (file-remote-p "/-:user@host#1234:")
466 (format "/%s:%s@%s:" "default-method" "user" "host#1234")))
467 (should (string-equal
468 (file-remote-p "/-:user@host#1234:" 'method) "default-method"))
469 (should (string-equal (file-remote-p "/-:user@host#1234:" 'user) "user"))
470 (should (string-equal (file-remote-p "/-:user@host#1234:" 'host) "host#1234"))
471 (should (string-equal (file-remote-p "/-:user@host#1234:" 'localname) ""))
472 (should (string-equal (file-remote-p "/-:user@host#1234:" 'hop) nil))
474 ;; Expand `tramp-default-user'.
475 (should (string-equal
476 (file-remote-p "/method:host#1234:")
477 (format "/%s:%s@%s:" "method" "default-user" "host#1234")))
478 (should (string-equal
479 (file-remote-p "/method:host#1234:" 'method) "method"))
480 (should (string-equal
481 (file-remote-p "/method:host#1234:" 'user) "default-user"))
482 (should (string-equal
483 (file-remote-p "/method:host#1234:" 'host) "host#1234"))
484 (should (string-equal (file-remote-p "/method:host#1234:" 'localname) ""))
485 (should (string-equal (file-remote-p "/method:host#1234:" 'hop) nil))
487 ;; No expansion.
488 (should (string-equal
489 (file-remote-p "/method:user@host#1234:")
490 (format "/%s:%s@%s:" "method" "user" "host#1234")))
491 (should (string-equal
492 (file-remote-p "/method:user@host#1234:" 'method) "method"))
493 (should (string-equal
494 (file-remote-p "/method:user@host#1234:" 'user) "user"))
495 (should (string-equal
496 (file-remote-p "/method:user@host#1234:" 'host) "host#1234"))
497 (should (string-equal
498 (file-remote-p "/method:user@host#1234:" 'localname) ""))
499 (should (string-equal
500 (file-remote-p "/method:user@host#1234:" 'hop) nil))
502 ;; Expand `tramp-default-method' and `tramp-default-user'.
503 (should (string-equal
504 (file-remote-p "/-:1.2.3.4:")
505 (format "/%s:%s@%s:" "default-method" "default-user" "1.2.3.4")))
506 (should (string-equal (file-remote-p "/-:1.2.3.4:" 'method) "default-method"))
507 (should (string-equal (file-remote-p "/-:1.2.3.4:" 'user) "default-user"))
508 (should (string-equal (file-remote-p "/-:1.2.3.4:" 'host) "1.2.3.4"))
509 (should (string-equal (file-remote-p "/-:1.2.3.4:" 'localname) ""))
510 (should (string-equal (file-remote-p "/-:1.2.3.4:" 'hop) nil))
512 ;; Expand `tramp-default-method'.
513 (should (string-equal
514 (file-remote-p "/-:user@1.2.3.4:")
515 (format "/%s:%s@%s:" "default-method" "user" "1.2.3.4")))
516 (should (string-equal
517 (file-remote-p "/-:user@1.2.3.4:" 'method) "default-method"))
518 (should (string-equal (file-remote-p "/-:user@1.2.3.4:" 'user) "user"))
519 (should (string-equal (file-remote-p "/-:user@1.2.3.4:" 'host) "1.2.3.4"))
520 (should (string-equal (file-remote-p "/-:user@1.2.3.4:" 'localname) ""))
521 (should (string-equal (file-remote-p "/-:user@1.2.3.4:" 'hop) nil))
523 ;; Expand `tramp-default-user'.
524 (should (string-equal
525 (file-remote-p "/method:1.2.3.4:")
526 (format "/%s:%s@%s:" "method" "default-user" "1.2.3.4")))
527 (should (string-equal (file-remote-p "/method:1.2.3.4:" 'method) "method"))
528 (should (string-equal
529 (file-remote-p "/method:1.2.3.4:" 'user) "default-user"))
530 (should (string-equal (file-remote-p "/method:1.2.3.4:" 'host) "1.2.3.4"))
531 (should (string-equal (file-remote-p "/method:1.2.3.4:" 'localname) ""))
532 (should (string-equal (file-remote-p "/method:1.2.3.4:" 'hop) nil))
534 ;; No expansion.
535 (should (string-equal
536 (file-remote-p "/method:user@1.2.3.4:")
537 (format "/%s:%s@%s:" "method" "user" "1.2.3.4")))
538 (should (string-equal
539 (file-remote-p "/method:user@1.2.3.4:" 'method) "method"))
540 (should (string-equal (file-remote-p "/method:user@1.2.3.4:" 'user) "user"))
541 (should (string-equal
542 (file-remote-p "/method:user@1.2.3.4:" 'host) "1.2.3.4"))
543 (should (string-equal
544 (file-remote-p "/method:user@1.2.3.4:" 'localname) ""))
545 (should (string-equal
546 (file-remote-p "/method:user@1.2.3.4:" 'hop) nil))
548 ;; Expand `tramp-default-method', `tramp-default-user' and
549 ;; `tramp-default-host'.
550 (should (string-equal
551 (file-remote-p "/-:[]:")
552 (format
553 "/%s:%s@%s:" "default-method" "default-user" "default-host")))
554 (should (string-equal (file-remote-p "/-:[]:" 'method) "default-method"))
555 (should (string-equal (file-remote-p "/-:[]:" 'user) "default-user"))
556 (should (string-equal (file-remote-p "/-:[]:" 'host) "default-host"))
557 (should (string-equal (file-remote-p "/-:[]:" 'localname) ""))
558 (should (string-equal (file-remote-p "/-:[]:" 'hop) nil))
560 ;; Expand `tramp-default-method' and `tramp-default-user'.
561 (let ((tramp-default-host "::1"))
562 (should (string-equal
563 (file-remote-p "/-:[]:")
564 (format "/%s:%s@%s:" "default-method" "default-user" "[::1]")))
565 (should (string-equal (file-remote-p "/-:[]:" 'method) "default-method"))
566 (should (string-equal (file-remote-p "/-:[]:" 'user) "default-user"))
567 (should (string-equal (file-remote-p "/-:[]:" 'host) "::1"))
568 (should (string-equal (file-remote-p "/-:[]:" 'localname) ""))
569 (should (string-equal (file-remote-p "/-:[]:" 'hop) nil)))
571 ;; Expand `tramp-default-method' and `tramp-default-user'.
572 (should (string-equal
573 (file-remote-p "/-:[::1]:")
574 (format "/%s:%s@%s:" "default-method" "default-user" "[::1]")))
575 (should (string-equal (file-remote-p "/-:[::1]:" 'method) "default-method"))
576 (should (string-equal (file-remote-p "/-:[::1]:" 'user) "default-user"))
577 (should (string-equal (file-remote-p "/-:[::1]:" 'host) "::1"))
578 (should (string-equal (file-remote-p "/-:[::1]:" 'localname) ""))
579 (should (string-equal (file-remote-p "/-:[::1]:" 'hop) nil))
581 ;; Expand `tramp-default-method'.
582 (should (string-equal
583 (file-remote-p "/-:user@[::1]:")
584 (format "/%s:%s@%s:" "default-method" "user" "[::1]")))
585 (should (string-equal
586 (file-remote-p "/-:user@[::1]:" 'method) "default-method"))
587 (should (string-equal (file-remote-p "/-:user@[::1]:" 'user) "user"))
588 (should (string-equal (file-remote-p "/-:user@[::1]:" 'host) "::1"))
589 (should (string-equal (file-remote-p "/-:user@[::1]:" 'localname) ""))
590 (should (string-equal (file-remote-p "/-:user@[::1]:" 'hop) nil))
592 ;; Expand `tramp-default-user'.
593 (should (string-equal
594 (file-remote-p "/method:[::1]:")
595 (format "/%s:%s@%s:" "method" "default-user" "[::1]")))
596 (should (string-equal (file-remote-p "/method:[::1]:" 'method) "method"))
597 (should (string-equal
598 (file-remote-p "/method:[::1]:" 'user) "default-user"))
599 (should (string-equal (file-remote-p "/method:[::1]:" 'host) "::1"))
600 (should (string-equal (file-remote-p "/method:[::1]:" 'localname) ""))
601 (should (string-equal (file-remote-p "/method:[::1]:" 'hop) nil))
603 ;; No expansion.
604 (should (string-equal
605 (file-remote-p "/method:user@[::1]:")
606 (format "/%s:%s@%s:" "method" "user" "[::1]")))
607 (should (string-equal
608 (file-remote-p "/method:user@[::1]:" 'method) "method"))
609 (should (string-equal (file-remote-p "/method:user@[::1]:" 'user) "user"))
610 (should (string-equal (file-remote-p "/method:user@[::1]:" 'host) "::1"))
611 (should (string-equal
612 (file-remote-p "/method:user@[::1]:" 'localname) ""))
613 (should (string-equal (file-remote-p "/method:user@[::1]:" 'hop) nil))
615 ;; Local file name part.
616 (should (string-equal (file-remote-p "/-:host:/:" 'localname) "/:"))
617 (should (string-equal (file-remote-p "/method:::" 'localname) ":"))
618 (should (string-equal (file-remote-p "/method:: " 'localname) " "))
619 (should (string-equal (file-remote-p "/method::file" 'localname) "file"))
620 (should (string-equal
621 (file-remote-p "/method::/path/to/file" 'localname)
622 "/path/to/file"))
624 ;; Multihop.
625 (should
626 (string-equal
627 (file-remote-p "/method1:user1@host1|method2:user2@host2:/path/to/file")
628 (format "/%s:%s@%s|%s:%s@%s:"
629 "method1" "user1" "host1" "method2" "user2" "host2")))
630 (should
631 (string-equal
632 (file-remote-p
633 "/method1:user1@host1|method2:user2@host2:/path/to/file" 'method)
634 "method2"))
635 (should
636 (string-equal
637 (file-remote-p
638 "/method1:user1@host1|method2:user2@host2:/path/to/file" 'user)
639 "user2"))
640 (should
641 (string-equal
642 (file-remote-p
643 "/method1:user1@host1|method2:user2@host2:/path/to/file" 'host)
644 "host2"))
645 (should
646 (string-equal
647 (file-remote-p
648 "/method1:user1@host1|method2:user2@host2:/path/to/file" 'localname)
649 "/path/to/file"))
650 (should
651 (string-equal
652 (file-remote-p
653 "/method1:user1@host1|method2:user2@host2:/path/to/file" 'hop)
654 (format "%s:%s@%s|"
655 "method1" "user1" "host1")))
657 (should
658 (string-equal
659 (file-remote-p
660 (concat
661 "/method1:user1@host1"
662 "|method2:user2@host2"
663 "|method3:user3@host3:/path/to/file"))
664 (format "/%s:%s@%s|%s:%s@%s|%s:%s@%s:"
665 "method1" "user1" "host1"
666 "method2" "user2" "host2"
667 "method3" "user3" "host3")))
668 (should
669 (string-equal
670 (file-remote-p
671 (concat
672 "/method1:user1@host1"
673 "|method2:user2@host2"
674 "|method3:user3@host3:/path/to/file")
675 'method)
676 "method3"))
677 (should
678 (string-equal
679 (file-remote-p
680 (concat
681 "/method1:user1@host1"
682 "|method2:user2@host2"
683 "|method3:user3@host3:/path/to/file")
684 'user)
685 "user3"))
686 (should
687 (string-equal
688 (file-remote-p
689 (concat
690 "/method1:user1@host1"
691 "|method2:user2@host2"
692 "|method3:user3@host3:/path/to/file")
693 'host)
694 "host3"))
695 (should
696 (string-equal
697 (file-remote-p
698 (concat
699 "/method1:user1@host1"
700 "|method2:user2@host2"
701 "|method3:user3@host3:/path/to/file")
702 'localname)
703 "/path/to/file"))
704 (should
705 (string-equal
706 (file-remote-p
707 (concat
708 "/method1:user1@host1"
709 "|method2:user2@host2"
710 "|method3:user3@host3:/path/to/file")
711 'hop)
712 (format "%s:%s@%s|%s:%s@%s|"
713 "method1" "user1" "host1" "method2" "user2" "host2")))))
715 (ert-deftest tramp-test02-file-name-dissect-simplified ()
716 "Check simplified file name components."
717 :tags '(:expensive-test)
718 (let ((tramp-default-method "default-method")
719 (tramp-default-user "default-user")
720 (tramp-default-host "default-host")
721 (syntax tramp-syntax))
722 (unwind-protect
723 (progn
724 (tramp-change-syntax 'simplified)
725 ;; Expand `tramp-default-method' and `tramp-default-user'.
726 (should (string-equal
727 (file-remote-p "/host:")
728 (format "/%s@%s:" "default-user" "host")))
729 (should (string-equal
730 (file-remote-p "/host:" 'method) "default-method"))
731 (should (string-equal (file-remote-p "/host:" 'user) "default-user"))
732 (should (string-equal (file-remote-p "/host:" 'host) "host"))
733 (should (string-equal (file-remote-p "/host:" 'localname) ""))
734 (should (string-equal (file-remote-p "/host:" 'hop) nil))
736 ;; Expand `tramp-default-method' and `tramp-default-host'.
737 (should (string-equal
738 (file-remote-p "/user@:")
739 (format "/%s@%s:" "user" "default-host")))
740 (should (string-equal
741 (file-remote-p "/user@:" 'method) "default-method"))
742 (should (string-equal (file-remote-p "/user@:" 'user) "user"))
743 (should (string-equal (file-remote-p "/user@:" 'host) "default-host"))
744 (should (string-equal (file-remote-p "/user@:" 'localname) ""))
745 (should (string-equal (file-remote-p "/user@:" 'hop) nil))
747 ;; Expand `tramp-default-method'.
748 (should (string-equal
749 (file-remote-p "/user@host:")
750 (format "/%s@%s:" "user" "host")))
751 (should (string-equal
752 (file-remote-p "/user@host:" 'method) "default-method"))
753 (should (string-equal (file-remote-p "/user@host:" 'user) "user"))
754 (should (string-equal (file-remote-p "/user@host:" 'host) "host"))
755 (should (string-equal (file-remote-p "/user@host:" 'localname) ""))
756 (should (string-equal (file-remote-p "/user@host:" 'hop) nil))
758 ;; No expansion.
759 (should (string-equal
760 (file-remote-p "/user@email@host:")
761 (format "/%s@%s:" "user@email" "host")))
762 (should (string-equal
763 (file-remote-p
764 "/user@email@host:" 'method) "default-method"))
765 (should (string-equal
766 (file-remote-p "/user@email@host:" 'user) "user@email"))
767 (should (string-equal
768 (file-remote-p "/user@email@host:" 'host) "host"))
769 (should (string-equal
770 (file-remote-p "/user@email@host:" 'localname) ""))
771 (should (string-equal
772 (file-remote-p "/user@email@host:" 'hop) nil))
774 ;; Expand `tramp-default-method' and `tramp-default-user'.
775 (should (string-equal
776 (file-remote-p "/host#1234:")
777 (format "/%s@%s:" "default-user" "host#1234")))
778 (should (string-equal
779 (file-remote-p "/host#1234:" 'method) "default-method"))
780 (should (string-equal
781 (file-remote-p "/host#1234:" 'user) "default-user"))
782 (should (string-equal
783 (file-remote-p "/host#1234:" 'host) "host#1234"))
784 (should (string-equal (file-remote-p "/host#1234:" 'localname) ""))
785 (should (string-equal (file-remote-p "/host#1234:" 'hop) nil))
787 ;; Expand `tramp-default-method'.
788 (should (string-equal
789 (file-remote-p "/user@host#1234:")
790 (format "/%s@%s:" "user" "host#1234")))
791 (should (string-equal
792 (file-remote-p "/user@host#1234:" 'method) "default-method"))
793 (should (string-equal
794 (file-remote-p "/user@host#1234:" 'user) "user"))
795 (should (string-equal
796 (file-remote-p "/user@host#1234:" 'host) "host#1234"))
797 (should (string-equal
798 (file-remote-p "/user@host#1234:" 'localname) ""))
799 (should (string-equal (file-remote-p "/user@host#1234:" 'hop) nil))
801 ;; Expand `tramp-default-method' and `tramp-default-user'.
802 (should (string-equal
803 (file-remote-p "/1.2.3.4:")
804 (format "/%s@%s:" "default-user" "1.2.3.4")))
805 (should (string-equal
806 (file-remote-p "/1.2.3.4:" 'method) "default-method"))
807 (should (string-equal
808 (file-remote-p "/1.2.3.4:" 'user) "default-user"))
809 (should (string-equal (file-remote-p "/1.2.3.4:" 'host) "1.2.3.4"))
810 (should (string-equal (file-remote-p "/1.2.3.4:" 'localname) ""))
811 (should (string-equal (file-remote-p "/1.2.3.4:" 'hop) nil))
813 ;; Expand `tramp-default-method'.
814 (should (string-equal
815 (file-remote-p "/user@1.2.3.4:")
816 (format "/%s@%s:" "user" "1.2.3.4")))
817 (should (string-equal
818 (file-remote-p "/user@1.2.3.4:" 'method) "default-method"))
819 (should (string-equal (file-remote-p "/user@1.2.3.4:" 'user) "user"))
820 (should (string-equal
821 (file-remote-p "/user@1.2.3.4:" 'host) "1.2.3.4"))
822 (should (string-equal (file-remote-p "/user@1.2.3.4:" 'localname) ""))
823 (should (string-equal (file-remote-p "/user@1.2.3.4:" 'hop) nil))
825 ;; Expand `tramp-default-method', `tramp-default-user' and
826 ;; `tramp-default-host'.
827 (should (string-equal
828 (file-remote-p "/[]:")
829 (format
830 "/%s@%s:" "default-user" "default-host")))
831 (should (string-equal
832 (file-remote-p "/[]:" 'method) "default-method"))
833 (should (string-equal (file-remote-p "/[]:" 'user) "default-user"))
834 (should (string-equal (file-remote-p "/[]:" 'host) "default-host"))
835 (should (string-equal (file-remote-p "/[]:" 'localname) ""))
836 (should (string-equal (file-remote-p "/[]:" 'hop) nil))
838 ;; Expand `tramp-default-method' and `tramp-default-user'.
839 (let ((tramp-default-host "::1"))
840 (should (string-equal
841 (file-remote-p "/[]:")
842 (format "/%s@%s:" "default-user" "[::1]")))
843 (should (string-equal
844 (file-remote-p "/[]:" 'method) "default-method"))
845 (should (string-equal (file-remote-p "/[]:" 'user) "default-user"))
846 (should (string-equal (file-remote-p "/[]:" 'host) "::1"))
847 (should (string-equal (file-remote-p "/[]:" 'localname) ""))
848 (should (string-equal (file-remote-p "/[]:" 'hop) nil)))
850 ;; Expand `tramp-default-method' and `tramp-default-user'.
851 (should (string-equal
852 (file-remote-p "/[::1]:")
853 (format "/%s@%s:" "default-user" "[::1]")))
854 (should (string-equal
855 (file-remote-p "/[::1]:" 'method) "default-method"))
856 (should (string-equal (file-remote-p "/[::1]:" 'user) "default-user"))
857 (should (string-equal (file-remote-p "/[::1]:" 'host) "::1"))
858 (should (string-equal (file-remote-p "/[::1]:" 'localname) ""))
859 (should (string-equal (file-remote-p "/[::1]:" 'hop) nil))
861 ;; Expand `tramp-default-method'.
862 (should (string-equal
863 (file-remote-p "/user@[::1]:")
864 (format "/%s@%s:" "user" "[::1]")))
865 (should (string-equal
866 (file-remote-p "/user@[::1]:" 'method) "default-method"))
867 (should (string-equal (file-remote-p "/user@[::1]:" 'user) "user"))
868 (should (string-equal (file-remote-p "/user@[::1]:" 'host) "::1"))
869 (should (string-equal (file-remote-p "/user@[::1]:" 'localname) ""))
870 (should (string-equal (file-remote-p "/user@[::1]:" 'hop) nil))
872 ;; Local file name part.
873 (should (string-equal (file-remote-p "/host:/:" 'localname) "/:"))
874 (should (string-equal (file-remote-p "/host::" 'localname) ":"))
875 (should (string-equal (file-remote-p "/host: " 'localname) " "))
876 (should (string-equal (file-remote-p "/host:file" 'localname) "file"))
877 (should (string-equal
878 (file-remote-p "/host:/path/to/file" 'localname)
879 "/path/to/file"))
881 ;; Multihop.
882 (should
883 (string-equal
884 (file-remote-p "/user1@host1|user2@host2:/path/to/file")
885 (format "/%s@%s|%s@%s:" "user1" "host1" "user2" "host2")))
886 (should
887 (string-equal
888 (file-remote-p
889 "/user1@host1|user2@host2:/path/to/file" 'method)
890 "default-method"))
891 (should
892 (string-equal
893 (file-remote-p
894 "/user1@host1|user2@host2:/path/to/file" 'user)
895 "user2"))
896 (should
897 (string-equal
898 (file-remote-p
899 "/user1@host1|user2@host2:/path/to/file" 'host)
900 "host2"))
901 (should
902 (string-equal
903 (file-remote-p
904 "/user1@host1|user2@host2:/path/to/file" 'localname)
905 "/path/to/file"))
906 (should
907 (string-equal
908 (file-remote-p
909 "/user1@host1|user2@host2:/path/to/file" 'hop)
910 (format "%s@%s|" "user1" "host1")))
912 (should
913 (string-equal
914 (file-remote-p
915 (concat
916 "/user1@host1"
917 "|user2@host2"
918 "|user3@host3:/path/to/file"))
919 (format "/%s@%s|%s@%s|%s@%s:"
920 "user1" "host1"
921 "user2" "host2"
922 "user3" "host3")))
923 (should
924 (string-equal
925 (file-remote-p
926 (concat
927 "/user1@host1"
928 "|user2@host2"
929 "|user3@host3:/path/to/file")
930 'method)
931 "default-method"))
932 (should
933 (string-equal
934 (file-remote-p
935 (concat
936 "/user1@host1"
937 "|user2@host2"
938 "|user3@host3:/path/to/file")
939 'user)
940 "user3"))
941 (should
942 (string-equal
943 (file-remote-p
944 (concat
945 "/user1@host1"
946 "|user2@host2"
947 "|user3@host3:/path/to/file")
948 'host)
949 "host3"))
950 (should
951 (string-equal
952 (file-remote-p
953 (concat
954 "/user1@host1"
955 "|user2@host2"
956 "|user3@host3:/path/to/file")
957 'localname)
958 "/path/to/file"))
959 (should
960 (string-equal
961 (file-remote-p
962 (concat
963 "/user1@host1"
964 "|user2@host2"
965 "|user3@host3:/path/to/file")
966 'hop)
967 (format "%s@%s|%s@%s|"
968 "user1" "host1" "user2" "host2"))))
970 ;; Exit.
971 (tramp-change-syntax syntax))))
973 (ert-deftest tramp-test02-file-name-dissect-separate ()
974 "Check separate file name components."
975 :tags '(:expensive-test)
976 (let ((tramp-default-method "default-method")
977 (tramp-default-user "default-user")
978 (tramp-default-host "default-host")
979 (syntax tramp-syntax))
980 (unwind-protect
981 (progn
982 (tramp-change-syntax 'separate)
983 ;; Expand `tramp-default-user' and `tramp-default-host'.
984 (should (string-equal
985 (file-remote-p "/[method/]")
986 (format
987 "/[%s/%s@%s]" "method" "default-user" "default-host")))
988 (should (string-equal (file-remote-p "/[method/]" 'method) "method"))
989 (should (string-equal
990 (file-remote-p "/[method/]" 'user) "default-user"))
991 (should (string-equal
992 (file-remote-p "/[method/]" 'host) "default-host"))
993 (should (string-equal (file-remote-p "/[method/]" 'localname) ""))
994 (should (string-equal (file-remote-p "/[method/]" 'hop) nil))
996 ;; Expand `tramp-default-method' and `tramp-default-user'.
997 (should (string-equal
998 (file-remote-p "/[/host]")
999 (format
1000 "/[%s/%s@%s]" "default-method" "default-user" "host")))
1001 (should (string-equal
1002 (file-remote-p "/[/host]" 'method) "default-method"))
1003 (should (string-equal
1004 (file-remote-p "/[/host]" 'user) "default-user"))
1005 (should (string-equal (file-remote-p "/[/host]" 'host) "host"))
1006 (should (string-equal (file-remote-p "/[/host]" 'localname) ""))
1007 (should (string-equal (file-remote-p "/[/host]" 'hop) nil))
1009 ;; Expand `tramp-default-method' and `tramp-default-host'.
1010 (should (string-equal
1011 (file-remote-p "/[/user@]")
1012 (format
1013 "/[%s/%s@%s]" "default-method" "user" "default-host")))
1014 (should (string-equal
1015 (file-remote-p "/[/user@]" 'method) "default-method"))
1016 (should (string-equal (file-remote-p "/[/user@]" 'user) "user"))
1017 (should (string-equal
1018 (file-remote-p "/[/user@]" 'host) "default-host"))
1019 (should (string-equal (file-remote-p "/[/user@]" 'localname) ""))
1020 (should (string-equal (file-remote-p "/[/user@]" 'hop) nil))
1022 ;; Expand `tramp-default-method'.
1023 (should (string-equal
1024 (file-remote-p "/[/user@host]")
1025 (format "/[%s/%s@%s]" "default-method" "user" "host")))
1026 (should (string-equal
1027 (file-remote-p "/[/user@host]" 'method) "default-method"))
1028 (should (string-equal (file-remote-p "/[/user@host]" 'user) "user"))
1029 (should (string-equal (file-remote-p "/[/user@host]" 'host) "host"))
1030 (should (string-equal (file-remote-p "/[/user@host]" 'localname) ""))
1031 (should (string-equal (file-remote-p "/[/user@host]" 'hop) nil))
1033 ;; Expand `tramp-default-method' and `tramp-default-user'.
1034 (should (string-equal
1035 (file-remote-p "/[-/host]")
1036 (format
1037 "/[%s/%s@%s]" "default-method" "default-user" "host")))
1038 (should (string-equal
1039 (file-remote-p "/[-/host]" 'method) "default-method"))
1040 (should (string-equal
1041 (file-remote-p "/[-/host]" 'user) "default-user"))
1042 (should (string-equal (file-remote-p "/[-/host]" 'host) "host"))
1043 (should (string-equal (file-remote-p "/[-/host]" 'localname) ""))
1044 (should (string-equal (file-remote-p "/[-/host]" 'hop) nil))
1046 ;; Expand `tramp-default-method' and `tramp-default-host'.
1047 (should (string-equal
1048 (file-remote-p "/[-/user@]")
1049 (format
1050 "/[%s/%s@%s]" "default-method" "user" "default-host")))
1051 (should (string-equal
1052 (file-remote-p "/[-/user@]" 'method) "default-method"))
1053 (should (string-equal (file-remote-p "/[-/user@]" 'user) "user"))
1054 (should (string-equal
1055 (file-remote-p "/[-/user@]" 'host) "default-host"))
1056 (should (string-equal (file-remote-p "/[-/user@]" 'localname) ""))
1057 (should (string-equal (file-remote-p "/[-/user@]" 'hop) nil))
1059 ;; Expand `tramp-default-method'.
1060 (should (string-equal
1061 (file-remote-p "/[-/user@host]")
1062 (format "/[%s/%s@%s]" "default-method" "user" "host")))
1063 (should (string-equal
1064 (file-remote-p "/[-/user@host]" 'method) "default-method"))
1065 (should (string-equal (file-remote-p "/[-/user@host]" 'user) "user"))
1066 (should (string-equal (file-remote-p "/[-/user@host]" 'host) "host"))
1067 (should (string-equal (file-remote-p "/[-/user@host]" 'localname) ""))
1068 (should (string-equal (file-remote-p "/[-/user@host]" 'hop) nil))
1070 ;; Expand `tramp-default-user'.
1071 (should (string-equal
1072 (file-remote-p "/[method/host]")
1073 (format "/[%s/%s@%s]" "method" "default-user" "host")))
1074 (should (string-equal
1075 (file-remote-p "/[method/host]" 'method) "method"))
1076 (should (string-equal
1077 (file-remote-p "/[method/host]" 'user) "default-user"))
1078 (should (string-equal (file-remote-p "/[method/host]" 'host) "host"))
1079 (should (string-equal (file-remote-p "/[method/host]" 'localname) ""))
1080 (should (string-equal (file-remote-p "/[method/host]" 'hop) nil))
1082 ;; Expand `tramp-default-host'.
1083 (should (string-equal
1084 (file-remote-p "/[method/user@]")
1085 (format "/[%s/%s@%s]" "method" "user" "default-host")))
1086 (should (string-equal
1087 (file-remote-p "/[method/user@]" 'method) "method"))
1088 (should (string-equal (file-remote-p "/[method/user@]" 'user) "user"))
1089 (should (string-equal
1090 (file-remote-p "/[method/user@]" 'host) "default-host"))
1091 (should (string-equal
1092 (file-remote-p "/[method/user@]" 'localname) ""))
1093 (should (string-equal (file-remote-p "/[method/user@]" 'hop) nil))
1095 ;; No expansion.
1096 (should (string-equal
1097 (file-remote-p "/[method/user@host]")
1098 (format "/[%s/%s@%s]" "method" "user" "host")))
1099 (should (string-equal
1100 (file-remote-p "/[method/user@host]" 'method) "method"))
1101 (should (string-equal
1102 (file-remote-p "/[method/user@host]" 'user) "user"))
1103 (should (string-equal
1104 (file-remote-p "/[method/user@host]" 'host) "host"))
1105 (should (string-equal
1106 (file-remote-p "/[method/user@host]" 'localname) ""))
1107 (should (string-equal
1108 (file-remote-p "/[method/user@host]" 'hop) nil))
1110 ;; No expansion.
1111 (should (string-equal
1112 (file-remote-p "/[method/user@email@host]")
1113 (format "/[%s/%s@%s]" "method" "user@email" "host")))
1114 (should (string-equal
1115 (file-remote-p
1116 "/[method/user@email@host]" 'method) "method"))
1117 (should (string-equal
1118 (file-remote-p
1119 "/[method/user@email@host]" 'user) "user@email"))
1120 (should (string-equal
1121 (file-remote-p "/[method/user@email@host]" 'host) "host"))
1122 (should (string-equal
1123 (file-remote-p "/[method/user@email@host]" 'localname) ""))
1124 (should (string-equal
1125 (file-remote-p "/[method/user@email@host]" 'hop) nil))
1127 ;; Expand `tramp-default-method' and `tramp-default-user'.
1128 (should (string-equal
1129 (file-remote-p "/[/host#1234]")
1130 (format
1131 "/[%s/%s@%s]" "default-method" "default-user" "host#1234")))
1132 (should (string-equal
1133 (file-remote-p "/[/host#1234]" 'method) "default-method"))
1134 (should (string-equal
1135 (file-remote-p "/[/host#1234]" 'user) "default-user"))
1136 (should (string-equal
1137 (file-remote-p "/[/host#1234]" 'host) "host#1234"))
1138 (should (string-equal (file-remote-p "/[/host#1234]" 'localname) ""))
1139 (should (string-equal (file-remote-p "/[/host#1234]" 'hop) nil))
1141 ;; Expand `tramp-default-method'.
1142 (should (string-equal
1143 (file-remote-p "/[/user@host#1234]")
1144 (format "/[%s/%s@%s]" "default-method" "user" "host#1234")))
1145 (should (string-equal
1146 (file-remote-p
1147 "/[/user@host#1234]" 'method) "default-method"))
1148 (should (string-equal
1149 (file-remote-p
1150 "/[/user@host#1234]" 'user) "user"))
1151 (should (string-equal
1152 (file-remote-p "/[/user@host#1234]" 'host) "host#1234"))
1153 (should (string-equal
1154 (file-remote-p "/[/user@host#1234]" 'localname) ""))
1155 (should (string-equal (file-remote-p "/[/user@host#1234]" 'hop) nil))
1157 ;; Expand `tramp-default-method' and `tramp-default-user'.
1158 (should (string-equal
1159 (file-remote-p "/[-/host#1234]")
1160 (format
1161 "/[%s/%s@%s]" "default-method" "default-user" "host#1234")))
1162 (should (string-equal
1163 (file-remote-p "/[-/host#1234]" 'method) "default-method"))
1164 (should (string-equal
1165 (file-remote-p "/[-/host#1234]" 'user) "default-user"))
1166 (should (string-equal
1167 (file-remote-p "/[-/host#1234]" 'host) "host#1234"))
1168 (should (string-equal (file-remote-p "/[-/host#1234]" 'localname) ""))
1169 (should (string-equal (file-remote-p "/[-/host#1234]" 'hop) nil))
1171 ;; Expand `tramp-default-method'.
1172 (should (string-equal
1173 (file-remote-p "/[-/user@host#1234]")
1174 (format "/[%s/%s@%s]" "default-method" "user" "host#1234")))
1175 (should (string-equal
1176 (file-remote-p
1177 "/[-/user@host#1234]" 'method) "default-method"))
1178 (should (string-equal
1179 (file-remote-p
1180 "/[-/user@host#1234]" 'user) "user"))
1181 (should (string-equal
1182 (file-remote-p "/[-/user@host#1234]" 'host) "host#1234"))
1183 (should (string-equal
1184 (file-remote-p "/[-/user@host#1234]" 'localname) ""))
1185 (should (string-equal (file-remote-p "/[-/user@host#1234]" 'hop) nil))
1187 ;; Expand `tramp-default-user'.
1188 (should (string-equal
1189 (file-remote-p "/[method/host#1234]")
1190 (format "/[%s/%s@%s]" "method" "default-user" "host#1234")))
1191 (should (string-equal
1192 (file-remote-p "/[method/host#1234]" 'method) "method"))
1193 (should (string-equal
1194 (file-remote-p "/[method/host#1234]" 'user) "default-user"))
1195 (should (string-equal
1196 (file-remote-p "/[method/host#1234]" 'host) "host#1234"))
1197 (should (string-equal
1198 (file-remote-p "/[method/host#1234]" 'localname) ""))
1199 (should (string-equal (file-remote-p "/[method/host#1234]" 'hop) nil))
1201 ;; No expansion.
1202 (should (string-equal
1203 (file-remote-p "/[method/user@host#1234]")
1204 (format "/[%s/%s@%s]" "method" "user" "host#1234")))
1205 (should (string-equal
1206 (file-remote-p "/[method/user@host#1234]" 'method) "method"))
1207 (should (string-equal
1208 (file-remote-p "/[method/user@host#1234]" 'user) "user"))
1209 (should (string-equal
1210 (file-remote-p
1211 "/[method/user@host#1234]" 'host) "host#1234"))
1212 (should (string-equal
1213 (file-remote-p "/[method/user@host#1234]" 'localname) ""))
1214 (should (string-equal
1215 (file-remote-p "/[method/user@host#1234]" 'hop) nil))
1217 ;; Expand `tramp-default-method' and `tramp-default-user'.
1218 (should (string-equal
1219 (file-remote-p "/[/1.2.3.4]")
1220 (format
1221 "/[%s/%s@%s]" "default-method" "default-user" "1.2.3.4")))
1222 (should (string-equal
1223 (file-remote-p "/[/1.2.3.4]" 'method) "default-method"))
1224 (should (string-equal
1225 (file-remote-p "/[/1.2.3.4]" 'user) "default-user"))
1226 (should (string-equal
1227 (file-remote-p "/[/1.2.3.4]" 'host) "1.2.3.4"))
1228 (should (string-equal (file-remote-p "/[/1.2.3.4]" 'localname) ""))
1229 (should (string-equal (file-remote-p "/[/1.2.3.4]" 'hop) nil))
1231 ;; Expand `tramp-default-method'.
1232 (should (string-equal
1233 (file-remote-p "/[/user@1.2.3.4]")
1234 (format "/[%s/%s@%s]" "default-method" "user" "1.2.3.4")))
1235 (should (string-equal
1236 (file-remote-p
1237 "/[/user@1.2.3.4]" 'method) "default-method"))
1238 (should (string-equal
1239 (file-remote-p "/[/user@1.2.3.4]" 'user) "user"))
1240 (should (string-equal
1241 (file-remote-p "/[/user@1.2.3.4]" 'host) "1.2.3.4"))
1242 (should (string-equal
1243 (file-remote-p "/[/user@1.2.3.4]" 'localname) ""))
1244 (should (string-equal (file-remote-p "/[/user@1.2.3.4]" 'hop) nil))
1246 ;; Expand `tramp-default-method' and `tramp-default-user'.
1247 (should (string-equal
1248 (file-remote-p "/[-/1.2.3.4]")
1249 (format
1250 "/[%s/%s@%s]" "default-method" "default-user" "1.2.3.4")))
1251 (should (string-equal
1252 (file-remote-p "/[-/1.2.3.4]" 'method) "default-method"))
1253 (should (string-equal
1254 (file-remote-p "/[-/1.2.3.4]" 'user) "default-user"))
1255 (should (string-equal
1256 (file-remote-p "/[-/1.2.3.4]" 'host) "1.2.3.4"))
1257 (should (string-equal (file-remote-p "/[-/1.2.3.4]" 'localname) ""))
1258 (should (string-equal (file-remote-p "/[-/1.2.3.4]" 'hop) nil))
1260 ;; Expand `tramp-default-method'.
1261 (should (string-equal
1262 (file-remote-p "/[-/user@1.2.3.4]")
1263 (format "/[%s/%s@%s]" "default-method" "user" "1.2.3.4")))
1264 (should (string-equal
1265 (file-remote-p
1266 "/[-/user@1.2.3.4]" 'method) "default-method"))
1267 (should (string-equal
1268 (file-remote-p "/[-/user@1.2.3.4]" 'user) "user"))
1269 (should (string-equal
1270 (file-remote-p "/[-/user@1.2.3.4]" 'host) "1.2.3.4"))
1271 (should (string-equal
1272 (file-remote-p "/[-/user@1.2.3.4]" 'localname) ""))
1273 (should (string-equal (file-remote-p "/[-/user@1.2.3.4]" 'hop) nil))
1275 ;; Expand `tramp-default-user'.
1276 (should (string-equal
1277 (file-remote-p "/[method/1.2.3.4]")
1278 (format "/[%s/%s@%s]" "method" "default-user" "1.2.3.4")))
1279 (should (string-equal
1280 (file-remote-p "/[method/1.2.3.4]" 'method) "method"))
1281 (should (string-equal
1282 (file-remote-p "/[method/1.2.3.4]" 'user) "default-user"))
1283 (should (string-equal
1284 (file-remote-p "/[method/1.2.3.4]" 'host) "1.2.3.4"))
1285 (should (string-equal
1286 (file-remote-p "/[method/1.2.3.4]" 'localname) ""))
1287 (should (string-equal (file-remote-p "/[method/1.2.3.4]" 'hop) nil))
1289 ;; No expansion.
1290 (should (string-equal
1291 (file-remote-p "/[method/user@1.2.3.4]")
1292 (format "/[%s/%s@%s]" "method" "user" "1.2.3.4")))
1293 (should (string-equal
1294 (file-remote-p "/[method/user@1.2.3.4]" 'method) "method"))
1295 (should (string-equal
1296 (file-remote-p "/[method/user@1.2.3.4]" 'user) "user"))
1297 (should (string-equal
1298 (file-remote-p "/[method/user@1.2.3.4]" 'host) "1.2.3.4"))
1299 (should (string-equal
1300 (file-remote-p "/[method/user@1.2.3.4]" 'localname) ""))
1301 (should (string-equal
1302 (file-remote-p "/[method/user@1.2.3.4]" 'hop) nil))
1304 ;; Expand `tramp-default-method', `tramp-default-user' and
1305 ;; `tramp-default-host'.
1306 (should (string-equal
1307 (file-remote-p "/[/]")
1308 (format
1309 "/[%s/%s@%s]"
1310 "default-method" "default-user" "default-host")))
1311 (should (string-equal
1312 (file-remote-p "/[/]" 'method) "default-method"))
1313 (should (string-equal (file-remote-p "/[/]" 'user) "default-user"))
1314 (should (string-equal (file-remote-p "/[/]" 'host) "default-host"))
1315 (should (string-equal (file-remote-p "/[/]" 'localname) ""))
1316 (should (string-equal (file-remote-p "/[/]" 'hop) nil))
1318 ;; Expand `tramp-default-method' and `tramp-default-user'.
1319 (let ((tramp-default-host "::1"))
1320 (should (string-equal
1321 (file-remote-p "/[/]")
1322 (format
1323 "/[%s/%s@%s]"
1324 "default-method" "default-user" "::1")))
1325 (should (string-equal
1326 (file-remote-p "/[/]" 'method) "default-method"))
1327 (should (string-equal (file-remote-p "/[/]" 'user) "default-user"))
1328 (should (string-equal (file-remote-p "/[/]" 'host) "::1"))
1329 (should (string-equal (file-remote-p "/[/]" 'localname) ""))
1330 (should (string-equal (file-remote-p "/[/]" 'hop) nil)))
1332 ;; Expand `tramp-default-method' and `tramp-default-user'.
1333 (should (string-equal
1334 (file-remote-p "/[/::1]")
1335 (format
1336 "/[%s/%s@%s]" "default-method" "default-user" "::1")))
1337 (should (string-equal
1338 (file-remote-p "/[/::1]" 'method) "default-method"))
1339 (should (string-equal
1340 (file-remote-p "/[/::1]" 'user) "default-user"))
1341 (should (string-equal (file-remote-p "/[/::1]" 'host) "::1"))
1342 (should (string-equal (file-remote-p "/[/::1]" 'localname) ""))
1343 (should (string-equal (file-remote-p "/[/::1]" 'hop) nil))
1345 ;; Expand `tramp-default-method'.
1346 (should (string-equal
1347 (file-remote-p "/[/user@::1]")
1348 (format "/[%s/%s@%s]" "default-method" "user" "::1")))
1349 (should (string-equal
1350 (file-remote-p "/[/user@::1]" 'method) "default-method"))
1351 (should (string-equal (file-remote-p "/[/user@::1]" 'user) "user"))
1352 (should (string-equal (file-remote-p "/[/user@::1]" 'host) "::1"))
1353 (should (string-equal (file-remote-p "/[/user@::1]" 'localname) ""))
1354 (should (string-equal (file-remote-p "/[/user@::1]" 'hop) nil))
1356 ;; Expand `tramp-default-method', `tramp-default-user' and
1357 ;; `tramp-default-host'.
1358 (should (string-equal
1359 (file-remote-p "/[-/]")
1360 (format
1361 "/[%s/%s@%s]"
1362 "default-method" "default-user" "default-host")))
1363 (should (string-equal
1364 (file-remote-p "/[-/]" 'method) "default-method"))
1365 (should (string-equal (file-remote-p "/[-/]" 'user) "default-user"))
1366 (should (string-equal (file-remote-p "/[-/]" 'host) "default-host"))
1367 (should (string-equal (file-remote-p "/[-/]" 'localname) ""))
1368 (should (string-equal (file-remote-p "/[-/]" 'hop) nil))
1370 ;; Expand `tramp-default-method' and `tramp-default-user'.
1371 (let ((tramp-default-host "::1"))
1372 (should (string-equal
1373 (file-remote-p "/[-/]")
1374 (format
1375 "/[%s/%s@%s]"
1376 "default-method" "default-user" "::1")))
1377 (should (string-equal
1378 (file-remote-p "/[-/]" 'method) "default-method"))
1379 (should (string-equal (file-remote-p "/[-/]" 'user) "default-user"))
1380 (should (string-equal (file-remote-p "/[-/]" 'host) "::1"))
1381 (should (string-equal (file-remote-p "/[-/]" 'localname) ""))
1382 (should (string-equal (file-remote-p "/[-/]" 'hop) nil)))
1384 ;; Expand `tramp-default-method' and `tramp-default-user'.
1385 (should (string-equal
1386 (file-remote-p "/[-/::1]")
1387 (format
1388 "/[%s/%s@%s]" "default-method" "default-user" "::1")))
1389 (should (string-equal
1390 (file-remote-p "/[-/::1]" 'method) "default-method"))
1391 (should (string-equal
1392 (file-remote-p "/[-/::1]" 'user) "default-user"))
1393 (should (string-equal (file-remote-p "/[-/::1]" 'host) "::1"))
1394 (should (string-equal (file-remote-p "/[-/::1]" 'localname) ""))
1395 (should (string-equal (file-remote-p "/[-/::1]" 'hop) nil))
1397 ;; Expand `tramp-default-method'.
1398 (should (string-equal
1399 (file-remote-p "/[-/user@::1]")
1400 (format "/[%s/%s@%s]" "default-method" "user" "::1")))
1401 (should (string-equal
1402 (file-remote-p "/[-/user@::1]" 'method) "default-method"))
1403 (should (string-equal (file-remote-p "/[-/user@::1]" 'user) "user"))
1404 (should (string-equal (file-remote-p "/[-/user@::1]" 'host) "::1"))
1405 (should (string-equal (file-remote-p "/[-/user@::1]" 'localname) ""))
1406 (should (string-equal (file-remote-p "/[-/user@::1]" 'hop) nil))
1408 ;; Expand `tramp-default-user'.
1409 (should (string-equal
1410 (file-remote-p "/[method/::1]")
1411 (format "/[%s/%s@%s]" "method" "default-user" "::1")))
1412 (should (string-equal
1413 (file-remote-p "/[method/::1]" 'method) "method"))
1414 (should (string-equal
1415 (file-remote-p "/[method/::1]" 'user) "default-user"))
1416 (should (string-equal (file-remote-p "/[method/::1]" 'host) "::1"))
1417 (should (string-equal (file-remote-p "/[method/::1]" 'localname) ""))
1418 (should (string-equal (file-remote-p "/[method/::1]" 'hop) nil))
1420 ;; No expansion.
1421 (should (string-equal
1422 (file-remote-p "/[method/user@::1]")
1423 (format "/[%s/%s@%s]" "method" "user" "::1")))
1424 (should (string-equal
1425 (file-remote-p "/[method/user@::1]" 'method) "method"))
1426 (should (string-equal
1427 (file-remote-p "/[method/user@::1]" 'user) "user"))
1428 (should (string-equal
1429 (file-remote-p "/[method/user@::1]" 'host) "::1"))
1430 (should (string-equal
1431 (file-remote-p "/[method/user@::1]" 'localname) ""))
1432 (should (string-equal (file-remote-p "/[method/user@::1]" 'hop) nil))
1434 ;; Local file name part.
1435 (should (string-equal (file-remote-p "/[/host]/:" 'localname) "/:"))
1436 (should (string-equal (file-remote-p "/[-/host]/:" 'localname) "/:"))
1437 (should (string-equal (file-remote-p "/[method/]:" 'localname) ":"))
1438 (should (string-equal (file-remote-p "/[method/] " 'localname) " "))
1439 (should (string-equal
1440 (file-remote-p "/[method/]file" 'localname) "file"))
1441 (should (string-equal
1442 (file-remote-p "/[method/]/path/to/file" 'localname)
1443 "/path/to/file"))
1445 ;; Multihop.
1446 (should
1447 (string-equal
1448 (file-remote-p
1449 "/[method1/user1@host1|method2/user2@host2]/path/to/file")
1450 (format "/[%s/%s@%s|%s/%s@%s]"
1451 "method1" "user1" "host1" "method2" "user2" "host2")))
1452 (should
1453 (string-equal
1454 (file-remote-p
1455 "/[method1/user1@host1|method2/user2@host2]/path/to/file" 'method)
1456 "method2"))
1457 (should
1458 (string-equal
1459 (file-remote-p
1460 "/[method1/user1@host1|method2/user2@host2]/path/to/file" 'user)
1461 "user2"))
1462 (should
1463 (string-equal
1464 (file-remote-p
1465 "/[method1/user1@host1|method2/user2@host2]/path/to/file" 'host)
1466 "host2"))
1467 (should
1468 (string-equal
1469 (file-remote-p
1470 "/[method1/user1@host1|method2/user2@host2]/path/to/file"
1471 'localname)
1472 "/path/to/file"))
1473 (should
1474 (string-equal
1475 (file-remote-p
1476 "/[method1/user1@host1|method2/user2@host2]/path/to/file" 'hop)
1477 (format "%s/%s@%s|"
1478 "method1" "user1" "host1")))
1480 (should
1481 (string-equal
1482 (file-remote-p
1483 (concat
1484 "/[method1/user1@host1"
1485 "|method2/user2@host2"
1486 "|method3/user3@host3]/path/to/file"))
1487 (format "/[%s/%s@%s|%s/%s@%s|%s/%s@%s]"
1488 "method1" "user1" "host1"
1489 "method2" "user2" "host2"
1490 "method3" "user3" "host3")))
1491 (should
1492 (string-equal
1493 (file-remote-p
1494 (concat
1495 "/[method1/user1@host1"
1496 "|method2/user2@host2"
1497 "|method3/user3@host3]/path/to/file")
1498 'method)
1499 "method3"))
1500 (should
1501 (string-equal
1502 (file-remote-p
1503 (concat
1504 "/[method1/user1@host1"
1505 "|method2/user2@host2"
1506 "|method3/user3@host3]/path/to/file")
1507 'user)
1508 "user3"))
1509 (should
1510 (string-equal
1511 (file-remote-p
1512 (concat
1513 "/[method1/user1@host1"
1514 "|method2/user2@host2"
1515 "|method3/user3@host3]/path/to/file")
1516 'host)
1517 "host3"))
1518 (should
1519 (string-equal
1520 (file-remote-p
1521 (concat
1522 "/[method1/user1@host1"
1523 "|method2/user2@host2"
1524 "|method3/user3@host3]/path/to/file")
1525 'localname)
1526 "/path/to/file"))
1527 (should
1528 (string-equal
1529 (file-remote-p
1530 (concat
1531 "/[method1/user1@host1"
1532 "|method2/user2@host2"
1533 "|method3/user3@host3]/path/to/file")
1534 'hop)
1535 (format "%s/%s@%s|%s/%s@%s|"
1536 "method1" "user1" "host1" "method2" "user2" "host2"))))
1538 ;; Exit.
1539 (tramp-change-syntax syntax))))
1541 (ert-deftest tramp-test03-file-name-defaults ()
1542 "Check default values for some methods."
1543 ;; Default values in tramp-adb.el.
1544 (should (string-equal (file-remote-p "/adb::" 'host) ""))
1545 ;; Default values in tramp-ftp.el.
1546 (should (string-equal (file-remote-p "/-:ftp.host:" 'method) "ftp"))
1547 (dolist (u '("ftp" "anonymous"))
1548 (should (string-equal (file-remote-p (format "/-:%s@:" u) 'method) "ftp")))
1549 ;; Default values in tramp-gvfs.el.
1550 (when (and (load "tramp-gvfs" 'noerror 'nomessage)
1551 (symbol-value 'tramp-gvfs-enabled))
1552 (should (string-equal (file-remote-p "/synce::" 'user) nil)))
1553 ;; Default values in tramp-sh.el.
1554 (dolist (h `("127.0.0.1" "[::1]" "localhost" "localhost6" ,(system-name)))
1555 (should
1556 (string-equal (file-remote-p (format "/-:root@%s:" h) 'method) "su")))
1557 (dolist (m '("su" "sudo" "ksu"))
1558 (should (string-equal (file-remote-p (format "/%s::" m) 'user) "root")))
1559 (dolist (m '("rcp" "remcp" "rsh" "telnet" "krlogin" "fcp"))
1560 (should
1561 (string-equal (file-remote-p (format "/%s::" m) 'user) (user-login-name))))
1562 ;; Default values in tramp-smb.el.
1563 (should (string-equal (file-remote-p "/smb::" 'user) nil)))
1565 (ert-deftest tramp-test04-substitute-in-file-name ()
1566 "Check `substitute-in-file-name'."
1567 (should (string-equal (substitute-in-file-name "/method:host://foo") "/foo"))
1568 (should
1569 (string-equal
1570 (substitute-in-file-name "/method:host:/path//foo") "/method:host:/foo"))
1571 (should
1572 (string-equal (substitute-in-file-name "/method:host:/path///foo") "/foo"))
1573 ;; Quoting local part.
1574 (should
1575 (string-equal
1576 (substitute-in-file-name "/method:host:/://foo") "/method:host:/://foo"))
1577 (should
1578 (string-equal
1579 (substitute-in-file-name "/method:host:/:/path//foo")
1580 "/method:host:/:/path//foo"))
1581 (should
1582 (string-equal
1583 (substitute-in-file-name "/method:host:/:/path///foo")
1584 "/method:host:/:/path///foo"))
1586 (should
1587 (string-equal
1588 (substitute-in-file-name "/method:host:/path/~/foo") "/method:host:~/foo"))
1589 (should
1590 (string-equal (substitute-in-file-name "/method:host:/path//~/foo") "~/foo"))
1591 ;; Quoting local part.
1592 (should
1593 (string-equal
1594 (substitute-in-file-name "/method:host:/:/path/~/foo")
1595 "/method:host:/:/path/~/foo"))
1596 (should
1597 (string-equal
1598 (substitute-in-file-name "/method:host:/:/path//~/foo")
1599 "/method:host:/:/path//~/foo"))
1601 (let (process-environment)
1602 (should
1603 (string-equal
1604 (substitute-in-file-name "/method:host:/path/$FOO")
1605 "/method:host:/path/$FOO"))
1606 (setenv "FOO" "bla")
1607 (should
1608 (string-equal
1609 (substitute-in-file-name "/method:host:/path/$FOO")
1610 "/method:host:/path/bla"))
1611 (should
1612 (string-equal
1613 (substitute-in-file-name "/method:host:/path/$$FOO")
1614 "/method:host:/path/$FOO"))
1615 ;; Quoting local part.
1616 (should
1617 (string-equal
1618 (substitute-in-file-name "/method:host:/:/path/$FOO")
1619 "/method:host:/:/path/$FOO"))
1620 (setenv "FOO" "bla")
1621 (should
1622 (string-equal
1623 (substitute-in-file-name "/method:host:/:/path/$FOO")
1624 "/method:host:/:/path/$FOO"))
1625 (should
1626 (string-equal
1627 (substitute-in-file-name "/method:host:/:/path/$$FOO")
1628 "/method:host:/:/path/$$FOO"))))
1630 (ert-deftest tramp-test05-expand-file-name ()
1631 "Check `expand-file-name'."
1632 (should
1633 (string-equal
1634 (expand-file-name "/method:host:/path/./file") "/method:host:/path/file"))
1635 (should
1636 (string-equal
1637 (expand-file-name "/method:host:/path/../file") "/method:host:/file"))
1638 ;; Quoting local part.
1639 (should
1640 (string-equal
1641 (expand-file-name "/method:host:/:/path/./file")
1642 "/method:host:/:/path/file"))
1643 (should
1644 (string-equal
1645 (expand-file-name "/method:host:/:/path/../file") "/method:host:/:/file"))
1646 (should
1647 (string-equal
1648 (expand-file-name "/method:host:/:/~/path/./file")
1649 "/method:host:/:/~/path/file")))
1651 ;; The following test is inspired by Bug#26911. It is rather a bug in
1652 ;; `expand-file-name', and it fails for all Emacs versions. Test
1653 ;; added for later, when it is fixed.
1654 (ert-deftest tramp-test05-expand-file-name-relative ()
1655 "Check `expand-file-name'."
1656 ;; Mark as failed until bug has been fixed.
1657 :expected-result :failed
1658 (skip-unless (tramp--test-enabled))
1659 ;; These are the methods the test doesn't fail.
1660 (when (or (tramp--test-adb-p) (tramp--test-gvfs-p)
1661 (tramp-smb-file-name-p tramp-test-temporary-file-directory))
1662 (setf (ert-test-expected-result-type
1663 (ert-get-test 'tramp-test05-expand-file-name-relative))
1664 :passed))
1666 (should
1667 (string-equal
1668 (let ((default-directory
1669 (concat
1670 (file-remote-p tramp-test-temporary-file-directory) "/path")))
1671 (expand-file-name ".." "./"))
1672 (concat (file-remote-p tramp-test-temporary-file-directory) "/"))))
1674 (ert-deftest tramp-test06-directory-file-name ()
1675 "Check `directory-file-name'.
1676 This checks also `file-name-as-directory', `file-name-directory',
1677 `file-name-nondirectory' and `unhandled-file-name-directory'."
1678 (should
1679 (string-equal
1680 (directory-file-name "/method:host:/path/to/file")
1681 "/method:host:/path/to/file"))
1682 (should
1683 (string-equal
1684 (directory-file-name "/method:host:/path/to/file/")
1685 "/method:host:/path/to/file"))
1686 (should
1687 (string-equal
1688 (file-name-as-directory "/method:host:/path/to/file")
1689 "/method:host:/path/to/file/"))
1690 (should
1691 (string-equal
1692 (file-name-as-directory "/method:host:/path/to/file/")
1693 "/method:host:/path/to/file/"))
1694 (should
1695 (string-equal
1696 (file-name-directory "/method:host:/path/to/file")
1697 "/method:host:/path/to/"))
1698 (should
1699 (string-equal
1700 (file-name-directory "/method:host:/path/to/file/")
1701 "/method:host:/path/to/file/"))
1702 (should
1703 (string-equal (file-name-nondirectory "/method:host:/path/to/file") "file"))
1704 (should
1705 (string-equal (file-name-nondirectory "/method:host:/path/to/file/") ""))
1706 (should-not
1707 (unhandled-file-name-directory "/method:host:/path/to/file"))
1709 ;; Bug#10085.
1710 (when (tramp--test-enabled) ;; Packages like tramp-gvfs.el might be disabled.
1711 (dolist (n-e '(nil t))
1712 ;; We must clear `tramp-default-method'. On hydra, it is "ftp",
1713 ;; which ruins the tests.
1714 (let ((non-essential n-e)
1715 tramp-default-method)
1716 (dolist
1717 (file
1718 `(,(format
1719 "/%s::"
1720 (file-remote-p tramp-test-temporary-file-directory 'method))
1721 ,(format
1722 "/-:%s:"
1723 (file-remote-p tramp-test-temporary-file-directory 'host))))
1724 (should (string-equal (directory-file-name file) file))
1725 (should
1726 (string-equal
1727 (file-name-as-directory file)
1728 (if (tramp-completion-mode-p)
1729 file (concat file "./"))))
1730 (should (string-equal (file-name-directory file) file))
1731 (should (string-equal (file-name-nondirectory file) "")))))))
1733 (ert-deftest tramp-test07-file-exists-p ()
1734 "Check `file-exist-p', `write-region' and `delete-file'."
1735 (skip-unless (tramp--test-enabled))
1737 (dolist (quoted (if tramp--test-expensive-test '(nil t) '(nil)))
1738 (let ((tmp-name (tramp--test-make-temp-name nil quoted)))
1739 (should-not (file-exists-p tmp-name))
1740 (write-region "foo" nil tmp-name)
1741 (should (file-exists-p tmp-name))
1742 (delete-file tmp-name)
1743 (should-not (file-exists-p tmp-name)))))
1745 (ert-deftest tramp-test08-file-local-copy ()
1746 "Check `file-local-copy'."
1747 (skip-unless (tramp--test-enabled))
1749 (dolist (quoted (if tramp--test-expensive-test '(nil t) '(nil)))
1750 (let ((tmp-name1 (tramp--test-make-temp-name nil quoted))
1751 tmp-name2)
1752 (unwind-protect
1753 (progn
1754 (write-region "foo" nil tmp-name1)
1755 (should (setq tmp-name2 (file-local-copy tmp-name1)))
1756 (with-temp-buffer
1757 (insert-file-contents tmp-name2)
1758 (should (string-equal (buffer-string) "foo")))
1759 ;; Check also that a file transfer with compression works.
1760 (let ((default-directory tramp-test-temporary-file-directory)
1761 (tramp-copy-size-limit 4)
1762 (tramp-inline-compress-start-size 2))
1763 (delete-file tmp-name2)
1764 (should (setq tmp-name2 (file-local-copy tmp-name1))))
1765 ;; Error case.
1766 (delete-file tmp-name1)
1767 (delete-file tmp-name2)
1768 (should-error
1769 (setq tmp-name2 (file-local-copy tmp-name1))
1770 :type tramp-file-missing))
1772 ;; Cleanup.
1773 (ignore-errors
1774 (delete-file tmp-name1)
1775 (delete-file tmp-name2))))))
1777 (ert-deftest tramp-test09-insert-file-contents ()
1778 "Check `insert-file-contents'."
1779 (skip-unless (tramp--test-enabled))
1781 (dolist (quoted (if tramp--test-expensive-test '(nil t) '(nil)))
1782 (let ((tmp-name (tramp--test-make-temp-name nil quoted)))
1783 (unwind-protect
1784 (with-temp-buffer
1785 (write-region "foo" nil tmp-name)
1786 (insert-file-contents tmp-name)
1787 (should (string-equal (buffer-string) "foo"))
1788 (insert-file-contents tmp-name)
1789 (should (string-equal (buffer-string) "foofoo"))
1790 ;; Insert partly.
1791 (insert-file-contents tmp-name nil 1 3)
1792 (should (string-equal (buffer-string) "oofoofoo"))
1793 ;; Replace.
1794 (insert-file-contents tmp-name nil nil nil 'replace)
1795 (should (string-equal (buffer-string) "foo"))
1796 ;; Error case.
1797 (delete-file tmp-name)
1798 (should-error
1799 (insert-file-contents tmp-name)
1800 :type tramp-file-missing))
1802 ;; Cleanup.
1803 (ignore-errors (delete-file tmp-name))))))
1805 (ert-deftest tramp-test10-write-region ()
1806 "Check `write-region'."
1807 (skip-unless (tramp--test-enabled))
1809 (dolist (quoted (if tramp--test-expensive-test '(nil t) '(nil)))
1810 (let ((tmp-name (tramp--test-make-temp-name nil quoted)))
1811 (unwind-protect
1812 (progn
1813 ;; Write buffer. Use absolute and relative file name.
1814 (with-temp-buffer
1815 (insert "foo")
1816 (write-region nil nil tmp-name))
1817 (with-temp-buffer
1818 (insert-file-contents tmp-name)
1819 (should (string-equal (buffer-string) "foo")))
1820 (delete-file tmp-name)
1821 (with-temp-buffer
1822 (insert "foo")
1823 (should-not (file-exists-p tmp-name))
1824 (let ((default-directory (file-name-directory tmp-name)))
1825 (should-not (file-exists-p (file-name-nondirectory tmp-name)))
1826 (write-region nil nil (file-name-nondirectory tmp-name))
1827 (should (file-exists-p (file-name-nondirectory tmp-name))))
1828 (should (file-exists-p tmp-name)))
1829 (with-temp-buffer
1830 (insert-file-contents tmp-name)
1831 (should (string-equal (buffer-string) "foo")))
1833 ;; Append.
1834 (with-temp-buffer
1835 (insert "bla")
1836 (write-region nil nil tmp-name 'append))
1837 (with-temp-buffer
1838 (insert-file-contents tmp-name)
1839 (should (string-equal (buffer-string) "foobla")))
1840 (with-temp-buffer
1841 (insert "baz")
1842 (write-region nil nil tmp-name 3))
1843 (with-temp-buffer
1844 (insert-file-contents tmp-name)
1845 (should (string-equal (buffer-string) "foobaz")))
1847 ;; Write string.
1848 (write-region "foo" nil tmp-name)
1849 (with-temp-buffer
1850 (insert-file-contents tmp-name)
1851 (should (string-equal (buffer-string) "foo")))
1853 ;; Write partly.
1854 (with-temp-buffer
1855 (insert "123456789")
1856 (write-region 3 5 tmp-name))
1857 (with-temp-buffer
1858 (insert-file-contents tmp-name)
1859 (should (string-equal (buffer-string) "34")))
1861 ;; Do not overwrite if excluded.
1862 (cl-letf (((symbol-function 'y-or-n-p) (lambda (_prompt) t)))
1863 (write-region "foo" nil tmp-name nil nil nil 'mustbenew))
1864 ;; `mustbenew' is passed to Tramp since Emacs 26.1.
1865 (when (tramp--test-emacs26-p)
1866 (should-error
1867 (cl-letf (((symbol-function 'y-or-n-p) 'ignore))
1868 (write-region "foo" nil tmp-name nil nil nil 'mustbenew))
1869 :type 'file-already-exists)
1870 (should-error
1871 (write-region "foo" nil tmp-name nil nil nil 'excl)
1872 :type 'file-already-exists)))
1874 ;; Cleanup.
1875 (ignore-errors (delete-file tmp-name))))))
1877 (ert-deftest tramp-test11-copy-file ()
1878 "Check `copy-file'."
1879 (skip-unless (tramp--test-enabled))
1881 ;; TODO: The quoted case does not work.
1882 ;;(dolist (quoted (if tramp--test-expensive-test '(nil t) '(nil)))
1883 (let (quoted)
1884 (let ((tmp-name1 (tramp--test-make-temp-name nil quoted))
1885 (tmp-name2 (tramp--test-make-temp-name nil quoted))
1886 (tmp-name3 (tramp--test-make-temp-name nil quoted))
1887 (tmp-name4 (tramp--test-make-temp-name 'local quoted))
1888 (tmp-name5 (tramp--test-make-temp-name 'local quoted)))
1890 ;; Copy on remote side.
1891 (unwind-protect
1892 (progn
1893 (write-region "foo" nil tmp-name1)
1894 (copy-file tmp-name1 tmp-name2)
1895 (should (file-exists-p tmp-name2))
1896 (with-temp-buffer
1897 (insert-file-contents tmp-name2)
1898 (should (string-equal (buffer-string) "foo")))
1899 (should-error
1900 (copy-file tmp-name1 tmp-name2)
1901 :type 'file-already-exists)
1902 (copy-file tmp-name1 tmp-name2 'ok)
1903 (make-directory tmp-name3)
1904 ;; This has been changed in Emacs 26.1.
1905 (when (tramp--test-emacs26-p)
1906 (should-error
1907 (copy-file tmp-name1 tmp-name3)
1908 :type 'file-already-exists))
1909 (copy-file tmp-name1 (file-name-as-directory tmp-name3))
1910 (should
1911 (file-exists-p
1912 (expand-file-name (file-name-nondirectory tmp-name1) tmp-name3))))
1914 ;; Cleanup.
1915 (ignore-errors (delete-file tmp-name1))
1916 (ignore-errors (delete-file tmp-name2))
1917 (ignore-errors (delete-directory tmp-name3 'recursive)))
1919 ;; Copy from remote side to local side.
1920 (unwind-protect
1921 (progn
1922 (write-region "foo" nil tmp-name1)
1923 (copy-file tmp-name1 tmp-name4)
1924 (should (file-exists-p tmp-name4))
1925 (with-temp-buffer
1926 (insert-file-contents tmp-name4)
1927 (should (string-equal (buffer-string) "foo")))
1928 (should-error
1929 (copy-file tmp-name1 tmp-name4)
1930 :type 'file-already-exists)
1931 (copy-file tmp-name1 tmp-name4 'ok)
1932 (make-directory tmp-name5)
1933 ;; This has been changed in Emacs 26.1.
1934 (when (tramp--test-emacs26-p)
1935 (should-error
1936 (copy-file tmp-name1 tmp-name5)
1937 :type 'file-already-exists))
1938 (copy-file tmp-name1 (file-name-as-directory tmp-name5))
1939 (should
1940 (file-exists-p
1941 (expand-file-name (file-name-nondirectory tmp-name1) tmp-name5))))
1943 ;; Cleanup.
1944 (ignore-errors (delete-file tmp-name1))
1945 (ignore-errors (delete-file tmp-name4))
1946 (ignore-errors (delete-directory tmp-name5 'recursive)))
1948 ;; Copy from local side to remote side.
1949 (unwind-protect
1950 (progn
1951 (write-region "foo" nil tmp-name4 nil 'nomessage)
1952 (copy-file tmp-name4 tmp-name1)
1953 (should (file-exists-p tmp-name1))
1954 (with-temp-buffer
1955 (insert-file-contents tmp-name1)
1956 (should (string-equal (buffer-string) "foo")))
1957 (should-error
1958 (copy-file tmp-name4 tmp-name1)
1959 :type 'file-already-exists)
1960 (copy-file tmp-name4 tmp-name1 'ok)
1961 (make-directory tmp-name3)
1962 ;; This has been changed in Emacs 26.1.
1963 (when (tramp--test-emacs26-p)
1964 (should-error
1965 (copy-file tmp-name4 tmp-name3)
1966 :type 'file-already-exists))
1967 (copy-file tmp-name4 (file-name-as-directory tmp-name3))
1968 (should
1969 (file-exists-p
1970 (expand-file-name (file-name-nondirectory tmp-name4) tmp-name3))))
1972 ;; Cleanup.
1973 (ignore-errors (delete-file tmp-name1))
1974 (ignore-errors (delete-file tmp-name4))
1975 (ignore-errors (delete-directory tmp-name3 'recursive))))))
1977 (ert-deftest tramp-test12-rename-file ()
1978 "Check `rename-file'."
1979 (skip-unless (tramp--test-enabled))
1981 ;; TODO: The quoted case does not work.
1982 ;;(dolist (quoted (if tramp--test-expensive-test '(nil t) '(nil)))
1983 (let (quoted)
1984 (let ((tmp-name1 (tramp--test-make-temp-name nil quoted))
1985 (tmp-name2 (tramp--test-make-temp-name nil quoted))
1986 (tmp-name3 (tramp--test-make-temp-name nil quoted))
1987 (tmp-name4 (tramp--test-make-temp-name 'local quoted))
1988 (tmp-name5 (tramp--test-make-temp-name 'local quoted)))
1990 ;; Rename on remote side.
1991 (unwind-protect
1992 (progn
1993 (write-region "foo" nil tmp-name1)
1994 (rename-file tmp-name1 tmp-name2)
1995 (should-not (file-exists-p tmp-name1))
1996 (should (file-exists-p tmp-name2))
1997 (with-temp-buffer
1998 (insert-file-contents tmp-name2)
1999 (should (string-equal (buffer-string) "foo")))
2000 (write-region "foo" nil tmp-name1)
2001 (should-error
2002 (rename-file tmp-name1 tmp-name2)
2003 :type 'file-already-exists)
2004 (rename-file tmp-name1 tmp-name2 'ok)
2005 (should-not (file-exists-p tmp-name1))
2006 (write-region "foo" nil tmp-name1)
2007 (make-directory tmp-name3)
2008 ;; This has been changed in Emacs 26.1.
2009 (when (tramp--test-emacs26-p)
2010 (should-error
2011 (rename-file tmp-name1 tmp-name3)
2012 :type 'file-already-exists))
2013 (rename-file tmp-name1 (file-name-as-directory tmp-name3))
2014 (should-not (file-exists-p tmp-name1))
2015 (should
2016 (file-exists-p
2017 (expand-file-name (file-name-nondirectory tmp-name1) tmp-name3))))
2019 ;; Cleanup.
2020 (ignore-errors (delete-file tmp-name1))
2021 (ignore-errors (delete-file tmp-name2))
2022 (ignore-errors (delete-directory tmp-name3 'recursive)))
2024 ;; Rename from remote side to local side.
2025 (unwind-protect
2026 (progn
2027 (write-region "foo" nil tmp-name1)
2028 (rename-file tmp-name1 tmp-name4)
2029 (should-not (file-exists-p tmp-name1))
2030 (should (file-exists-p tmp-name4))
2031 (with-temp-buffer
2032 (insert-file-contents tmp-name4)
2033 (should (string-equal (buffer-string) "foo")))
2034 (write-region "foo" nil tmp-name1)
2035 (should-error
2036 (rename-file tmp-name1 tmp-name4)
2037 :type 'file-already-exists)
2038 (rename-file tmp-name1 tmp-name4 'ok)
2039 (should-not (file-exists-p tmp-name1))
2040 (write-region "foo" nil tmp-name1)
2041 (make-directory tmp-name5)
2042 ;; This has been changed in Emacs 26.1.
2043 (when (tramp--test-emacs26-p)
2044 (should-error
2045 (rename-file tmp-name1 tmp-name5)
2046 :type 'file-already-exists))
2047 (rename-file tmp-name1 (file-name-as-directory tmp-name5))
2048 (should-not (file-exists-p tmp-name1))
2049 (should
2050 (file-exists-p
2051 (expand-file-name (file-name-nondirectory tmp-name1) tmp-name5))))
2053 ;; Cleanup.
2054 (ignore-errors (delete-file tmp-name1))
2055 (ignore-errors (delete-file tmp-name4))
2056 (ignore-errors (delete-directory tmp-name5 'recursive)))
2058 ;; Rename from local side to remote side.
2059 (unwind-protect
2060 (progn
2061 (write-region "foo" nil tmp-name4 nil 'nomessage)
2062 (rename-file tmp-name4 tmp-name1)
2063 (should-not (file-exists-p tmp-name4))
2064 (should (file-exists-p tmp-name1))
2065 (with-temp-buffer
2066 (insert-file-contents tmp-name1)
2067 (should (string-equal (buffer-string) "foo")))
2068 (write-region "foo" nil tmp-name4 nil 'nomessage)
2069 (should-error
2070 (rename-file tmp-name4 tmp-name1)
2071 :type 'file-already-exists)
2072 (rename-file tmp-name4 tmp-name1 'ok)
2073 (should-not (file-exists-p tmp-name4))
2074 (write-region "foo" nil tmp-name4 nil 'nomessage)
2075 (make-directory tmp-name3)
2076 ;; This has been changed in Emacs 26.1.
2077 (when (tramp--test-emacs26-p)
2078 (should-error
2079 (rename-file tmp-name4 tmp-name3)
2080 :type 'file-already-exists))
2081 (rename-file tmp-name4 (file-name-as-directory tmp-name3))
2082 (should-not (file-exists-p tmp-name4))
2083 (should
2084 (file-exists-p
2085 (expand-file-name (file-name-nondirectory tmp-name4) tmp-name3))))
2087 ;; Cleanup.
2088 (ignore-errors (delete-file tmp-name1))
2089 (ignore-errors (delete-file tmp-name4))
2090 (ignore-errors (delete-directory tmp-name3 'recursive))))))
2092 (ert-deftest tramp-test13-make-directory ()
2093 "Check `make-directory'.
2094 This tests also `file-directory-p' and `file-accessible-directory-p'."
2095 (skip-unless (tramp--test-enabled))
2097 (dolist (quoted (if tramp--test-expensive-test '(nil t) '(nil)))
2098 (let* ((tmp-name1 (tramp--test-make-temp-name nil quoted))
2099 (tmp-name2 (expand-file-name "foo/bar" tmp-name1)))
2100 (unwind-protect
2101 (progn
2102 (make-directory tmp-name1)
2103 (should (file-directory-p tmp-name1))
2104 (should (file-accessible-directory-p tmp-name1))
2105 (should-error (make-directory tmp-name2) :type 'file-error)
2106 (make-directory tmp-name2 'parents)
2107 (should (file-directory-p tmp-name2))
2108 (should (file-accessible-directory-p tmp-name2)))
2110 ;; Cleanup.
2111 (ignore-errors (delete-directory tmp-name1 'recursive))))))
2113 (ert-deftest tramp-test14-delete-directory ()
2114 "Check `delete-directory'."
2115 (skip-unless (tramp--test-enabled))
2117 (dolist (quoted (if tramp--test-expensive-test '(nil t) '(nil)))
2118 (let ((tmp-name (tramp--test-make-temp-name nil quoted)))
2119 ;; Delete empty directory.
2120 (make-directory tmp-name)
2121 (should (file-directory-p tmp-name))
2122 (delete-directory tmp-name)
2123 (should-not (file-directory-p tmp-name))
2124 ;; Delete non-empty directory.
2125 (make-directory tmp-name)
2126 (should (file-directory-p tmp-name))
2127 (write-region "foo" nil (expand-file-name "bla" tmp-name))
2128 (should (file-exists-p (expand-file-name "bla" tmp-name)))
2129 (should-error (delete-directory tmp-name) :type 'file-error)
2130 (delete-directory tmp-name 'recursive)
2131 (should-not (file-directory-p tmp-name)))))
2133 (ert-deftest tramp-test15-copy-directory ()
2134 "Check `copy-directory'."
2135 (skip-unless (tramp--test-enabled))
2137 (dolist (quoted (if tramp--test-expensive-test '(nil t) '(nil)))
2138 (let* ((tmp-name1 (tramp--test-make-temp-name nil quoted))
2139 (tmp-name2 (tramp--test-make-temp-name nil quoted))
2140 (tmp-name3 (expand-file-name
2141 (file-name-nondirectory tmp-name1) tmp-name2))
2142 (tmp-name4 (expand-file-name "foo" tmp-name1))
2143 (tmp-name5 (expand-file-name "foo" tmp-name2))
2144 (tmp-name6 (expand-file-name "foo" tmp-name3)))
2146 ;; Copy complete directory.
2147 (unwind-protect
2148 (progn
2149 ;; Copy empty directory.
2150 (make-directory tmp-name1)
2151 (write-region "foo" nil tmp-name4)
2152 (should (file-directory-p tmp-name1))
2153 (should (file-exists-p tmp-name4))
2154 (copy-directory tmp-name1 tmp-name2)
2155 (should (file-directory-p tmp-name2))
2156 (should (file-exists-p tmp-name5))
2157 ;; Target directory does exist already.
2158 ;; This has been changed in Emacs 26.1.
2159 (when (tramp--test-emacs26-p)
2160 (should-error
2161 (copy-directory tmp-name1 tmp-name2)
2162 :type 'file-error))
2163 (copy-directory tmp-name1 (file-name-as-directory tmp-name2))
2164 (should (file-directory-p tmp-name3))
2165 (should (file-exists-p tmp-name6)))
2167 ;; Cleanup.
2168 (ignore-errors
2169 (delete-directory tmp-name1 'recursive)
2170 (delete-directory tmp-name2 'recursive)))
2172 ;; Copy directory contents.
2173 (unwind-protect
2174 (progn
2175 ;; Copy empty directory.
2176 (make-directory tmp-name1)
2177 (write-region "foo" nil tmp-name4)
2178 (should (file-directory-p tmp-name1))
2179 (should (file-exists-p tmp-name4))
2180 (copy-directory tmp-name1 tmp-name2 nil 'parents 'contents)
2181 (should (file-directory-p tmp-name2))
2182 (should (file-exists-p tmp-name5))
2183 ;; Target directory does exist already.
2184 (delete-file tmp-name5)
2185 (should-not (file-exists-p tmp-name5))
2186 (copy-directory
2187 tmp-name1 (file-name-as-directory tmp-name2)
2188 nil 'parents 'contents)
2189 (should (file-directory-p tmp-name2))
2190 (should (file-exists-p tmp-name5))
2191 (should-not (file-directory-p tmp-name3))
2192 (should-not (file-exists-p tmp-name6)))
2194 ;; Cleanup.
2195 (ignore-errors
2196 (delete-directory tmp-name1 'recursive)
2197 (delete-directory tmp-name2 'recursive))))))
2199 (ert-deftest tramp-test16-directory-files ()
2200 "Check `directory-files'."
2201 (skip-unless (tramp--test-enabled))
2203 (dolist (quoted (if tramp--test-expensive-test '(nil t) '(nil)))
2204 (let* ((tmp-name1 (tramp--test-make-temp-name nil quoted))
2205 (tmp-name2 (expand-file-name "bla" tmp-name1))
2206 (tmp-name3 (expand-file-name "foo" tmp-name1)))
2207 (unwind-protect
2208 (progn
2209 (make-directory tmp-name1)
2210 (write-region "foo" nil tmp-name2)
2211 (write-region "bla" nil tmp-name3)
2212 (should (file-directory-p tmp-name1))
2213 (should (file-exists-p tmp-name2))
2214 (should (file-exists-p tmp-name3))
2215 (should (equal (directory-files tmp-name1) '("." ".." "bla" "foo")))
2216 (should (equal (directory-files tmp-name1 'full)
2217 `(,(concat tmp-name1 "/.")
2218 ,(concat tmp-name1 "/..")
2219 ,tmp-name2 ,tmp-name3)))
2220 (should (equal (directory-files
2221 tmp-name1 nil directory-files-no-dot-files-regexp)
2222 '("bla" "foo")))
2223 (should (equal (directory-files
2224 tmp-name1 'full directory-files-no-dot-files-regexp)
2225 `(,tmp-name2 ,tmp-name3))))
2227 ;; Cleanup.
2228 (ignore-errors (delete-directory tmp-name1 'recursive))))))
2230 ;; This is not a file name handler test. But Tramp needed to apply an
2231 ;; advice for older Emacs versions, so we check that this has been fixed.
2232 (ert-deftest tramp-test16-file-expand-wildcards ()
2233 "Check `file-expand-wildcards'."
2234 (skip-unless (tramp--test-enabled))
2236 (dolist (quoted (if tramp--test-expensive-test '(nil t) '(nil)))
2237 (let* ((tmp-name1 (tramp--test-make-temp-name nil quoted))
2238 (tmp-name2 (expand-file-name "foo" tmp-name1))
2239 (tmp-name3 (expand-file-name "bar" tmp-name1))
2240 (tmp-name4 (expand-file-name "baz" tmp-name1))
2241 (default-directory tmp-name1))
2242 (unwind-protect
2243 (progn
2244 (make-directory tmp-name1)
2245 (write-region "foo" nil tmp-name2)
2246 (write-region "bar" nil tmp-name3)
2247 (write-region "baz" nil tmp-name4)
2248 (should (file-directory-p tmp-name1))
2249 (should (file-exists-p tmp-name2))
2250 (should (file-exists-p tmp-name3))
2251 (should (file-exists-p tmp-name4))
2253 ;; `sort' works destructive.
2254 (should
2255 (equal (file-expand-wildcards "*")
2256 (sort (copy-sequence '("foo" "bar" "baz")) 'string<)))
2257 (should
2258 (equal (file-expand-wildcards "ba?")
2259 (sort (copy-sequence '("bar" "baz")) 'string<)))
2260 (should
2261 (equal (file-expand-wildcards "ba[rz]")
2262 (sort (copy-sequence '("bar" "baz")) 'string<)))
2264 (should
2265 (equal
2266 (file-expand-wildcards "*" 'full)
2267 (sort
2268 (copy-sequence `(,tmp-name2 ,tmp-name3 ,tmp-name4)) 'string<)))
2269 (should
2270 (equal
2271 (file-expand-wildcards "ba?" 'full)
2272 (sort (copy-sequence `(,tmp-name3 ,tmp-name4)) 'string<)))
2273 (should
2274 (equal
2275 (file-expand-wildcards "ba[rz]" 'full)
2276 (sort (copy-sequence `(,tmp-name3 ,tmp-name4)) 'string<)))
2278 (should
2279 (equal
2280 (file-expand-wildcards (concat tmp-name1 "/" "*"))
2281 (sort
2282 (copy-sequence `(,tmp-name2 ,tmp-name3 ,tmp-name4)) 'string<)))
2283 (should
2284 (equal
2285 (file-expand-wildcards (concat tmp-name1 "/" "ba?"))
2286 (sort (copy-sequence `(,tmp-name3 ,tmp-name4)) 'string<)))
2287 (should
2288 (equal
2289 (file-expand-wildcards (concat tmp-name1 "/" "ba[rz]"))
2290 (sort (copy-sequence `(,tmp-name3 ,tmp-name4)) 'string<))))
2292 ;; Cleanup.
2293 (ignore-errors
2294 (delete-directory tmp-name1))))))
2296 (ert-deftest tramp-test17-insert-directory ()
2297 "Check `insert-directory'."
2298 (skip-unless (tramp--test-enabled))
2300 (dolist (quoted (if tramp--test-expensive-test '(nil t) '(nil)))
2301 (let* ((tmp-name1
2302 (expand-file-name (tramp--test-make-temp-name nil quoted)))
2303 (tmp-name2 (expand-file-name "foo" tmp-name1))
2304 ;; We test for the summary line. Keyword "total" could be localized.
2305 (process-environment
2306 (append '("LANG=C" "LANGUAGE=C" "LC_ALL=C") process-environment)))
2307 (unwind-protect
2308 (progn
2309 (make-directory tmp-name1)
2310 (write-region "foo" nil tmp-name2)
2311 (should (file-directory-p tmp-name1))
2312 (should (file-exists-p tmp-name2))
2313 (with-temp-buffer
2314 (insert-directory tmp-name1 nil)
2315 (goto-char (point-min))
2316 (should (looking-at-p (regexp-quote tmp-name1))))
2317 (with-temp-buffer
2318 (insert-directory tmp-name1 "-al")
2319 (goto-char (point-min))
2320 (should
2321 (looking-at-p (format "^.+ %s$" (regexp-quote tmp-name1)))))
2322 (with-temp-buffer
2323 (insert-directory (file-name-as-directory tmp-name1) "-al")
2324 (goto-char (point-min))
2325 (should
2326 (looking-at-p (format "^.+ %s/$" (regexp-quote tmp-name1)))))
2327 (with-temp-buffer
2328 (insert-directory
2329 (file-name-as-directory tmp-name1) "-al" nil 'full-directory-p)
2330 (goto-char (point-min))
2331 (should
2332 (looking-at-p
2333 (concat
2334 ;; There might be a summary line.
2335 "\\(total.+[[:digit:]]+\n\\)?"
2336 ;; We don't know in which order ".", ".." and "foo" appear.
2337 "\\(.+ \\(\\.?\\.\\|foo\\)\n\\)\\{3\\}")))))
2339 ;; Cleanup.
2340 (ignore-errors (delete-directory tmp-name1 'recursive))))))
2342 (ert-deftest tramp-test17-dired-with-wildcards ()
2343 "Check `dired' with wildcards."
2344 (skip-unless (tramp--test-enabled))
2345 (skip-unless (tramp--test-sh-p))
2346 ;; Since Emacs 26.1.
2347 (skip-unless (fboundp 'insert-directory-wildcard-in-dir-p))
2349 (dolist (quoted (if tramp--test-expensive-test '(nil t) '(nil)))
2350 (let* ((tmp-name1
2351 (expand-file-name (tramp--test-make-temp-name nil quoted)))
2352 (tmp-name2
2353 (expand-file-name (tramp--test-make-temp-name nil quoted)))
2354 (tmp-name3 (expand-file-name "foo" tmp-name1))
2355 (tmp-name4 (expand-file-name "bar" tmp-name2))
2356 (tramp-test-temporary-file-directory
2357 (funcall
2358 (if quoted 'tramp-compat-file-name-quote 'identity)
2359 tramp-test-temporary-file-directory))
2360 buffer)
2361 (unwind-protect
2362 (progn
2363 (make-directory tmp-name1)
2364 (write-region "foo" nil tmp-name3)
2365 (should (file-directory-p tmp-name1))
2366 (should (file-exists-p tmp-name3))
2367 (make-directory tmp-name2)
2368 (write-region "foo" nil tmp-name4)
2369 (should (file-directory-p tmp-name2))
2370 (should (file-exists-p tmp-name4))
2372 ;; Check for expanded directory names.
2373 (with-current-buffer
2374 (setq buffer
2375 (dired-noselect
2376 (expand-file-name
2377 "tramp-test*" tramp-test-temporary-file-directory)))
2378 (goto-char (point-min))
2379 (should
2380 (re-search-forward
2381 (regexp-quote
2382 (file-relative-name
2383 tmp-name1 tramp-test-temporary-file-directory))))
2384 (goto-char (point-min))
2385 (should
2386 (re-search-forward
2387 (regexp-quote
2388 (file-relative-name
2389 tmp-name2 tramp-test-temporary-file-directory)))))
2390 (kill-buffer buffer)
2392 ;; Check for expanded directory and file names.
2393 (with-current-buffer
2394 (setq buffer
2395 (dired-noselect
2396 (expand-file-name
2397 "tramp-test*/*" tramp-test-temporary-file-directory)))
2398 (goto-char (point-min))
2399 (should
2400 (re-search-forward
2401 (regexp-quote
2402 (file-relative-name
2403 tmp-name3 tramp-test-temporary-file-directory))))
2404 (goto-char (point-min))
2405 (should
2406 (re-search-forward
2407 (regexp-quote
2408 (file-relative-name
2409 tmp-name4
2410 tramp-test-temporary-file-directory)))))
2411 (kill-buffer buffer)
2413 ;; Check for special characters.
2414 (setq tmp-name3 (expand-file-name "*?" tmp-name1))
2415 (setq tmp-name4 (expand-file-name "[a-z0-9]" tmp-name2))
2416 (write-region "foo" nil tmp-name3)
2417 (should (file-exists-p tmp-name3))
2418 (write-region "foo" nil tmp-name4)
2419 (should (file-exists-p tmp-name4))
2421 (with-current-buffer
2422 (setq buffer
2423 (dired-noselect
2424 (expand-file-name
2425 "tramp-test*/*" tramp-test-temporary-file-directory)))
2426 (goto-char (point-min))
2427 (should
2428 (re-search-forward
2429 (regexp-quote
2430 (file-relative-name
2431 tmp-name3 tramp-test-temporary-file-directory))))
2432 (goto-char (point-min))
2433 (should
2434 (re-search-forward
2435 (regexp-quote
2436 (file-relative-name
2437 tmp-name4
2438 tramp-test-temporary-file-directory)))))
2439 (kill-buffer buffer))
2441 ;; Cleanup.
2442 (ignore-errors (kill-buffer buffer))
2443 (ignore-errors (delete-directory tmp-name1 'recursive))
2444 (ignore-errors (delete-directory tmp-name2 'recursive))))))
2446 ;; Method "smb" supports `make-symbolic-link' only if the remote host
2447 ;; has CIFS capabilities. tramp-adb.el and tramp-gvfs.el do not
2448 ;; support symbolic links at all.
2449 (defmacro tramp--test-ignore-make-symbolic-link-error (&rest body)
2450 "Run BODY, ignoring \"make-symbolic-link not supported\" file error."
2451 (declare (indent defun) (debug t))
2452 `(condition-case err
2453 (progn ,@body)
2454 ((error quit debug)
2455 (unless (and (eq (car err) 'file-error)
2456 (string-equal (error-message-string err)
2457 "make-symbolic-link not supported"))
2458 (signal (car err) (cdr err))))))
2460 (ert-deftest tramp-test18-file-attributes ()
2461 "Check `file-attributes'.
2462 This tests also `file-readable-p', `file-regular-p' and
2463 `file-ownership-preserved-p'."
2464 (skip-unless (tramp--test-enabled))
2466 (dolist (quoted (if tramp--test-expensive-test '(nil t) '(nil)))
2467 ;; We must use `file-truename' for the temporary directory,
2468 ;; because it could be located on a symlinked directory. This
2469 ;; would let the test fail.
2470 (let* ((tramp-test-temporary-file-directory
2471 (file-truename tramp-test-temporary-file-directory))
2472 (tmp-name1 (tramp--test-make-temp-name nil quoted))
2473 (tmp-name2 (tramp--test-make-temp-name nil quoted))
2474 ;; File name with "//".
2475 (tmp-name3
2476 (format
2477 "%s%s"
2478 (file-remote-p tmp-name1)
2479 (replace-regexp-in-string
2480 "/" "//" (file-remote-p tmp-name1 'localname))))
2481 attr)
2482 (unwind-protect
2483 (progn
2484 ;; `file-ownership-preserved-p' should return t for
2485 ;; non-existing files. It is implemented only in tramp-sh.el.
2486 (when (tramp--test-sh-p)
2487 (should (file-ownership-preserved-p tmp-name1 'group)))
2488 (write-region "foo" nil tmp-name1)
2489 (should (file-exists-p tmp-name1))
2490 (should (file-readable-p tmp-name1))
2491 (should (file-regular-p tmp-name1))
2492 (when (tramp--test-sh-p)
2493 (should (file-ownership-preserved-p tmp-name1 'group)))
2495 ;; We do not test inodes and device numbers.
2496 (setq attr (file-attributes tmp-name1))
2497 (should (consp attr))
2498 (should (null (car attr)))
2499 (should (numberp (nth 1 attr))) ;; Link.
2500 (should (numberp (nth 2 attr))) ;; Uid.
2501 (should (numberp (nth 3 attr))) ;; Gid.
2502 ;; Last access time.
2503 (should (stringp (current-time-string (nth 4 attr))))
2504 ;; Last modification time.
2505 (should (stringp (current-time-string (nth 5 attr))))
2506 ;; Last status change time.
2507 (should (stringp (current-time-string (nth 6 attr))))
2508 (should (numberp (nth 7 attr))) ;; Size.
2509 (should (stringp (nth 8 attr))) ;; Modes.
2511 (setq attr (file-attributes tmp-name1 'string))
2512 (should (stringp (nth 2 attr))) ;; Uid.
2513 (should (stringp (nth 3 attr))) ;; Gid.
2515 (tramp--test-ignore-make-symbolic-link-error
2516 (when (tramp--test-sh-p)
2517 (should (file-ownership-preserved-p tmp-name2 'group)))
2518 (make-symbolic-link tmp-name1 tmp-name2)
2519 (should (file-exists-p tmp-name2))
2520 (should (file-symlink-p tmp-name2))
2521 (when (tramp--test-sh-p)
2522 (should (file-ownership-preserved-p tmp-name2 'group)))
2523 (setq attr (file-attributes tmp-name2))
2524 (should
2525 (string-equal
2526 (funcall
2527 (if quoted 'tramp-compat-file-name-quote 'identity)
2528 (car attr))
2529 (file-remote-p (file-truename tmp-name1) 'localname)))
2530 (delete-file tmp-name2))
2532 ;; Check, that "//" in symlinks are handled properly.
2533 (with-temp-buffer
2534 (let ((default-directory tramp-test-temporary-file-directory))
2535 (shell-command
2536 (format
2537 "ln -s %s %s"
2538 (tramp-file-name-localname
2539 (tramp-dissect-file-name tmp-name3))
2540 (tramp-file-name-localname
2541 (tramp-dissect-file-name tmp-name2)))
2542 t)))
2543 (when (file-symlink-p tmp-name2)
2544 (setq attr (file-attributes tmp-name2))
2545 (should
2546 (string-equal
2547 (car attr)
2548 (tramp-file-name-localname
2549 (tramp-dissect-file-name tmp-name3))))
2550 (delete-file tmp-name2))
2552 (when (tramp--test-sh-p)
2553 (should (file-ownership-preserved-p tmp-name1 'group)))
2554 (delete-file tmp-name1)
2555 (make-directory tmp-name1)
2556 (should (file-exists-p tmp-name1))
2557 (should (file-readable-p tmp-name1))
2558 (should-not (file-regular-p tmp-name1))
2559 (when (tramp--test-sh-p)
2560 (should (file-ownership-preserved-p tmp-name1 'group)))
2561 (setq attr (file-attributes tmp-name1))
2562 (should (eq (car attr) t)))
2564 ;; Cleanup.
2565 (ignore-errors (delete-directory tmp-name1))
2566 (ignore-errors (delete-file tmp-name1))
2567 (ignore-errors (delete-file tmp-name2))))))
2569 (ert-deftest tramp-test19-directory-files-and-attributes ()
2570 "Check `directory-files-and-attributes'."
2571 (skip-unless (tramp--test-enabled))
2573 (dolist (quoted (if tramp--test-expensive-test '(nil t) '(nil)))
2574 ;; `directory-files-and-attributes' contains also values for
2575 ;; "../". Ensure that this doesn't change during tests, for
2576 ;; example due to handling temporary files.
2577 (let* ((tmp-name1 (tramp--test-make-temp-name nil quoted))
2578 (tmp-name2 (expand-file-name "bla" tmp-name1))
2579 attr)
2580 (unwind-protect
2581 (progn
2582 (make-directory tmp-name1)
2583 (should (file-directory-p tmp-name1))
2584 (make-directory tmp-name2)
2585 (should (file-directory-p tmp-name2))
2586 (write-region "foo" nil (expand-file-name "foo" tmp-name2))
2587 (write-region "bar" nil (expand-file-name "bar" tmp-name2))
2588 (write-region "boz" nil (expand-file-name "boz" tmp-name2))
2589 (setq attr (directory-files-and-attributes tmp-name2))
2590 (should (consp attr))
2591 ;; Dumb remote shells without perl(1) or stat(1) are not
2592 ;; able to return the date correctly. They say "don't know".
2593 (dolist (elt attr)
2594 (unless
2595 (equal
2596 (nth
2597 5 (file-attributes (expand-file-name (car elt) tmp-name2)))
2598 '(0 0))
2599 (should
2600 (equal (file-attributes (expand-file-name (car elt) tmp-name2))
2601 (cdr elt)))))
2602 (setq attr (directory-files-and-attributes tmp-name2 'full))
2603 (dolist (elt attr)
2604 (unless (equal (nth 5 (file-attributes (car elt))) '(0 0))
2605 (should
2606 (equal (file-attributes (car elt)) (cdr elt)))))
2607 (setq attr (directory-files-and-attributes tmp-name2 nil "^b"))
2608 (should (equal (mapcar 'car attr) '("bar" "boz"))))
2610 ;; Cleanup.
2611 (ignore-errors (delete-directory tmp-name1 'recursive))))))
2613 (ert-deftest tramp-test20-file-modes ()
2614 "Check `file-modes'.
2615 This tests also `file-executable-p', `file-writable-p' and `set-file-modes'."
2616 (skip-unless (tramp--test-enabled))
2617 (skip-unless (tramp--test-sh-p))
2619 (dolist (quoted (if tramp--test-expensive-test '(nil t) '(nil)))
2620 (let ((tmp-name (tramp--test-make-temp-name nil quoted)))
2621 (unwind-protect
2622 (progn
2623 (write-region "foo" nil tmp-name)
2624 (should (file-exists-p tmp-name))
2625 (set-file-modes tmp-name #o777)
2626 (should (= (file-modes tmp-name) #o777))
2627 (should (file-executable-p tmp-name))
2628 (should (file-writable-p tmp-name))
2629 (set-file-modes tmp-name #o444)
2630 (should (= (file-modes tmp-name) #o444))
2631 (should-not (file-executable-p tmp-name))
2632 ;; A file is always writable for user "root".
2633 (unless (zerop (nth 2 (file-attributes tmp-name)))
2634 (should-not (file-writable-p tmp-name))))
2636 ;; Cleanup.
2637 (ignore-errors (delete-file tmp-name))))))
2639 (ert-deftest tramp-test21-file-links ()
2640 "Check `file-symlink-p'.
2641 This tests also `make-symbolic-link', `file-truename' and `add-name-to-file'."
2642 (skip-unless (tramp--test-enabled))
2643 ;; The semantics has changed heavily in Emacs 26.1. We cannot test
2644 ;; older Emacsen, therefore.
2645 (skip-unless (tramp--test-emacs26-p))
2647 (dolist (quoted (if tramp--test-expensive-test '(nil t) '(nil)))
2648 ;; We must use `file-truename' for the temporary directory,
2649 ;; because it could be located on a symlinked directory. This
2650 ;; would let the test fail.
2651 (let* ((tramp-test-temporary-file-directory
2652 (file-truename tramp-test-temporary-file-directory))
2653 (tmp-name1 (tramp--test-make-temp-name nil quoted))
2654 (tmp-name2 (tramp--test-make-temp-name nil quoted))
2655 (tmp-name3 (tramp--test-make-temp-name 'local quoted))
2656 (tmp-name4 (tramp--test-make-temp-name nil quoted))
2657 (tmp-name5
2658 (expand-file-name (file-name-nondirectory tmp-name1) tmp-name4)))
2659 ;; Check `make-symbolic-link'.
2660 (unwind-protect
2661 (tramp--test-ignore-make-symbolic-link-error
2662 (write-region "foo" nil tmp-name1)
2663 (should (file-exists-p tmp-name1))
2664 (make-symbolic-link tmp-name1 tmp-name2)
2665 (should
2666 (string-equal
2667 (funcall
2668 (if quoted 'tramp-compat-file-name-unquote 'identity)
2669 (file-remote-p tmp-name1 'localname))
2670 (file-symlink-p tmp-name2)))
2671 (should-error
2672 (make-symbolic-link tmp-name1 tmp-name2)
2673 :type 'file-already-exists)
2674 ;; number means interactive case.
2675 (cl-letf (((symbol-function 'yes-or-no-p) 'ignore))
2676 (should-error
2677 (make-symbolic-link tmp-name1 tmp-name2 0)
2678 :type 'file-already-exists))
2679 (cl-letf (((symbol-function 'yes-or-no-p) (lambda (_prompt) t)))
2680 (make-symbolic-link tmp-name1 tmp-name2 0)
2681 (should
2682 (string-equal
2683 (funcall
2684 (if quoted 'tramp-compat-file-name-unquote 'identity)
2685 (file-remote-p tmp-name1 'localname))
2686 (file-symlink-p tmp-name2))))
2687 (make-symbolic-link tmp-name1 tmp-name2 'ok-if-already-exists)
2688 (should
2689 (string-equal
2690 (funcall
2691 (if quoted 'tramp-compat-file-name-unquote 'identity)
2692 (file-remote-p tmp-name1 'localname))
2693 (file-symlink-p tmp-name2)))
2694 ;; If we use the local part of `tmp-name1', it shall still work.
2695 (make-symbolic-link
2696 (file-remote-p tmp-name1 'localname)
2697 tmp-name2 'ok-if-already-exists)
2698 (should
2699 (string-equal
2700 (funcall
2701 (if quoted 'tramp-compat-file-name-unquote 'identity)
2702 (file-remote-p tmp-name1 'localname))
2703 (file-symlink-p tmp-name2)))
2704 ;; `tmp-name3' is a local file name. Therefore, the link
2705 ;; target remains unchanged, even if quoted.
2706 (make-symbolic-link tmp-name1 tmp-name3)
2707 (should
2708 (string-equal tmp-name1 (file-symlink-p tmp-name3)))
2709 ;; Check directory as newname.
2710 (make-directory tmp-name4)
2711 (should-error
2712 (make-symbolic-link tmp-name1 tmp-name4)
2713 :type 'file-already-exists)
2714 (make-symbolic-link tmp-name1 (file-name-as-directory tmp-name4))
2715 (should
2716 (string-equal
2717 (funcall
2718 (if quoted 'tramp-compat-file-name-unquote 'identity)
2719 (file-remote-p tmp-name1 'localname))
2720 (file-symlink-p tmp-name5)))
2721 ;; `smbclient' does not show symlinks in directories, so
2722 ;; we cannot delete a non-empty directory. We delete the
2723 ;; file explicitely.
2724 (delete-file tmp-name5))
2726 ;; Cleanup.
2727 (ignore-errors
2728 (delete-file tmp-name1)
2729 (delete-file tmp-name2)
2730 (delete-file tmp-name3)
2731 (delete-directory tmp-name4 'recursive)))
2733 ;; Check `add-name-to-file'.
2734 (unwind-protect
2735 (progn
2736 (write-region "foo" nil tmp-name1)
2737 (should (file-exists-p tmp-name1))
2738 (add-name-to-file tmp-name1 tmp-name2)
2739 (should (file-regular-p tmp-name2))
2740 (should-error
2741 (add-name-to-file tmp-name1 tmp-name2)
2742 :type 'file-already-exists)
2743 ;; A number means interactive case.
2744 (cl-letf (((symbol-function 'yes-or-no-p) 'ignore))
2745 (should-error
2746 (add-name-to-file tmp-name1 tmp-name2 0)
2747 :type 'file-already-exists))
2748 (cl-letf (((symbol-function 'yes-or-no-p) (lambda (_prompt) t)))
2749 (add-name-to-file tmp-name1 tmp-name2 0)
2750 (should (file-regular-p tmp-name2)))
2751 (add-name-to-file tmp-name1 tmp-name2 'ok-if-already-exists)
2752 (should-not (file-symlink-p tmp-name2))
2753 (should (file-regular-p tmp-name2))
2754 ;; `tmp-name3' is a local file name.
2755 (should-error
2756 (add-name-to-file tmp-name1 tmp-name3)
2757 :type 'file-error)
2758 ;; Check directory as newname.
2759 (make-directory tmp-name4)
2760 (should-error
2761 (add-name-to-file tmp-name1 tmp-name4)
2762 :type 'file-already-exists)
2763 (add-name-to-file tmp-name1 (file-name-as-directory tmp-name4))
2764 (should
2765 (file-regular-p
2766 (expand-file-name (file-name-nondirectory tmp-name1) tmp-name4))))
2768 ;; Cleanup.
2769 (ignore-errors
2770 (delete-file tmp-name1)
2771 (delete-file tmp-name2)
2772 (delete-directory tmp-name4 'recursive)))
2774 ;; Check `file-truename'.
2775 (unwind-protect
2776 (tramp--test-ignore-make-symbolic-link-error
2777 (write-region "foo" nil tmp-name1)
2778 (should (file-exists-p tmp-name1))
2779 (should (string-equal tmp-name1 (file-truename tmp-name1)))
2780 (make-symbolic-link tmp-name1 tmp-name2)
2781 (should (file-symlink-p tmp-name2))
2782 (should-not (string-equal tmp-name2 (file-truename tmp-name2)))
2783 (should
2784 (string-equal (file-truename tmp-name1) (file-truename tmp-name2)))
2785 (should (file-equal-p tmp-name1 tmp-name2))
2786 ;; Symbolic links could look like a remote file name.
2787 ;; They must be quoted then.
2788 (delete-file tmp-name2)
2789 (make-symbolic-link "/penguin:motd:" tmp-name2)
2790 (should (file-symlink-p tmp-name2))
2791 (should
2792 (string-equal
2793 (file-truename tmp-name2)
2794 (tramp-compat-file-name-quote
2795 (concat (file-remote-p tmp-name2) "/penguin:motd:"))))
2796 ;; `tmp-name3' is a local file name.
2797 (make-symbolic-link tmp-name1 tmp-name3)
2798 (should (file-symlink-p tmp-name3))
2799 (should-not (string-equal tmp-name3 (file-truename tmp-name3)))
2800 ;; `file-truename' returns a quoted file name for `tmp-name3'.
2801 ;; We must unquote it.
2802 (should
2803 (string-equal
2804 (file-truename tmp-name1)
2805 (tramp-compat-file-name-unquote (file-truename tmp-name3)))))
2807 ;; Cleanup.
2808 (ignore-errors
2809 (delete-file tmp-name1)
2810 (delete-file tmp-name2)
2811 (delete-file tmp-name3)))
2813 ;; Symbolic links could be nested.
2814 (unwind-protect
2815 (tramp--test-ignore-make-symbolic-link-error
2816 (make-directory tmp-name1)
2817 (should (file-directory-p tmp-name1))
2818 (let* ((tramp-test-temporary-file-directory
2819 (file-truename tmp-name1))
2820 (tmp-name2 (tramp--test-make-temp-name nil quoted))
2821 (tmp-name3 tmp-name2)
2822 (number-nesting 15))
2823 (dotimes (_ number-nesting)
2824 (make-symbolic-link
2825 tmp-name3
2826 (setq tmp-name3 (tramp--test-make-temp-name nil quoted))))
2827 (should
2828 (string-equal
2829 (file-truename tmp-name2)
2830 (file-truename tmp-name3)))
2831 (should-error
2832 (with-temp-buffer (insert-file-contents tmp-name2))
2833 :type tramp-file-missing)
2834 (should-error
2835 (with-temp-buffer (insert-file-contents tmp-name3))
2836 :type tramp-file-missing)
2837 ;; `directory-files' does not show symlinks to
2838 ;; non-existing targets in the "smb" case. So we remove
2839 ;; the symlinks manually.
2840 (while (stringp (setq tmp-name2 (file-symlink-p tmp-name3)))
2841 (delete-file tmp-name3)
2842 (setq tmp-name3 (concat (file-remote-p tmp-name3) tmp-name2)))))
2844 ;; Cleanup.
2845 (ignore-errors (delete-directory tmp-name1 'recursive)))
2847 ;; Detect cyclic symbolic links.
2848 (unwind-protect
2849 (tramp--test-ignore-make-symbolic-link-error
2850 (make-symbolic-link tmp-name2 tmp-name1)
2851 (should (file-symlink-p tmp-name1))
2852 (make-symbolic-link tmp-name1 tmp-name2)
2853 (should (file-symlink-p tmp-name2))
2854 (should-error (file-truename tmp-name1) :type 'file-error))
2856 ;; Cleanup.
2857 (ignore-errors
2858 (delete-file tmp-name1)
2859 (delete-file tmp-name2)))
2861 ;; `file-truename' shall preserve trailing link of directories.
2862 (unless (file-symlink-p tramp-test-temporary-file-directory)
2863 (let* ((dir1 (directory-file-name tramp-test-temporary-file-directory))
2864 (dir2 (file-name-as-directory dir1)))
2865 (should (string-equal (file-truename dir1) (expand-file-name dir1)))
2866 (should
2867 (string-equal (file-truename dir2) (expand-file-name dir2))))))))
2869 (ert-deftest tramp-test22-file-times ()
2870 "Check `set-file-times' and `file-newer-than-file-p'."
2871 (skip-unless (tramp--test-enabled))
2872 (skip-unless (or (tramp--test-adb-p) (tramp--test-sh-p)))
2874 (dolist (quoted (if tramp--test-expensive-test '(nil t) '(nil)))
2875 (let ((tmp-name1 (tramp--test-make-temp-name nil quoted))
2876 (tmp-name2 (tramp--test-make-temp-name nil quoted))
2877 (tmp-name3 (tramp--test-make-temp-name nil quoted)))
2878 (unwind-protect
2879 (progn
2880 (write-region "foo" nil tmp-name1)
2881 (should (file-exists-p tmp-name1))
2882 (should (consp (nth 5 (file-attributes tmp-name1))))
2883 ;; '(0 0) means don't know, and will be replaced by
2884 ;; `current-time'. Therefore, we use '(0 1). We skip the
2885 ;; test, if the remote handler is not able to set the
2886 ;; correct time.
2887 (skip-unless (set-file-times tmp-name1 '(0 1)))
2888 ;; Dumb remote shells without perl(1) or stat(1) are not
2889 ;; able to return the date correctly. They say "don't know".
2890 (unless (equal (nth 5 (file-attributes tmp-name1)) '(0 0))
2891 (should (equal (nth 5 (file-attributes tmp-name1)) '(0 1)))
2892 (write-region "bla" nil tmp-name2)
2893 (should (file-exists-p tmp-name2))
2894 (should (file-newer-than-file-p tmp-name2 tmp-name1))
2895 ;; `tmp-name3' does not exist.
2896 (should (file-newer-than-file-p tmp-name2 tmp-name3))
2897 (should-not (file-newer-than-file-p tmp-name3 tmp-name1))))
2899 ;; Cleanup.
2900 (ignore-errors
2901 (delete-file tmp-name1)
2902 (delete-file tmp-name2))))))
2904 (ert-deftest tramp-test23-visited-file-modtime ()
2905 "Check `set-visited-file-modtime' and `verify-visited-file-modtime'."
2906 (skip-unless (tramp--test-enabled))
2908 (dolist (quoted (if tramp--test-expensive-test '(nil t) '(nil)))
2909 (let ((tmp-name (tramp--test-make-temp-name nil quoted)))
2910 (unwind-protect
2911 (progn
2912 (write-region "foo" nil tmp-name)
2913 (should (file-exists-p tmp-name))
2914 (with-temp-buffer
2915 (insert-file-contents tmp-name)
2916 (should (verify-visited-file-modtime))
2917 (set-visited-file-modtime '(0 1))
2918 (should (verify-visited-file-modtime))
2919 (should (equal (visited-file-modtime) '(0 1 0 0)))))
2921 ;; Cleanup.
2922 (ignore-errors (delete-file tmp-name))))))
2924 (ert-deftest tramp-test24-file-name-completion ()
2925 "Check `file-name-completion' and `file-name-all-completions'."
2926 (skip-unless (tramp--test-enabled))
2928 ;; Method and host name in completion mode. This kind of completion
2929 ;; does not work on MS Windows.
2930 (when (not (memq system-type '(cygwin windows-nt)))
2931 (let ((method (file-remote-p tramp-test-temporary-file-directory 'method))
2932 (host (file-remote-p tramp-test-temporary-file-directory 'host))
2933 (orig-syntax tramp-syntax))
2934 (when (and (stringp host) (string-match tramp-host-with-port-regexp host))
2935 (setq host (match-string 1 host)))
2937 (unwind-protect
2938 (dolist
2939 (syntax
2940 (if tramp--test-expensive-test
2941 (tramp-syntax-values) `(,orig-syntax)))
2942 (tramp-change-syntax syntax)
2943 (let ;; This is needed for the `simplified' syntax.
2944 ((method-marker
2945 (if (zerop (length tramp-method-regexp))
2946 "" tramp-default-method-marker))
2947 ;; This is needed for the `separate' syntax.
2948 (prefix-format (substring tramp-prefix-format 1)))
2949 ;; Complete method name.
2950 (unless (or (zerop (length method))
2951 (zerop (length tramp-method-regexp)))
2952 (should
2953 (member
2954 (concat prefix-format method tramp-postfix-method-format)
2955 (file-name-all-completions
2956 (concat prefix-format (substring method 0 1)) "/"))))
2957 ;; Complete host name for default method. With gvfs
2958 ;; based methods, host name will be determined as
2959 ;; host.local, so we omit the test.
2960 (let ((tramp-default-method (or method tramp-default-method)))
2961 (unless (or (zerop (length host))
2962 (tramp--test-gvfs-p tramp-default-method))
2963 (should
2964 (member
2965 (concat
2966 prefix-format method-marker tramp-postfix-method-format
2967 host tramp-postfix-host-format)
2968 (file-name-all-completions
2969 (concat
2970 prefix-format method-marker tramp-postfix-method-format
2971 (substring host 0 1))
2972 "/")))))
2973 ;; Complete host name.
2974 (unless (or (zerop (length method))
2975 (zerop (length tramp-method-regexp))
2976 (zerop (length host))
2977 (tramp--test-gvfs-p method))
2978 (should
2979 (member
2980 (concat
2981 prefix-format method tramp-postfix-method-format
2982 host tramp-postfix-host-format)
2983 (file-name-all-completions
2984 (concat prefix-format method tramp-postfix-method-format)
2985 "/"))))))
2987 ;; Cleanup.
2988 (tramp-change-syntax orig-syntax))))
2990 (dolist (n-e '(nil t))
2991 (dolist (quoted (if tramp--test-expensive-test '(nil t) '(nil)))
2992 (let ((non-essential n-e)
2993 (tmp-name (tramp--test-make-temp-name nil quoted)))
2995 (unwind-protect
2996 (progn
2997 ;; Local files.
2998 (make-directory tmp-name)
2999 (should (file-directory-p tmp-name))
3000 (write-region "foo" nil (expand-file-name "foo" tmp-name))
3001 (should (file-exists-p (expand-file-name "foo" tmp-name)))
3002 (write-region "bar" nil (expand-file-name "bold" tmp-name))
3003 (should (file-exists-p (expand-file-name "bold" tmp-name)))
3004 (make-directory (expand-file-name "boz" tmp-name))
3005 (should (file-directory-p (expand-file-name "boz" tmp-name)))
3006 (should (equal (file-name-completion "fo" tmp-name) "foo"))
3007 (should (equal (file-name-completion "foo" tmp-name) t))
3008 (should (equal (file-name-completion "b" tmp-name) "bo"))
3009 (should-not (file-name-completion "a" tmp-name))
3010 (should
3011 (equal
3012 (file-name-completion "b" tmp-name 'file-directory-p) "boz/"))
3013 (should
3014 (equal (file-name-all-completions "fo" tmp-name) '("foo")))
3015 (should
3016 (equal
3017 (sort (file-name-all-completions "b" tmp-name) 'string-lessp)
3018 '("bold" "boz/")))
3019 (should-not (file-name-all-completions "a" tmp-name))
3020 ;; `completion-regexp-list' restricts the completion to
3021 ;; files which match all expressions in this list.
3022 (let ((completion-regexp-list
3023 `(,directory-files-no-dot-files-regexp "b")))
3024 (should
3025 (equal (file-name-completion "" tmp-name) "bo"))
3026 (should
3027 (equal
3028 (sort (file-name-all-completions "" tmp-name) 'string-lessp)
3029 '("bold" "boz/"))))
3030 ;; `file-name-completion' ignores file names that end in
3031 ;; any string in `completion-ignored-extensions'.
3032 (let ((completion-ignored-extensions '(".ext")))
3033 (write-region "foo" nil (expand-file-name "foo.ext" tmp-name))
3034 (should (file-exists-p (expand-file-name "foo.ext" tmp-name)))
3035 (should (equal (file-name-completion "fo" tmp-name) "foo"))
3036 (should (equal (file-name-completion "foo" tmp-name) t))
3037 (should
3038 (equal (file-name-completion "foo." tmp-name) "foo.ext"))
3039 (should (equal (file-name-completion "foo.ext" tmp-name) t))
3040 ;; `file-name-all-completions' is not affected.
3041 (should
3042 (equal
3043 (sort (file-name-all-completions "" tmp-name) 'string-lessp)
3044 '("../" "./" "bold" "boz/" "foo" "foo.ext")))))
3046 ;; Cleanup.
3047 (ignore-errors (delete-directory tmp-name 'recursive)))))))
3049 (ert-deftest tramp-test25-load ()
3050 "Check `load'."
3051 (skip-unless (tramp--test-enabled))
3053 (dolist (quoted (if tramp--test-expensive-test '(nil t) '(nil)))
3054 (let ((tmp-name (tramp--test-make-temp-name nil quoted)))
3055 (unwind-protect
3056 (progn
3057 (load tmp-name 'noerror 'nomessage)
3058 (should-not (featurep 'tramp-test-load))
3059 (write-region "(provide 'tramp-test-load)" nil tmp-name)
3060 ;; `load' in lread.c does not pass `must-suffix'. Why?
3061 ;;(should-error
3062 ;; (load tmp-name nil 'nomessage 'nosuffix 'must-suffix)
3063 ;; :type 'file-error)
3064 (load tmp-name nil 'nomessage 'nosuffix)
3065 (should (featurep 'tramp-test-load)))
3067 ;; Cleanup.
3068 (ignore-errors
3069 (and (featurep 'tramp-test-load) (unload-feature 'tramp-test-load))
3070 (delete-file tmp-name))))))
3072 (ert-deftest tramp-test26-process-file ()
3073 "Check `process-file'."
3074 :tags '(:expensive-test)
3075 (skip-unless (tramp--test-enabled))
3076 (skip-unless (or (tramp--test-adb-p) (tramp--test-sh-p)))
3078 (dolist (quoted (if tramp--test-expensive-test '(nil t) '(nil)))
3079 (let* ((tmp-name (tramp--test-make-temp-name nil quoted))
3080 (fnnd (file-name-nondirectory tmp-name))
3081 (default-directory tramp-test-temporary-file-directory)
3082 kill-buffer-query-functions)
3083 (unwind-protect
3084 (progn
3085 ;; We cannot use "/bin/true" and "/bin/false"; those paths
3086 ;; do not exist on hydra.
3087 (should (zerop (process-file "true")))
3088 (should-not (zerop (process-file "false")))
3089 (should-not (zerop (process-file "binary-does-not-exist")))
3090 (with-temp-buffer
3091 (write-region "foo" nil tmp-name)
3092 (should (file-exists-p tmp-name))
3093 (should (zerop (process-file "ls" nil t nil fnnd)))
3094 ;; `ls' could produce colorized output.
3095 (goto-char (point-min))
3096 (while
3097 (re-search-forward tramp-display-escape-sequence-regexp nil t)
3098 (replace-match "" nil nil))
3099 (should (string-equal (format "%s\n" fnnd) (buffer-string)))
3100 (should-not (get-buffer-window (current-buffer) t))
3102 ;; Second run. The output must be appended.
3103 (goto-char (point-max))
3104 (should (zerop (process-file "ls" nil t t fnnd)))
3105 ;; `ls' could produce colorized output.
3106 (goto-char (point-min))
3107 (while
3108 (re-search-forward tramp-display-escape-sequence-regexp nil t)
3109 (replace-match "" nil nil))
3110 (should
3111 (string-equal (format "%s\n%s\n" fnnd fnnd) (buffer-string)))
3112 ;; A non-nil DISPLAY must not raise the buffer.
3113 (should-not (get-buffer-window (current-buffer) t))))
3115 ;; Cleanup.
3116 (ignore-errors (delete-file tmp-name))))))
3118 (ert-deftest tramp-test27-start-file-process ()
3119 "Check `start-file-process'."
3120 :tags '(:expensive-test)
3121 (skip-unless (tramp--test-enabled))
3122 (skip-unless (tramp--test-sh-p))
3124 (dolist (quoted (if tramp--test-expensive-test '(nil t) '(nil)))
3125 (let ((default-directory tramp-test-temporary-file-directory)
3126 (tmp-name (tramp--test-make-temp-name nil quoted))
3127 kill-buffer-query-functions proc)
3128 (unwind-protect
3129 (with-temp-buffer
3130 (setq proc (start-file-process "test1" (current-buffer) "cat"))
3131 (should (processp proc))
3132 (should (equal (process-status proc) 'run))
3133 (process-send-string proc "foo")
3134 (process-send-eof proc)
3135 ;; Read output.
3136 (with-timeout (10 (ert-fail "`start-file-process' timed out"))
3137 (while (< (- (point-max) (point-min)) (length "foo"))
3138 (accept-process-output proc 0.1)))
3139 (should (string-equal (buffer-string) "foo")))
3141 ;; Cleanup.
3142 (ignore-errors (delete-process proc)))
3144 (unwind-protect
3145 (with-temp-buffer
3146 (write-region "foo" nil tmp-name)
3147 (should (file-exists-p tmp-name))
3148 (setq proc
3149 (start-file-process
3150 "test2" (current-buffer)
3151 "cat" (file-name-nondirectory tmp-name)))
3152 (should (processp proc))
3153 ;; Read output.
3154 (with-timeout (10 (ert-fail "`start-file-process' timed out"))
3155 (while (< (- (point-max) (point-min)) (length "foo"))
3156 (accept-process-output proc 0.1)))
3157 (should (string-equal (buffer-string) "foo")))
3159 ;; Cleanup.
3160 (ignore-errors
3161 (delete-process proc)
3162 (delete-file tmp-name)))
3164 (unwind-protect
3165 (with-temp-buffer
3166 (setq proc (start-file-process "test3" (current-buffer) "cat"))
3167 (should (processp proc))
3168 (should (equal (process-status proc) 'run))
3169 (set-process-filter
3170 proc
3171 (lambda (p s) (with-current-buffer (process-buffer p) (insert s))))
3172 (process-send-string proc "foo")
3173 (process-send-eof proc)
3174 ;; Read output.
3175 (with-timeout (10 (ert-fail "`start-file-process' timed out"))
3176 (while (< (- (point-max) (point-min)) (length "foo"))
3177 (accept-process-output proc 0.1)))
3178 (should (string-equal (buffer-string) "foo")))
3180 ;; Cleanup.
3181 (ignore-errors (delete-process proc))))))
3183 (ert-deftest tramp-test28-interrupt-process ()
3184 "Check `interrupt-process'."
3185 :tags '(:expensive-test)
3186 (skip-unless (tramp--test-enabled))
3187 (skip-unless (tramp--test-sh-p))
3188 ;; Since Emacs 26.1.
3189 (skip-unless (boundp 'interrupt-process-functions))
3191 (let ((default-directory tramp-test-temporary-file-directory)
3192 kill-buffer-query-functions proc)
3193 (unwind-protect
3194 (with-temp-buffer
3195 (setq proc (start-file-process "test" (current-buffer) "sleep" "10"))
3196 (should (processp proc))
3197 (should (process-live-p proc))
3198 (should (equal (process-status proc) 'run))
3199 (should (numberp (process-get proc 'remote-pid)))
3200 (should (interrupt-process proc))
3201 ;; Let the process accept the interrupt.
3202 (accept-process-output proc 1 nil 0)
3203 (should-not (process-live-p proc))
3204 ;; An interrupted process cannot be interrupted, again.
3205 (should-error (interrupt-process proc) :type 'error))
3207 ;; Cleanup.
3208 (ignore-errors (delete-process proc)))))
3210 (ert-deftest tramp-test29-shell-command ()
3211 "Check `shell-command'."
3212 :tags '(:expensive-test)
3213 (skip-unless (tramp--test-enabled))
3214 (skip-unless (tramp--test-sh-p))
3216 (dolist (quoted (if tramp--test-expensive-test '(nil t) '(nil)))
3217 (let ((tmp-name (tramp--test-make-temp-name nil quoted))
3218 (default-directory tramp-test-temporary-file-directory)
3219 ;; Suppress nasty messages.
3220 (inhibit-message t)
3221 kill-buffer-query-functions)
3222 (unwind-protect
3223 (with-temp-buffer
3224 (write-region "foo" nil tmp-name)
3225 (should (file-exists-p tmp-name))
3226 (shell-command
3227 (format "ls %s" (file-name-nondirectory tmp-name))
3228 (current-buffer))
3229 ;; `ls' could produce colorized output.
3230 (goto-char (point-min))
3231 (while
3232 (re-search-forward tramp-display-escape-sequence-regexp nil t)
3233 (replace-match "" nil nil))
3234 (should
3235 (string-equal
3236 (format "%s\n" (file-name-nondirectory tmp-name))
3237 (buffer-string))))
3239 ;; Cleanup.
3240 (ignore-errors (delete-file tmp-name)))
3242 (unwind-protect
3243 (with-temp-buffer
3244 (write-region "foo" nil tmp-name)
3245 (should (file-exists-p tmp-name))
3246 (async-shell-command
3247 (format "ls %s" (file-name-nondirectory tmp-name))
3248 (current-buffer))
3249 ;; Read output.
3250 (with-timeout (10 (ert-fail "`async-shell-command' timed out"))
3251 (while (< (- (point-max) (point-min))
3252 (1+ (length (file-name-nondirectory tmp-name))))
3253 (accept-process-output
3254 (get-buffer-process (current-buffer)) 0.1)))
3255 ;; `ls' could produce colorized output.
3256 (goto-char (point-min))
3257 (while
3258 (re-search-forward tramp-display-escape-sequence-regexp nil t)
3259 (replace-match "" nil nil))
3260 ;; There might be a nasty "Process *Async Shell* finished" message.
3261 (goto-char (point-min))
3262 (forward-line)
3263 (narrow-to-region (point-min) (point))
3264 (should
3265 (string-equal
3266 (format "%s\n" (file-name-nondirectory tmp-name))
3267 (buffer-string))))
3269 ;; Cleanup.
3270 (ignore-errors (delete-file tmp-name)))
3272 (unwind-protect
3273 (with-temp-buffer
3274 (write-region "foo" nil tmp-name)
3275 (should (file-exists-p tmp-name))
3276 (async-shell-command "read line; ls $line" (current-buffer))
3277 (process-send-string
3278 (get-buffer-process (current-buffer))
3279 (format "%s\n" (file-name-nondirectory tmp-name)))
3280 ;; Read output.
3281 (with-timeout (10 (ert-fail "`async-shell-command' timed out"))
3282 (while (< (- (point-max) (point-min))
3283 (1+ (length (file-name-nondirectory tmp-name))))
3284 (accept-process-output
3285 (get-buffer-process (current-buffer)) 0.1)))
3286 ;; `ls' could produce colorized output.
3287 (goto-char (point-min))
3288 (while
3289 (re-search-forward tramp-display-escape-sequence-regexp nil t)
3290 (replace-match "" nil nil))
3291 ;; There might be a nasty "Process *Async Shell* finished" message.
3292 (goto-char (point-min))
3293 (forward-line)
3294 (narrow-to-region (point-min) (point))
3295 (should
3296 (string-equal
3297 (format "%s\n" (file-name-nondirectory tmp-name))
3298 (buffer-string))))
3300 ;; Cleanup.
3301 (ignore-errors (delete-file tmp-name))))))
3303 (defun tramp--test-shell-command-to-string-asynchronously (command)
3304 "Like `shell-command-to-string', but for asynchronous processes."
3305 (with-temp-buffer
3306 (async-shell-command command (current-buffer))
3307 (with-timeout (10)
3308 (while (get-buffer-process (current-buffer))
3309 (accept-process-output (get-buffer-process (current-buffer)) 0.1)))
3310 (accept-process-output nil 0.1)
3311 (buffer-substring-no-properties (point-min) (point-max))))
3313 ;; This test is inspired by Bug#23952.
3314 (ert-deftest tramp-test30-environment-variables ()
3315 "Check that remote processes set / unset environment variables properly."
3316 :tags '(:expensive-test)
3317 (skip-unless (tramp--test-enabled))
3318 (skip-unless (tramp--test-sh-p))
3320 (dolist (this-shell-command-to-string
3321 '(;; Synchronously.
3322 shell-command-to-string
3323 ;; Asynchronously.
3324 tramp--test-shell-command-to-string-asynchronously))
3326 (let ((default-directory tramp-test-temporary-file-directory)
3327 (shell-file-name "/bin/sh")
3328 (envvar (concat "VAR_" (upcase (md5 (current-time-string)))))
3329 kill-buffer-query-functions)
3331 (unwind-protect
3332 ;; Set a value.
3333 (let ((process-environment
3334 (cons (concat envvar "=foo") process-environment)))
3335 ;; Default value.
3336 (should
3337 (string-match
3338 "foo"
3339 (funcall
3340 this-shell-command-to-string
3341 (format "echo -n ${%s:?bla}" envvar))))))
3343 (unwind-protect
3344 ;; Set the empty value.
3345 (let ((process-environment
3346 (cons (concat envvar "=") process-environment)))
3347 ;; Value is null.
3348 (should
3349 (string-match
3350 "bla"
3351 (funcall
3352 this-shell-command-to-string
3353 (format "echo -n ${%s:?bla}" envvar))))
3354 ;; Variable is set.
3355 (should
3356 (string-match
3357 (regexp-quote envvar)
3358 (funcall this-shell-command-to-string "set")))))
3360 ;; We force a reconnect, in order to have a clean environment.
3361 (tramp-cleanup-connection
3362 (tramp-dissect-file-name tramp-test-temporary-file-directory)
3363 'keep-debug 'keep-password)
3364 (unwind-protect
3365 ;; Unset the variable.
3366 (let ((tramp-remote-process-environment
3367 (cons (concat envvar "=foo")
3368 tramp-remote-process-environment)))
3369 ;; Set the initial value, we want to unset below.
3370 (should
3371 (string-match
3372 "foo"
3373 (funcall
3374 this-shell-command-to-string
3375 (format "echo -n ${%s:?bla}" envvar))))
3376 (let ((process-environment
3377 (cons envvar process-environment)))
3378 ;; Variable is unset.
3379 (should
3380 (string-match
3381 "bla"
3382 (funcall
3383 this-shell-command-to-string
3384 (format "echo -n ${%s:?bla}" envvar))))
3385 ;; Variable is unset.
3386 (should-not
3387 (string-match
3388 (regexp-quote envvar)
3389 (funcall this-shell-command-to-string "set")))))))))
3391 ;; This test is inspired by Bug#27009.
3392 (ert-deftest tramp-test30-environment-variables-and-port-numbers ()
3393 "Check that two connections with separate ports are different."
3394 (skip-unless (tramp--test-enabled))
3395 ;; We test it only for the mock-up connection; otherwise there might
3396 ;; be problems with the used ports.
3397 (skip-unless
3398 (and
3399 (eq tramp-syntax 'default)
3400 (string-equal
3401 "mock" (file-remote-p tramp-test-temporary-file-directory 'method))))
3403 ;; We force a reconnect, in order to have a clean environment.
3404 (dolist (dir `(,tramp-test-temporary-file-directory
3405 "/mock:localhost#11111:" "/mock:localhost#22222:"))
3406 (tramp-cleanup-connection
3407 (tramp-dissect-file-name dir) 'keep-debug 'keep-password))
3409 (unwind-protect
3410 (dolist (port '(11111 22222))
3411 (let* ((default-directory
3412 (format "/mock:localhost#%d:%s" port temporary-file-directory))
3413 (shell-file-name "/bin/sh")
3414 (envvar (concat "VAR_" (upcase (md5 (current-time-string)))))
3415 ;; We cannot use `process-environment', because this
3416 ;; would be applied in `process-file'.
3417 (tramp-remote-process-environment
3418 (cons
3419 (format "%s=%d" envvar port)
3420 tramp-remote-process-environment)))
3421 (should
3422 (string-equal
3423 (number-to-string port)
3424 (shell-command-to-string (format "echo -n $%s" envvar))))))
3426 ;; Cleanup.
3427 (dolist (dir '("/mock:localhost#11111:" "/mock:localhost#22222:"))
3428 (tramp-cleanup-connection (tramp-dissect-file-name dir)))))
3430 ;; The functions were introduced in Emacs 26.1.
3431 (ert-deftest tramp-test31-explicit-shell-file-name ()
3432 "Check that connection-local `explicit-shell-file-name' is set."
3433 :tags '(:expensive-test)
3434 (skip-unless (tramp--test-enabled))
3435 (skip-unless (tramp--test-sh-p))
3436 ;; Since Emacs 26.1.
3437 (skip-unless (and (fboundp 'connection-local-set-profile-variables)
3438 (fboundp 'connection-local-set-profiles)))
3440 ;; `connection-local-set-profile-variables' and
3441 ;; `connection-local-set-profiles' exists since Emacs 26. We don't
3442 ;; want to see compiler warnings for older Emacsen.
3443 (let ((default-directory tramp-test-temporary-file-directory)
3444 explicit-shell-file-name kill-buffer-query-functions)
3445 (unwind-protect
3446 (progn
3447 ;; `shell-mode' would ruin our test, because it deletes all
3448 ;; buffer local variables.
3449 (put 'explicit-shell-file-name 'permanent-local t)
3450 ;; Declare connection-local variable `explicit-shell-file-name'.
3451 (with-no-warnings
3452 (connection-local-set-profile-variables
3453 'remote-sh
3454 '((explicit-shell-file-name . "/bin/sh")
3455 (explicit-sh-args . ("-i"))))
3456 (connection-local-set-profiles
3457 `(:application tramp
3458 :protocol ,(file-remote-p default-directory 'method)
3459 :user ,(file-remote-p default-directory 'user)
3460 :machine ,(file-remote-p default-directory 'host))
3461 'remote-sh))
3463 ;; Run interactive shell. Since the default directory is
3464 ;; remote, `explicit-shell-file-name' shall be set in order
3465 ;; to avoid a question.
3466 (with-current-buffer (get-buffer-create "*shell*")
3467 (ignore-errors (kill-process (current-buffer)))
3468 (should-not explicit-shell-file-name)
3469 (call-interactively 'shell)
3470 (should explicit-shell-file-name)))
3472 (put 'explicit-shell-file-name 'permanent-local nil)
3473 (kill-buffer "*shell*"))))
3475 (ert-deftest tramp-test32-vc-registered ()
3476 "Check `vc-registered'."
3477 :tags '(:expensive-test)
3478 (skip-unless (tramp--test-enabled))
3479 (skip-unless (tramp--test-sh-p))
3481 (dolist (quoted (if tramp--test-expensive-test '(nil t) '(nil)))
3482 (let* ((default-directory tramp-test-temporary-file-directory)
3483 (tmp-name1 (tramp--test-make-temp-name nil quoted))
3484 (tmp-name2 (expand-file-name "foo" tmp-name1))
3485 (tramp-remote-process-environment tramp-remote-process-environment)
3486 (vc-handled-backends
3487 (with-parsed-tramp-file-name tramp-test-temporary-file-directory nil
3488 (cond
3489 ((tramp-find-executable
3490 v vc-git-program (tramp-get-remote-path v))
3491 '(Git))
3492 ((tramp-find-executable
3493 v vc-hg-program (tramp-get-remote-path v))
3494 '(Hg))
3495 ((tramp-find-executable
3496 v vc-bzr-program (tramp-get-remote-path v))
3497 (setq tramp-remote-process-environment
3498 (cons (format "BZR_HOME=%s"
3499 (file-remote-p tmp-name1 'localname))
3500 tramp-remote-process-environment))
3501 ;; We must force a reconnect, in order to activate $BZR_HOME.
3502 (tramp-cleanup-connection
3503 (tramp-dissect-file-name tramp-test-temporary-file-directory)
3504 'keep-debug 'keep-password)
3505 '(Bzr))
3506 (t nil))))
3507 ;; Suppress nasty messages.
3508 (inhibit-message t))
3509 (skip-unless vc-handled-backends)
3510 (unless quoted (tramp--test-message "%s" vc-handled-backends))
3512 (unwind-protect
3513 (progn
3514 (make-directory tmp-name1)
3515 (write-region "foo" nil tmp-name2)
3516 (should (file-directory-p tmp-name1))
3517 (should (file-exists-p tmp-name2))
3518 (should-not (vc-registered tmp-name1))
3519 (should-not (vc-registered tmp-name2))
3521 (let ((default-directory tmp-name1))
3522 ;; Create empty repository, and register the file.
3523 ;; Sometimes, creation of repository fails (bzr!); we
3524 ;; skip the test then.
3525 (condition-case nil
3526 (vc-create-repo (car vc-handled-backends))
3527 (error (skip-unless nil)))
3528 ;; The structure of VC-FILESET is not documented. Let's
3529 ;; hope it won't change.
3530 (condition-case nil
3531 (vc-register
3532 (list (car vc-handled-backends)
3533 (list (file-name-nondirectory tmp-name2))))
3534 ;; `vc-register' has changed its arguments in Emacs 25.1.
3535 (error
3536 (vc-register
3537 nil (list (car vc-handled-backends)
3538 (list (file-name-nondirectory tmp-name2))))))
3539 ;; vc-git uses an own process sentinel, Tramp's sentinel
3540 ;; for flushing the cache isn't used.
3541 (dired-uncache (concat (file-remote-p default-directory) "/"))
3542 (should (vc-registered (file-name-nondirectory tmp-name2)))))
3544 ;; Cleanup.
3545 (ignore-errors (delete-directory tmp-name1 'recursive))))))
3547 (ert-deftest tramp-test33-make-auto-save-file-name ()
3548 "Check `make-auto-save-file-name'."
3549 (skip-unless (tramp--test-enabled))
3551 (dolist (quoted (if tramp--test-expensive-test '(nil t) '(nil)))
3552 (let ((tmp-name1 (tramp--test-make-temp-name nil quoted))
3553 (tmp-name2 (tramp--test-make-temp-name nil quoted)))
3555 (unwind-protect
3556 (progn
3557 ;; Use default `auto-save-file-name-transforms' mechanism.
3558 (let (tramp-auto-save-directory)
3559 (with-temp-buffer
3560 (setq buffer-file-name tmp-name1)
3561 (should
3562 (string-equal
3563 (make-auto-save-file-name)
3564 ;; This is taken from original `make-auto-save-file-name'.
3565 ;; We call `convert-standard-filename', because on
3566 ;; MS Windows the (local) colons must be replaced by
3567 ;; exclamation marks.
3568 (convert-standard-filename
3569 (expand-file-name
3570 (format
3571 "#%s#"
3572 (subst-char-in-string
3573 ?/ ?! (replace-regexp-in-string "!" "!!" tmp-name1)))
3574 temporary-file-directory))))))
3576 ;; No mapping.
3577 (let (tramp-auto-save-directory auto-save-file-name-transforms)
3578 (with-temp-buffer
3579 (setq buffer-file-name tmp-name1)
3580 (should
3581 (string-equal
3582 (make-auto-save-file-name)
3583 (funcall
3584 (if quoted 'tramp-compat-file-name-quote 'identity)
3585 (expand-file-name
3586 (format "#%s#" (file-name-nondirectory tmp-name1))
3587 tramp-test-temporary-file-directory))))))
3589 ;; TODO: The following two cases don't work yet.
3590 (when nil
3591 ;; Use default `tramp-auto-save-directory' mechanism.
3592 (let ((tramp-auto-save-directory tmp-name2))
3593 (with-temp-buffer
3594 (setq buffer-file-name tmp-name1)
3595 (should
3596 (string-equal
3597 (make-auto-save-file-name)
3598 ;; This is taken from Tramp.
3599 (expand-file-name
3600 (format
3601 "#%s#"
3602 (tramp-subst-strs-in-string
3603 '(("_" . "|")
3604 ("/" . "_a")
3605 (":" . "_b")
3606 ("|" . "__")
3607 ("[" . "_l")
3608 ("]" . "_r"))
3609 (tramp-compat-file-name-unquote tmp-name1)))
3610 tmp-name2)))
3611 (should (file-directory-p tmp-name2))))
3613 ;; Relative file names shall work, too.
3614 (let ((tramp-auto-save-directory "."))
3615 (with-temp-buffer
3616 (setq buffer-file-name tmp-name1
3617 default-directory tmp-name2)
3618 (should
3619 (string-equal
3620 (make-auto-save-file-name)
3621 ;; This is taken from Tramp.
3622 (expand-file-name
3623 (format
3624 "#%s#"
3625 (tramp-subst-strs-in-string
3626 '(("_" . "|")
3627 ("/" . "_a")
3628 (":" . "_b")
3629 ("|" . "__")
3630 ("[" . "_l")
3631 ("]" . "_r"))
3632 (tramp-compat-file-name-unquote tmp-name1)))
3633 tmp-name2)))
3634 (should (file-directory-p tmp-name2)))))
3635 ) ;; TODO
3637 ;; Cleanup.
3638 (ignore-errors (delete-file tmp-name1))
3639 (ignore-errors (delete-directory tmp-name2 'recursive))))))
3641 ;; The functions were introduced in Emacs 26.1.
3642 (ert-deftest tramp-test34-make-nearby-temp-file ()
3643 "Check `make-nearby-temp-file' and `temporary-file-directory'."
3644 (skip-unless (tramp--test-enabled))
3645 ;; Since Emacs 26.1.
3646 (skip-unless
3647 (and (fboundp 'make-nearby-temp-file) (fboundp 'temporary-file-directory)))
3649 ;; `make-nearby-temp-file' and `temporary-file-directory' exists
3650 ;; since Emacs 26. We don't want to see compiler warnings for older
3651 ;; Emacsen.
3652 (let ((default-directory tramp-test-temporary-file-directory)
3653 tmp-file)
3654 ;; The remote host shall know a temporary file directory.
3655 (should (stringp (with-no-warnings (temporary-file-directory))))
3656 (should
3657 (string-equal
3658 (file-remote-p default-directory)
3659 (file-remote-p (with-no-warnings (temporary-file-directory)))))
3661 ;; The temporary file shall be located on the remote host.
3662 (setq tmp-file (with-no-warnings (make-nearby-temp-file "tramp-test")))
3663 (should (file-exists-p tmp-file))
3664 (should (file-regular-p tmp-file))
3665 (should
3666 (string-equal
3667 (file-remote-p default-directory)
3668 (file-remote-p tmp-file)))
3669 (delete-file tmp-file)
3670 (should-not (file-exists-p tmp-file))
3672 (setq tmp-file (with-no-warnings (make-nearby-temp-file "tramp-test" 'dir)))
3673 (should (file-exists-p tmp-file))
3674 (should (file-directory-p tmp-file))
3675 (delete-directory tmp-file)
3676 (should-not (file-exists-p tmp-file))))
3678 (defun tramp--test-emacs26-p ()
3679 "Check for Emacs version >= 26.1.
3680 Some semantics has been changed for there, w/o new functions or
3681 variables, so we check function Emacs version directly."
3682 (>= emacs-major-version 26))
3684 (defun tramp--test-adb-p ()
3685 "Check, whether the remote host runs Android.
3686 This requires restrictions of file name syntax."
3687 (tramp-adb-file-name-p tramp-test-temporary-file-directory))
3689 (defun tramp--test-docker-p ()
3690 "Check, whether the docker method is used.
3691 This does not support some special file names."
3692 (string-equal
3693 "docker" (file-remote-p tramp-test-temporary-file-directory 'method)))
3695 (defun tramp--test-ftp-p ()
3696 "Check, whether an FTP-like method is used.
3697 This does not support globbing characters in file names (yet)."
3698 ;; Globbing characters are ??, ?* and ?\[.
3699 (string-match
3700 "ftp$" (file-remote-p tramp-test-temporary-file-directory 'method)))
3702 (defun tramp--test-gvfs-p (&optional method)
3703 "Check, whether the remote host runs a GVFS based method.
3704 This requires restrictions of file name syntax."
3705 (or (member method tramp-gvfs-methods)
3706 (tramp-gvfs-file-name-p tramp-test-temporary-file-directory)))
3708 (defun tramp--test-hpux-p ()
3709 "Check, whether the remote host runs HP-UX.
3710 Several special characters do not work properly there."
3711 ;; We must refill the cache. `file-truename' does it.
3712 (with-parsed-tramp-file-name
3713 (file-truename tramp-test-temporary-file-directory) nil
3714 (string-match "^HP-UX" (tramp-get-connection-property v "uname" ""))))
3716 (defun tramp--test-rsync-p ()
3717 "Check, whether the rsync method is used.
3718 This does not support special file names."
3719 (string-equal
3720 "rsync" (file-remote-p tramp-test-temporary-file-directory 'method)))
3722 (defun tramp--test-sh-p ()
3723 "Check, whether the remote host runs a based method from tramp-sh.el."
3725 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory)
3726 'tramp-sh-file-name-handler))
3728 (defun tramp--test-windows-nt-and-batch ()
3729 "Check, whether the locale host runs MS Windows in batch mode.
3730 This does not support special characters."
3731 (and (eq system-type 'windows-nt) noninteractive))
3733 (defun tramp--test-windows-nt-and-pscp-psftp-p ()
3734 "Check, whether the locale host runs MS Windows, and ps{cp,ftp} is used.
3735 This does not support utf8 based file transfer."
3736 (and (eq system-type 'windows-nt)
3737 (string-match
3738 (regexp-opt '("pscp" "psftp"))
3739 (file-remote-p tramp-test-temporary-file-directory 'method))))
3741 (defun tramp--test-windows-nt-or-smb-p ()
3742 "Check, whether the locale or remote host runs MS Windows.
3743 This requires restrictions of file name syntax."
3744 (or (eq system-type 'windows-nt)
3745 (tramp-smb-file-name-p tramp-test-temporary-file-directory)))
3747 (defun tramp--test-check-files (&rest files)
3748 "Run a simple but comprehensive test over every file in FILES."
3749 ;; TODO: The quoted case does not work.
3750 ;;(dolist (quoted (if tramp--test-expensive-test '(nil t) '(nil)))
3751 (let (quoted)
3752 ;; We must use `file-truename' for the temporary directory,
3753 ;; because it could be located on a symlinked directory. This
3754 ;; would let the test fail.
3755 (let* ((tramp-test-temporary-file-directory
3756 (file-truename tramp-test-temporary-file-directory))
3757 (tmp-name1 (tramp--test-make-temp-name nil quoted))
3758 (tmp-name2 (tramp--test-make-temp-name 'local quoted))
3759 (files (delq nil files))
3760 (process-environment process-environment))
3761 (unwind-protect
3762 (progn
3763 (make-directory tmp-name1)
3764 (make-directory tmp-name2)
3766 (dolist (elt files)
3767 (let* ((file1 (expand-file-name elt tmp-name1))
3768 (file2 (expand-file-name elt tmp-name2))
3769 (file3 (expand-file-name (concat elt "foo") tmp-name1)))
3770 (write-region elt nil file1)
3771 (should (file-exists-p file1))
3773 ;; Check file contents.
3774 (with-temp-buffer
3775 (insert-file-contents file1)
3776 (should (string-equal (buffer-string) elt)))
3778 ;; Copy file both directions.
3779 (copy-file file1 (file-name-as-directory tmp-name2))
3780 (should (file-exists-p file2))
3781 (delete-file file1)
3782 (should-not (file-exists-p file1))
3783 (copy-file file2 (file-name-as-directory tmp-name1))
3784 (should (file-exists-p file1))
3786 (tramp--test-ignore-make-symbolic-link-error
3787 (make-symbolic-link file1 file3)
3788 (should (file-symlink-p file3))
3789 (should
3790 (string-equal
3791 (expand-file-name file1) (file-truename file3)))
3792 (should
3793 (string-equal
3794 (funcall
3795 (if quoted 'tramp-compat-file-name-quote 'identity)
3796 (car (file-attributes file3)))
3797 (file-remote-p (file-truename file1) 'localname)))
3798 ;; Check file contents.
3799 (with-temp-buffer
3800 (insert-file-contents file3)
3801 (should (string-equal (buffer-string) elt)))
3802 (delete-file file3))))
3804 ;; Check file names.
3805 (should (equal (directory-files
3806 tmp-name1 nil directory-files-no-dot-files-regexp)
3807 (sort (copy-sequence files) 'string-lessp)))
3808 (should (equal (directory-files
3809 tmp-name2 nil directory-files-no-dot-files-regexp)
3810 (sort (copy-sequence files) 'string-lessp)))
3812 ;; `substitute-in-file-name' could return different
3813 ;; values. For `adb', there could be strange file
3814 ;; permissions preventing overwriting a file. We don't
3815 ;; care in this testcase.
3816 (dolist (elt files)
3817 (let ((file1
3818 (substitute-in-file-name (expand-file-name elt tmp-name1)))
3819 (file2
3820 (substitute-in-file-name
3821 (expand-file-name elt tmp-name2))))
3822 (ignore-errors (write-region elt nil file1))
3823 (should (file-exists-p file1))
3824 (ignore-errors (write-region elt nil file2 nil 'nomessage))
3825 (should (file-exists-p file2))))
3827 (should (equal (directory-files
3828 tmp-name1 nil directory-files-no-dot-files-regexp)
3829 (directory-files
3830 tmp-name2 nil directory-files-no-dot-files-regexp)))
3832 ;; Check directory creation. We use a subdirectory "foo"
3833 ;; in order to avoid conflicts with previous file name tests.
3834 (dolist (elt files)
3835 (let* ((elt1 (concat elt "foo"))
3836 (file1 (expand-file-name (concat "foo/" elt) tmp-name1))
3837 (file2 (expand-file-name elt file1))
3838 (file3 (expand-file-name elt1 file1)))
3839 (make-directory file1 'parents)
3840 (should (file-directory-p file1))
3841 (write-region elt nil file2)
3842 (should (file-exists-p file2))
3843 (should
3844 (equal
3845 (directory-files
3846 file1 nil directory-files-no-dot-files-regexp)
3847 `(,elt)))
3848 (should
3849 (equal
3850 (caar (directory-files-and-attributes
3851 file1 nil directory-files-no-dot-files-regexp))
3852 elt))
3854 ;; Check symlink in `directory-files-and-attributes'.
3855 ;; It does not work in the "smb" case, only relative
3856 ;; symlinks to existing files are shown there.
3857 (tramp--test-ignore-make-symbolic-link-error
3858 (unless
3859 (tramp-smb-file-name-p tramp-test-temporary-file-directory)
3860 (make-symbolic-link file2 file3)
3861 (should (file-symlink-p file3))
3862 (should
3863 (string-equal
3864 (caar (directory-files-and-attributes
3865 file1 nil (regexp-quote elt1)))
3866 elt1))
3867 (should
3868 (string-equal
3869 (funcall
3870 (if quoted 'tramp-compat-file-name-quote 'identity)
3871 (cadr (car (directory-files-and-attributes
3872 file1 nil (regexp-quote elt1)))))
3873 (file-remote-p (file-truename file2) 'localname)))
3874 (delete-file file3)
3875 (should-not (file-exists-p file3))))
3877 (delete-file file2)
3878 (should-not (file-exists-p file2))
3879 (delete-directory file1)
3880 (should-not (file-exists-p file1))))
3882 ;; Check, that environment variables are set correctly.
3883 (when (and tramp--test-expensive-test (tramp--test-sh-p))
3884 (dolist (elt files)
3885 (let ((envvar (concat "VAR_" (upcase (md5 elt))))
3886 (default-directory tramp-test-temporary-file-directory)
3887 (process-environment process-environment))
3888 (setenv envvar elt)
3889 ;; The value of PS1 could confuse Tramp's detection
3890 ;; of process output. So we unset it temporarily.
3891 (setenv "PS1")
3892 (with-temp-buffer
3893 (should (zerop (process-file "env" nil t nil)))
3894 (goto-char (point-min))
3895 (should
3896 (re-search-forward
3897 (format
3898 "^%s=%s$"
3899 (regexp-quote envvar)
3900 (regexp-quote (getenv envvar))))))))))
3902 ;; Cleanup.
3903 (ignore-errors (delete-directory tmp-name1 'recursive))
3904 (ignore-errors (delete-directory tmp-name2 'recursive))))))
3906 (defun tramp--test-special-characters ()
3907 "Perform the test in `tramp-test35-special-characters*'."
3908 ;; Newlines, slashes and backslashes in file names are not
3909 ;; supported. So we don't test. And we don't test the tab
3910 ;; character on Windows or Cygwin, because the backslash is
3911 ;; interpreted as a path separator, preventing "\t" from being
3912 ;; expanded to <TAB>.
3913 (tramp--test-check-files
3914 (if (or (tramp--test-gvfs-p) (tramp--test-windows-nt-or-smb-p))
3915 "foo bar baz"
3916 (if (or (tramp--test-adb-p)
3917 (tramp--test-docker-p)
3918 (eq system-type 'cygwin))
3919 " foo bar baz "
3920 " foo\tbar baz\t"))
3921 "$foo$bar$$baz$"
3922 "-foo-bar-baz-"
3923 "%foo%bar%baz%"
3924 "&foo&bar&baz&"
3925 (unless (or (tramp--test-ftp-p)
3926 (tramp--test-gvfs-p)
3927 (tramp--test-windows-nt-or-smb-p))
3928 "?foo?bar?baz?")
3929 (unless (or (tramp--test-ftp-p)
3930 (tramp--test-gvfs-p)
3931 (tramp--test-windows-nt-or-smb-p))
3932 "*foo*bar*baz*")
3933 (if (or (tramp--test-gvfs-p) (tramp--test-windows-nt-or-smb-p))
3934 "'foo'bar'baz'"
3935 "'foo\"bar'baz\"")
3936 "#foo~bar#baz~"
3937 (if (or (tramp--test-gvfs-p) (tramp--test-windows-nt-or-smb-p))
3938 "!foo!bar!baz!"
3939 "!foo|bar!baz|")
3940 (if (or (tramp--test-gvfs-p) (tramp--test-windows-nt-or-smb-p))
3941 ";foo;bar;baz;"
3942 ":foo;bar:baz;")
3943 (unless (or (tramp--test-gvfs-p) (tramp--test-windows-nt-or-smb-p))
3944 "<foo>bar<baz>")
3945 "(foo)bar(baz)"
3946 (unless (or (tramp--test-ftp-p) (tramp--test-gvfs-p)) "[foo]bar[baz]")
3947 "{foo}bar{baz}"))
3949 ;; These tests are inspired by Bug#17238.
3950 (ert-deftest tramp-test35-special-characters ()
3951 "Check special characters in file names."
3952 (skip-unless (tramp--test-enabled))
3953 (skip-unless (not (tramp--test-rsync-p)))
3954 (skip-unless (not (tramp--test-windows-nt-and-pscp-psftp-p)))
3956 (tramp--test-special-characters))
3958 (ert-deftest tramp-test35-special-characters-with-stat ()
3959 "Check special characters in file names.
3960 Use the `stat' command."
3961 :tags '(:expensive-test)
3962 (skip-unless (tramp--test-enabled))
3963 (skip-unless (tramp--test-sh-p))
3964 (skip-unless (not (tramp--test-rsync-p)))
3965 (skip-unless (not (tramp--test-windows-nt-and-pscp-psftp-p)))
3966 (with-parsed-tramp-file-name tramp-test-temporary-file-directory nil
3967 (skip-unless (tramp-get-remote-stat v)))
3969 (let ((tramp-connection-properties
3970 (append
3971 `((,(regexp-quote (file-remote-p tramp-test-temporary-file-directory))
3972 "perl" nil))
3973 tramp-connection-properties)))
3974 (tramp--test-special-characters)))
3976 (ert-deftest tramp-test35-special-characters-with-perl ()
3977 "Check special characters in file names.
3978 Use the `perl' command."
3979 :tags '(:expensive-test)
3980 (skip-unless (tramp--test-enabled))
3981 (skip-unless (tramp--test-sh-p))
3982 (skip-unless (not (tramp--test-rsync-p)))
3983 (skip-unless (not (tramp--test-windows-nt-and-pscp-psftp-p)))
3984 (with-parsed-tramp-file-name tramp-test-temporary-file-directory nil
3985 (skip-unless (tramp-get-remote-perl v)))
3987 (let ((tramp-connection-properties
3988 (append
3989 `((,(regexp-quote (file-remote-p tramp-test-temporary-file-directory))
3990 "stat" nil)
3991 ;; See `tramp-sh-handle-file-truename'.
3992 (,(regexp-quote (file-remote-p tramp-test-temporary-file-directory))
3993 "readlink" nil))
3994 tramp-connection-properties)))
3995 (tramp--test-special-characters)))
3997 (ert-deftest tramp-test35-special-characters-with-ls ()
3998 "Check special characters in file names.
3999 Use the `ls' command."
4000 :tags '(:expensive-test)
4001 (skip-unless (tramp--test-enabled))
4002 (skip-unless (tramp--test-sh-p))
4003 (skip-unless (not (tramp--test-rsync-p)))
4004 (skip-unless (not (tramp--test-windows-nt-and-batch)))
4005 (skip-unless (not (tramp--test-windows-nt-and-pscp-psftp-p)))
4007 (let ((tramp-connection-properties
4008 (append
4009 `((,(regexp-quote (file-remote-p tramp-test-temporary-file-directory))
4010 "perl" nil)
4011 (,(regexp-quote (file-remote-p tramp-test-temporary-file-directory))
4012 "stat" nil)
4013 ;; See `tramp-sh-handle-file-truename'.
4014 (,(regexp-quote (file-remote-p tramp-test-temporary-file-directory))
4015 "readlink" nil))
4016 tramp-connection-properties)))
4017 (tramp--test-special-characters)))
4019 (defun tramp--test-utf8 ()
4020 "Perform the test in `tramp-test36-utf8*'."
4021 (let* ((utf8 (if (and (eq system-type 'darwin)
4022 (memq 'utf-8-hfs (coding-system-list)))
4023 'utf-8-hfs 'utf-8))
4024 (coding-system-for-read utf8)
4025 (coding-system-for-write utf8)
4026 (file-name-coding-system
4027 (coding-system-change-eol-conversion utf8 'unix)))
4028 (tramp--test-check-files
4029 (unless (tramp--test-hpux-p) "Γυρίστε το Γαλαξία με Ώτο Στοπ")
4030 (unless (tramp--test-hpux-p)
4031 "أصبح بوسعك الآن تنزيل نسخة كاملة من موسوعة ويكيبيديا العربية لتصفحها بلا اتصال بالإنترنت")
4032 "银河系漫游指南系列"
4033 "Автостопом по гала́ктике")))
4035 (ert-deftest tramp-test36-utf8 ()
4036 "Check UTF8 encoding in file names and file contents."
4037 (skip-unless (tramp--test-enabled))
4038 (skip-unless (not (tramp--test-docker-p)))
4039 (skip-unless (not (tramp--test-rsync-p)))
4040 (skip-unless (not (tramp--test-windows-nt-and-batch)))
4041 (skip-unless (not (tramp--test-windows-nt-and-pscp-psftp-p)))
4043 (tramp--test-utf8))
4045 (ert-deftest tramp-test36-utf8-with-stat ()
4046 "Check UTF8 encoding in file names and file contents.
4047 Use the `stat' command."
4048 :tags '(:expensive-test)
4049 (skip-unless (tramp--test-enabled))
4050 (skip-unless (tramp--test-sh-p))
4051 (skip-unless (not (tramp--test-docker-p)))
4052 (skip-unless (not (tramp--test-rsync-p)))
4053 (skip-unless (not (tramp--test-windows-nt-and-batch)))
4054 (skip-unless (not (tramp--test-windows-nt-and-pscp-psftp-p)))
4055 (with-parsed-tramp-file-name tramp-test-temporary-file-directory nil
4056 (skip-unless (tramp-get-remote-stat v)))
4058 (let ((tramp-connection-properties
4059 (append
4060 `((,(regexp-quote (file-remote-p tramp-test-temporary-file-directory))
4061 "perl" nil))
4062 tramp-connection-properties)))
4063 (tramp--test-utf8)))
4065 (ert-deftest tramp-test36-utf8-with-perl ()
4066 "Check UTF8 encoding in file names and file contents.
4067 Use the `perl' command."
4068 :tags '(:expensive-test)
4069 (skip-unless (tramp--test-enabled))
4070 (skip-unless (tramp--test-sh-p))
4071 (skip-unless (not (tramp--test-docker-p)))
4072 (skip-unless (not (tramp--test-rsync-p)))
4073 (skip-unless (not (tramp--test-windows-nt-and-batch)))
4074 (skip-unless (not (tramp--test-windows-nt-and-pscp-psftp-p)))
4075 (with-parsed-tramp-file-name tramp-test-temporary-file-directory nil
4076 (skip-unless (tramp-get-remote-perl v)))
4078 (let ((tramp-connection-properties
4079 (append
4080 `((,(regexp-quote (file-remote-p tramp-test-temporary-file-directory))
4081 "stat" nil)
4082 ;; See `tramp-sh-handle-file-truename'.
4083 (,(regexp-quote (file-remote-p tramp-test-temporary-file-directory))
4084 "readlink" nil))
4085 tramp-connection-properties)))
4086 (tramp--test-utf8)))
4088 (ert-deftest tramp-test36-utf8-with-ls ()
4089 "Check UTF8 encoding in file names and file contents.
4090 Use the `ls' command."
4091 :tags '(:expensive-test)
4092 (skip-unless (tramp--test-enabled))
4093 (skip-unless (tramp--test-sh-p))
4094 (skip-unless (not (tramp--test-docker-p)))
4095 (skip-unless (not (tramp--test-rsync-p)))
4096 (skip-unless (not (tramp--test-windows-nt-and-batch)))
4097 (skip-unless (not (tramp--test-windows-nt-and-pscp-psftp-p)))
4099 (let ((tramp-connection-properties
4100 (append
4101 `((,(regexp-quote (file-remote-p tramp-test-temporary-file-directory))
4102 "perl" nil)
4103 (,(regexp-quote (file-remote-p tramp-test-temporary-file-directory))
4104 "stat" nil)
4105 ;; See `tramp-sh-handle-file-truename'.
4106 (,(regexp-quote (file-remote-p tramp-test-temporary-file-directory))
4107 "readlink" nil))
4108 tramp-connection-properties)))
4109 (tramp--test-utf8)))
4111 (defun tramp--test-timeout-handler ()
4112 (interactive)
4113 (ert-fail (format "`%s' timed out" (ert-test-name (ert-running-test)))))
4115 ;; This test is inspired by Bug#16928.
4116 (ert-deftest tramp-test37-asynchronous-requests ()
4117 "Check parallel asynchronous requests.
4118 Such requests could arrive from timers, process filters and
4119 process sentinels. They shall not disturb each other."
4120 :tags '(:expensive-test)
4121 (skip-unless (tramp--test-enabled))
4122 (skip-unless (tramp--test-sh-p))
4124 ;; This test could be blocked on hydra. So we set a timeout of 300
4125 ;; seconds, and we send a SIGUSR1 signal after 300 seconds.
4126 (with-timeout (300 (tramp--test-timeout-handler))
4127 (define-key special-event-map [sigusr1] 'tramp--test-timeout-handler)
4128 (tramp--test-instrument-test-case (if (getenv "EMACS_HYDRA_CI") 10 0)
4129 (let* (;; For the watchdog.
4130 (default-directory (expand-file-name temporary-file-directory))
4131 (watchdog
4132 (start-process
4133 "*watchdog*" nil shell-file-name shell-command-switch
4134 (format "sleep 300; kill -USR1 %d" (emacs-pid))))
4135 (tmp-name (tramp--test-make-temp-name))
4136 (default-directory tmp-name)
4137 ;; Do not cache Tramp properties.
4138 (remote-file-name-inhibit-cache t)
4139 (process-file-side-effects t)
4140 ;; Suppress nasty messages.
4141 (inhibit-message t)
4142 ;; Do not run delayed timers.
4143 (timer-max-repeats 0)
4144 ;; Number of asynchronous processes for test.
4145 (number-proc 10)
4146 ;; On hydra, timings are bad.
4147 (timer-repeat
4148 (cond
4149 ((getenv "EMACS_HYDRA_CI") 10)
4150 (t 1)))
4151 ;; We must distinguish due to performance reasons.
4152 (timer-operation
4153 (cond
4154 ((string-equal "mock" (file-remote-p tmp-name 'method))
4155 'vc-registered)
4156 (t 'file-attributes)))
4157 timer buffers kill-buffer-query-functions)
4159 (unwind-protect
4160 (progn
4161 (make-directory tmp-name)
4163 ;; Setup a timer in order to raise an ordinary command
4164 ;; again and again. `vc-registered' is well suited,
4165 ;; because there are many checks.
4166 (setq
4167 timer
4168 (run-at-time
4169 0 timer-repeat
4170 (lambda ()
4171 (when buffers
4172 (let ((time (float-time))
4173 (default-directory tmp-name)
4174 (file
4175 (buffer-name (nth (random (length buffers)) buffers))))
4176 (tramp--test-message
4177 "Start timer %s %s" file (current-time-string))
4178 (funcall timer-operation file)
4179 ;; Adjust timer if it takes too much time.
4180 (when (> (- (float-time) time) timer-repeat)
4181 (setq timer-repeat (* 1.5 timer-repeat))
4182 (setf (timer--repeat-delay timer) timer-repeat)
4183 (tramp--test-message "Increase timer %s" timer-repeat))
4184 (tramp--test-message
4185 "Stop timer %s %s" file (current-time-string)))))))
4187 ;; Create temporary buffers. The number of buffers
4188 ;; corresponds to the number of processes; it could be
4189 ;; increased in order to make pressure on Tramp.
4190 (dotimes (_ number-proc)
4191 (setq buffers (cons (generate-new-buffer "foo") buffers)))
4193 ;; Open asynchronous processes. Set process filter and sentinel.
4194 (dolist (buf buffers)
4195 ;; Activate timer.
4196 (sit-for 0.01 'nodisp)
4197 (let ((proc
4198 (start-file-process-shell-command
4199 (buffer-name buf) buf
4200 (concat
4201 "(read line && echo $line >$line);"
4202 "(read line && cat $line);"
4203 "(read line && rm $line)")))
4204 (file (expand-file-name (buffer-name buf))))
4205 ;; Remember the file name. Add counter.
4206 (process-put proc 'foo file)
4207 (process-put proc 'bar 0)
4208 ;; Add process filter.
4209 (set-process-filter
4210 proc
4211 (lambda (proc string)
4212 (with-current-buffer (process-buffer proc)
4213 (insert string))
4214 (unless (zerop (length string))
4215 (should (file-attributes (process-get proc 'foo))))))
4216 ;; Add process sentinel.
4217 (set-process-sentinel
4218 proc
4219 (lambda (proc _state)
4220 (should-not (file-attributes (process-get proc 'foo)))))))
4222 ;; Send a string. Use a random order of the buffers. Mix
4223 ;; with regular operation.
4224 (let ((buffers (copy-sequence buffers)))
4225 (while buffers
4226 ;; Activate timer.
4227 (sit-for 0.01 'nodisp)
4228 (let* ((buf (nth (random (length buffers)) buffers))
4229 (proc (get-buffer-process buf))
4230 (file (process-get proc 'foo))
4231 (count (process-get proc 'bar)))
4232 (tramp--test-message
4233 "Start action %d %s %s" count buf (current-time-string))
4234 ;; Regular operation prior process action.
4235 (if (= count 0)
4236 (should-not (file-attributes file))
4237 (should (file-attributes file)))
4238 ;; Send string to process.
4239 (process-send-string proc (format "%s\n" (buffer-name buf)))
4240 (accept-process-output proc 0.1 nil 0)
4241 ;; Give the watchdog a chance.
4242 (read-event nil nil 0.01)
4243 ;; Regular operation post process action.
4244 (if (= count 2)
4245 (should-not (file-attributes file))
4246 (should (file-attributes file)))
4247 (tramp--test-message
4248 "Stop action %d %s %s" count buf (current-time-string))
4249 (process-put proc 'bar (1+ count))
4250 (unless (process-live-p proc)
4251 (setq buffers (delq buf buffers))))))
4253 ;; Checks. All process output shall exists in the
4254 ;; respective buffers. All created files shall be
4255 ;; deleted.
4256 (tramp--test-message "Check %s" (current-time-string))
4257 (dolist (buf buffers)
4258 (with-current-buffer buf
4259 (should (string-equal (format "%s\n" buf) (buffer-string)))))
4260 (should-not
4261 (directory-files
4262 tmp-name nil directory-files-no-dot-files-regexp)))
4264 ;; Cleanup.
4265 (define-key special-event-map [sigusr1] 'ignore)
4266 (ignore-errors (quit-process watchdog))
4267 (dolist (buf buffers)
4268 (ignore-errors (delete-process (get-buffer-process buf)))
4269 (ignore-errors (kill-buffer buf)))
4270 (ignore-errors (cancel-timer timer))
4271 (ignore-errors (delete-directory tmp-name 'recursive)))))))
4273 (ert-deftest tramp-test38-recursive-load ()
4274 "Check that Tramp does not fail due to recursive load."
4275 (skip-unless (tramp--test-enabled))
4277 (let ((default-directory (expand-file-name temporary-file-directory)))
4278 (dolist (code
4279 (list
4280 (format
4281 "(expand-file-name %S)" tramp-test-temporary-file-directory)
4282 (format
4283 "(let ((default-directory %S)) (expand-file-name %S))"
4284 tramp-test-temporary-file-directory
4285 temporary-file-directory)))
4286 (should-not
4287 (string-match
4288 "Recursive load"
4289 (shell-command-to-string
4290 (format
4291 "%s -batch -Q -L %s --eval %s"
4292 (expand-file-name invocation-name invocation-directory)
4293 (mapconcat 'shell-quote-argument load-path " -L ")
4294 (shell-quote-argument code))))))))
4296 (ert-deftest tramp-test39-remote-load-path ()
4297 "Check that Tramp autoloads its packages with remote `load-path'."
4298 ;; `tramp-cleanup-all-connections' is autoloaded from tramp-cmds.el.
4299 ;; It shall still work, when a remote file name is in the
4300 ;; `load-path'.
4301 (let ((default-directory (expand-file-name temporary-file-directory))
4302 (code
4303 "(let ((force-load-messages t) \
4304 (load-path (cons \"/foo:bar:\" load-path))) \
4305 (tramp-cleanup-all-connections))"))
4306 (should
4307 (string-match
4308 (format
4309 "Loading %s"
4310 (expand-file-name
4311 "tramp-cmds" (file-name-directory (locate-library "tramp"))))
4312 (shell-command-to-string
4313 (format
4314 "%s -batch -Q -L %s -l tramp-sh --eval %s"
4315 (expand-file-name invocation-name invocation-directory)
4316 (mapconcat 'shell-quote-argument load-path " -L ")
4317 (shell-quote-argument code)))))))
4319 (ert-deftest tramp-test40-unload ()
4320 "Check that Tramp and its subpackages unload completely.
4321 Since it unloads Tramp, it shall be the last test to run."
4322 :tags '(:expensive-test)
4323 (skip-unless noninteractive)
4325 (when (featurep 'tramp)
4326 (unload-feature 'tramp 'force)
4327 ;; No Tramp feature must be left.
4328 (should-not (featurep 'tramp))
4329 (should-not (all-completions "tramp" (delq 'tramp-tests features)))
4330 ;; `file-name-handler-alist' must be clean.
4331 (should-not (all-completions "tramp" (mapcar 'cdr file-name-handler-alist)))
4332 ;; There shouldn't be left a bound symbol, except buffer-local
4333 ;; variables, and autoload functions. We do not regard our test
4334 ;; symbols, and the Tramp unload hooks.
4335 (mapatoms
4336 (lambda (x)
4337 (and (or (and (boundp x) (null (local-variable-if-set-p x)))
4338 (and (functionp x) (null (autoloadp (symbol-function x)))))
4339 (string-match "^tramp" (symbol-name x))
4340 (not (string-match "^tramp--?test" (symbol-name x)))
4341 (not (string-match "unload-hook$" (symbol-name x)))
4342 (ert-fail (format "`%s' still bound" x)))))
4343 ;; The defstruct `tramp-file-name' and all its internal functions
4344 ;; shall be purged.
4345 (should-not (cl--find-class 'tramp-file-name))
4346 (mapatoms
4347 (lambda (x)
4348 (and (functionp x)
4349 (string-match "tramp-file-name" (symbol-name x))
4350 (ert-fail (format "Structure function `%s' still exists" x)))))
4351 ;; There shouldn't be left a hook function containing a Tramp
4352 ;; function. We do not regard the Tramp unload hooks.
4353 (mapatoms
4354 (lambda (x)
4355 (and (boundp x)
4356 (string-match "-\\(hook\\|function\\)s?$" (symbol-name x))
4357 (not (string-match "unload-hook$" (symbol-name x)))
4358 (consp (symbol-value x))
4359 (ignore-errors (all-completions "tramp" (symbol-value x)))
4360 (ert-fail (format "Hook `%s' still contains Tramp function" x)))))))
4362 ;; TODO:
4364 ;; * dired-compress-file
4365 ;; * dired-uncache
4366 ;; * file-acl
4367 ;; * file-name-case-insensitive-p
4368 ;; * file-selinux-context
4369 ;; * find-backup-file-name
4370 ;; * set-file-acl
4371 ;; * set-file-selinux-context
4373 ;; * Work on skipped tests. Make a comment, when it is impossible.
4374 ;; * Fix `tramp-test05-expand-file-name-relative' in `expand-file-name'.
4375 ;; * Fix `tramp-test06-directory-file-name' for `ftp'.
4376 ;; * Fix `tramp-test27-start-file-process' on MS Windows (`process-send-eof'?).
4377 ;; * Fix Bug#16928 in `tramp-test37-asynchronous-requests'.
4379 (defun tramp-test-all (&optional interactive)
4380 "Run all tests for \\[tramp]."
4381 (interactive "p")
4382 (funcall
4383 (if interactive 'ert-run-tests-interactively 'ert-run-tests-batch) "^tramp"))
4385 (provide 'tramp-tests)
4386 ;;; tramp-tests.el ends here