Do not use map.el in seq-tests.el
[emacs.git] / test / lisp / net / tramp-tests.el
blob4c3b83cdcd1a9a3266688e1d8825987e458ca655
1 ;;; tramp-tests.el --- Tests of remote file access
3 ;; Copyright (C) 2013-2016 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 `http://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 'ert)
41 (require 'tramp)
42 (require 'vc)
43 (require 'vc-bzr)
44 (require 'vc-git)
45 (require 'vc-hg)
47 (autoload 'dired-uncache "dired")
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 tramp-copy-size-limit)
53 (defvar tramp-persistency-file-name)
54 (defvar tramp-remote-process-environment)
56 ;; There is no default value on w32 systems, which could work out of the box.
57 (defconst tramp-test-temporary-file-directory
58 (cond
59 ((getenv "REMOTE_TEMPORARY_FILE_DIRECTORY"))
60 ((eq system-type 'windows-nt) null-device)
61 (t (add-to-list
62 'tramp-methods
63 '("mock"
64 (tramp-login-program "sh")
65 (tramp-login-args (("-i")))
66 (tramp-remote-shell "/bin/sh")
67 (tramp-remote-shell-args ("-c"))
68 (tramp-connection-timeout 10)))
69 (format "/mock::%s" temporary-file-directory)))
70 "Temporary directory for Tramp tests.")
72 (setq password-cache-expiry nil
73 tramp-verbose 0
74 tramp-copy-size-limit nil
75 tramp-message-show-message nil
76 tramp-persistency-file-name nil)
78 ;; This shall happen on hydra only.
79 (when (getenv "NIX_STORE")
80 (add-to-list 'tramp-remote-path 'tramp-own-remote-path))
82 (defvar tramp--test-enabled-checked nil
83 "Cached result of `tramp--test-enabled'.
84 If the function did run, the value is a cons cell, the `cdr'
85 being the result.")
87 (defun tramp--test-enabled ()
88 "Whether remote file access is enabled."
89 (unless (consp tramp--test-enabled-checked)
90 (setq
91 tramp--test-enabled-checked
92 (cons
93 t (ignore-errors
94 (and
95 (file-remote-p tramp-test-temporary-file-directory)
96 (file-directory-p tramp-test-temporary-file-directory)
97 (file-writable-p tramp-test-temporary-file-directory))))))
99 (when (cdr tramp--test-enabled-checked)
100 ;; Cleanup connection.
101 (ignore-errors
102 (tramp-cleanup-connection
103 (tramp-dissect-file-name tramp-test-temporary-file-directory)
104 nil 'keep-password)))
106 ;; Return result.
107 (cdr tramp--test-enabled-checked))
109 (defun tramp--test-make-temp-name (&optional local)
110 "Create a temporary file name for test."
111 (expand-file-name
112 (make-temp-name "tramp-test")
113 (if local temporary-file-directory tramp-test-temporary-file-directory)))
115 (defmacro tramp--instrument-test-case (verbose &rest body)
116 "Run BODY with `tramp-verbose' equal VERBOSE.
117 Print the the content of the Tramp debug buffer, if BODY does not
118 eval properly in `should' or `should-not'. `should-error' is not
119 handled properly. BODY shall not contain a timeout."
120 (declare (indent 1) (debug (natnump body)))
121 `(let ((tramp-verbose ,verbose)
122 (tramp-debug-on-error t)
123 (debug-ignored-errors
124 (cons "^make-symbolic-link not supported$" debug-ignored-errors)))
125 (unwind-protect
126 (progn ,@body)
127 (when (> tramp-verbose 3)
128 (with-parsed-tramp-file-name tramp-test-temporary-file-directory nil
129 (with-current-buffer (tramp-get-connection-buffer v)
130 (message "%s" (buffer-string)))
131 (with-current-buffer (tramp-get-debug-buffer v)
132 (message "%s" (buffer-string))))))))
134 (ert-deftest tramp-test00-availability ()
135 "Test availability of Tramp functions."
136 :expected-result (if (tramp--test-enabled) :passed :failed)
137 (message "Remote directory: `%s'" tramp-test-temporary-file-directory)
138 (should (ignore-errors
139 (and
140 (file-remote-p tramp-test-temporary-file-directory)
141 (file-directory-p tramp-test-temporary-file-directory)
142 (file-writable-p tramp-test-temporary-file-directory)))))
144 (ert-deftest tramp-test01-file-name-syntax ()
145 "Check remote file name syntax."
146 ;; Simple cases.
147 (should (tramp-tramp-file-p "/method::"))
148 (should (tramp-tramp-file-p "/host:"))
149 (should (tramp-tramp-file-p "/user@:"))
150 (should (tramp-tramp-file-p "/user@host:"))
151 (should (tramp-tramp-file-p "/method:host:"))
152 (should (tramp-tramp-file-p "/method:user@:"))
153 (should (tramp-tramp-file-p "/method:user@host:"))
154 (should (tramp-tramp-file-p "/method:user@email@host:"))
156 ;; Using a port.
157 (should (tramp-tramp-file-p "/host#1234:"))
158 (should (tramp-tramp-file-p "/user@host#1234:"))
159 (should (tramp-tramp-file-p "/method:host#1234:"))
160 (should (tramp-tramp-file-p "/method:user@host#1234:"))
162 ;; Using an IPv4 address.
163 (should (tramp-tramp-file-p "/1.2.3.4:"))
164 (should (tramp-tramp-file-p "/user@1.2.3.4:"))
165 (should (tramp-tramp-file-p "/method:1.2.3.4:"))
166 (should (tramp-tramp-file-p "/method:user@1.2.3.4:"))
168 ;; Using an IPv6 address.
169 (should (tramp-tramp-file-p "/[]:"))
170 (should (tramp-tramp-file-p "/[::1]:"))
171 (should (tramp-tramp-file-p "/user@[::1]:"))
172 (should (tramp-tramp-file-p "/method:[::1]:"))
173 (should (tramp-tramp-file-p "/method:user@[::1]:"))
175 ;; Local file name part.
176 (should (tramp-tramp-file-p "/host:/:"))
177 (should (tramp-tramp-file-p "/method:::"))
178 (should (tramp-tramp-file-p "/method::/path/to/file"))
179 (should (tramp-tramp-file-p "/method::file"))
181 ;; Multihop.
182 (should (tramp-tramp-file-p "/method1:|method2::"))
183 (should (tramp-tramp-file-p "/method1:host1|host2:"))
184 (should (tramp-tramp-file-p "/method1:host1|method2:host2:"))
185 (should (tramp-tramp-file-p "/method1:user1@host1|method2:user2@host2:"))
186 (should (tramp-tramp-file-p
187 "/method1:user1@host1|method2:user2@host2|method3:user3@host3:"))
188 (should (tramp-tramp-file-p "/host1|host2:"))
189 (should (tramp-tramp-file-p "/user1@host1|user2@host2:"))
191 ;; No strings.
192 (should-not (tramp-tramp-file-p nil))
193 (should-not (tramp-tramp-file-p 'symbol))
194 ;; "/:" suppresses file name handlers.
195 (should-not (tramp-tramp-file-p "/::"))
196 (should-not (tramp-tramp-file-p "/:@:"))
197 (should-not (tramp-tramp-file-p "/:[]:"))
198 ;; Methods or hostnames shall be at least two characters on MS Windows.
199 (let ((system-type 'windows-nt))
200 (should-not (tramp-tramp-file-p "/c:/path/to/file"))
201 (should-not (tramp-tramp-file-p "/c::/path/to/file")))
202 (let ((system-type 'gnu/linux))
203 (should (tramp-tramp-file-p "/h:/path/to/file"))
204 (should (tramp-tramp-file-p "/m::/path/to/file"))))
206 (ert-deftest tramp-test02-file-name-dissect ()
207 "Check remote file name components."
208 (let ((tramp-default-method "default-method")
209 (tramp-default-user "default-user")
210 (tramp-default-host "default-host"))
211 ;; Expand `tramp-default-user' and `tramp-default-host'.
212 (should (string-equal
213 (file-remote-p "/method::")
214 (format "/%s:%s@%s:" "method" "default-user" "default-host")))
215 (should (string-equal (file-remote-p "/method::" 'method) "method"))
216 (should (string-equal (file-remote-p "/method::" 'user) "default-user"))
217 (should (string-equal (file-remote-p "/method::" 'host) "default-host"))
218 (should (string-equal (file-remote-p "/method::" 'localname) ""))
219 (should (string-equal (file-remote-p "/method::" 'hop) nil))
221 ;; Expand `tramp-default-method' and `tramp-default-user'.
222 (should (string-equal
223 (file-remote-p "/host:")
224 (format "/%s:%s@%s:" "default-method" "default-user" "host")))
225 (should (string-equal (file-remote-p "/host:" 'method) "default-method"))
226 (should (string-equal (file-remote-p "/host:" 'user) "default-user"))
227 (should (string-equal (file-remote-p "/host:" 'host) "host"))
228 (should (string-equal (file-remote-p "/host:" 'localname) ""))
229 (should (string-equal (file-remote-p "/host:" 'hop) nil))
231 ;; Expand `tramp-default-method' and `tramp-default-host'.
232 (should (string-equal
233 (file-remote-p "/user@:")
234 (format "/%s:%s@%s:" "default-method""user" "default-host")))
235 (should (string-equal (file-remote-p "/user@:" 'method) "default-method"))
236 (should (string-equal (file-remote-p "/user@:" 'user) "user"))
237 (should (string-equal (file-remote-p "/user@:" 'host) "default-host"))
238 (should (string-equal (file-remote-p "/user@:" 'localname) ""))
239 (should (string-equal (file-remote-p "/user@:" 'hop) nil))
241 ;; Expand `tramp-default-method'.
242 (should (string-equal
243 (file-remote-p "/user@host:")
244 (format "/%s:%s@%s:" "default-method" "user" "host")))
245 (should (string-equal
246 (file-remote-p "/user@host:" 'method) "default-method"))
247 (should (string-equal (file-remote-p "/user@host:" 'user) "user"))
248 (should (string-equal (file-remote-p "/user@host:" 'host) "host"))
249 (should (string-equal (file-remote-p "/user@host:" 'localname) ""))
250 (should (string-equal (file-remote-p "/user@host:" 'hop) nil))
252 ;; Expand `tramp-default-user'.
253 (should (string-equal
254 (file-remote-p "/method:host:")
255 (format "/%s:%s@%s:" "method" "default-user" "host")))
256 (should (string-equal (file-remote-p "/method:host:" 'method) "method"))
257 (should (string-equal (file-remote-p "/method:host:" 'user) "default-user"))
258 (should (string-equal (file-remote-p "/method:host:" 'host) "host"))
259 (should (string-equal (file-remote-p "/method:host:" 'localname) ""))
260 (should (string-equal (file-remote-p "/method:host:" 'hop) nil))
262 ;; Expand `tramp-default-host'.
263 (should (string-equal
264 (file-remote-p "/method:user@:")
265 (format "/%s:%s@%s:" "method" "user" "default-host")))
266 (should (string-equal (file-remote-p "/method:user@:" 'method) "method"))
267 (should (string-equal (file-remote-p "/method:user@:" 'user) "user"))
268 (should (string-equal (file-remote-p "/method:user@:" 'host)
269 "default-host"))
270 (should (string-equal (file-remote-p "/method:user@:" 'localname) ""))
271 (should (string-equal (file-remote-p "/method:user@:" 'hop) nil))
273 ;; No expansion.
274 (should (string-equal
275 (file-remote-p "/method:user@host:")
276 (format "/%s:%s@%s:" "method" "user" "host")))
277 (should (string-equal
278 (file-remote-p "/method:user@host:" 'method) "method"))
279 (should (string-equal (file-remote-p "/method:user@host:" 'user) "user"))
280 (should (string-equal (file-remote-p "/method:user@host:" 'host) "host"))
281 (should (string-equal (file-remote-p "/method:user@host:" 'localname) ""))
282 (should (string-equal (file-remote-p "/method:user@host:" 'hop) nil))
284 ;; No expansion.
285 (should (string-equal
286 (file-remote-p "/method:user@email@host:")
287 (format "/%s:%s@%s:" "method" "user@email" "host")))
288 (should (string-equal
289 (file-remote-p "/method:user@email@host:" 'method) "method"))
290 (should (string-equal
291 (file-remote-p "/method:user@email@host:" 'user) "user@email"))
292 (should (string-equal
293 (file-remote-p "/method:user@email@host:" 'host) "host"))
294 (should (string-equal
295 (file-remote-p "/method:user@email@host:" 'localname) ""))
296 (should (string-equal
297 (file-remote-p "/method:user@email@host:" 'hop) nil))
299 ;; Expand `tramp-default-method' and `tramp-default-user'.
300 (should (string-equal
301 (file-remote-p "/host#1234:")
302 (format "/%s:%s@%s:" "default-method" "default-user" "host#1234")))
303 (should (string-equal
304 (file-remote-p "/host#1234:" 'method) "default-method"))
305 (should (string-equal (file-remote-p "/host#1234:" 'user) "default-user"))
306 (should (string-equal (file-remote-p "/host#1234:" 'host) "host#1234"))
307 (should (string-equal (file-remote-p "/host#1234:" 'localname) ""))
308 (should (string-equal (file-remote-p "/host#1234:" 'hop) nil))
310 ;; Expand `tramp-default-method'.
311 (should (string-equal
312 (file-remote-p "/user@host#1234:")
313 (format "/%s:%s@%s:" "default-method" "user" "host#1234")))
314 (should (string-equal
315 (file-remote-p "/user@host#1234:" 'method) "default-method"))
316 (should (string-equal (file-remote-p "/user@host#1234:" 'user) "user"))
317 (should (string-equal (file-remote-p "/user@host#1234:" 'host) "host#1234"))
318 (should (string-equal (file-remote-p "/user@host#1234:" 'localname) ""))
319 (should (string-equal (file-remote-p "/user@host#1234:" 'hop) nil))
321 ;; Expand `tramp-default-user'.
322 (should (string-equal
323 (file-remote-p "/method:host#1234:")
324 (format "/%s:%s@%s:" "method" "default-user" "host#1234")))
325 (should (string-equal
326 (file-remote-p "/method:host#1234:" 'method) "method"))
327 (should (string-equal
328 (file-remote-p "/method:host#1234:" 'user) "default-user"))
329 (should (string-equal
330 (file-remote-p "/method:host#1234:" 'host) "host#1234"))
331 (should (string-equal (file-remote-p "/method:host#1234:" 'localname) ""))
332 (should (string-equal (file-remote-p "/method:host#1234:" 'hop) nil))
334 ;; No expansion.
335 (should (string-equal
336 (file-remote-p "/method:user@host#1234:")
337 (format "/%s:%s@%s:" "method" "user" "host#1234")))
338 (should (string-equal
339 (file-remote-p "/method:user@host#1234:" 'method) "method"))
340 (should (string-equal
341 (file-remote-p "/method:user@host#1234:" 'user) "user"))
342 (should (string-equal
343 (file-remote-p "/method:user@host#1234:" 'host) "host#1234"))
344 (should (string-equal
345 (file-remote-p "/method:user@host#1234:" 'localname) ""))
346 (should (string-equal
347 (file-remote-p "/method:user@host#1234:" 'hop) nil))
349 ;; Expand `tramp-default-method' and `tramp-default-user'.
350 (should (string-equal
351 (file-remote-p "/1.2.3.4:")
352 (format "/%s:%s@%s:" "default-method" "default-user" "1.2.3.4")))
353 (should (string-equal (file-remote-p "/1.2.3.4:" 'method) "default-method"))
354 (should (string-equal (file-remote-p "/1.2.3.4:" 'user) "default-user"))
355 (should (string-equal (file-remote-p "/1.2.3.4:" 'host) "1.2.3.4"))
356 (should (string-equal (file-remote-p "/1.2.3.4:" 'localname) ""))
357 (should (string-equal (file-remote-p "/1.2.3.4:" 'hop) nil))
359 ;; Expand `tramp-default-method'.
360 (should (string-equal
361 (file-remote-p "/user@1.2.3.4:")
362 (format "/%s:%s@%s:" "default-method" "user" "1.2.3.4")))
363 (should (string-equal
364 (file-remote-p "/user@1.2.3.4:" 'method) "default-method"))
365 (should (string-equal (file-remote-p "/user@1.2.3.4:" 'user) "user"))
366 (should (string-equal (file-remote-p "/user@1.2.3.4:" 'host) "1.2.3.4"))
367 (should (string-equal (file-remote-p "/user@1.2.3.4:" 'localname) ""))
368 (should (string-equal (file-remote-p "/user@1.2.3.4:" 'hop) nil))
370 ;; Expand `tramp-default-user'.
371 (should (string-equal
372 (file-remote-p "/method:1.2.3.4:")
373 (format "/%s:%s@%s:" "method" "default-user" "1.2.3.4")))
374 (should (string-equal (file-remote-p "/method:1.2.3.4:" 'method) "method"))
375 (should (string-equal
376 (file-remote-p "/method:1.2.3.4:" 'user) "default-user"))
377 (should (string-equal (file-remote-p "/method:1.2.3.4:" 'host) "1.2.3.4"))
378 (should (string-equal (file-remote-p "/method:1.2.3.4:" 'localname) ""))
379 (should (string-equal (file-remote-p "/method:1.2.3.4:" 'hop) nil))
381 ;; No expansion.
382 (should (string-equal
383 (file-remote-p "/method:user@1.2.3.4:")
384 (format "/%s:%s@%s:" "method" "user" "1.2.3.4")))
385 (should (string-equal
386 (file-remote-p "/method:user@1.2.3.4:" 'method) "method"))
387 (should (string-equal (file-remote-p "/method:user@1.2.3.4:" 'user) "user"))
388 (should (string-equal
389 (file-remote-p "/method:user@1.2.3.4:" 'host) "1.2.3.4"))
390 (should (string-equal
391 (file-remote-p "/method:user@1.2.3.4:" 'localname) ""))
392 (should (string-equal
393 (file-remote-p "/method:user@1.2.3.4:" 'hop) nil))
395 ;; Expand `tramp-default-method', `tramp-default-user' and
396 ;; `tramp-default-host'.
397 (should (string-equal
398 (file-remote-p "/[]:")
399 (format
400 "/%s:%s@%s:" "default-method" "default-user" "default-host")))
401 (should (string-equal (file-remote-p "/[]:" 'method) "default-method"))
402 (should (string-equal (file-remote-p "/[]:" 'user) "default-user"))
403 (should (string-equal (file-remote-p "/[]:" 'host) "default-host"))
404 (should (string-equal (file-remote-p "/[]:" 'localname) ""))
405 (should (string-equal (file-remote-p "/[]:" 'hop) nil))
407 ;; Expand `tramp-default-method' and `tramp-default-user'.
408 (let ((tramp-default-host "::1"))
409 (should (string-equal
410 (file-remote-p "/[]:")
411 (format "/%s:%s@%s:" "default-method" "default-user" "[::1]")))
412 (should (string-equal (file-remote-p "/[]:" 'method) "default-method"))
413 (should (string-equal (file-remote-p "/[]:" 'user) "default-user"))
414 (should (string-equal (file-remote-p "/[]:" 'host) "::1"))
415 (should (string-equal (file-remote-p "/[]:" 'localname) ""))
416 (should (string-equal (file-remote-p "/[]:" 'hop) nil)))
418 ;; Expand `tramp-default-method' and `tramp-default-user'.
419 (should (string-equal
420 (file-remote-p "/[::1]:")
421 (format "/%s:%s@%s:" "default-method" "default-user" "[::1]")))
422 (should (string-equal (file-remote-p "/[::1]:" 'method) "default-method"))
423 (should (string-equal (file-remote-p "/[::1]:" 'user) "default-user"))
424 (should (string-equal (file-remote-p "/[::1]:" 'host) "::1"))
425 (should (string-equal (file-remote-p "/[::1]:" 'localname) ""))
426 (should (string-equal (file-remote-p "/[::1]:" 'hop) nil))
428 ;; Expand `tramp-default-method'.
429 (should (string-equal
430 (file-remote-p "/user@[::1]:")
431 (format "/%s:%s@%s:" "default-method" "user" "[::1]")))
432 (should (string-equal
433 (file-remote-p "/user@[::1]:" 'method) "default-method"))
434 (should (string-equal (file-remote-p "/user@[::1]:" 'user) "user"))
435 (should (string-equal (file-remote-p "/user@[::1]:" 'host) "::1"))
436 (should (string-equal (file-remote-p "/user@[::1]:" 'localname) ""))
437 (should (string-equal (file-remote-p "/user@[::1]:" 'hop) nil))
439 ;; Expand `tramp-default-user'.
440 (should (string-equal
441 (file-remote-p "/method:[::1]:")
442 (format "/%s:%s@%s:" "method" "default-user" "[::1]")))
443 (should (string-equal (file-remote-p "/method:[::1]:" 'method) "method"))
444 (should (string-equal
445 (file-remote-p "/method:[::1]:" 'user) "default-user"))
446 (should (string-equal (file-remote-p "/method:[::1]:" 'host) "::1"))
447 (should (string-equal (file-remote-p "/method:[::1]:" 'localname) ""))
448 (should (string-equal (file-remote-p "/method:[::1]:" 'hop) nil))
450 ;; No expansion.
451 (should (string-equal
452 (file-remote-p "/method:user@[::1]:")
453 (format "/%s:%s@%s:" "method" "user" "[::1]")))
454 (should (string-equal
455 (file-remote-p "/method:user@[::1]:" 'method) "method"))
456 (should (string-equal (file-remote-p "/method:user@[::1]:" 'user) "user"))
457 (should (string-equal (file-remote-p "/method:user@[::1]:" 'host) "::1"))
458 (should (string-equal
459 (file-remote-p "/method:user@[::1]:" 'localname) ""))
460 (should (string-equal (file-remote-p "/method:user@[::1]:" 'hop) nil))
462 ;; Local file name part.
463 (should (string-equal (file-remote-p "/host:/:" 'localname) "/:"))
464 (should (string-equal (file-remote-p "/method:::" 'localname) ":"))
465 (should (string-equal (file-remote-p "/method:: " 'localname) " "))
466 (should (string-equal (file-remote-p "/method::file" 'localname) "file"))
467 (should (string-equal
468 (file-remote-p "/method::/path/to/file" 'localname)
469 "/path/to/file"))
471 ;; Multihop.
472 (should
473 (string-equal
474 (file-remote-p "/method1:user1@host1|method2:user2@host2:/path/to/file")
475 (format "/%s:%s@%s|%s:%s@%s:"
476 "method1" "user1" "host1" "method2" "user2" "host2")))
477 (should
478 (string-equal
479 (file-remote-p
480 "/method1:user1@host1|method2:user2@host2:/path/to/file" 'method)
481 "method2"))
482 (should
483 (string-equal
484 (file-remote-p
485 "/method1:user1@host1|method2:user2@host2:/path/to/file" 'user)
486 "user2"))
487 (should
488 (string-equal
489 (file-remote-p
490 "/method1:user1@host1|method2:user2@host2:/path/to/file" 'host)
491 "host2"))
492 (should
493 (string-equal
494 (file-remote-p
495 "/method1:user1@host1|method2:user2@host2:/path/to/file" 'localname)
496 "/path/to/file"))
497 (should
498 (string-equal
499 (file-remote-p
500 "/method1:user1@host1|method2:user2@host2:/path/to/file" 'hop)
501 (format "%s:%s@%s|"
502 "method1" "user1" "host1")))
504 (should
505 (string-equal
506 (file-remote-p
507 "/method1:user1@host1|method2:user2@host2|method3:user3@host3:/path/to/file")
508 (format "/%s:%s@%s|%s:%s@%s|%s:%s@%s:"
509 "method1" "user1" "host1"
510 "method2" "user2" "host2"
511 "method3" "user3" "host3")))
512 (should
513 (string-equal
514 (file-remote-p
515 "/method1:user1@host1|method2:user2@host2|method3:user3@host3:/path/to/file"
516 'method)
517 "method3"))
518 (should
519 (string-equal
520 (file-remote-p
521 "/method1:user1@host1|method2:user2@host2|method3:user3@host3:/path/to/file"
522 'user)
523 "user3"))
524 (should
525 (string-equal
526 (file-remote-p
527 "/method1:user1@host1|method2:user2@host2|method3:user3@host3:/path/to/file"
528 'host)
529 "host3"))
530 (should
531 (string-equal
532 (file-remote-p
533 "/method1:user1@host1|method2:user2@host2|method3:user3@host3:/path/to/file"
534 'localname)
535 "/path/to/file"))
536 (should
537 (string-equal
538 (file-remote-p
539 "/method1:user1@host1|method2:user2@host2|method3:user3@host3:/path/to/file"
540 'hop)
541 (format "%s:%s@%s|%s:%s@%s|"
542 "method1" "user1" "host1" "method2" "user2" "host2")))))
544 (ert-deftest tramp-test03-file-name-defaults ()
545 "Check default values for some methods."
546 ;; Default values in tramp-adb.el.
547 (should (string-equal (file-remote-p "/adb::" 'host) ""))
548 ;; Default values in tramp-ftp.el.
549 (should (string-equal (file-remote-p "/ftp.host:" 'method) "ftp"))
550 (dolist (u '("ftp" "anonymous"))
551 (should (string-equal (file-remote-p (format "/%s@:" u) 'method) "ftp")))
552 ;; Default values in tramp-gvfs.el.
553 (when (and (load "tramp-gvfs" 'noerror 'nomessage)
554 (symbol-value 'tramp-gvfs-enabled))
555 (should (string-equal (file-remote-p "/synce::" 'user) nil)))
556 ;; Default values in tramp-gw.el.
557 (dolist (m '("tunnel" "socks"))
558 (should
559 (string-equal (file-remote-p (format "/%s::" m) 'user) (user-login-name))))
560 ;; Default values in tramp-sh.el.
561 (dolist (h `("127.0.0.1" "[::1]" "localhost" "localhost6" ,(system-name)))
562 (should (string-equal (file-remote-p (format "/root@%s:" h) 'method) "su")))
563 (dolist (m '("su" "sudo" "ksu"))
564 (should (string-equal (file-remote-p (format "/%s::" m) 'user) "root")))
565 (dolist (m '("rcp" "remcp" "rsh" "telnet" "krlogin" "fcp"))
566 (should
567 (string-equal (file-remote-p (format "/%s::" m) 'user) (user-login-name))))
568 ;; Default values in tramp-smb.el.
569 (should (string-equal (file-remote-p "/user%domain@host:" 'method) "smb"))
570 (should (string-equal (file-remote-p "/smb::" 'user) nil)))
572 (ert-deftest tramp-test04-substitute-in-file-name ()
573 "Check `substitute-in-file-name'."
574 (should (string-equal (substitute-in-file-name "/method:host://foo") "/foo"))
575 (should
576 (string-equal
577 (substitute-in-file-name "/method:host:/path//foo") "/method:host:/foo"))
578 (should
579 (string-equal (substitute-in-file-name "/method:host:/path///foo") "/foo"))
580 (should
581 (string-equal
582 (substitute-in-file-name "/method:host:/path/~/foo") "/method:host:~/foo"))
583 (should
584 (string-equal (substitute-in-file-name "/method:host:/path//~/foo") "~/foo"))
585 (let (process-environment)
586 (should
587 (string-equal
588 (substitute-in-file-name "/method:host:/path/$FOO")
589 "/method:host:/path/$FOO"))
590 (setenv "FOO" "bla")
591 (should
592 (string-equal
593 (substitute-in-file-name "/method:host:/path/$FOO")
594 "/method:host:/path/bla"))
595 (should
596 (string-equal
597 (substitute-in-file-name "/method:host:/path/$$FOO")
598 "/method:host:/path/$FOO"))))
600 (ert-deftest tramp-test05-expand-file-name ()
601 "Check `expand-file-name'."
602 (should
603 (string-equal
604 (expand-file-name "/method:host:/path/./file") "/method:host:/path/file"))
605 (should
606 (string-equal
607 (expand-file-name "/method:host:/path/../file") "/method:host:/file")))
609 (ert-deftest tramp-test06-directory-file-name ()
610 "Check `directory-file-name'.
611 This checks also `file-name-as-directory', `file-name-directory',
612 `file-name-nondirectory' and `unhandled-file-name-directory'."
613 (should
614 (string-equal
615 (directory-file-name "/method:host:/path/to/file")
616 "/method:host:/path/to/file"))
617 (should
618 (string-equal
619 (directory-file-name "/method:host:/path/to/file/")
620 "/method:host:/path/to/file"))
621 (should
622 (string-equal
623 (file-name-as-directory "/method:host:/path/to/file")
624 "/method:host:/path/to/file/"))
625 (should
626 (string-equal
627 (file-name-as-directory "/method:host:/path/to/file/")
628 "/method:host:/path/to/file/"))
629 (should
630 (string-equal
631 (file-name-directory "/method:host:/path/to/file")
632 "/method:host:/path/to/"))
633 (should
634 (string-equal
635 (file-name-directory "/method:host:/path/to/file/")
636 "/method:host:/path/to/file/"))
637 (should
638 (string-equal (file-name-nondirectory "/method:host:/path/to/file") "file"))
639 (should
640 (string-equal (file-name-nondirectory "/method:host:/path/to/file/") ""))
641 (should-not
642 (unhandled-file-name-directory "/method:host:/path/to/file"))
644 ;; Bug#10085.
645 (when (tramp--test-enabled) ;; Packages like tramp-gvfs.el might be disabled.
646 (dolist (n-e '(nil t))
647 ;; We must clear `tramp-default-method'. On hydra, it is "ftp",
648 ;; which ruins the tests.
649 (let ((non-essential n-e)
650 tramp-default-method)
651 (dolist (file
652 `(,(file-remote-p tramp-test-temporary-file-directory 'method)
653 ,(file-remote-p tramp-test-temporary-file-directory 'host)))
654 (unless (zerop (length file))
655 (setq file (format "/%s:" file))
656 (should (string-equal (directory-file-name file) file))
657 (should
658 (string-equal
659 (file-name-as-directory file)
660 (if (tramp-completion-mode-p) file (concat file "./"))))
661 (should (string-equal (file-name-directory file) file))
662 (should (string-equal (file-name-nondirectory file) ""))))))))
664 (ert-deftest tramp-test07-file-exists-p ()
665 "Check `file-exist-p', `write-region' and `delete-file'."
666 (skip-unless (tramp--test-enabled))
668 (let ((tmp-name (tramp--test-make-temp-name)))
669 (should-not (file-exists-p tmp-name))
670 (write-region "foo" nil tmp-name)
671 (should (file-exists-p tmp-name))
672 (delete-file tmp-name)
673 (should-not (file-exists-p tmp-name))))
675 (ert-deftest tramp-test08-file-local-copy ()
676 "Check `file-local-copy'."
677 (skip-unless (tramp--test-enabled))
679 (let ((tmp-name1 (tramp--test-make-temp-name))
680 tmp-name2)
681 (unwind-protect
682 (progn
683 (write-region "foo" nil tmp-name1)
684 (should (setq tmp-name2 (file-local-copy tmp-name1)))
685 (with-temp-buffer
686 (insert-file-contents tmp-name2)
687 (should (string-equal (buffer-string) "foo")))
688 ;; Check also that a file transfer with compression works.
689 (let ((default-directory tramp-test-temporary-file-directory)
690 (tramp-copy-size-limit 4)
691 (tramp-inline-compress-start-size 2))
692 (delete-file tmp-name2)
693 (should (setq tmp-name2 (file-local-copy tmp-name1)))))
695 ;; Cleanup.
696 (ignore-errors
697 (delete-file tmp-name1)
698 (delete-file tmp-name2)))))
700 (ert-deftest tramp-test09-insert-file-contents ()
701 "Check `insert-file-contents'."
702 (skip-unless (tramp--test-enabled))
704 (let ((tmp-name (tramp--test-make-temp-name)))
705 (unwind-protect
706 (progn
707 (write-region "foo" nil tmp-name)
708 (with-temp-buffer
709 (insert-file-contents tmp-name)
710 (should (string-equal (buffer-string) "foo"))
711 (insert-file-contents tmp-name)
712 (should (string-equal (buffer-string) "foofoo"))
713 ;; Insert partly.
714 (insert-file-contents tmp-name nil 1 3)
715 (should (string-equal (buffer-string) "oofoofoo"))
716 ;; Replace.
717 (insert-file-contents tmp-name nil nil nil 'replace)
718 (should (string-equal (buffer-string) "foo"))))
720 ;; Cleanup.
721 (ignore-errors (delete-file tmp-name)))))
723 (ert-deftest tramp-test10-write-region ()
724 "Check `write-region'."
725 (skip-unless (tramp--test-enabled))
727 (let ((tmp-name (tramp--test-make-temp-name)))
728 (unwind-protect
729 (progn
730 (with-temp-buffer
731 (insert "foo")
732 (write-region nil nil tmp-name))
733 (with-temp-buffer
734 (insert-file-contents tmp-name)
735 (should (string-equal (buffer-string) "foo")))
736 ;; Append.
737 (with-temp-buffer
738 (insert "bla")
739 (write-region nil nil tmp-name 'append))
740 (with-temp-buffer
741 (insert-file-contents tmp-name)
742 (should (string-equal (buffer-string) "foobla")))
743 ;; Write string.
744 (write-region "foo" nil tmp-name)
745 (with-temp-buffer
746 (insert-file-contents tmp-name)
747 (should (string-equal (buffer-string) "foo")))
748 ;; Write partly.
749 (with-temp-buffer
750 (insert "123456789")
751 (write-region 3 5 tmp-name))
752 (with-temp-buffer
753 (insert-file-contents tmp-name)
754 (should (string-equal (buffer-string) "34"))))
756 ;; Cleanup.
757 (ignore-errors (delete-file tmp-name)))))
759 (ert-deftest tramp-test11-copy-file ()
760 "Check `copy-file'."
761 (skip-unless (tramp--test-enabled))
763 (let ((tmp-name1 (tramp--test-make-temp-name))
764 (tmp-name2 (tramp--test-make-temp-name))
765 (tmp-name3 (tramp--test-make-temp-name))
766 (tmp-name4 (tramp--test-make-temp-name 'local))
767 (tmp-name5 (tramp--test-make-temp-name 'local)))
769 ;; Copy on remote side.
770 (unwind-protect
771 (progn
772 (write-region "foo" nil tmp-name1)
773 (copy-file tmp-name1 tmp-name2)
774 (should (file-exists-p tmp-name2))
775 (with-temp-buffer
776 (insert-file-contents tmp-name2)
777 (should (string-equal (buffer-string) "foo")))
778 (should-error (copy-file tmp-name1 tmp-name2))
779 (copy-file tmp-name1 tmp-name2 'ok)
780 (make-directory tmp-name3)
781 (copy-file tmp-name1 tmp-name3)
782 (should
783 (file-exists-p
784 (expand-file-name (file-name-nondirectory tmp-name1) tmp-name3))))
786 ;; Cleanup.
787 (ignore-errors (delete-file tmp-name1))
788 (ignore-errors (delete-file tmp-name2))
789 (ignore-errors (delete-directory tmp-name3 'recursive)))
791 ;; Copy from remote side to local side.
792 (unwind-protect
793 (progn
794 (write-region "foo" nil tmp-name1)
795 (copy-file tmp-name1 tmp-name4)
796 (should (file-exists-p tmp-name4))
797 (with-temp-buffer
798 (insert-file-contents tmp-name4)
799 (should (string-equal (buffer-string) "foo")))
800 (should-error (copy-file tmp-name1 tmp-name4))
801 (copy-file tmp-name1 tmp-name4 'ok)
802 (make-directory tmp-name5)
803 (copy-file tmp-name1 tmp-name5)
804 (should
805 (file-exists-p
806 (expand-file-name (file-name-nondirectory tmp-name1) tmp-name5))))
808 ;; Cleanup.
809 (ignore-errors (delete-file tmp-name1))
810 (ignore-errors (delete-file tmp-name4))
811 (ignore-errors (delete-directory tmp-name5 'recursive)))
813 ;; Copy from local side to remote side.
814 (unwind-protect
815 (progn
816 (write-region "foo" nil tmp-name4 nil 'nomessage)
817 (copy-file tmp-name4 tmp-name1)
818 (should (file-exists-p tmp-name1))
819 (with-temp-buffer
820 (insert-file-contents tmp-name1)
821 (should (string-equal (buffer-string) "foo")))
822 (should-error (copy-file tmp-name4 tmp-name1))
823 (copy-file tmp-name4 tmp-name1 'ok)
824 (make-directory tmp-name3)
825 (copy-file tmp-name4 tmp-name3)
826 (should
827 (file-exists-p
828 (expand-file-name (file-name-nondirectory tmp-name4) tmp-name3))))
830 ;; Cleanup.
831 (ignore-errors (delete-file tmp-name1))
832 (ignore-errors (delete-file tmp-name4))
833 (ignore-errors (delete-directory tmp-name3 'recursive)))))
835 (ert-deftest tramp-test12-rename-file ()
836 "Check `rename-file'."
837 (skip-unless (tramp--test-enabled))
839 (let ((tmp-name1 (tramp--test-make-temp-name))
840 (tmp-name2 (tramp--test-make-temp-name))
841 (tmp-name3 (tramp--test-make-temp-name))
842 (tmp-name4 (tramp--test-make-temp-name 'local))
843 (tmp-name5 (tramp--test-make-temp-name 'local)))
845 ;; Rename on remote side.
846 (unwind-protect
847 (progn
848 (write-region "foo" nil tmp-name1)
849 (rename-file tmp-name1 tmp-name2)
850 (should-not (file-exists-p tmp-name1))
851 (should (file-exists-p tmp-name2))
852 (with-temp-buffer
853 (insert-file-contents tmp-name2)
854 (should (string-equal (buffer-string) "foo")))
855 (write-region "foo" nil tmp-name1)
856 (should-error (rename-file tmp-name1 tmp-name2))
857 (rename-file tmp-name1 tmp-name2 'ok)
858 (should-not (file-exists-p tmp-name1))
859 (write-region "foo" nil tmp-name1)
860 (make-directory tmp-name3)
861 (rename-file tmp-name1 tmp-name3)
862 (should-not (file-exists-p tmp-name1))
863 (should
864 (file-exists-p
865 (expand-file-name (file-name-nondirectory tmp-name1) tmp-name3))))
867 ;; Cleanup.
868 (ignore-errors (delete-file tmp-name1))
869 (ignore-errors (delete-file tmp-name2))
870 (ignore-errors (delete-directory tmp-name3 'recursive)))
872 ;; Rename from remote side to local side.
873 (unwind-protect
874 (progn
875 (write-region "foo" nil tmp-name1)
876 (rename-file tmp-name1 tmp-name4)
877 (should-not (file-exists-p tmp-name1))
878 (should (file-exists-p tmp-name4))
879 (with-temp-buffer
880 (insert-file-contents tmp-name4)
881 (should (string-equal (buffer-string) "foo")))
882 (write-region "foo" nil tmp-name1)
883 (should-error (rename-file tmp-name1 tmp-name4))
884 (rename-file tmp-name1 tmp-name4 'ok)
885 (should-not (file-exists-p tmp-name1))
886 (write-region "foo" nil tmp-name1)
887 (make-directory tmp-name5)
888 (rename-file tmp-name1 tmp-name5)
889 (should-not (file-exists-p tmp-name1))
890 (should
891 (file-exists-p
892 (expand-file-name (file-name-nondirectory tmp-name1) tmp-name5))))
894 ;; Cleanup.
895 (ignore-errors (delete-file tmp-name1))
896 (ignore-errors (delete-file tmp-name4))
897 (ignore-errors (delete-directory tmp-name5 'recursive)))
899 ;; Rename from local side to remote side.
900 (unwind-protect
901 (progn
902 (write-region "foo" nil tmp-name4 nil 'nomessage)
903 (rename-file tmp-name4 tmp-name1)
904 (should-not (file-exists-p tmp-name4))
905 (should (file-exists-p tmp-name1))
906 (with-temp-buffer
907 (insert-file-contents tmp-name1)
908 (should (string-equal (buffer-string) "foo")))
909 (write-region "foo" nil tmp-name4 nil 'nomessage)
910 (should-error (rename-file tmp-name4 tmp-name1))
911 (rename-file tmp-name4 tmp-name1 'ok)
912 (should-not (file-exists-p tmp-name4))
913 (write-region "foo" nil tmp-name4 nil 'nomessage)
914 (make-directory tmp-name3)
915 (rename-file tmp-name4 tmp-name3)
916 (should-not (file-exists-p tmp-name4))
917 (should
918 (file-exists-p
919 (expand-file-name (file-name-nondirectory tmp-name4) tmp-name3))))
921 ;; Cleanup.
922 (ignore-errors (delete-file tmp-name1))
923 (ignore-errors (delete-file tmp-name4))
924 (ignore-errors (delete-directory tmp-name3 'recursive)))))
926 (ert-deftest tramp-test13-make-directory ()
927 "Check `make-directory'.
928 This tests also `file-directory-p' and `file-accessible-directory-p'."
929 (skip-unless (tramp--test-enabled))
931 (let* ((tmp-name1 (tramp--test-make-temp-name))
932 (tmp-name2 (expand-file-name "foo/bar" tmp-name1)))
933 (unwind-protect
934 (progn
935 (make-directory tmp-name1)
936 (should (file-directory-p tmp-name1))
937 (should (file-accessible-directory-p tmp-name1))
938 (should-error (make-directory tmp-name2))
939 (make-directory tmp-name2 'parents)
940 (should (file-directory-p tmp-name2))
941 (should (file-accessible-directory-p tmp-name2)))
943 ;; Cleanup.
944 (ignore-errors (delete-directory tmp-name1 'recursive)))))
946 (ert-deftest tramp-test14-delete-directory ()
947 "Check `delete-directory'."
948 (skip-unless (tramp--test-enabled))
950 (let ((tmp-name (tramp--test-make-temp-name)))
951 ;; Delete empty directory.
952 (make-directory tmp-name)
953 (should (file-directory-p tmp-name))
954 (delete-directory tmp-name)
955 (should-not (file-directory-p tmp-name))
956 ;; Delete non-empty directory.
957 (make-directory tmp-name)
958 (should (file-directory-p tmp-name))
959 (write-region "foo" nil (expand-file-name "bla" tmp-name))
960 (should (file-exists-p (expand-file-name "bla" tmp-name)))
961 (should-error (delete-directory tmp-name))
962 (delete-directory tmp-name 'recursive)
963 (should-not (file-directory-p tmp-name))))
965 (ert-deftest tramp-test15-copy-directory ()
966 "Check `copy-directory'."
967 (skip-unless (tramp--test-enabled))
969 (let* ((tmp-name1 (tramp--test-make-temp-name))
970 (tmp-name2 (tramp--test-make-temp-name))
971 (tmp-name3 (expand-file-name
972 (file-name-nondirectory tmp-name1) tmp-name2))
973 (tmp-name4 (expand-file-name "foo" tmp-name1))
974 (tmp-name5 (expand-file-name "foo" tmp-name2))
975 (tmp-name6 (expand-file-name "foo" tmp-name3)))
977 ;; Copy complete directory.
978 (unwind-protect
979 (progn
980 ;; Copy empty directory.
981 (make-directory tmp-name1)
982 (write-region "foo" nil tmp-name4)
983 (should (file-directory-p tmp-name1))
984 (should (file-exists-p tmp-name4))
985 (copy-directory tmp-name1 tmp-name2)
986 (should (file-directory-p tmp-name2))
987 (should (file-exists-p tmp-name5))
988 ;; Target directory does exist already.
989 (copy-directory tmp-name1 tmp-name2)
990 (should (file-directory-p tmp-name3))
991 (should (file-exists-p tmp-name6)))
993 ;; Cleanup.
994 (ignore-errors
995 (delete-directory tmp-name1 'recursive)
996 (delete-directory tmp-name2 'recursive)))
998 ;; Copy directory contents.
999 (unwind-protect
1000 (progn
1001 ;; Copy empty directory.
1002 (make-directory tmp-name1)
1003 (write-region "foo" nil tmp-name4)
1004 (should (file-directory-p tmp-name1))
1005 (should (file-exists-p tmp-name4))
1006 (copy-directory tmp-name1 tmp-name2 nil 'parents 'contents)
1007 (should (file-directory-p tmp-name2))
1008 (should (file-exists-p tmp-name5))
1009 ;; Target directory does exist already.
1010 (delete-file tmp-name5)
1011 (should-not (file-exists-p tmp-name5))
1012 (copy-directory tmp-name1 tmp-name2 nil 'parents 'contents)
1013 (should (file-directory-p tmp-name2))
1014 (should (file-exists-p tmp-name5))
1015 (should-not (file-directory-p tmp-name3))
1016 (should-not (file-exists-p tmp-name6)))
1018 ;; Cleanup.
1019 (ignore-errors
1020 (delete-directory tmp-name1 'recursive)
1021 (delete-directory tmp-name2 'recursive)))))
1023 (ert-deftest tramp-test16-directory-files ()
1024 "Check `directory-files'."
1025 (skip-unless (tramp--test-enabled))
1027 (let* ((tmp-name1 (tramp--test-make-temp-name))
1028 (tmp-name2 (expand-file-name "bla" tmp-name1))
1029 (tmp-name3 (expand-file-name "foo" tmp-name1)))
1030 (unwind-protect
1031 (progn
1032 (make-directory tmp-name1)
1033 (write-region "foo" nil tmp-name2)
1034 (write-region "bla" nil tmp-name3)
1035 (should (file-directory-p tmp-name1))
1036 (should (file-exists-p tmp-name2))
1037 (should (file-exists-p tmp-name3))
1038 (should (equal (directory-files tmp-name1) '("." ".." "bla" "foo")))
1039 (should (equal (directory-files tmp-name1 'full)
1040 `(,(concat tmp-name1 "/.")
1041 ,(concat tmp-name1 "/..")
1042 ,tmp-name2 ,tmp-name3)))
1043 (should (equal (directory-files
1044 tmp-name1 nil directory-files-no-dot-files-regexp)
1045 '("bla" "foo")))
1046 (should (equal (directory-files
1047 tmp-name1 'full directory-files-no-dot-files-regexp)
1048 `(,tmp-name2 ,tmp-name3))))
1050 ;; Cleanup.
1051 (ignore-errors (delete-directory tmp-name1 'recursive)))))
1053 (ert-deftest tramp-test17-insert-directory ()
1054 "Check `insert-directory'."
1055 (skip-unless (tramp--test-enabled))
1057 (let* ((tmp-name1 (tramp--test-make-temp-name))
1058 (tmp-name2 (expand-file-name "foo" tmp-name1))
1059 ;; We test for the summary line. Keyword "total" could be localized.
1060 (process-environment
1061 (append '("LANG=C" "LANGUAGE=C" "LC_ALL=C") process-environment)))
1062 (unwind-protect
1063 (progn
1064 (make-directory tmp-name1)
1065 (write-region "foo" nil tmp-name2)
1066 (should (file-directory-p tmp-name1))
1067 (should (file-exists-p tmp-name2))
1068 (with-temp-buffer
1069 (insert-directory tmp-name1 nil)
1070 (goto-char (point-min))
1071 (should (looking-at-p (regexp-quote tmp-name1))))
1072 (with-temp-buffer
1073 (insert-directory tmp-name1 "-al")
1074 (goto-char (point-min))
1075 (should (looking-at-p (format "^.+ %s$" (regexp-quote tmp-name1)))))
1076 (with-temp-buffer
1077 (insert-directory (file-name-as-directory tmp-name1) "-al")
1078 (goto-char (point-min))
1079 (should
1080 (looking-at-p (format "^.+ %s/$" (regexp-quote tmp-name1)))))
1081 (with-temp-buffer
1082 (insert-directory
1083 (file-name-as-directory tmp-name1) "-al" nil 'full-directory-p)
1084 (goto-char (point-min))
1085 (should
1086 (looking-at-p
1087 (concat
1088 ;; There might be a summary line.
1089 "\\(total.+[[:digit:]]+\n\\)?"
1090 ;; We don't know in which order ".", ".." and "foo" appear.
1091 "\\(.+ \\(\\.?\\.\\|foo\\)\n\\)\\{3\\}")))))
1093 ;; Cleanup.
1094 (ignore-errors (delete-directory tmp-name1 'recursive)))))
1096 (ert-deftest tramp-test18-file-attributes ()
1097 "Check `file-attributes'.
1098 This tests also `file-readable-p', `file-regular-p' and
1099 `file-ownership-preserved-p'."
1100 (skip-unless (tramp--test-enabled))
1102 ;; We must use `file-truename' for the temporary directory, because
1103 ;; it could be located on a symlinked directory. This would let the
1104 ;; test fail.
1105 (let* ((tramp-test-temporary-file-directory
1106 (file-truename tramp-test-temporary-file-directory))
1107 (tmp-name1 (tramp--test-make-temp-name))
1108 (tmp-name2 (tramp--test-make-temp-name))
1109 ;; File name with "//".
1110 (tmp-name3
1111 (format
1112 "%s%s"
1113 (file-remote-p tmp-name1)
1114 (replace-regexp-in-string
1115 "/" "//" (file-remote-p tmp-name1 'localname))))
1116 attr)
1117 (unwind-protect
1118 (progn
1119 ;; `file-ownership-preserved-p' should return t for
1120 ;; non-existing files. It is implemented only in tramp-sh.el.
1121 (when (tramp--test-sh-p)
1122 (should (file-ownership-preserved-p tmp-name1 'group)))
1123 (write-region "foo" nil tmp-name1)
1124 (should (file-exists-p tmp-name1))
1125 (should (file-readable-p tmp-name1))
1126 (should (file-regular-p tmp-name1))
1127 (when (tramp--test-sh-p)
1128 (should (file-ownership-preserved-p tmp-name1 'group)))
1130 ;; We do not test inodes and device numbers.
1131 (setq attr (file-attributes tmp-name1))
1132 (should (consp attr))
1133 (should (null (car attr)))
1134 (should (numberp (nth 1 attr))) ;; Link.
1135 (should (numberp (nth 2 attr))) ;; Uid.
1136 (should (numberp (nth 3 attr))) ;; Gid.
1137 ;; Last access time.
1138 (should (stringp (current-time-string (nth 4 attr))))
1139 ;; Last modification time.
1140 (should (stringp (current-time-string (nth 5 attr))))
1141 ;; Last status change time.
1142 (should (stringp (current-time-string (nth 6 attr))))
1143 (should (numberp (nth 7 attr))) ;; Size.
1144 (should (stringp (nth 8 attr))) ;; Modes.
1146 (setq attr (file-attributes tmp-name1 'string))
1147 (should (stringp (nth 2 attr))) ;; Uid.
1148 (should (stringp (nth 3 attr))) ;; Gid.
1150 (condition-case err
1151 (progn
1152 (when (tramp--test-sh-p)
1153 (should (file-ownership-preserved-p tmp-name2 'group)))
1154 (make-symbolic-link tmp-name1 tmp-name2)
1155 (should (file-exists-p tmp-name2))
1156 (should (file-symlink-p tmp-name2))
1157 (when (tramp--test-sh-p)
1158 (should (file-ownership-preserved-p tmp-name2 'group)))
1159 (setq attr (file-attributes tmp-name2))
1160 (should (string-equal
1161 (car attr)
1162 (file-remote-p (file-truename tmp-name1) 'localname)))
1163 (delete-file tmp-name2))
1164 (file-error
1165 (should (string-equal (error-message-string err)
1166 "make-symbolic-link not supported"))))
1168 ;; Check, that "//" in symlinks are handled properly.
1169 (with-temp-buffer
1170 (let ((default-directory tramp-test-temporary-file-directory))
1171 (shell-command
1172 (format
1173 "ln -s %s %s"
1174 (tramp-file-name-localname (tramp-dissect-file-name tmp-name3))
1175 (tramp-file-name-localname (tramp-dissect-file-name tmp-name2)))
1176 t)))
1177 (when (file-symlink-p tmp-name2)
1178 (setq attr (file-attributes tmp-name2))
1179 (should
1180 (string-equal
1181 (car attr)
1182 (tramp-file-name-localname (tramp-dissect-file-name tmp-name3))))
1183 (delete-file tmp-name2))
1185 (when (tramp--test-sh-p)
1186 (should (file-ownership-preserved-p tmp-name1 'group)))
1187 (delete-file tmp-name1)
1188 (make-directory tmp-name1)
1189 (should (file-exists-p tmp-name1))
1190 (should (file-readable-p tmp-name1))
1191 (should-not (file-regular-p tmp-name1))
1192 (when (tramp--test-sh-p)
1193 (should (file-ownership-preserved-p tmp-name1 'group)))
1194 (setq attr (file-attributes tmp-name1))
1195 (should (eq (car attr) t)))
1197 ;; Cleanup.
1198 (ignore-errors (delete-directory tmp-name1))
1199 (ignore-errors (delete-file tmp-name1))
1200 (ignore-errors (delete-file tmp-name2)))))
1202 (ert-deftest tramp-test19-directory-files-and-attributes ()
1203 "Check `directory-files-and-attributes'."
1204 (skip-unless (tramp--test-enabled))
1206 ;; `directory-files-and-attributes' contains also values for "../".
1207 ;; Ensure that this doesn't change during tests, for
1208 ;; example due to handling temporary files.
1209 (let* ((tmp-name1 (tramp--test-make-temp-name))
1210 (tmp-name2 (expand-file-name "bla" tmp-name1))
1211 attr)
1212 (unwind-protect
1213 (progn
1214 (make-directory tmp-name1)
1215 (should (file-directory-p tmp-name1))
1216 (make-directory tmp-name2)
1217 (should (file-directory-p tmp-name2))
1218 (write-region "foo" nil (expand-file-name "foo" tmp-name2))
1219 (write-region "bar" nil (expand-file-name "bar" tmp-name2))
1220 (write-region "boz" nil (expand-file-name "boz" tmp-name2))
1221 (setq attr (directory-files-and-attributes tmp-name2))
1222 (should (consp attr))
1223 ;; Dumb remote shells without perl(1) or stat(1) are not
1224 ;; able to return the date correctly. They say "don't know".
1225 (dolist (elt attr)
1226 (unless
1227 (equal
1228 (nth 5
1229 (file-attributes (expand-file-name (car elt) tmp-name2)))
1230 '(0 0))
1231 (should
1232 (equal (file-attributes (expand-file-name (car elt) tmp-name2))
1233 (cdr elt)))))
1234 (setq attr (directory-files-and-attributes tmp-name2 'full))
1235 (dolist (elt attr)
1236 (unless (equal (nth 5 (file-attributes (car elt))) '(0 0))
1237 (should
1238 (equal (file-attributes (car elt)) (cdr elt)))))
1239 (setq attr (directory-files-and-attributes tmp-name2 nil "^b"))
1240 (should (equal (mapcar 'car attr) '("bar" "boz"))))
1242 ;; Cleanup.
1243 (ignore-errors (delete-directory tmp-name1 'recursive)))))
1245 (ert-deftest tramp-test20-file-modes ()
1246 "Check `file-modes'.
1247 This tests also `file-executable-p', `file-writable-p' and `set-file-modes'."
1248 (skip-unless (tramp--test-enabled))
1249 (skip-unless (tramp--test-sh-p))
1251 (let ((tmp-name (tramp--test-make-temp-name)))
1252 (unwind-protect
1253 (progn
1254 (write-region "foo" nil tmp-name)
1255 (should (file-exists-p tmp-name))
1256 (set-file-modes tmp-name #o777)
1257 (should (= (file-modes tmp-name) #o777))
1258 (should (file-executable-p tmp-name))
1259 (should (file-writable-p tmp-name))
1260 (set-file-modes tmp-name #o444)
1261 (should (= (file-modes tmp-name) #o444))
1262 (should-not (file-executable-p tmp-name))
1263 ;; A file is always writable for user "root".
1264 (unless (zerop (nth 2 (file-attributes tmp-name)))
1265 (should-not (file-writable-p tmp-name))))
1267 ;; Cleanup.
1268 (ignore-errors (delete-file tmp-name)))))
1270 (ert-deftest tramp-test21-file-links ()
1271 "Check `file-symlink-p'.
1272 This tests also `make-symbolic-link', `file-truename' and `add-name-to-file'."
1273 (skip-unless (tramp--test-enabled))
1275 ;; We must use `file-truename' for the temporary directory, because
1276 ;; it could be located on a symlinked directory. This would let the
1277 ;; test fail.
1278 (let* ((tramp-test-temporary-file-directory
1279 (file-truename tramp-test-temporary-file-directory))
1280 (tmp-name1 (tramp--test-make-temp-name))
1281 (tmp-name2 (tramp--test-make-temp-name))
1282 (tmp-name3 (tramp--test-make-temp-name 'local)))
1284 ;; Check `make-symbolic-link'.
1285 (unwind-protect
1286 (progn
1287 (write-region "foo" nil tmp-name1)
1288 (should (file-exists-p tmp-name1))
1289 ;; Method "smb" supports `make-symbolic-link' only if the
1290 ;; remote host has CIFS capabilities. tramp-adb.el and
1291 ;; tramp-gvfs.el do not support symbolic links at all.
1292 (condition-case err
1293 (make-symbolic-link tmp-name1 tmp-name2)
1294 (file-error
1295 (skip-unless
1296 (not (string-equal (error-message-string err)
1297 "make-symbolic-link not supported")))))
1298 (should (file-symlink-p tmp-name2))
1299 (should-error (make-symbolic-link tmp-name1 tmp-name2))
1300 (make-symbolic-link tmp-name1 tmp-name2 'ok-if-already-exists)
1301 (should (file-symlink-p tmp-name2))
1302 ;; `tmp-name3' is a local file name.
1303 (should-error (make-symbolic-link tmp-name1 tmp-name3)))
1305 ;; Cleanup.
1306 (ignore-errors
1307 (delete-file tmp-name1)
1308 (delete-file tmp-name2)))
1310 ;; Check `add-name-to-file'.
1311 (unwind-protect
1312 (progn
1313 (write-region "foo" nil tmp-name1)
1314 (should (file-exists-p tmp-name1))
1315 (add-name-to-file tmp-name1 tmp-name2)
1316 (should-not (file-symlink-p tmp-name2))
1317 (should-error (add-name-to-file tmp-name1 tmp-name2))
1318 (add-name-to-file tmp-name1 tmp-name2 'ok-if-already-exists)
1319 (should-not (file-symlink-p tmp-name2))
1320 ;; `tmp-name3' is a local file name.
1321 (should-error (add-name-to-file tmp-name1 tmp-name3)))
1323 ;; Cleanup.
1324 (ignore-errors
1325 (delete-file tmp-name1)
1326 (delete-file tmp-name2)))
1328 ;; Check `file-truename'.
1329 (unwind-protect
1330 (progn
1331 (write-region "foo" nil tmp-name1)
1332 (should (file-exists-p tmp-name1))
1333 (make-symbolic-link tmp-name1 tmp-name2)
1334 (should (file-symlink-p tmp-name2))
1335 (should-not (string-equal tmp-name2 (file-truename tmp-name2)))
1336 (should
1337 (string-equal (file-truename tmp-name1) (file-truename tmp-name2)))
1338 (should (file-equal-p tmp-name1 tmp-name2)))
1339 (ignore-errors
1340 (delete-file tmp-name1)
1341 (delete-file tmp-name2)))
1343 ;; `file-truename' shall preserve trailing link of directories.
1344 (unless (file-symlink-p tramp-test-temporary-file-directory)
1345 (let* ((dir1 (directory-file-name tramp-test-temporary-file-directory))
1346 (dir2 (file-name-as-directory dir1)))
1347 (should (string-equal (file-truename dir1) (expand-file-name dir1)))
1348 (should (string-equal (file-truename dir2) (expand-file-name dir2)))))))
1350 (ert-deftest tramp-test22-file-times ()
1351 "Check `set-file-times' and `file-newer-than-file-p'."
1352 (skip-unless (tramp--test-enabled))
1353 (skip-unless (or (tramp--test-adb-p) (tramp--test-sh-p)))
1355 (let ((tmp-name1 (tramp--test-make-temp-name))
1356 (tmp-name2 (tramp--test-make-temp-name))
1357 (tmp-name3 (tramp--test-make-temp-name)))
1358 (unwind-protect
1359 (progn
1360 (write-region "foo" nil tmp-name1)
1361 (should (file-exists-p tmp-name1))
1362 (should (consp (nth 5 (file-attributes tmp-name1))))
1363 ;; '(0 0) means don't know, and will be replaced by
1364 ;; `current-time'. Therefore, we use '(0 1).
1365 ;; We skip the test, if the remote handler is not able to
1366 ;; set the correct time.
1367 (skip-unless (set-file-times tmp-name1 '(0 1)))
1368 ;; Dumb remote shells without perl(1) or stat(1) are not
1369 ;; able to return the date correctly. They say "don't know".
1370 (unless (equal (nth 5 (file-attributes tmp-name1)) '(0 0))
1371 (should (equal (nth 5 (file-attributes tmp-name1)) '(0 1)))
1372 (write-region "bla" nil tmp-name2)
1373 (should (file-exists-p tmp-name2))
1374 (should (file-newer-than-file-p tmp-name2 tmp-name1))
1375 ;; `tmp-name3' does not exist.
1376 (should (file-newer-than-file-p tmp-name2 tmp-name3))
1377 (should-not (file-newer-than-file-p tmp-name3 tmp-name1))))
1379 ;; Cleanup.
1380 (ignore-errors
1381 (delete-file tmp-name1)
1382 (delete-file tmp-name2)))))
1384 (ert-deftest tramp-test23-visited-file-modtime ()
1385 "Check `set-visited-file-modtime' and `verify-visited-file-modtime'."
1386 (skip-unless (tramp--test-enabled))
1388 (let ((tmp-name (tramp--test-make-temp-name)))
1389 (unwind-protect
1390 (progn
1391 (write-region "foo" nil tmp-name)
1392 (should (file-exists-p tmp-name))
1393 (with-temp-buffer
1394 (insert-file-contents tmp-name)
1395 (should (verify-visited-file-modtime))
1396 (set-visited-file-modtime '(0 1))
1397 (should (verify-visited-file-modtime))
1398 (should (equal (visited-file-modtime) '(0 1 0 0)))))
1400 ;; Cleanup.
1401 (ignore-errors (delete-file tmp-name)))))
1403 (ert-deftest tramp-test24-file-name-completion ()
1404 "Check `file-name-completion' and `file-name-all-completions'."
1405 (skip-unless (tramp--test-enabled))
1407 (dolist (n-e '(nil t))
1408 (let ((non-essential n-e)
1409 (tmp-name (tramp--test-make-temp-name))
1410 (method (file-remote-p tramp-test-temporary-file-directory 'method))
1411 (host (file-remote-p tramp-test-temporary-file-directory 'host)))
1413 (unwind-protect
1414 (progn
1415 ;; Method and host name in completion mode. This kind of
1416 ;; completion does not work on MS Windows.
1417 (when (and (tramp-completion-mode-p)
1418 (not (memq system-type '(cygwin windows-nt))))
1419 (unless (zerop (length method))
1420 (should
1421 (member
1422 (format "%s:" method)
1423 (file-name-all-completions (substring method 0 1) "/"))))
1424 (unless (zerop (length host))
1425 (let ((tramp-default-method (or method tramp-default-method)))
1426 (should
1427 (member
1428 (format "%s:" host)
1429 (file-name-all-completions (substring host 0 1) "/")))))
1430 (unless (or (zerop (length method)) (zerop (length host)))
1431 (should
1432 (member
1433 (format "%s:" host)
1434 (file-name-all-completions
1435 (substring host 0 1) (format "/%s:" method))))))
1437 ;; Local files.
1438 (make-directory tmp-name)
1439 (should (file-directory-p tmp-name))
1440 (write-region "foo" nil (expand-file-name "foo" tmp-name))
1441 (should (file-exists-p (expand-file-name "foo" tmp-name)))
1442 (write-region "bar" nil (expand-file-name "bold" tmp-name))
1443 (should (file-exists-p (expand-file-name "bold" tmp-name)))
1444 (make-directory (expand-file-name "boz" tmp-name))
1445 (should (file-directory-p (expand-file-name "boz" tmp-name)))
1446 (should (equal (file-name-completion "fo" tmp-name) "foo"))
1447 (should (equal (file-name-completion "foo" tmp-name) t))
1448 (should (equal (file-name-completion "b" tmp-name) "bo"))
1449 (should-not (file-name-completion "a" tmp-name))
1450 (should
1451 (equal
1452 (file-name-completion "b" tmp-name 'file-directory-p) "boz/"))
1453 (should (equal (file-name-all-completions "fo" tmp-name) '("foo")))
1454 (should
1455 (equal
1456 (sort (file-name-all-completions "b" tmp-name) 'string-lessp)
1457 '("bold" "boz/")))
1458 (should-not (file-name-all-completions "a" tmp-name))
1459 ;; `completion-regexp-list' restricts the completion to
1460 ;; files which match all expressions in this list.
1461 (let ((completion-regexp-list
1462 `(,directory-files-no-dot-files-regexp "b")))
1463 (should
1464 (equal (file-name-completion "" tmp-name) "bo"))
1465 (should
1466 (equal
1467 (sort (file-name-all-completions "" tmp-name) 'string-lessp)
1468 '("bold" "boz/"))))
1469 ;; `file-name-completion' ignores file names that end in
1470 ;; any string in `completion-ignored-extensions'.
1471 (let ((completion-ignored-extensions '(".ext")))
1472 (write-region "foo" nil (expand-file-name "foo.ext" tmp-name))
1473 (should (file-exists-p (expand-file-name "foo.ext" tmp-name)))
1474 (should (equal (file-name-completion "fo" tmp-name) "foo"))
1475 (should (equal (file-name-completion "foo" tmp-name) t))
1476 (should (equal (file-name-completion "foo." tmp-name) "foo.ext"))
1477 (should (equal (file-name-completion "foo.ext" tmp-name) t))
1478 ;; `file-name-all-completions' is not affected.
1479 (should
1480 (equal
1481 (sort (file-name-all-completions "" tmp-name) 'string-lessp)
1482 '("../" "./" "bold" "boz/" "foo" "foo.ext")))))
1484 ;; Cleanup.
1485 (ignore-errors (delete-directory tmp-name 'recursive))))))
1487 (ert-deftest tramp-test25-load ()
1488 "Check `load'."
1489 (skip-unless (tramp--test-enabled))
1491 (let ((tmp-name (tramp--test-make-temp-name)))
1492 (unwind-protect
1493 (progn
1494 (load tmp-name 'noerror 'nomessage)
1495 (should-not (featurep 'tramp-test-load))
1496 (write-region "(provide 'tramp-test-load)" nil tmp-name)
1497 ;; `load' in lread.c does not pass `must-suffix'. Why?
1498 ;(should-error (load tmp-name nil 'nomessage 'nosuffix 'must-suffix))
1499 (load tmp-name nil 'nomessage 'nosuffix)
1500 (should (featurep 'tramp-test-load)))
1502 ;; Cleanup.
1503 (ignore-errors
1504 (and (featurep 'tramp-test-load) (unload-feature 'tramp-test-load))
1505 (delete-file tmp-name)))))
1507 (ert-deftest tramp-test26-process-file ()
1508 "Check `process-file'."
1509 :tags '(:expensive-test)
1510 (skip-unless (tramp--test-enabled))
1511 (skip-unless (or (tramp--test-adb-p) (tramp--test-sh-p)))
1513 (let* ((tmp-name (tramp--test-make-temp-name))
1514 (fnnd (file-name-nondirectory tmp-name))
1515 (default-directory tramp-test-temporary-file-directory)
1516 kill-buffer-query-functions)
1517 (unwind-protect
1518 (progn
1519 ;; We cannot use "/bin/true" and "/bin/false"; those paths
1520 ;; do not exist on hydra.
1521 (should (zerop (process-file "true")))
1522 (should-not (zerop (process-file "false")))
1523 (should-not (zerop (process-file "binary-does-not-exist")))
1524 (with-temp-buffer
1525 (write-region "foo" nil tmp-name)
1526 (should (file-exists-p tmp-name))
1527 (should (zerop (process-file "ls" nil t nil fnnd)))
1528 ;; `ls' could produce colorized output.
1529 (goto-char (point-min))
1530 (while
1531 (re-search-forward tramp-display-escape-sequence-regexp nil t)
1532 (replace-match "" nil nil))
1533 (should (string-equal (format "%s\n" fnnd) (buffer-string)))
1534 (should-not (get-buffer-window (current-buffer) t))
1536 ;; Second run. The output must be appended.
1537 (goto-char (point-max))
1538 (should (zerop (process-file "ls" nil t t fnnd)))
1539 ;; `ls' could produce colorized output.
1540 (goto-char (point-min))
1541 (while
1542 (re-search-forward tramp-display-escape-sequence-regexp nil t)
1543 (replace-match "" nil nil))
1544 (should
1545 (string-equal (format "%s\n%s\n" fnnd fnnd) (buffer-string)))
1546 ;; A non-nil DISPLAY must not raise the buffer.
1547 (should-not (get-buffer-window (current-buffer) t))))
1549 ;; Cleanup.
1550 (ignore-errors (delete-file tmp-name)))))
1552 (ert-deftest tramp-test27-start-file-process ()
1553 "Check `start-file-process'."
1554 :tags '(:expensive-test)
1555 (skip-unless (tramp--test-enabled))
1556 (skip-unless (tramp--test-sh-p))
1558 (let ((default-directory tramp-test-temporary-file-directory)
1559 (tmp-name (tramp--test-make-temp-name))
1560 kill-buffer-query-functions proc)
1561 (unwind-protect
1562 (with-temp-buffer
1563 (setq proc (start-file-process "test1" (current-buffer) "cat"))
1564 (should (processp proc))
1565 (should (equal (process-status proc) 'run))
1566 (process-send-string proc "foo")
1567 (process-send-eof proc)
1568 ;; Read output.
1569 (with-timeout (10 (ert-fail "`start-file-process' timed out"))
1570 (while (< (- (point-max) (point-min)) (length "foo"))
1571 (accept-process-output proc 0.1)))
1572 (should (string-equal (buffer-string) "foo")))
1574 ;; Cleanup.
1575 (ignore-errors (delete-process proc)))
1577 (unwind-protect
1578 (with-temp-buffer
1579 (write-region "foo" nil tmp-name)
1580 (should (file-exists-p tmp-name))
1581 (setq proc
1582 (start-file-process
1583 "test2" (current-buffer)
1584 "cat" (file-name-nondirectory tmp-name)))
1585 (should (processp proc))
1586 ;; Read output.
1587 (with-timeout (10 (ert-fail "`start-file-process' timed out"))
1588 (while (< (- (point-max) (point-min)) (length "foo"))
1589 (accept-process-output proc 0.1)))
1590 (should (string-equal (buffer-string) "foo")))
1592 ;; Cleanup.
1593 (ignore-errors
1594 (delete-process proc)
1595 (delete-file tmp-name)))
1597 (unwind-protect
1598 (with-temp-buffer
1599 (setq proc (start-file-process "test3" (current-buffer) "cat"))
1600 (should (processp proc))
1601 (should (equal (process-status proc) 'run))
1602 (set-process-filter
1603 proc
1604 (lambda (p s) (with-current-buffer (process-buffer p) (insert s))))
1605 (process-send-string proc "foo")
1606 (process-send-eof proc)
1607 ;; Read output.
1608 (with-timeout (10 (ert-fail "`start-file-process' timed out"))
1609 (while (< (- (point-max) (point-min)) (length "foo"))
1610 (accept-process-output proc 0.1)))
1611 (should (string-equal (buffer-string) "foo")))
1613 ;; Cleanup.
1614 (ignore-errors (delete-process proc)))))
1616 (ert-deftest tramp-test28-shell-command ()
1617 "Check `shell-command'."
1618 :tags '(:expensive-test)
1619 (skip-unless (tramp--test-enabled))
1620 (skip-unless (tramp--test-sh-p))
1622 (let ((tmp-name (tramp--test-make-temp-name))
1623 (default-directory tramp-test-temporary-file-directory)
1624 kill-buffer-query-functions)
1625 (unwind-protect
1626 (with-temp-buffer
1627 (write-region "foo" nil tmp-name)
1628 (should (file-exists-p tmp-name))
1629 (shell-command
1630 (format "ls %s" (file-name-nondirectory tmp-name)) (current-buffer))
1631 ;; `ls' could produce colorized output.
1632 (goto-char (point-min))
1633 (while (re-search-forward tramp-display-escape-sequence-regexp nil t)
1634 (replace-match "" nil nil))
1635 (should
1636 (string-equal
1637 (format "%s\n" (file-name-nondirectory tmp-name)) (buffer-string))))
1639 ;; Cleanup.
1640 (ignore-errors (delete-file tmp-name)))
1642 (unwind-protect
1643 (with-temp-buffer
1644 (write-region "foo" nil tmp-name)
1645 (should (file-exists-p tmp-name))
1646 (async-shell-command
1647 (format "ls %s" (file-name-nondirectory tmp-name)) (current-buffer))
1648 (set-process-sentinel (get-buffer-process (current-buffer)) nil)
1649 ;; Read output.
1650 (with-timeout (10 (ert-fail "`async-shell-command' timed out"))
1651 (while (< (- (point-max) (point-min))
1652 (1+ (length (file-name-nondirectory tmp-name))))
1653 (accept-process-output
1654 (get-buffer-process (current-buffer)) 0.1)))
1655 ;; `ls' could produce colorized output.
1656 (goto-char (point-min))
1657 (while (re-search-forward tramp-display-escape-sequence-regexp nil t)
1658 (replace-match "" nil nil))
1659 ;; There might be a nasty "Process *Async Shell* finished" message.
1660 (goto-char (point-min))
1661 (forward-line)
1662 (narrow-to-region (point-min) (point))
1663 (should
1664 (string-equal
1665 (format "%s\n" (file-name-nondirectory tmp-name)) (buffer-string))))
1667 ;; Cleanup.
1668 (ignore-errors (delete-file tmp-name)))
1670 (unwind-protect
1671 (with-temp-buffer
1672 (write-region "foo" nil tmp-name)
1673 (should (file-exists-p tmp-name))
1674 (async-shell-command "read line; ls $line" (current-buffer))
1675 (set-process-sentinel (get-buffer-process (current-buffer)) nil)
1676 (process-send-string
1677 (get-buffer-process (current-buffer))
1678 (format "%s\n" (file-name-nondirectory tmp-name)))
1679 ;; Read output.
1680 (with-timeout (10 (ert-fail "`async-shell-command' timed out"))
1681 (while (< (- (point-max) (point-min))
1682 (1+ (length (file-name-nondirectory tmp-name))))
1683 (accept-process-output
1684 (get-buffer-process (current-buffer)) 0.1)))
1685 ;; `ls' could produce colorized output.
1686 (goto-char (point-min))
1687 (while (re-search-forward tramp-display-escape-sequence-regexp nil t)
1688 (replace-match "" nil nil))
1689 ;; There might be a nasty "Process *Async Shell* finished" message.
1690 (goto-char (point-min))
1691 (forward-line)
1692 (narrow-to-region (point-min) (point))
1693 (should
1694 (string-equal
1695 (format "%s\n" (file-name-nondirectory tmp-name)) (buffer-string))))
1697 ;; Cleanup.
1698 (ignore-errors (delete-file tmp-name)))))
1700 (defun tramp-test--shell-command-to-string-asynchronously (command)
1701 "Like `shell-command-to-string', but for asynchronous processes."
1702 (with-temp-buffer
1703 (async-shell-command command (current-buffer))
1704 ;; Suppress nasty messages.
1705 (set-process-sentinel (get-buffer-process (current-buffer)) nil)
1706 (with-timeout (10)
1707 (while (get-buffer-process (current-buffer))
1708 (accept-process-output (get-buffer-process (current-buffer)) 0.1)))
1709 (accept-process-output nil 0.1)
1710 (buffer-substring-no-properties (point-min) (point-max))))
1712 ;; This test is inspired by Bug#23952.
1713 (ert-deftest tramp-test29-environment-variables ()
1714 "Check that remote processes set / unset environment variables properly."
1715 :tags '(:expensive-test)
1716 (skip-unless (tramp--test-enabled))
1717 (skip-unless (tramp--test-sh-p))
1719 (dolist (this-shell-command-to-string
1720 '(;; Synchronously.
1721 shell-command-to-string
1722 ;; Asynchronously.
1723 tramp-test--shell-command-to-string-asynchronously))
1725 (let ((default-directory tramp-test-temporary-file-directory)
1726 (shell-file-name "/bin/sh")
1727 (envvar (concat "VAR_" (upcase (md5 (current-time-string)))))
1728 kill-buffer-query-functions)
1730 (unwind-protect
1731 ;; Set a value.
1732 (let ((process-environment
1733 (cons (concat envvar "=foo") process-environment)))
1734 ;; Default value.
1735 (should
1736 (string-match
1737 "foo"
1738 (funcall
1739 this-shell-command-to-string
1740 (format "echo -n ${%s:?bla}" envvar))))))
1742 (unwind-protect
1743 ;; Set the empty value.
1744 (let ((process-environment
1745 (cons (concat envvar "=") process-environment)))
1746 ;; Value is null.
1747 (should
1748 (string-match
1749 "bla"
1750 (funcall
1751 this-shell-command-to-string
1752 (format "echo -n ${%s:?bla}" envvar))))
1753 ;; Variable is set.
1754 (should
1755 (string-match
1756 (regexp-quote envvar)
1757 (funcall this-shell-command-to-string "set")))))
1759 ;; We force a reconnect, in order to have a clean environment.
1760 (tramp-cleanup-connection
1761 (tramp-dissect-file-name tramp-test-temporary-file-directory)
1762 'keep-debug 'keep-password)
1763 (unwind-protect
1764 ;; Unset the variable.
1765 (let ((tramp-remote-process-environment
1766 (cons (concat envvar "=foo")
1767 tramp-remote-process-environment)))
1768 ;; Set the initial value, we want to unset below.
1769 (should
1770 (string-match
1771 "foo"
1772 (funcall
1773 this-shell-command-to-string
1774 (format "echo -n ${%s:?bla}" envvar))))
1775 (let ((process-environment
1776 (cons envvar process-environment)))
1777 ;; Variable is unset.
1778 (should
1779 (string-match
1780 "bla"
1781 (funcall
1782 this-shell-command-to-string
1783 (format "echo -n ${%s:?bla}" envvar))))
1784 ;; Variable is unset.
1785 (should-not
1786 (string-match
1787 (regexp-quote envvar)
1788 (funcall this-shell-command-to-string "set")))))))))
1790 (ert-deftest tramp-test30-vc-registered ()
1791 "Check `vc-registered'."
1792 :tags '(:expensive-test)
1793 (skip-unless (tramp--test-enabled))
1794 (skip-unless (tramp--test-sh-p))
1796 (let* ((default-directory tramp-test-temporary-file-directory)
1797 (tmp-name1 (tramp--test-make-temp-name))
1798 (tmp-name2 (expand-file-name "foo" tmp-name1))
1799 (tramp-remote-process-environment tramp-remote-process-environment)
1800 (vc-handled-backends
1801 (with-parsed-tramp-file-name tramp-test-temporary-file-directory nil
1802 (cond
1803 ((tramp-find-executable v vc-git-program (tramp-get-remote-path v))
1804 '(Git))
1805 ((tramp-find-executable v vc-hg-program (tramp-get-remote-path v))
1806 '(Hg))
1807 ((tramp-find-executable v vc-bzr-program (tramp-get-remote-path v))
1808 (setq tramp-remote-process-environment
1809 (cons (format "BZR_HOME=%s"
1810 (file-remote-p tmp-name1 'localname))
1811 tramp-remote-process-environment))
1812 ;; We must force a reconnect, in order to activate $BZR_HOME.
1813 (tramp-cleanup-connection
1814 (tramp-dissect-file-name tramp-test-temporary-file-directory)
1815 nil 'keep-password)
1816 '(Bzr))
1817 (t nil)))))
1818 (skip-unless vc-handled-backends)
1819 (message "%s" vc-handled-backends)
1821 (unwind-protect
1822 (progn
1823 (make-directory tmp-name1)
1824 (write-region "foo" nil tmp-name2)
1825 (should (file-directory-p tmp-name1))
1826 (should (file-exists-p tmp-name2))
1827 (should-not (vc-registered tmp-name1))
1828 (should-not (vc-registered tmp-name2))
1830 (let ((default-directory tmp-name1))
1831 ;; Create empty repository, and register the file.
1832 ;; Sometimes, creation of repository fails (bzr!); we skip
1833 ;; the test then.
1834 (condition-case nil
1835 (vc-create-repo (car vc-handled-backends))
1836 (error (skip-unless nil)))
1837 ;; The structure of VC-FILESET is not documented. Let's
1838 ;; hope it won't change.
1839 (condition-case nil
1840 (vc-register
1841 (list (car vc-handled-backends)
1842 (list (file-name-nondirectory tmp-name2))))
1843 ;; `vc-register' has changed its arguments in Emacs 25.1.
1844 (error
1845 (vc-register
1846 nil (list (car vc-handled-backends)
1847 (list (file-name-nondirectory tmp-name2))))))
1848 ;; vc-git uses an own process sentinel, Tramp's sentinel
1849 ;; for flushing the cache isn't used.
1850 (dired-uncache (concat (file-remote-p default-directory) "/"))
1851 (should (vc-registered (file-name-nondirectory tmp-name2)))))
1853 ;; Cleanup.
1854 (ignore-errors (delete-directory tmp-name1 'recursive)))))
1856 (ert-deftest tramp-test31-make-auto-save-file-name ()
1857 "Check `make-auto-save-file-name'."
1858 (skip-unless (tramp--test-enabled))
1860 (let ((tmp-name1 (tramp--test-make-temp-name))
1861 (tmp-name2 (tramp--test-make-temp-name)))
1863 (unwind-protect
1864 (progn
1865 ;; Use default `auto-save-file-name-transforms' mechanism.
1866 (let (tramp-auto-save-directory)
1867 (with-temp-buffer
1868 (setq buffer-file-name tmp-name1)
1869 (should
1870 (string-equal
1871 (make-auto-save-file-name)
1872 ;; This is taken from original `make-auto-save-file-name'.
1873 (expand-file-name
1874 (format
1875 "#%s#"
1876 (subst-char-in-string
1877 ?/ ?! (replace-regexp-in-string "!" "!!" tmp-name1)))
1878 temporary-file-directory)))))
1880 ;; No mapping.
1881 (let (tramp-auto-save-directory auto-save-file-name-transforms)
1882 (with-temp-buffer
1883 (setq buffer-file-name tmp-name1)
1884 (should
1885 (string-equal
1886 (make-auto-save-file-name)
1887 (expand-file-name
1888 (format "#%s#" (file-name-nondirectory tmp-name1))
1889 tramp-test-temporary-file-directory)))))
1891 ;; Use default `tramp-auto-save-directory' mechanism.
1892 (let ((tramp-auto-save-directory tmp-name2))
1893 (with-temp-buffer
1894 (setq buffer-file-name tmp-name1)
1895 (should
1896 (string-equal
1897 (make-auto-save-file-name)
1898 ;; This is taken from Tramp.
1899 (expand-file-name
1900 (format
1901 "#%s#"
1902 (tramp-subst-strs-in-string
1903 '(("_" . "|")
1904 ("/" . "_a")
1905 (":" . "_b")
1906 ("|" . "__")
1907 ("[" . "_l")
1908 ("]" . "_r"))
1909 tmp-name1))
1910 tmp-name2)))
1911 (should (file-directory-p tmp-name2))))
1913 ;; Relative file names shall work, too.
1914 (let ((tramp-auto-save-directory "."))
1915 (with-temp-buffer
1916 (setq buffer-file-name tmp-name1
1917 default-directory tmp-name2)
1918 (should
1919 (string-equal
1920 (make-auto-save-file-name)
1921 ;; This is taken from Tramp.
1922 (expand-file-name
1923 (format
1924 "#%s#"
1925 (tramp-subst-strs-in-string
1926 '(("_" . "|")
1927 ("/" . "_a")
1928 (":" . "_b")
1929 ("|" . "__")
1930 ("[" . "_l")
1931 ("]" . "_r"))
1932 tmp-name1))
1933 tmp-name2)))
1934 (should (file-directory-p tmp-name2)))))
1936 ;; Cleanup.
1937 (ignore-errors (delete-file tmp-name1))
1938 (ignore-errors (delete-directory tmp-name2 'recursive)))))
1940 ;; The functions have been introduced in Emacs 25.2.
1941 (ert-deftest tramp-test32-make-nearby-temp-file ()
1942 "Check `make-nearby-temp-file' and `temporary-file-directory'."
1943 (skip-unless (tramp--test-enabled))
1944 (skip-unless
1945 (and (fboundp 'make-nearby-temp-file) (fboundp 'temporary-file-directory)))
1947 (let ((default-directory tramp-test-temporary-file-directory)
1948 tmp-file)
1949 ;; The remote host shall know a temporary file directory.
1950 (should (stringp (temporary-file-directory)))
1951 (should
1952 (string-equal
1953 (file-remote-p default-directory)
1954 (file-remote-p (temporary-file-directory))))
1956 ;; The temporary file shall be located on the remote host.
1957 (setq tmp-file (make-nearby-temp-file "tramp-test"))
1958 (should (file-exists-p tmp-file))
1959 (should (file-regular-p tmp-file))
1960 (should
1961 (string-equal
1962 (file-remote-p default-directory)
1963 (file-remote-p tmp-file)))
1964 (delete-file tmp-file)
1965 (should-not (file-exists-p tmp-file))
1967 (setq tmp-file (make-nearby-temp-file "tramp-test" 'dir))
1968 (should (file-exists-p tmp-file))
1969 (should (file-directory-p tmp-file))
1970 (delete-directory tmp-file)
1971 (should-not (file-exists-p tmp-file))))
1973 (defun tramp--test-adb-p ()
1974 "Check, whether the remote host runs Android.
1975 This requires restrictions of file name syntax."
1976 (tramp-adb-file-name-p tramp-test-temporary-file-directory))
1978 (defun tramp--test-ftp-p ()
1979 "Check, whether an FTP-like method is used.
1980 This does not support globbing characters in file names (yet)."
1981 ;; Globbing characters are ??, ?* and ?\[.
1982 (string-match
1983 "ftp$" (file-remote-p tramp-test-temporary-file-directory 'method)))
1985 (defun tramp--test-gvfs-p ()
1986 "Check, whether the remote host runs a GVFS based method.
1987 This requires restrictions of file name syntax."
1988 (tramp-gvfs-file-name-p tramp-test-temporary-file-directory))
1990 (defun tramp--test-rsync-p ()
1991 "Check, whether the rsync method is used.
1992 This does not support special file names."
1993 (string-equal
1994 "rsync" (file-remote-p tramp-test-temporary-file-directory 'method)))
1996 (defun tramp--test-sh-p ()
1997 "Check, whether the remote host runs a based method from tramp-sh.el."
1999 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory)
2000 'tramp-sh-file-name-handler))
2002 (defun tramp--test-smb-or-windows-nt-p ()
2003 "Check, whether the locale or remote host runs MS Windows.
2004 This requires restrictions of file name syntax."
2005 (or (eq system-type 'windows-nt)
2006 (tramp-smb-file-name-p tramp-test-temporary-file-directory)))
2008 (defun tramp--test-hpux-p ()
2009 "Check, whether the remote host runs HP-UX.
2010 Several special characters do not work properly there."
2011 ;; We must refill the cache. `file-truename' does it.
2012 (with-parsed-tramp-file-name
2013 (file-truename tramp-test-temporary-file-directory) nil
2014 (string-match "^HP-UX" (tramp-get-connection-property v "uname" ""))))
2016 (defun tramp--test-check-files (&rest files)
2017 "Run a simple but comprehensive test over every file in FILES."
2018 ;; We must use `file-truename' for the temporary directory, because
2019 ;; it could be located on a symlinked directory. This would let the
2020 ;; test fail.
2021 (let* ((tramp-test-temporary-file-directory
2022 (file-truename tramp-test-temporary-file-directory))
2023 (tmp-name1 (tramp--test-make-temp-name))
2024 (tmp-name2 (tramp--test-make-temp-name 'local))
2025 (files (delq nil files)))
2026 (unwind-protect
2027 (progn
2028 (make-directory tmp-name1)
2029 (make-directory tmp-name2)
2030 (dolist (elt files)
2031 (let* ((file1 (expand-file-name elt tmp-name1))
2032 (file2 (expand-file-name elt tmp-name2))
2033 (file3 (expand-file-name (concat elt "foo") tmp-name1)))
2034 (write-region elt nil file1)
2035 (should (file-exists-p file1))
2037 ;; Check file contents.
2038 (with-temp-buffer
2039 (insert-file-contents file1)
2040 (should (string-equal (buffer-string) elt)))
2042 ;; Copy file both directions.
2043 (copy-file file1 tmp-name2)
2044 (should (file-exists-p file2))
2045 (delete-file file1)
2046 (should-not (file-exists-p file1))
2047 (copy-file file2 tmp-name1)
2048 (should (file-exists-p file1))
2050 ;; Method "smb" supports `make-symbolic-link' only if the
2051 ;; remote host has CIFS capabilities. tramp-adb.el and
2052 ;; tramp-gvfs.el do not support symbolic links at all.
2053 (condition-case err
2054 (progn
2055 (make-symbolic-link file1 file3)
2056 (should (file-symlink-p file3))
2057 (should
2058 (string-equal
2059 (expand-file-name file1) (file-truename file3)))
2060 (should
2061 (string-equal
2062 (car (file-attributes file3))
2063 (file-remote-p (file-truename file1) 'localname)))
2064 ;; Check file contents.
2065 (with-temp-buffer
2066 (insert-file-contents file3)
2067 (should (string-equal (buffer-string) elt)))
2068 (delete-file file3))
2069 (file-error
2070 (should (string-equal (error-message-string err)
2071 "make-symbolic-link not supported"))))))
2073 ;; Check file names.
2074 (should (equal (directory-files
2075 tmp-name1 nil directory-files-no-dot-files-regexp)
2076 (sort (copy-sequence files) 'string-lessp)))
2077 (should (equal (directory-files
2078 tmp-name2 nil directory-files-no-dot-files-regexp)
2079 (sort (copy-sequence files) 'string-lessp)))
2081 ;; `substitute-in-file-name' could return different values.
2082 ;; For `adb', there could be strange file permissions
2083 ;; preventing overwriting a file. We don't care in this
2084 ;; testcase.
2085 (dolist (elt files)
2086 (let ((file1
2087 (substitute-in-file-name (expand-file-name elt tmp-name1)))
2088 (file2
2089 (substitute-in-file-name (expand-file-name elt tmp-name2))))
2090 (ignore-errors (write-region elt nil file1))
2091 (should (file-exists-p file1))
2092 (ignore-errors (write-region elt nil file2 nil 'nomessage))
2093 (should (file-exists-p file2))))
2095 (should (equal (directory-files
2096 tmp-name1 nil directory-files-no-dot-files-regexp)
2097 (directory-files
2098 tmp-name2 nil directory-files-no-dot-files-regexp)))
2100 ;; Check directory creation. We use a subdirectory "foo"
2101 ;; in order to avoid conflicts with previous file name tests.
2102 (dolist (elt files)
2103 (let* ((elt1 (concat elt "foo"))
2104 (file1 (expand-file-name (concat "foo/" elt) tmp-name1))
2105 (file2 (expand-file-name elt file1))
2106 (file3 (expand-file-name elt1 file1)))
2107 (make-directory file1 'parents)
2108 (should (file-directory-p file1))
2109 (write-region elt nil file2)
2110 (should (file-exists-p file2))
2111 (should
2112 (equal
2113 (directory-files file1 nil directory-files-no-dot-files-regexp)
2114 `(,elt)))
2115 (should
2116 (equal
2117 (caar (directory-files-and-attributes
2118 file1 nil directory-files-no-dot-files-regexp))
2119 elt))
2121 ;; Check symlink in `directory-files-and-attributes'.
2122 (condition-case err
2123 (progn
2124 (make-symbolic-link file2 file3)
2125 (should (file-symlink-p file3))
2126 (should
2127 (string-equal
2128 (caar (directory-files-and-attributes
2129 file1 nil (regexp-quote elt1)))
2130 elt1))
2131 (should
2132 (string-equal
2133 (cadr (car (directory-files-and-attributes
2134 file1 nil (regexp-quote elt1))))
2135 (file-remote-p (file-truename file2) 'localname)))
2136 (delete-file file3)
2137 (should-not (file-exists-p file3)))
2138 (file-error
2139 (should (string-equal (error-message-string err)
2140 "make-symbolic-link not supported"))))
2142 (delete-file file2)
2143 (should-not (file-exists-p file2))
2144 (delete-directory file1)
2145 (should-not (file-exists-p file1)))))
2147 ;; Cleanup.
2148 (ignore-errors (delete-directory tmp-name1 'recursive))
2149 (ignore-errors (delete-directory tmp-name2 'recursive)))))
2151 (defun tramp--test-special-characters ()
2152 "Perform the test in `tramp-test33-special-characters*'."
2153 ;; Newlines, slashes and backslashes in file names are not
2154 ;; supported. So we don't test. And we don't test the tab
2155 ;; character on Windows or Cygwin, because the backslash is
2156 ;; interpreted as a path separator, preventing "\t" from being
2157 ;; expanded to <TAB>.
2158 (tramp--test-check-files
2159 (if (or (tramp--test-gvfs-p) (tramp--test-smb-or-windows-nt-p))
2160 "foo bar baz"
2161 (if (or (tramp--test-adb-p) (eq system-type 'cygwin))
2162 " foo bar baz "
2163 " foo\tbar baz\t"))
2164 "$foo$bar$$baz$"
2165 "-foo-bar-baz-"
2166 "%foo%bar%baz%"
2167 "&foo&bar&baz&"
2168 (unless (or (tramp--test-ftp-p)
2169 (tramp--test-gvfs-p)
2170 (tramp--test-smb-or-windows-nt-p))
2171 "?foo?bar?baz?")
2172 (unless (or (tramp--test-ftp-p)
2173 (tramp--test-gvfs-p)
2174 (tramp--test-smb-or-windows-nt-p))
2175 "*foo*bar*baz*")
2176 (if (or (tramp--test-gvfs-p) (tramp--test-smb-or-windows-nt-p))
2177 "'foo'bar'baz'"
2178 "'foo\"bar'baz\"")
2179 "#foo~bar#baz~"
2180 (if (or (tramp--test-gvfs-p) (tramp--test-smb-or-windows-nt-p))
2181 "!foo!bar!baz!"
2182 "!foo|bar!baz|")
2183 (if (or (tramp--test-gvfs-p) (tramp--test-smb-or-windows-nt-p))
2184 ";foo;bar;baz;"
2185 ":foo;bar:baz;")
2186 (unless (or (tramp--test-gvfs-p) (tramp--test-smb-or-windows-nt-p))
2187 "<foo>bar<baz>")
2188 "(foo)bar(baz)"
2189 (unless (or (tramp--test-ftp-p) (tramp--test-gvfs-p)) "[foo]bar[baz]")
2190 "{foo}bar{baz}"))
2192 ;; These tests are inspired by Bug#17238.
2193 (ert-deftest tramp-test33-special-characters ()
2194 "Check special characters in file names."
2195 (skip-unless (tramp--test-enabled))
2196 (skip-unless (not (tramp--test-rsync-p)))
2198 (tramp--test-special-characters))
2200 (ert-deftest tramp-test33-special-characters-with-stat ()
2201 "Check special characters in file names.
2202 Use the `stat' command."
2203 :tags '(:expensive-test)
2204 (skip-unless (tramp--test-enabled))
2205 (skip-unless (and (tramp--test-sh-p) (not (tramp--test-rsync-p))))
2206 (with-parsed-tramp-file-name tramp-test-temporary-file-directory nil
2207 (skip-unless (tramp-get-remote-stat v)))
2209 (let ((tramp-connection-properties
2210 (append
2211 `((,(regexp-quote (file-remote-p tramp-test-temporary-file-directory))
2212 "perl" nil))
2213 tramp-connection-properties)))
2214 (tramp--test-special-characters)))
2216 (ert-deftest tramp-test33-special-characters-with-perl ()
2217 "Check special characters in file names.
2218 Use the `perl' command."
2219 :tags '(:expensive-test)
2220 (skip-unless (tramp--test-enabled))
2221 (skip-unless (and (tramp--test-sh-p) (not (tramp--test-rsync-p))))
2222 (with-parsed-tramp-file-name tramp-test-temporary-file-directory nil
2223 (skip-unless (tramp-get-remote-perl v)))
2225 (let ((tramp-connection-properties
2226 (append
2227 `((,(regexp-quote (file-remote-p tramp-test-temporary-file-directory))
2228 "stat" nil)
2229 ;; See `tramp-sh-handle-file-truename'.
2230 (,(regexp-quote (file-remote-p tramp-test-temporary-file-directory))
2231 "readlink" nil))
2232 tramp-connection-properties)))
2233 (tramp--test-special-characters)))
2235 (ert-deftest tramp-test33-special-characters-with-ls ()
2236 "Check special characters in file names.
2237 Use the `ls' command."
2238 :tags '(:expensive-test)
2239 (skip-unless (tramp--test-enabled))
2240 (skip-unless (and (tramp--test-sh-p) (not (tramp--test-rsync-p))))
2242 (let ((tramp-connection-properties
2243 (append
2244 `((,(regexp-quote (file-remote-p tramp-test-temporary-file-directory))
2245 "perl" nil)
2246 (,(regexp-quote (file-remote-p tramp-test-temporary-file-directory))
2247 "stat" nil)
2248 ;; See `tramp-sh-handle-file-truename'.
2249 (,(regexp-quote (file-remote-p tramp-test-temporary-file-directory))
2250 "readlink" nil))
2251 tramp-connection-properties)))
2252 (tramp--test-special-characters)))
2254 (defun tramp--test-utf8 ()
2255 "Perform the test in `tramp-test34-utf8*'."
2256 (let* ((utf8 (if (and (eq system-type 'darwin)
2257 (memq 'utf-8-hfs (coding-system-list)))
2258 'utf-8-hfs 'utf-8))
2259 (coding-system-for-read utf8)
2260 (coding-system-for-write utf8)
2261 (file-name-coding-system utf8))
2262 (tramp--test-check-files
2263 (unless (tramp--test-hpux-p) "Γυρίστε το Γαλαξία με Ώτο Στοπ")
2264 (unless (tramp--test-hpux-p)
2265 "أصبح بوسعك الآن تنزيل نسخة كاملة من موسوعة ويكيبيديا العربية لتصفحها بلا اتصال بالإنترنت")
2266 "银河系漫游指南系列"
2267 "Автостопом по гала́ктике")))
2269 (ert-deftest tramp-test34-utf8 ()
2270 "Check UTF8 encoding in file names and file contents."
2271 (skip-unless (tramp--test-enabled))
2272 (skip-unless (not (tramp--test-rsync-p)))
2274 (tramp--test-utf8))
2276 (ert-deftest tramp-test34-utf8-with-stat ()
2277 "Check UTF8 encoding in file names and file contents.
2278 Use the `stat' command."
2279 :tags '(:expensive-test)
2280 (skip-unless (tramp--test-enabled))
2281 (skip-unless (and (tramp--test-sh-p) (not (tramp--test-rsync-p))))
2282 (with-parsed-tramp-file-name tramp-test-temporary-file-directory nil
2283 (skip-unless (tramp-get-remote-stat v)))
2285 (let ((tramp-connection-properties
2286 (append
2287 `((,(regexp-quote (file-remote-p tramp-test-temporary-file-directory))
2288 "perl" nil))
2289 tramp-connection-properties)))
2290 (tramp--test-utf8)))
2292 (ert-deftest tramp-test34-utf8-with-perl ()
2293 "Check UTF8 encoding in file names and file contents.
2294 Use the `perl' command."
2295 :tags '(:expensive-test)
2296 (skip-unless (tramp--test-enabled))
2297 (skip-unless (and (tramp--test-sh-p) (not (tramp--test-rsync-p))))
2298 (with-parsed-tramp-file-name tramp-test-temporary-file-directory nil
2299 (skip-unless (tramp-get-remote-perl v)))
2301 (let ((tramp-connection-properties
2302 (append
2303 `((,(regexp-quote (file-remote-p tramp-test-temporary-file-directory))
2304 "stat" nil)
2305 ;; See `tramp-sh-handle-file-truename'.
2306 (,(regexp-quote (file-remote-p tramp-test-temporary-file-directory))
2307 "readlink" nil))
2308 tramp-connection-properties)))
2309 (tramp--test-utf8)))
2311 (ert-deftest tramp-test34-utf8-with-ls ()
2312 "Check UTF8 encoding in file names and file contents.
2313 Use the `ls' command."
2314 :tags '(:expensive-test)
2315 (skip-unless (tramp--test-enabled))
2316 (skip-unless (and (tramp--test-sh-p) (not (tramp--test-rsync-p))))
2318 (let ((tramp-connection-properties
2319 (append
2320 `((,(regexp-quote (file-remote-p tramp-test-temporary-file-directory))
2321 "perl" nil)
2322 (,(regexp-quote (file-remote-p tramp-test-temporary-file-directory))
2323 "stat" nil)
2324 ;; See `tramp-sh-handle-file-truename'.
2325 (,(regexp-quote (file-remote-p tramp-test-temporary-file-directory))
2326 "readlink" nil))
2327 tramp-connection-properties)))
2328 (tramp--test-utf8)))
2330 ;; This test is inspired by Bug#16928.
2331 (ert-deftest tramp-test35-asynchronous-requests ()
2332 "Check parallel asynchronous requests.
2333 Such requests could arrive from timers, process filters and
2334 process sentinels. They shall not disturb each other."
2335 ;; Mark as failed until bug has been fixed.
2336 :expected-result :failed
2337 :tags '(:expensive-test)
2338 (skip-unless (tramp--test-enabled))
2339 (skip-unless (tramp--test-sh-p))
2341 ;; Keep instrumentation verbosity 0 until Tramp bug is fixed. This
2342 ;; has the side effect, that this test fails instead to abort. Good
2343 ;; for hydra.
2344 (tramp--instrument-test-case 0
2345 (let* ((tmp-name (tramp--test-make-temp-name))
2346 (default-directory tmp-name)
2347 (remote-file-name-inhibit-cache t)
2348 timer buffers kill-buffer-query-functions)
2350 (unwind-protect
2351 (progn
2352 (make-directory tmp-name)
2354 ;; Setup a timer in order to raise an ordinary command again
2355 ;; and again. `vc-registered' is well suited, because there
2356 ;; are many checks.
2357 (setq
2358 timer
2359 (run-at-time
2361 (lambda ()
2362 (when buffers
2363 (vc-registered
2364 (buffer-name (nth (random (length buffers)) buffers)))))))
2366 ;; Create temporary buffers. The number of buffers
2367 ;; corresponds to the number of processes; it could be
2368 ;; increased in order to make pressure on Tramp.
2369 (dotimes (i 5)
2370 (add-to-list 'buffers (generate-new-buffer "*temp*")))
2372 ;; Open asynchronous processes. Set process sentinel.
2373 (dolist (buf buffers)
2374 (async-shell-command "read line; touch $line; echo $line" buf)
2375 (set-process-sentinel
2376 (get-buffer-process buf)
2377 (lambda (proc _state)
2378 (delete-file (buffer-name (process-buffer proc))))))
2380 ;; Send a string. Use a random order of the buffers. Mix
2381 ;; with regular operation.
2382 (let ((buffers (copy-sequence buffers))
2383 buf)
2384 (while buffers
2385 (setq buf (nth (random (length buffers)) buffers))
2386 (process-send-string
2387 (get-buffer-process buf) (format "'%s'\n" buf))
2388 (file-attributes (buffer-name buf))
2389 (setq buffers (delq buf buffers))))
2391 ;; Wait until the whole output has been read.
2392 (with-timeout ((* 10 (length buffers))
2393 (ert-fail "`async-shell-command' timed out"))
2394 (let ((buffers (copy-sequence buffers))
2395 buf)
2396 (while buffers
2397 (setq buf (nth (random (length buffers)) buffers))
2398 (if (ignore-errors
2399 (memq (process-status (get-buffer-process buf))
2400 '(run open)))
2401 (accept-process-output (get-buffer-process buf) 0.1)
2402 (setq buffers (delq buf buffers))))))
2404 ;; Check.
2405 (dolist (buf buffers)
2406 (with-current-buffer buf
2407 (should
2408 (string-equal (format "'%s'\n" buf) (buffer-string)))))
2409 (should-not
2410 (directory-files tmp-name nil directory-files-no-dot-files-regexp)))
2412 ;; Cleanup.
2413 (ignore-errors (cancel-timer timer))
2414 (ignore-errors (delete-directory tmp-name 'recursive))
2415 (dolist (buf buffers)
2416 (ignore-errors (kill-buffer buf)))))))
2418 (ert-deftest tramp-test36-recursive-load ()
2419 "Check that Tramp does not fail due to recursive load."
2420 (skip-unless (tramp--test-enabled))
2422 (dolist (code
2423 (list
2424 (format
2425 "(expand-file-name %S)"
2426 tramp-test-temporary-file-directory)
2427 (format
2428 "(let ((default-directory %S)) (expand-file-name %S))"
2429 tramp-test-temporary-file-directory
2430 temporary-file-directory)))
2431 (should-not
2432 (string-match
2433 "Recursive load"
2434 (shell-command-to-string
2435 (format
2436 "%s -batch -Q -L %s --eval %s"
2437 (expand-file-name invocation-name invocation-directory)
2438 (mapconcat 'shell-quote-argument load-path " -L ")
2439 (shell-quote-argument code)))))))
2441 (ert-deftest tramp-test37-unload ()
2442 "Check that Tramp and its subpackages unload completely.
2443 Since it unloads Tramp, it shall be the last test to run."
2444 ;; Mark as failed until all symbols are unbound.
2445 :expected-result (if (featurep 'tramp) :failed :passed)
2446 :tags '(:expensive-test)
2447 (when (featurep 'tramp)
2448 (unload-feature 'tramp 'force)
2449 ;; No Tramp feature must be left.
2450 (should-not (featurep 'tramp))
2451 (should-not (all-completions "tramp" (delq 'tramp-tests features)))
2452 ;; `file-name-handler-alist' must be clean.
2453 (should-not (all-completions "tramp" (mapcar 'cdr file-name-handler-alist)))
2454 ;; There shouldn't be left a bound symbol. We do not regard our
2455 ;; test symbols, and the Tramp unload hooks.
2456 (mapatoms
2457 (lambda (x)
2458 (and (or (boundp x) (functionp x))
2459 (string-match "^tramp" (symbol-name x))
2460 (not (string-match "^tramp--?test" (symbol-name x)))
2461 (not (string-match "unload-hook$" (symbol-name x)))
2462 (ert-fail (format "`%s' still bound" x)))))
2463 ;; There shouldn't be left a hook function containing a Tramp
2464 ;; function. We do not regard the Tramp unload hooks.
2465 (mapatoms
2466 (lambda (x)
2467 (and (boundp x)
2468 (string-match "-hooks?$" (symbol-name x))
2469 (not (string-match "unload-hook$" (symbol-name x)))
2470 (consp (symbol-value x))
2471 (ignore-errors (all-completions "tramp" (symbol-value x)))
2472 (ert-fail (format "Hook `%s' still contains Tramp function" x)))))))
2474 ;; TODO:
2476 ;; * dired-compress-file
2477 ;; * dired-uncache
2478 ;; * file-acl
2479 ;; * file-selinux-context
2480 ;; * find-backup-file-name
2481 ;; * set-file-acl
2482 ;; * set-file-selinux-context
2484 ;; * Work on skipped tests. Make a comment, when it is impossible.
2485 ;; * Fix `tramp-test06-directory-file-name' for `ftp'.
2486 ;; * Fix `tramp-test27-start-file-process' on MS Windows (`process-send-eof'?).
2487 ;; * Fix Bug#16928. Set expected error of `tramp-test35-asynchronous-requests'.
2488 ;; * Fix `tramp-test37-unload' (Not all symbols are unbound). Set
2489 ;; expected error.
2491 (defun tramp-test-all (&optional interactive)
2492 "Run all tests for \\[tramp]."
2493 (interactive "p")
2494 (funcall
2495 (if interactive 'ert-run-tests-interactively 'ert-run-tests-batch) "^tramp"))
2497 (provide 'tramp-tests)
2498 ;;; tramp-tests.el ends here