Update copyright year to 2015
[emacs.git] / test / automated / tramp-tests.el
blob2c4610c811331909cc88b4ec587c7bbab99f5704
1 ;;; tramp-tests.el --- Tests of remote file access
3 ;; Copyright (C) 2013-2015 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 (declare-function tramp-find-executable "tramp-sh")
48 (declare-function tramp-get-remote-path "tramp-sh")
49 (defvar tramp-copy-size-limit)
50 (defvar tramp-remote-process-environment)
52 ;; There is no default value on w32 systems, which could work out of the box.
53 (defconst tramp-test-temporary-file-directory
54 (cond
55 ((getenv "REMOTE_TEMPORARY_FILE_DIRECTORY"))
56 ((eq system-type 'windows-nt) null-device)
57 (t (add-to-list
58 'tramp-methods
59 '("mock"
60 (tramp-login-program "sh")
61 (tramp-login-args (("-i")))
62 (tramp-remote-shell "/bin/sh")
63 (tramp-remote-shell-args ("-c"))
64 (tramp-connection-timeout 10)))
65 (format "/mock::%s" temporary-file-directory)))
66 "Temporary directory for Tramp tests.")
68 (setq password-cache-expiry nil
69 tramp-verbose 0
70 tramp-copy-size-limit nil
71 tramp-message-show-message nil)
73 ;; This shall happen on hydra only.
74 (when (getenv "NIX_STORE")
75 (add-to-list 'tramp-remote-path 'tramp-own-remote-path))
77 (defvar tramp--test-enabled-checked nil
78 "Cached result of `tramp--test-enabled'.
79 If the function did run, the value is a cons cell, the `cdr'
80 being the result.")
82 (defun tramp--test-enabled ()
83 "Whether remote file access is enabled."
84 (unless (consp tramp--test-enabled-checked)
85 (setq
86 tramp--test-enabled-checked
87 (cons
88 t (ignore-errors
89 (and
90 (file-remote-p tramp-test-temporary-file-directory)
91 (file-directory-p tramp-test-temporary-file-directory)
92 (file-writable-p tramp-test-temporary-file-directory))))))
94 (when (cdr tramp--test-enabled-checked)
95 ;; Cleanup connection.
96 (ignore-errors
97 (tramp-cleanup-connection
98 (tramp-dissect-file-name tramp-test-temporary-file-directory)
99 nil 'keep-password)))
101 ;; Return result.
102 (cdr tramp--test-enabled-checked))
104 (defun tramp--test-make-temp-name (&optional local)
105 "Create a temporary file name for test."
106 (expand-file-name
107 (make-temp-name "tramp-test")
108 (if local temporary-file-directory tramp-test-temporary-file-directory)))
110 (defmacro tramp--instrument-test-case (verbose &rest body)
111 "Run BODY with `tramp-verbose' equal VERBOSE.
112 Print the the content of the Tramp debug buffer, if BODY does not
113 eval properly in `should', `should-not' or `should-error'. BODY
114 shall not contain a timeout."
115 (declare (indent 1) (debug (natnump body)))
116 `(let ((tramp-verbose ,verbose)
117 (tramp-message-show-message t)
118 (tramp-debug-on-error t))
119 (unwind-protect
120 (progn ,@body)
121 (when (> tramp-verbose 3)
122 (with-parsed-tramp-file-name tramp-test-temporary-file-directory nil
123 (with-current-buffer (tramp-get-connection-buffer v)
124 (message "%s" (buffer-string)))
125 (with-current-buffer
126 (tramp-get-debug-buffer v)
127 (message "%s" (buffer-string))))))))
129 (ert-deftest tramp-test00-availability ()
130 "Test availability of Tramp functions."
131 :expected-result (if (tramp--test-enabled) :passed :failed)
132 (message "Remote directory: `%s'" tramp-test-temporary-file-directory)
133 (should (ignore-errors
134 (and
135 (file-remote-p tramp-test-temporary-file-directory)
136 (file-directory-p tramp-test-temporary-file-directory)
137 (file-writable-p tramp-test-temporary-file-directory)))))
139 (ert-deftest tramp-test01-file-name-syntax ()
140 "Check remote file name syntax."
141 ;; Simple cases.
142 (should (tramp-tramp-file-p "/method::"))
143 (should (tramp-tramp-file-p "/host:"))
144 (should (tramp-tramp-file-p "/user@:"))
145 (should (tramp-tramp-file-p "/user@host:"))
146 (should (tramp-tramp-file-p "/method:host:"))
147 (should (tramp-tramp-file-p "/method:user@:"))
148 (should (tramp-tramp-file-p "/method:user@host:"))
149 (should (tramp-tramp-file-p "/method:user@email@host:"))
151 ;; Using a port.
152 (should (tramp-tramp-file-p "/host#1234:"))
153 (should (tramp-tramp-file-p "/user@host#1234:"))
154 (should (tramp-tramp-file-p "/method:host#1234:"))
155 (should (tramp-tramp-file-p "/method:user@host#1234:"))
157 ;; Using an IPv4 address.
158 (should (tramp-tramp-file-p "/1.2.3.4:"))
159 (should (tramp-tramp-file-p "/user@1.2.3.4:"))
160 (should (tramp-tramp-file-p "/method:1.2.3.4:"))
161 (should (tramp-tramp-file-p "/method:user@1.2.3.4:"))
163 ;; Using an IPv6 address.
164 (should (tramp-tramp-file-p "/[]:"))
165 (should (tramp-tramp-file-p "/[::1]:"))
166 (should (tramp-tramp-file-p "/user@[::1]:"))
167 (should (tramp-tramp-file-p "/method:[::1]:"))
168 (should (tramp-tramp-file-p "/method:user@[::1]:"))
170 ;; Local file name part.
171 (should (tramp-tramp-file-p "/host:/:"))
172 (should (tramp-tramp-file-p "/method:::"))
173 (should (tramp-tramp-file-p "/method::/path/to/file"))
174 (should (tramp-tramp-file-p "/method::file"))
176 ;; Multihop.
177 (should (tramp-tramp-file-p "/method1:|method2::"))
178 (should (tramp-tramp-file-p "/method1:host1|host2:"))
179 (should (tramp-tramp-file-p "/method1:host1|method2:host2:"))
180 (should (tramp-tramp-file-p "/method1:user1@host1|method2:user2@host2:"))
181 (should (tramp-tramp-file-p
182 "/method1:user1@host1|method2:user2@host2|method3:user3@host3:"))
184 ;; No strings.
185 (should-not (tramp-tramp-file-p nil))
186 (should-not (tramp-tramp-file-p 'symbol))
187 ;; "/:" suppresses file name handlers.
188 (should-not (tramp-tramp-file-p "/::"))
189 (should-not (tramp-tramp-file-p "/:@:"))
190 (should-not (tramp-tramp-file-p "/:[]:"))
191 ;; Multihops require a method.
192 (should-not (tramp-tramp-file-p "/host1|host2:"))
193 ;; Methods or hostnames shall be at least two characters on MS Windows.
194 (when (memq system-type '(cygwin windows-nt))
195 (should-not (tramp-tramp-file-p "/c:/path/to/file"))
196 (should-not (tramp-tramp-file-p "/c::/path/to/file"))))
198 (ert-deftest tramp-test02-file-name-dissect ()
199 "Check remote file name components."
200 (let ((tramp-default-method "default-method")
201 (tramp-default-user "default-user")
202 (tramp-default-host "default-host"))
203 ;; Expand `tramp-default-user' and `tramp-default-host'.
204 (should (string-equal
205 (file-remote-p "/method::")
206 (format "/%s:%s@%s:" "method" "default-user" "default-host")))
207 (should (string-equal (file-remote-p "/method::" 'method) "method"))
208 (should (string-equal (file-remote-p "/method::" 'user) "default-user"))
209 (should (string-equal (file-remote-p "/method::" 'host) "default-host"))
210 (should (string-equal (file-remote-p "/method::" 'localname) ""))
212 ;; Expand `tramp-default-method' and `tramp-default-user'.
213 (should (string-equal
214 (file-remote-p "/host:")
215 (format "/%s:%s@%s:" "default-method" "default-user" "host")))
216 (should (string-equal (file-remote-p "/host:" 'method) "default-method"))
217 (should (string-equal (file-remote-p "/host:" 'user) "default-user"))
218 (should (string-equal (file-remote-p "/host:" 'host) "host"))
219 (should (string-equal (file-remote-p "/host:" 'localname) ""))
221 ;; Expand `tramp-default-method' and `tramp-default-host'.
222 (should (string-equal
223 (file-remote-p "/user@:")
224 (format "/%s:%s@%s:" "default-method""user" "default-host")))
225 (should (string-equal (file-remote-p "/user@:" 'method) "default-method"))
226 (should (string-equal (file-remote-p "/user@:" 'user) "user"))
227 (should (string-equal (file-remote-p "/user@:" 'host) "default-host"))
228 (should (string-equal (file-remote-p "/user@:" 'localname) ""))
230 ;; Expand `tramp-default-method'.
231 (should (string-equal
232 (file-remote-p "/user@host:")
233 (format "/%s:%s@%s:" "default-method" "user" "host")))
234 (should (string-equal
235 (file-remote-p "/user@host:" 'method) "default-method"))
236 (should (string-equal (file-remote-p "/user@host:" 'user) "user"))
237 (should (string-equal (file-remote-p "/user@host:" 'host) "host"))
238 (should (string-equal (file-remote-p "/user@host:" 'localname) ""))
240 ;; Expand `tramp-default-user'.
241 (should (string-equal
242 (file-remote-p "/method:host:")
243 (format "/%s:%s@%s:" "method" "default-user" "host")))
244 (should (string-equal (file-remote-p "/method:host:" 'method) "method"))
245 (should (string-equal (file-remote-p "/method:host:" 'user) "default-user"))
246 (should (string-equal (file-remote-p "/method:host:" 'host) "host"))
247 (should (string-equal (file-remote-p "/method:host:" 'localname) ""))
249 ;; Expand `tramp-default-host'.
250 (should (string-equal
251 (file-remote-p "/method:user@:")
252 (format "/%s:%s@%s:" "method" "user" "default-host")))
253 (should (string-equal (file-remote-p "/method:user@:" 'method) "method"))
254 (should (string-equal (file-remote-p "/method:user@:" 'user) "user"))
255 (should (string-equal (file-remote-p "/method:user@:" 'host)
256 "default-host"))
257 (should (string-equal (file-remote-p "/method:user@:" 'localname) ""))
259 ;; No expansion.
260 (should (string-equal
261 (file-remote-p "/method:user@host:")
262 (format "/%s:%s@%s:" "method" "user" "host")))
263 (should (string-equal
264 (file-remote-p "/method:user@host:" 'method) "method"))
265 (should (string-equal (file-remote-p "/method:user@host:" 'user) "user"))
266 (should (string-equal (file-remote-p "/method:user@host:" 'host) "host"))
267 (should (string-equal (file-remote-p "/method:user@host:" 'localname) ""))
269 ;; No expansion.
270 (should (string-equal
271 (file-remote-p "/method:user@email@host:")
272 (format "/%s:%s@%s:" "method" "user@email" "host")))
273 (should (string-equal
274 (file-remote-p "/method:user@email@host:" 'method) "method"))
275 (should (string-equal
276 (file-remote-p "/method:user@email@host:" 'user) "user@email"))
277 (should (string-equal
278 (file-remote-p "/method:user@email@host:" 'host) "host"))
279 (should (string-equal
280 (file-remote-p "/method:user@email@host:" 'localname) ""))
282 ;; Expand `tramp-default-method' and `tramp-default-user'.
283 (should (string-equal
284 (file-remote-p "/host#1234:")
285 (format "/%s:%s@%s:" "default-method" "default-user" "host#1234")))
286 (should (string-equal
287 (file-remote-p "/host#1234:" 'method) "default-method"))
288 (should (string-equal (file-remote-p "/host#1234:" 'user) "default-user"))
289 (should (string-equal (file-remote-p "/host#1234:" 'host) "host#1234"))
290 (should (string-equal (file-remote-p "/host#1234:" 'localname) ""))
292 ;; Expand `tramp-default-method'.
293 (should (string-equal
294 (file-remote-p "/user@host#1234:")
295 (format "/%s:%s@%s:" "default-method" "user" "host#1234")))
296 (should (string-equal
297 (file-remote-p "/user@host#1234:" 'method) "default-method"))
298 (should (string-equal (file-remote-p "/user@host#1234:" 'user) "user"))
299 (should (string-equal (file-remote-p "/user@host#1234:" 'host) "host#1234"))
300 (should (string-equal (file-remote-p "/user@host#1234:" 'localname) ""))
302 ;; Expand `tramp-default-user'.
303 (should (string-equal
304 (file-remote-p "/method:host#1234:")
305 (format "/%s:%s@%s:" "method" "default-user" "host#1234")))
306 (should (string-equal
307 (file-remote-p "/method:host#1234:" 'method) "method"))
308 (should (string-equal
309 (file-remote-p "/method:host#1234:" 'user) "default-user"))
310 (should (string-equal
311 (file-remote-p "/method:host#1234:" 'host) "host#1234"))
312 (should (string-equal (file-remote-p "/method:host#1234:" 'localname) ""))
314 ;; No expansion.
315 (should (string-equal
316 (file-remote-p "/method:user@host#1234:")
317 (format "/%s:%s@%s:" "method" "user" "host#1234")))
318 (should (string-equal
319 (file-remote-p "/method:user@host#1234:" 'method) "method"))
320 (should (string-equal
321 (file-remote-p "/method:user@host#1234:" 'user) "user"))
322 (should (string-equal
323 (file-remote-p "/method:user@host#1234:" 'host) "host#1234"))
324 (should (string-equal
325 (file-remote-p "/method:user@host#1234:" 'localname) ""))
327 ;; Expand `tramp-default-method' and `tramp-default-user'.
328 (should (string-equal
329 (file-remote-p "/1.2.3.4:")
330 (format "/%s:%s@%s:" "default-method" "default-user" "1.2.3.4")))
331 (should (string-equal (file-remote-p "/1.2.3.4:" 'method) "default-method"))
332 (should (string-equal (file-remote-p "/1.2.3.4:" 'user) "default-user"))
333 (should (string-equal (file-remote-p "/1.2.3.4:" 'host) "1.2.3.4"))
334 (should (string-equal (file-remote-p "/1.2.3.4:" 'localname) ""))
336 ;; Expand `tramp-default-method'.
337 (should (string-equal
338 (file-remote-p "/user@1.2.3.4:")
339 (format "/%s:%s@%s:" "default-method" "user" "1.2.3.4")))
340 (should (string-equal
341 (file-remote-p "/user@1.2.3.4:" 'method) "default-method"))
342 (should (string-equal (file-remote-p "/user@1.2.3.4:" 'user) "user"))
343 (should (string-equal (file-remote-p "/user@1.2.3.4:" 'host) "1.2.3.4"))
344 (should (string-equal (file-remote-p "/user@1.2.3.4:" 'localname) ""))
346 ;; Expand `tramp-default-user'.
347 (should (string-equal
348 (file-remote-p "/method:1.2.3.4:")
349 (format "/%s:%s@%s:" "method" "default-user" "1.2.3.4")))
350 (should (string-equal (file-remote-p "/method:1.2.3.4:" 'method) "method"))
351 (should (string-equal
352 (file-remote-p "/method:1.2.3.4:" 'user) "default-user"))
353 (should (string-equal (file-remote-p "/method:1.2.3.4:" 'host) "1.2.3.4"))
354 (should (string-equal (file-remote-p "/method:1.2.3.4:" 'localname) ""))
356 ;; No expansion.
357 (should (string-equal
358 (file-remote-p "/method:user@1.2.3.4:")
359 (format "/%s:%s@%s:" "method" "user" "1.2.3.4")))
360 (should (string-equal
361 (file-remote-p "/method:user@1.2.3.4:" 'method) "method"))
362 (should (string-equal (file-remote-p "/method:user@1.2.3.4:" 'user) "user"))
363 (should (string-equal
364 (file-remote-p "/method:user@1.2.3.4:" 'host) "1.2.3.4"))
365 (should (string-equal
366 (file-remote-p "/method:user@1.2.3.4:" 'localname) ""))
368 ;; Expand `tramp-default-method', `tramp-default-user' and
369 ;; `tramp-default-host'.
370 (should (string-equal
371 (file-remote-p "/[]:")
372 (format
373 "/%s:%s@%s:" "default-method" "default-user" "default-host")))
374 (should (string-equal (file-remote-p "/[]:" 'method) "default-method"))
375 (should (string-equal (file-remote-p "/[]:" 'user) "default-user"))
376 (should (string-equal (file-remote-p "/[]:" 'host) "default-host"))
377 (should (string-equal (file-remote-p "/[]:" 'localname) ""))
379 ;; Expand `tramp-default-method' and `tramp-default-user'.
380 (let ((tramp-default-host "::1"))
381 (should (string-equal
382 (file-remote-p "/[]:")
383 (format "/%s:%s@%s:" "default-method" "default-user" "[::1]")))
384 (should (string-equal (file-remote-p "/[]:" 'method) "default-method"))
385 (should (string-equal (file-remote-p "/[]:" 'user) "default-user"))
386 (should (string-equal (file-remote-p "/[]:" 'host) "::1"))
387 (should (string-equal (file-remote-p "/[]:" 'localname) "")))
389 ;; Expand `tramp-default-method' and `tramp-default-user'.
390 (should (string-equal
391 (file-remote-p "/[::1]:")
392 (format "/%s:%s@%s:" "default-method" "default-user" "[::1]")))
393 (should (string-equal (file-remote-p "/[::1]:" 'method) "default-method"))
394 (should (string-equal (file-remote-p "/[::1]:" 'user) "default-user"))
395 (should (string-equal (file-remote-p "/[::1]:" 'host) "::1"))
396 (should (string-equal (file-remote-p "/[::1]:" 'localname) ""))
398 ;; Expand `tramp-default-method'.
399 (should (string-equal
400 (file-remote-p "/user@[::1]:")
401 (format "/%s:%s@%s:" "default-method" "user" "[::1]")))
402 (should (string-equal
403 (file-remote-p "/user@[::1]:" 'method) "default-method"))
404 (should (string-equal (file-remote-p "/user@[::1]:" 'user) "user"))
405 (should (string-equal (file-remote-p "/user@[::1]:" 'host) "::1"))
406 (should (string-equal (file-remote-p "/user@[::1]:" 'localname) ""))
408 ;; Expand `tramp-default-user'.
409 (should (string-equal
410 (file-remote-p "/method:[::1]:")
411 (format "/%s:%s@%s:" "method" "default-user" "[::1]")))
412 (should (string-equal (file-remote-p "/method:[::1]:" 'method) "method"))
413 (should (string-equal
414 (file-remote-p "/method:[::1]:" 'user) "default-user"))
415 (should (string-equal (file-remote-p "/method:[::1]:" 'host) "::1"))
416 (should (string-equal (file-remote-p "/method:[::1]:" 'localname) ""))
418 ;; No expansion.
419 (should (string-equal
420 (file-remote-p "/method:user@[::1]:")
421 (format "/%s:%s@%s:" "method" "user" "[::1]")))
422 (should (string-equal
423 (file-remote-p "/method:user@[::1]:" 'method) "method"))
424 (should (string-equal (file-remote-p "/method:user@[::1]:" 'user) "user"))
425 (should (string-equal (file-remote-p "/method:user@[::1]:" 'host) "::1"))
426 (should (string-equal
427 (file-remote-p "/method:user@[::1]:" 'localname) ""))
429 ;; Local file name part.
430 (should (string-equal (file-remote-p "/host:/:" 'localname) "/:"))
431 (should (string-equal (file-remote-p "/method:::" 'localname) ":"))
432 (should (string-equal (file-remote-p "/method:: " 'localname) " "))
433 (should (string-equal (file-remote-p "/method::file" 'localname) "file"))
434 (should (string-equal
435 (file-remote-p "/method::/path/to/file" 'localname)
436 "/path/to/file"))
438 ;; Multihop.
439 (should
440 (string-equal
441 (file-remote-p "/method1:user1@host1|method2:user2@host2:/path/to/file")
442 (format "/%s:%s@%s:" "method2" "user2" "host2")))
443 (should
444 (string-equal
445 (file-remote-p
446 "/method1:user1@host1|method2:user2@host2:/path/to/file" 'method)
447 "method2"))
448 (should
449 (string-equal
450 (file-remote-p
451 "/method1:user1@host1|method2:user2@host2:/path/to/file" 'user)
452 "user2"))
453 (should
454 (string-equal
455 (file-remote-p
456 "/method1:user1@host1|method2:user2@host2:/path/to/file" 'host)
457 "host2"))
458 (should
459 (string-equal
460 (file-remote-p
461 "/method1:user1@host1|method2:user2@host2:/path/to/file" 'localname)
462 "/path/to/file"))
464 (should
465 (string-equal
466 (file-remote-p
467 "/method1:user1@host1|method2:user2@host2|method3:user3@host3:/path/to/file")
468 (format "/%s:%s@%s:" "method3" "user3" "host3")))
469 (should
470 (string-equal
471 (file-remote-p
472 "/method1:user1@host1|method2:user2@host2|method3:user3@host3:/path/to/file"
473 'method)
474 "method3"))
475 (should
476 (string-equal
477 (file-remote-p
478 "/method1:user1@host1|method2:user2@host2|method3:user3@host3:/path/to/file"
479 'user)
480 "user3"))
481 (should
482 (string-equal
483 (file-remote-p
484 "/method1:user1@host1|method2:user2@host2|method3:user3@host3:/path/to/file"
485 'host)
486 "host3"))
487 (should
488 (string-equal
489 (file-remote-p
490 "/method1:user1@host1|method2:user2@host2|method3:user3@host3:/path/to/file"
491 'localname)
492 "/path/to/file"))))
494 (ert-deftest tramp-test03-file-name-defaults ()
495 "Check default values for some methods."
496 ;; Default values in tramp-adb.el.
497 (should (string-equal (file-remote-p "/adb::" 'host) ""))
498 ;; Default values in tramp-ftp.el.
499 (should (string-equal (file-remote-p "/ftp.host:" 'method) "ftp"))
500 (dolist (u '("ftp" "anonymous"))
501 (should (string-equal (file-remote-p (format "/%s@:" u) 'method) "ftp")))
502 ;; Default values in tramp-gvfs.el.
503 (when (and (load "tramp-gvfs" 'noerror 'nomessage)
504 (symbol-value 'tramp-gvfs-enabled))
505 (should (string-equal (file-remote-p "/synce::" 'user) nil)))
506 ;; Default values in tramp-gw.el.
507 (dolist (m '("tunnel" "socks"))
508 (should
509 (string-equal (file-remote-p (format "/%s::" m) 'user) (user-login-name))))
510 ;; Default values in tramp-sh.el.
511 (dolist (h `("127.0.0.1" "[::1]" "localhost" "localhost6" ,(system-name)))
512 (should (string-equal (file-remote-p (format "/root@%s:" h) 'method) "su")))
513 (dolist (m '("su" "sudo" "ksu"))
514 (should (string-equal (file-remote-p (format "/%s::" m) 'user) "root")))
515 (dolist (m '("rcp" "remcp" "rsh" "telnet" "krlogin" "fcp"))
516 (should
517 (string-equal (file-remote-p (format "/%s::" m) 'user) (user-login-name))))
518 ;; Default values in tramp-smb.el.
519 (should (string-equal (file-remote-p "/user%domain@host:" 'method) "smb"))
520 (should (string-equal (file-remote-p "/smb::" 'user) nil)))
522 (ert-deftest tramp-test04-substitute-in-file-name ()
523 "Check `substitute-in-file-name'."
524 (should (string-equal (substitute-in-file-name "/method:host://foo") "/foo"))
525 (should
526 (string-equal
527 (substitute-in-file-name "/method:host:/path//foo") "/method:host:/foo"))
528 (should
529 (string-equal (substitute-in-file-name "/method:host:/path///foo") "/foo"))
530 (should
531 (string-equal
532 (substitute-in-file-name "/method:host:/path/~/foo") "/method:host:~/foo"))
533 (should
534 (string-equal (substitute-in-file-name "/method:host:/path//~/foo") "~/foo"))
535 (let (process-environment)
536 (should
537 (string-equal
538 (substitute-in-file-name "/method:host:/path/$FOO")
539 "/method:host:/path/$FOO"))
540 (setenv "FOO" "bla")
541 (should
542 (string-equal
543 (substitute-in-file-name "/method:host:/path/$FOO")
544 "/method:host:/path/bla"))
545 (should
546 (string-equal
547 (substitute-in-file-name "/method:host:/path/$$FOO")
548 "/method:host:/path/$FOO"))))
550 (ert-deftest tramp-test05-expand-file-name ()
551 "Check `expand-file-name'."
552 (should
553 (string-equal
554 (expand-file-name "/method:host:/path/./file") "/method:host:/path/file"))
555 (should
556 (string-equal
557 (expand-file-name "/method:host:/path/../file") "/method:host:/file")))
559 (ert-deftest tramp-test06-directory-file-name ()
560 "Check `directory-file-name'.
561 This checks also `file-name-as-directory', `file-name-directory'
562 and `file-name-nondirectory'."
563 (should
564 (string-equal
565 (directory-file-name "/method:host:/path/to/file")
566 "/method:host:/path/to/file"))
567 (should
568 (string-equal
569 (directory-file-name "/method:host:/path/to/file/")
570 "/method:host:/path/to/file"))
571 (should
572 (string-equal
573 (file-name-as-directory "/method:host:/path/to/file")
574 "/method:host:/path/to/file/"))
575 (should
576 (string-equal
577 (file-name-as-directory "/method:host:/path/to/file/")
578 "/method:host:/path/to/file/"))
579 (should
580 (string-equal
581 (file-name-directory "/method:host:/path/to/file")
582 "/method:host:/path/to/"))
583 (should
584 (string-equal
585 (file-name-directory "/method:host:/path/to/file/")
586 "/method:host:/path/to/file/"))
587 (should
588 (string-equal (file-name-nondirectory "/method:host:/path/to/file") "file"))
589 (should
590 (string-equal (file-name-nondirectory "/method:host:/path/to/file/") ""))
591 (should-not
592 (file-remote-p
593 (unhandled-file-name-directory "/method:host:/path/to/file"))))
595 (ert-deftest tramp-test07-file-exists-p ()
596 "Check `file-exist-p', `write-region' and `delete-file'."
597 (skip-unless (tramp--test-enabled))
599 (let ((tmp-name (tramp--test-make-temp-name)))
600 (should-not (file-exists-p tmp-name))
601 (write-region "foo" nil tmp-name)
602 (should (file-exists-p tmp-name))
603 (delete-file tmp-name)
604 (should-not (file-exists-p tmp-name))))
606 (ert-deftest tramp-test08-file-local-copy ()
607 "Check `file-local-copy'."
608 (skip-unless (tramp--test-enabled))
610 (let ((tmp-name1 (tramp--test-make-temp-name))
611 tmp-name2)
612 (unwind-protect
613 (progn
614 (write-region "foo" nil tmp-name1)
615 (should (setq tmp-name2 (file-local-copy tmp-name1)))
616 (with-temp-buffer
617 (insert-file-contents tmp-name2)
618 (should (string-equal (buffer-string) "foo"))))
619 (ignore-errors
620 (delete-file tmp-name1)
621 (delete-file tmp-name2)))))
623 (ert-deftest tramp-test09-insert-file-contents ()
624 "Check `insert-file-contents'."
625 (skip-unless (tramp--test-enabled))
627 (let ((tmp-name (tramp--test-make-temp-name)))
628 (unwind-protect
629 (progn
630 (write-region "foo" nil tmp-name)
631 (with-temp-buffer
632 (insert-file-contents tmp-name)
633 (should (string-equal (buffer-string) "foo"))
634 (insert-file-contents tmp-name)
635 (should (string-equal (buffer-string) "foofoo"))
636 ;; Insert partly.
637 (insert-file-contents tmp-name nil 1 3)
638 (should (string-equal (buffer-string) "oofoofoo"))
639 ;; Replace.
640 (insert-file-contents tmp-name nil nil nil 'replace)
641 (should (string-equal (buffer-string) "foo"))))
642 (ignore-errors (delete-file tmp-name)))))
644 (ert-deftest tramp-test10-write-region ()
645 "Check `write-region'."
646 (skip-unless (tramp--test-enabled))
648 (let ((tmp-name (tramp--test-make-temp-name)))
649 (unwind-protect
650 (progn
651 (with-temp-buffer
652 (insert "foo")
653 (write-region nil nil tmp-name))
654 (with-temp-buffer
655 (insert-file-contents tmp-name)
656 (should (string-equal (buffer-string) "foo")))
657 ;; Append.
658 (with-temp-buffer
659 (insert "bla")
660 (write-region nil nil tmp-name 'append))
661 (with-temp-buffer
662 (insert-file-contents tmp-name)
663 (should (string-equal (buffer-string) "foobla")))
664 ;; Write string.
665 (write-region "foo" nil tmp-name)
666 (with-temp-buffer
667 (insert-file-contents tmp-name)
668 (should (string-equal (buffer-string) "foo")))
669 ;; Write partly.
670 (with-temp-buffer
671 (insert "123456789")
672 (write-region 3 5 tmp-name))
673 (with-temp-buffer
674 (insert-file-contents tmp-name)
675 (should (string-equal (buffer-string) "34"))))
676 (ignore-errors (delete-file tmp-name)))))
678 (ert-deftest tramp-test11-copy-file ()
679 "Check `copy-file'."
680 (skip-unless (tramp--test-enabled))
682 (let ((tmp-name1 (tramp--test-make-temp-name))
683 (tmp-name2 (tramp--test-make-temp-name))
684 (tmp-name3 (tramp--test-make-temp-name))
685 (tmp-name4 (tramp--test-make-temp-name 'local))
686 (tmp-name5 (tramp--test-make-temp-name 'local)))
688 ;; Copy on remote side.
689 (unwind-protect
690 (progn
691 (write-region "foo" nil tmp-name1)
692 (copy-file tmp-name1 tmp-name2)
693 (should (file-exists-p tmp-name2))
694 (with-temp-buffer
695 (insert-file-contents tmp-name2)
696 (should (string-equal (buffer-string) "foo")))
697 (should-error (copy-file tmp-name1 tmp-name2))
698 (copy-file tmp-name1 tmp-name2 'ok)
699 (make-directory tmp-name3)
700 (copy-file tmp-name1 tmp-name3)
701 (should
702 (file-exists-p
703 (expand-file-name (file-name-nondirectory tmp-name1) tmp-name3))))
704 (ignore-errors (delete-file tmp-name1))
705 (ignore-errors (delete-file tmp-name2))
706 (ignore-errors (delete-directory tmp-name3 'recursive)))
708 ;; Copy from remote side to local side.
709 (unwind-protect
710 (progn
711 (write-region "foo" nil tmp-name1)
712 (copy-file tmp-name1 tmp-name4)
713 (should (file-exists-p tmp-name4))
714 (with-temp-buffer
715 (insert-file-contents tmp-name4)
716 (should (string-equal (buffer-string) "foo")))
717 (should-error (copy-file tmp-name1 tmp-name4))
718 (copy-file tmp-name1 tmp-name4 'ok)
719 (make-directory tmp-name5)
720 (copy-file tmp-name1 tmp-name5)
721 (should
722 (file-exists-p
723 (expand-file-name (file-name-nondirectory tmp-name1) tmp-name5))))
724 (ignore-errors (delete-file tmp-name1))
725 (ignore-errors (delete-file tmp-name4))
726 (ignore-errors (delete-directory tmp-name5 'recursive)))
728 ;; Copy from local side to remote side.
729 (unwind-protect
730 (progn
731 (write-region "foo" nil tmp-name4 nil 'nomessage)
732 (copy-file tmp-name4 tmp-name1)
733 (should (file-exists-p tmp-name1))
734 (with-temp-buffer
735 (insert-file-contents tmp-name1)
736 (should (string-equal (buffer-string) "foo")))
737 (should-error (copy-file tmp-name4 tmp-name1))
738 (copy-file tmp-name4 tmp-name1 'ok)
739 (make-directory tmp-name3)
740 (copy-file tmp-name4 tmp-name3)
741 (should
742 (file-exists-p
743 (expand-file-name (file-name-nondirectory tmp-name4) tmp-name3))))
744 (ignore-errors (delete-file tmp-name1))
745 (ignore-errors (delete-file tmp-name4))
746 (ignore-errors (delete-directory tmp-name3 'recursive)))))
748 (ert-deftest tramp-test12-rename-file ()
749 "Check `rename-file'."
750 (skip-unless (tramp--test-enabled))
752 (let ((tmp-name1 (tramp--test-make-temp-name))
753 (tmp-name2 (tramp--test-make-temp-name))
754 (tmp-name3 (tramp--test-make-temp-name))
755 (tmp-name4 (tramp--test-make-temp-name 'local))
756 (tmp-name5 (tramp--test-make-temp-name 'local)))
758 ;; Rename on remote side.
759 (unwind-protect
760 (progn
761 (write-region "foo" nil tmp-name1)
762 (rename-file tmp-name1 tmp-name2)
763 (should-not (file-exists-p tmp-name1))
764 (should (file-exists-p tmp-name2))
765 (with-temp-buffer
766 (insert-file-contents tmp-name2)
767 (should (string-equal (buffer-string) "foo")))
768 (write-region "foo" nil tmp-name1)
769 (should-error (rename-file tmp-name1 tmp-name2))
770 (rename-file tmp-name1 tmp-name2 'ok)
771 (should-not (file-exists-p tmp-name1))
772 (write-region "foo" nil tmp-name1)
773 (make-directory tmp-name3)
774 (rename-file tmp-name1 tmp-name3)
775 (should-not (file-exists-p tmp-name1))
776 (should
777 (file-exists-p
778 (expand-file-name (file-name-nondirectory tmp-name1) tmp-name3))))
779 (ignore-errors (delete-file tmp-name1))
780 (ignore-errors (delete-file tmp-name2))
781 (ignore-errors (delete-directory tmp-name3 'recursive)))
783 ;; Rename from remote side to local side.
784 (unwind-protect
785 (progn
786 (write-region "foo" nil tmp-name1)
787 (rename-file tmp-name1 tmp-name4)
788 (should-not (file-exists-p tmp-name1))
789 (should (file-exists-p tmp-name4))
790 (with-temp-buffer
791 (insert-file-contents tmp-name4)
792 (should (string-equal (buffer-string) "foo")))
793 (write-region "foo" nil tmp-name1)
794 (should-error (rename-file tmp-name1 tmp-name4))
795 (rename-file tmp-name1 tmp-name4 'ok)
796 (should-not (file-exists-p tmp-name1))
797 (write-region "foo" nil tmp-name1)
798 (make-directory tmp-name5)
799 (rename-file tmp-name1 tmp-name5)
800 (should-not (file-exists-p tmp-name1))
801 (should
802 (file-exists-p
803 (expand-file-name (file-name-nondirectory tmp-name1) tmp-name5))))
804 (ignore-errors (delete-file tmp-name1))
805 (ignore-errors (delete-file tmp-name4))
806 (ignore-errors (delete-directory tmp-name5 'recursive)))
808 ;; Rename from local side to remote side.
809 (unwind-protect
810 (progn
811 (write-region "foo" nil tmp-name4 nil 'nomessage)
812 (rename-file tmp-name4 tmp-name1)
813 (should-not (file-exists-p tmp-name4))
814 (should (file-exists-p tmp-name1))
815 (with-temp-buffer
816 (insert-file-contents tmp-name1)
817 (should (string-equal (buffer-string) "foo")))
818 (write-region "foo" nil tmp-name4 nil 'nomessage)
819 (should-error (rename-file tmp-name4 tmp-name1))
820 (rename-file tmp-name4 tmp-name1 'ok)
821 (should-not (file-exists-p tmp-name4))
822 (write-region "foo" nil tmp-name4 nil 'nomessage)
823 (make-directory tmp-name3)
824 (rename-file tmp-name4 tmp-name3)
825 (should-not (file-exists-p tmp-name4))
826 (should
827 (file-exists-p
828 (expand-file-name (file-name-nondirectory tmp-name4) tmp-name3))))
829 (ignore-errors (delete-file tmp-name1))
830 (ignore-errors (delete-file tmp-name4))
831 (ignore-errors (delete-directory tmp-name3 'recursive)))))
833 (ert-deftest tramp-test13-make-directory ()
834 "Check `make-directory'.
835 This tests also `file-directory-p' and `file-accessible-directory-p'."
836 (skip-unless (tramp--test-enabled))
838 (let ((tmp-name (tramp--test-make-temp-name)))
839 (unwind-protect
840 (progn
841 (make-directory tmp-name)
842 (should (file-directory-p tmp-name))
843 (should (file-accessible-directory-p tmp-name)))
844 (ignore-errors (delete-directory tmp-name)))))
846 (ert-deftest tramp-test14-delete-directory ()
847 "Check `delete-directory'."
848 (skip-unless (tramp--test-enabled))
850 (let ((tmp-name (tramp--test-make-temp-name)))
851 ;; Delete empty directory.
852 (make-directory tmp-name)
853 (should (file-directory-p tmp-name))
854 (delete-directory tmp-name)
855 (should-not (file-directory-p tmp-name))
856 ;; Delete non-empty directory.
857 (make-directory tmp-name)
858 (write-region "foo" nil (expand-file-name "bla" tmp-name))
859 (should-error (delete-directory tmp-name) :type 'file-error)
860 (delete-directory tmp-name 'recursive)
861 (should-not (file-directory-p tmp-name))))
863 (ert-deftest tramp-test15-copy-directory ()
864 "Check `copy-directory'."
865 (skip-unless (tramp--test-enabled))
866 (skip-unless
867 (not
869 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory)
870 'tramp-smb-file-name-handler)))
872 (let* ((tmp-name1 (tramp--test-make-temp-name))
873 (tmp-name2 (tramp--test-make-temp-name))
874 (tmp-name3 (expand-file-name
875 (file-name-nondirectory tmp-name1) tmp-name2))
876 (tmp-name4 (expand-file-name "foo" tmp-name1))
877 (tmp-name5 (expand-file-name "foo" tmp-name2))
878 (tmp-name6 (expand-file-name "foo" tmp-name3)))
879 (unwind-protect
880 (progn
881 ;; Copy empty directory.
882 (make-directory tmp-name1)
883 (write-region "foo" nil tmp-name4)
884 (should (file-directory-p tmp-name1))
885 (should (file-exists-p tmp-name4))
886 (copy-directory tmp-name1 tmp-name2)
887 (should (file-directory-p tmp-name2))
888 (should (file-exists-p tmp-name5))
889 ;; Target directory does exist already.
890 (copy-directory tmp-name1 tmp-name2)
891 (should (file-directory-p tmp-name3))
892 (should (file-exists-p tmp-name6)))
893 (ignore-errors
894 (delete-directory tmp-name1 'recursive)
895 (delete-directory tmp-name2 'recursive)))))
897 (ert-deftest tramp-test16-directory-files ()
898 "Check `directory-files'."
899 (skip-unless (tramp--test-enabled))
901 (let* ((tmp-name1 (tramp--test-make-temp-name))
902 (tmp-name2 (expand-file-name "bla" tmp-name1))
903 (tmp-name3 (expand-file-name "foo" tmp-name1)))
904 (unwind-protect
905 (progn
906 (make-directory tmp-name1)
907 (write-region "foo" nil tmp-name2)
908 (write-region "bla" nil tmp-name3)
909 (should (file-directory-p tmp-name1))
910 (should (file-exists-p tmp-name2))
911 (should (file-exists-p tmp-name3))
912 (should (equal (directory-files tmp-name1) '("." ".." "bla" "foo")))
913 (should (equal (directory-files tmp-name1 'full)
914 `(,(concat tmp-name1 "/.")
915 ,(concat tmp-name1 "/..")
916 ,tmp-name2 ,tmp-name3)))
917 (should (equal (directory-files
918 tmp-name1 nil directory-files-no-dot-files-regexp)
919 '("bla" "foo")))
920 (should (equal (directory-files
921 tmp-name1 'full directory-files-no-dot-files-regexp)
922 `(,tmp-name2 ,tmp-name3))))
923 (ignore-errors (delete-directory tmp-name1 'recursive)))))
925 (ert-deftest tramp-test17-insert-directory ()
926 "Check `insert-directory'."
927 (skip-unless (tramp--test-enabled))
929 (let* ((tmp-name1 (tramp--test-make-temp-name))
930 (tmp-name2 (expand-file-name "foo" tmp-name1)))
931 (unwind-protect
932 (progn
933 (make-directory tmp-name1)
934 (write-region "foo" nil tmp-name2)
935 (should (file-directory-p tmp-name1))
936 (should (file-exists-p tmp-name2))
937 (with-temp-buffer
938 (insert-directory tmp-name1 nil)
939 (goto-char (point-min))
940 (should (looking-at-p (regexp-quote tmp-name1))))
941 (with-temp-buffer
942 (insert-directory tmp-name1 "-al")
943 (goto-char (point-min))
944 (should (looking-at-p (format "^.+ %s$" (regexp-quote tmp-name1)))))
945 (with-temp-buffer
946 (insert-directory (file-name-as-directory tmp-name1) "-al")
947 (goto-char (point-min))
948 (should
949 (looking-at-p (format "^.+ %s/$" (regexp-quote tmp-name1)))))
950 (with-temp-buffer
951 (insert-directory
952 (file-name-as-directory tmp-name1) "-al" nil 'full-directory-p)
953 (goto-char (point-min))
954 (should
955 (looking-at-p
956 (concat
957 ;; There might be a summary line.
958 "\\(total.+[[:digit:]]+\n\\)?"
959 ;; We don't know in which order "." and ".." appear.
960 "\\(.+ \\.?\\.\n\\)\\{2\\}"
961 ".+ foo$")))))
962 (ignore-errors (delete-directory tmp-name1 'recursive)))))
964 (ert-deftest tramp-test18-file-attributes ()
965 "Check `file-attributes'.
966 This tests also `file-readable-p' and `file-regular-p'."
967 (skip-unless (tramp--test-enabled))
969 (let ((tmp-name (tramp--test-make-temp-name))
970 attr)
971 (unwind-protect
972 (progn
973 (write-region "foo" nil tmp-name)
974 (should (file-exists-p tmp-name))
975 (setq attr (file-attributes tmp-name))
976 (should (consp attr))
977 (should (file-exists-p tmp-name))
978 (should (file-readable-p tmp-name))
979 (should (file-regular-p tmp-name))
980 ;; We do not test inodes and device numbers.
981 (should (null (car attr)))
982 (should (numberp (nth 1 attr))) ;; Link.
983 (should (numberp (nth 2 attr))) ;; Uid.
984 (should (numberp (nth 3 attr))) ;; Gid.
985 ;; Last access time.
986 (should (stringp (current-time-string (nth 4 attr))))
987 ;; Last modification time.
988 (should (stringp (current-time-string (nth 5 attr))))
989 ;; Last status change time.
990 (should (stringp (current-time-string (nth 6 attr))))
991 (should (numberp (nth 7 attr))) ;; Size.
992 (should (stringp (nth 8 attr))) ;; Modes.
994 (setq attr (file-attributes tmp-name 'string))
995 (should (stringp (nth 2 attr))) ;; Uid.
996 (should (stringp (nth 3 attr))) ;; Gid.
997 (delete-file tmp-name)
999 (make-directory tmp-name)
1000 (should (file-exists-p tmp-name))
1001 (should (file-readable-p tmp-name))
1002 (should-not (file-regular-p tmp-name))
1003 (setq attr (file-attributes tmp-name))
1004 (should (eq (car attr) t)))
1005 (ignore-errors (delete-directory tmp-name)))))
1007 (ert-deftest tramp-test19-directory-files-and-attributes ()
1008 "Check `directory-files-and-attributes'."
1009 (skip-unless (tramp--test-enabled))
1011 ;; `directory-files-and-attributes' contains also values for "../".
1012 ;; Ensure that this doesn't change during tests, for
1013 ;; example due to handling temporary files.
1014 (let* ((tmp-name1 (tramp--test-make-temp-name))
1015 (tmp-name2 (expand-file-name "bla" tmp-name1))
1016 attr)
1017 (unwind-protect
1018 (progn
1019 (make-directory tmp-name1)
1020 (should (file-directory-p tmp-name1))
1021 (make-directory tmp-name2)
1022 (should (file-directory-p tmp-name2))
1023 (write-region "foo" nil (expand-file-name "foo" tmp-name2))
1024 (write-region "bar" nil (expand-file-name "bar" tmp-name2))
1025 (write-region "boz" nil (expand-file-name "boz" tmp-name2))
1026 (setq attr (directory-files-and-attributes tmp-name2))
1027 (should (consp attr))
1028 ;; Dumb remote shells without perl(1) or stat(1) are not
1029 ;; able to return the date correctly. They say "don't know".
1030 (dolist (elt attr)
1031 (unless
1032 (equal
1033 (nth 5
1034 (file-attributes (expand-file-name (car elt) tmp-name2)))
1035 '(0 0))
1036 (should
1037 (equal (file-attributes (expand-file-name (car elt) tmp-name2))
1038 (cdr elt)))))
1039 (setq attr (directory-files-and-attributes tmp-name2 'full))
1040 (dolist (elt attr)
1041 (unless (equal (nth 5 (file-attributes (car elt))) '(0 0))
1042 (should
1043 (equal (file-attributes (car elt)) (cdr elt)))))
1044 (setq attr (directory-files-and-attributes tmp-name2 nil "^b"))
1045 (should (equal (mapcar 'car attr) '("bar" "boz"))))
1046 (ignore-errors (delete-directory tmp-name1 'recursive)))))
1048 (ert-deftest tramp-test20-file-modes ()
1049 "Check `file-modes'.
1050 This tests also `file-executable-p', `file-writable-p' and `set-file-modes'."
1051 (skip-unless (tramp--test-enabled))
1052 (skip-unless
1053 (not
1054 (memq
1055 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory)
1056 '(tramp-adb-file-name-handler
1057 tramp-gvfs-file-name-handler
1058 tramp-smb-file-name-handler))))
1060 (let ((tmp-name (tramp--test-make-temp-name)))
1061 (unwind-protect
1062 (progn
1063 (write-region "foo" nil tmp-name)
1064 (should (file-exists-p tmp-name))
1065 (set-file-modes tmp-name #o777)
1066 (should (= (file-modes tmp-name) #o777))
1067 (should (file-executable-p tmp-name))
1068 (should (file-writable-p tmp-name))
1069 (set-file-modes tmp-name #o444)
1070 (should (= (file-modes tmp-name) #o444))
1071 (should-not (file-executable-p tmp-name))
1072 ;; A file is always writable for user "root".
1073 (unless (zerop (nth 2 (file-attributes tmp-name)))
1074 (should-not (file-writable-p tmp-name))))
1075 (ignore-errors (delete-file tmp-name)))))
1077 (ert-deftest tramp-test21-file-links ()
1078 "Check `file-symlink-p'.
1079 This tests also `make-symbolic-link', `file-truename' and `add-name-to-file'."
1080 (skip-unless (tramp--test-enabled))
1082 ;; We must use `file-truename' for the temporary directory, because
1083 ;; it could be located on a symlinked directory. This would let the
1084 ;; test fail.
1085 (let* ((tramp-test-temporary-file-directory
1086 (file-truename tramp-test-temporary-file-directory))
1087 (tmp-name1 (tramp--test-make-temp-name))
1088 (tmp-name2 (tramp--test-make-temp-name))
1089 (tmp-name3 (tramp--test-make-temp-name 'local)))
1090 (unwind-protect
1091 (progn
1092 (write-region "foo" nil tmp-name1)
1093 (should (file-exists-p tmp-name1))
1094 ;; Method "smb" supports `make-symbolic-link' only if the
1095 ;; remote host has CIFS capabilities. tramp-adb.el and
1096 ;; tramp-gvfs.el do not support symbolic links at all.
1097 (condition-case err
1098 (make-symbolic-link tmp-name1 tmp-name2)
1099 (file-error
1100 (skip-unless
1101 (not (string-equal (error-message-string err)
1102 "make-symbolic-link not supported")))))
1103 (should (file-symlink-p tmp-name2))
1104 (should-error (make-symbolic-link tmp-name1 tmp-name2))
1105 (make-symbolic-link tmp-name1 tmp-name2 'ok-if-already-exists)
1106 (should (file-symlink-p tmp-name2))
1107 ;; `tmp-name3' is a local file name.
1108 (should-error (make-symbolic-link tmp-name1 tmp-name3)))
1109 (ignore-errors
1110 (delete-file tmp-name1)
1111 (delete-file tmp-name2)))
1113 (unwind-protect
1114 (progn
1115 (write-region "foo" nil tmp-name1)
1116 (should (file-exists-p tmp-name1))
1117 (add-name-to-file tmp-name1 tmp-name2)
1118 (should-not (file-symlink-p tmp-name2))
1119 (should-error (add-name-to-file tmp-name1 tmp-name2))
1120 (add-name-to-file tmp-name1 tmp-name2 'ok-if-already-exists)
1121 (should-not (file-symlink-p tmp-name2))
1122 ;; `tmp-name3' is a local file name.
1123 (should-error (add-name-to-file tmp-name1 tmp-name3)))
1124 (ignore-errors
1125 (delete-file tmp-name1)
1126 (delete-file tmp-name2)))
1128 (unwind-protect
1129 (progn
1130 (write-region "foo" nil tmp-name1)
1131 (should (file-exists-p tmp-name1))
1132 (make-symbolic-link tmp-name1 tmp-name2)
1133 (should (file-symlink-p tmp-name2))
1134 (should-not (string-equal tmp-name2 (file-truename tmp-name2)))
1135 (should
1136 (string-equal (file-truename tmp-name1) (file-truename tmp-name2)))
1137 (should (file-equal-p tmp-name1 tmp-name2)))
1138 (ignore-errors
1139 (delete-file tmp-name1)
1140 (delete-file tmp-name2)))
1142 ;; `file-truename' shall preserve trailing link of directories.
1143 (unless (file-symlink-p tramp-test-temporary-file-directory)
1144 (let* ((dir1 (directory-file-name tramp-test-temporary-file-directory))
1145 (dir2 (file-name-as-directory dir1)))
1146 (should (string-equal (file-truename dir1) (expand-file-name dir1)))
1147 (should (string-equal (file-truename dir2) (expand-file-name dir2)))))))
1149 (ert-deftest tramp-test22-file-times ()
1150 "Check `set-file-times' and `file-newer-than-file-p'."
1151 (skip-unless (tramp--test-enabled))
1152 (skip-unless
1153 (not
1154 (memq
1155 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory)
1156 '(tramp-gvfs-file-name-handler tramp-smb-file-name-handler))))
1158 (let ((tmp-name1 (tramp--test-make-temp-name))
1159 (tmp-name2 (tramp--test-make-temp-name))
1160 (tmp-name3 (tramp--test-make-temp-name)))
1161 (unwind-protect
1162 (progn
1163 (write-region "foo" nil tmp-name1)
1164 (should (file-exists-p tmp-name1))
1165 (should (consp (nth 5 (file-attributes tmp-name1))))
1166 ;; '(0 0) means don't know, and will be replaced by
1167 ;; `current-time'. Therefore, we use '(0 1).
1168 ;; We skip the test, if the remote handler is not able to
1169 ;; set the correct time.
1170 (skip-unless (set-file-times tmp-name1 '(0 1)))
1171 ;; Dumb remote shells without perl(1) or stat(1) are not
1172 ;; able to return the date correctly. They say "don't know".
1173 (unless (equal (nth 5 (file-attributes tmp-name1)) '(0 0))
1174 (should (equal (nth 5 (file-attributes tmp-name1)) '(0 1)))
1175 (write-region "bla" nil tmp-name2)
1176 (should (file-exists-p tmp-name2))
1177 (should (file-newer-than-file-p tmp-name2 tmp-name1))
1178 ;; `tmp-name3' does not exist.
1179 (should (file-newer-than-file-p tmp-name2 tmp-name3))
1180 (should-not (file-newer-than-file-p tmp-name3 tmp-name1))))
1181 (ignore-errors
1182 (delete-file tmp-name1)
1183 (delete-file tmp-name2)))))
1185 (ert-deftest tramp-test23-visited-file-modtime ()
1186 "Check `set-visited-file-modtime' and `verify-visited-file-modtime'."
1187 (skip-unless (tramp--test-enabled))
1189 (let ((tmp-name (tramp--test-make-temp-name)))
1190 (unwind-protect
1191 (progn
1192 (write-region "foo" nil tmp-name)
1193 (should (file-exists-p tmp-name))
1194 (with-temp-buffer
1195 (insert-file-contents tmp-name)
1196 (should (verify-visited-file-modtime))
1197 (set-visited-file-modtime '(0 1))
1198 (should (verify-visited-file-modtime))
1199 (should (equal (visited-file-modtime) '(0 1 0 0)))))
1200 (ignore-errors (delete-file tmp-name)))))
1202 (ert-deftest tramp-test24-file-name-completion ()
1203 "Check `file-name-completion' and `file-name-all-completions'."
1204 (skip-unless (tramp--test-enabled))
1206 (let ((tmp-name (tramp--test-make-temp-name)))
1207 (unwind-protect
1208 (progn
1209 (make-directory tmp-name)
1210 (should (file-directory-p tmp-name))
1211 (write-region "foo" nil (expand-file-name "foo" tmp-name))
1212 (write-region "bar" nil (expand-file-name "bold" tmp-name))
1213 (make-directory (expand-file-name "boz" tmp-name))
1214 (should (equal (file-name-completion "fo" tmp-name) "foo"))
1215 (should (equal (file-name-completion "b" tmp-name) "bo"))
1216 (should
1217 (equal (file-name-completion "b" tmp-name 'file-directory-p) "boz/"))
1218 (should (equal (file-name-all-completions "fo" tmp-name) '("foo")))
1219 (should
1220 (equal (sort (file-name-all-completions "b" tmp-name) 'string-lessp)
1221 '("bold" "boz/"))))
1222 (ignore-errors (delete-directory tmp-name 'recursive)))))
1224 (ert-deftest tramp-test25-load ()
1225 "Check `load'."
1226 (skip-unless (tramp--test-enabled))
1228 (let ((tmp-name (tramp--test-make-temp-name)))
1229 (unwind-protect
1230 (progn
1231 (load tmp-name 'noerror 'nomessage)
1232 (should-not (featurep 'tramp-test-load))
1233 (write-region "(provide 'tramp-test-load)" nil tmp-name)
1234 ;; `load' in lread.c does not pass `must-suffix'. Why?
1235 ;(should-error (load tmp-name nil 'nomessage 'nosuffix 'must-suffix))
1236 (load tmp-name nil 'nomessage 'nosuffix)
1237 (should (featurep 'tramp-test-load)))
1238 (ignore-errors
1239 (and (featurep 'tramp-test-load) (unload-feature 'tramp-test-load))
1240 (delete-file tmp-name)))))
1242 (ert-deftest tramp-test26-process-file ()
1243 "Check `process-file'."
1244 (skip-unless (tramp--test-enabled))
1245 (skip-unless
1246 (not
1247 (memq
1248 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory)
1249 '(tramp-gvfs-file-name-handler tramp-smb-file-name-handler))))
1251 (let* ((tmp-name (tramp--test-make-temp-name))
1252 (fnnd (file-name-nondirectory tmp-name))
1253 (default-directory tramp-test-temporary-file-directory)
1254 kill-buffer-query-functions)
1255 (unwind-protect
1256 (progn
1257 ;; We cannot use "/bin/true" and "/bin/false"; those paths
1258 ;; do not exist on hydra.
1259 (should (zerop (process-file "true")))
1260 (should-not (zerop (process-file "false")))
1261 (should-not (zerop (process-file "binary-does-not-exist")))
1262 (with-temp-buffer
1263 (write-region "foo" nil tmp-name)
1264 (should (file-exists-p tmp-name))
1265 (should (zerop (process-file "ls" nil t nil fnnd)))
1266 ;; `ls' could produce colorized output.
1267 (goto-char (point-min))
1268 (while (re-search-forward tramp-color-escape-sequence-regexp nil t)
1269 (replace-match "" nil nil))
1270 (should (string-equal (format "%s\n" fnnd) (buffer-string)))
1271 (should-not (get-buffer-window (current-buffer) t))
1273 ;; Second run. The output must be appended.
1274 (should (zerop (process-file "ls" nil t t fnnd)))
1275 ;; `ls' could produce colorized output.
1276 (goto-char (point-min))
1277 (while (re-search-forward tramp-color-escape-sequence-regexp nil t)
1278 (replace-match "" nil nil))
1279 (should
1280 (string-equal (format "%s\n%s\n" fnnd fnnd) (buffer-string)))
1281 ;; A non-nil DISPLAY must not raise the buffer.
1282 (should-not (get-buffer-window (current-buffer) t))))
1284 (ignore-errors (delete-file tmp-name)))))
1286 (ert-deftest tramp-test27-start-file-process ()
1287 "Check `start-file-process'."
1288 (skip-unless (tramp--test-enabled))
1289 (skip-unless
1290 (not
1291 (memq
1292 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory)
1293 '(tramp-adb-file-name-handler
1294 tramp-gvfs-file-name-handler
1295 tramp-smb-file-name-handler))))
1297 (let ((default-directory tramp-test-temporary-file-directory)
1298 (tmp-name (tramp--test-make-temp-name))
1299 kill-buffer-query-functions proc)
1300 (unwind-protect
1301 (with-temp-buffer
1302 (setq proc (start-file-process "test1" (current-buffer) "cat"))
1303 (should (processp proc))
1304 (should (equal (process-status proc) 'run))
1305 (process-send-string proc "foo")
1306 (process-send-eof proc)
1307 ;; Read output.
1308 (with-timeout (10 (ert-fail "`start-file-process' timed out"))
1309 (while (< (- (point-max) (point-min)) (length "foo"))
1310 (accept-process-output proc 1)))
1311 (should (string-equal (buffer-string) "foo")))
1312 (ignore-errors (delete-process proc)))
1314 (unwind-protect
1315 (with-temp-buffer
1316 (write-region "foo" nil tmp-name)
1317 (should (file-exists-p tmp-name))
1318 (setq proc
1319 (start-file-process
1320 "test2" (current-buffer)
1321 "cat" (file-name-nondirectory tmp-name)))
1322 (should (processp proc))
1323 ;; Read output.
1324 (with-timeout (10 (ert-fail "`start-file-process' timed out"))
1325 (while (< (- (point-max) (point-min)) (length "foo"))
1326 (accept-process-output proc 1)))
1327 (should (string-equal (buffer-string) "foo")))
1328 (ignore-errors
1329 (delete-process proc)
1330 (delete-file tmp-name)))
1332 (unwind-protect
1333 (with-temp-buffer
1334 (setq proc (start-file-process "test3" (current-buffer) "cat"))
1335 (should (processp proc))
1336 (should (equal (process-status proc) 'run))
1337 (set-process-filter
1338 proc
1339 (lambda (p s) (with-current-buffer (process-buffer p) (insert s))))
1340 (process-send-string proc "foo")
1341 (process-send-eof proc)
1342 ;; Read output.
1343 (with-timeout (10 (ert-fail "`start-file-process' timed out"))
1344 (while (< (- (point-max) (point-min)) (length "foo"))
1345 (accept-process-output proc 1)))
1346 (should (string-equal (buffer-string) "foo")))
1347 (ignore-errors (delete-process proc)))))
1349 (ert-deftest tramp-test28-shell-command ()
1350 "Check `shell-command'."
1351 (skip-unless (tramp--test-enabled))
1352 (skip-unless
1353 (not
1354 (memq
1355 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory)
1356 '(tramp-adb-file-name-handler
1357 tramp-gvfs-file-name-handler
1358 tramp-smb-file-name-handler))))
1360 (let ((tmp-name (tramp--test-make-temp-name))
1361 (default-directory tramp-test-temporary-file-directory)
1362 kill-buffer-query-functions)
1363 (unwind-protect
1364 (with-temp-buffer
1365 (write-region "foo" nil tmp-name)
1366 (should (file-exists-p tmp-name))
1367 (shell-command
1368 (format "ls %s" (file-name-nondirectory tmp-name)) (current-buffer))
1369 ;; `ls' could produce colorized output.
1370 (goto-char (point-min))
1371 (while (re-search-forward tramp-color-escape-sequence-regexp nil t)
1372 (replace-match "" nil nil))
1373 (should
1374 (string-equal
1375 (format "%s\n" (file-name-nondirectory tmp-name)) (buffer-string))))
1376 (ignore-errors (delete-file tmp-name)))
1378 (unwind-protect
1379 (with-temp-buffer
1380 (write-region "foo" nil tmp-name)
1381 (should (file-exists-p tmp-name))
1382 (async-shell-command
1383 (format "ls %s" (file-name-nondirectory tmp-name)) (current-buffer))
1384 (set-process-sentinel (get-buffer-process (current-buffer)) nil)
1385 ;; Read output.
1386 (with-timeout (10 (ert-fail "`async-shell-command' timed out"))
1387 (while (< (- (point-max) (point-min))
1388 (1+ (length (file-name-nondirectory tmp-name))))
1389 (accept-process-output (get-buffer-process (current-buffer)) 1)))
1390 ;; `ls' could produce colorized output.
1391 (goto-char (point-min))
1392 (while (re-search-forward tramp-color-escape-sequence-regexp nil t)
1393 (replace-match "" nil nil))
1394 ;; There might be a nasty "Process *Async Shell* finished" message.
1395 (goto-char (point-min))
1396 (forward-line)
1397 (narrow-to-region (point-min) (point))
1398 (should
1399 (string-equal
1400 (format "%s\n" (file-name-nondirectory tmp-name)) (buffer-string))))
1401 (ignore-errors (delete-file tmp-name)))
1403 (unwind-protect
1404 (with-temp-buffer
1405 (write-region "foo" nil tmp-name)
1406 (should (file-exists-p tmp-name))
1407 (async-shell-command "read line; ls $line" (current-buffer))
1408 (set-process-sentinel (get-buffer-process (current-buffer)) nil)
1409 (process-send-string
1410 (get-buffer-process (current-buffer))
1411 (format "%s\n" (file-name-nondirectory tmp-name)))
1412 ;; Read output.
1413 (with-timeout (10 (ert-fail "`async-shell-command' timed out"))
1414 (while (< (- (point-max) (point-min))
1415 (1+ (length (file-name-nondirectory tmp-name))))
1416 (accept-process-output (get-buffer-process (current-buffer)) 1)))
1417 ;; `ls' could produce colorized output.
1418 (goto-char (point-min))
1419 (while (re-search-forward tramp-color-escape-sequence-regexp nil t)
1420 (replace-match "" nil nil))
1421 ;; There might be a nasty "Process *Async Shell* finished" message.
1422 (goto-char (point-min))
1423 (forward-line)
1424 (narrow-to-region (point-min) (point))
1425 (should
1426 (string-equal
1427 (format "%s\n" (file-name-nondirectory tmp-name)) (buffer-string))))
1428 (ignore-errors (delete-file tmp-name)))))
1430 (ert-deftest tramp-test29-vc-registered ()
1431 "Check `vc-registered'."
1432 (skip-unless (tramp--test-enabled))
1433 (skip-unless
1435 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory)
1436 'tramp-sh-file-name-handler))
1438 (let* ((default-directory tramp-test-temporary-file-directory)
1439 (tmp-name1 (tramp--test-make-temp-name))
1440 (tmp-name2 (expand-file-name "foo" tmp-name1))
1441 (tramp-remote-process-environment tramp-remote-process-environment)
1442 (vc-handled-backends
1443 (with-parsed-tramp-file-name tramp-test-temporary-file-directory nil
1444 (cond
1445 ((tramp-find-executable v vc-bzr-program (tramp-get-remote-path v))
1446 (setq tramp-remote-process-environment
1447 (cons (format "BZR_HOME=%s"
1448 (file-remote-p tmp-name1 'localname))
1449 tramp-remote-process-environment))
1450 ;; We must force a reconnect, in order to activate $BZR_HOME.
1451 (tramp-cleanup-connection
1452 (tramp-dissect-file-name tramp-test-temporary-file-directory)
1453 nil 'keep-password)
1454 '(Bzr))
1455 ((tramp-find-executable v vc-git-program (tramp-get-remote-path v))
1456 '(Git))
1457 ((tramp-find-executable v vc-hg-program (tramp-get-remote-path v))
1458 '(Hg))
1459 (t nil)))))
1460 (skip-unless vc-handled-backends)
1461 (message "%s" vc-handled-backends)
1463 (unwind-protect
1464 (progn
1465 (make-directory tmp-name1)
1466 (write-region "foo" nil tmp-name2)
1467 (should (file-directory-p tmp-name1))
1468 (should (file-exists-p tmp-name2))
1469 (should-not (vc-registered tmp-name1))
1470 (should-not (vc-registered tmp-name2))
1472 (let ((default-directory tmp-name1))
1473 ;; Create empty repository, and register the file.
1474 (vc-create-repo (car vc-handled-backends))
1475 ;; The structure of VC-FILESET is not documented. Let's
1476 ;; hope it won't change.
1477 (condition-case nil
1478 (vc-register
1479 (list (car vc-handled-backends)
1480 (list (file-name-nondirectory tmp-name2))))
1481 ;; `vc-register' has changed its arguments in Emacs 25.1.
1482 (error
1483 (vc-register
1484 nil (list (car vc-handled-backends)
1485 (list (file-name-nondirectory tmp-name2)))))))
1486 (should (vc-registered tmp-name2)))
1488 (ignore-errors (delete-directory tmp-name1 'recursive)))))
1490 (defun tramp--test-smb-or-windows-nt-p ()
1491 "Check, whether the locale or remote host runs MS Windows.
1492 This requires restrictions of file name syntax."
1493 (or (eq system-type 'windows-nt)
1494 (eq (tramp-find-foreign-file-name-handler
1495 tramp-test-temporary-file-directory)
1496 'tramp-smb-file-name-handler)))
1498 (defun tramp--test-check-files (&rest files)
1499 "Runs a simple but comprehensive test over every file in FILES."
1500 (let ((tmp-name1 (tramp--test-make-temp-name))
1501 (tmp-name2 (tramp--test-make-temp-name 'local)))
1502 (unwind-protect
1503 (progn
1504 (make-directory tmp-name1)
1505 (make-directory tmp-name2)
1506 (dolist (elt (delq nil files))
1507 (let ((file1 (expand-file-name elt tmp-name1))
1508 (file2 (expand-file-name elt tmp-name2)))
1509 (write-region elt nil file1)
1510 (should (file-exists-p file1))
1511 ;; Check file contents.
1512 (with-temp-buffer
1513 (insert-file-contents file1)
1514 (should (string-equal (buffer-string) elt)))
1515 ;; Copy file both directions.
1516 (copy-file file1 tmp-name2)
1517 (should (file-exists-p file2))
1518 (delete-file file1)
1519 (should-not (file-exists-p file1))
1520 (copy-file file2 tmp-name1)
1521 (should (file-exists-p file1))))
1523 ;; Check file names.
1524 (should (equal (directory-files
1525 tmp-name1 nil directory-files-no-dot-files-regexp)
1526 (sort (copy-sequence files) 'string-lessp)))
1527 (should (equal (directory-files
1528 tmp-name2 nil directory-files-no-dot-files-regexp)
1529 (sort (copy-sequence files) 'string-lessp)))
1531 ;; `substitute-in-file-name' could return different values.
1532 ;; For `adb', there could be strange file permissions
1533 ;; preventing overwriting a file. We don't care in this
1534 ;; testcase.
1535 (dolist (elt files)
1536 (let ((file1
1537 (substitute-in-file-name (expand-file-name elt tmp-name1)))
1538 (file2
1539 (substitute-in-file-name (expand-file-name elt tmp-name2))))
1540 (ignore-errors (write-region elt nil file1))
1541 (should (file-exists-p file1))
1542 (ignore-errors (write-region elt nil file2 nil 'nomessage))
1543 (should (file-exists-p file2))))
1545 (should (equal (directory-files
1546 tmp-name1 nil directory-files-no-dot-files-regexp)
1547 (directory-files
1548 tmp-name2 nil directory-files-no-dot-files-regexp))))
1550 (ignore-errors (delete-directory tmp-name1 'recursive))
1551 (ignore-errors (delete-directory tmp-name2 'recursive)))))
1553 ;; This test is inspired by Bug#17238.
1554 (ert-deftest tramp-test30-special-characters ()
1555 "Check special characters in file names."
1556 (skip-unless (tramp--test-enabled))
1557 (skip-unless
1558 (not
1559 (memq
1560 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory)
1561 '(tramp-adb-file-name-handler
1562 tramp-gvfs-file-name-handler))))
1564 ;; Newlines, slashes and backslashes in file names are not supported.
1565 ;; So we don't test.
1566 (tramp--test-check-files
1567 (if (tramp--test-smb-or-windows-nt-p) "foo bar baz" " foo\tbar baz\t")
1568 "$foo$bar$$baz$"
1569 "-foo-bar-baz-"
1570 "%foo%bar%baz%"
1571 "&foo&bar&baz&"
1572 (unless (tramp--test-smb-or-windows-nt-p) "?foo?bar?baz?")
1573 (unless (tramp--test-smb-or-windows-nt-p) "*foo*bar*baz*")
1574 (if (tramp--test-smb-or-windows-nt-p) "'foo'bar'baz'" "'foo\"bar'baz\"")
1575 "#foo~bar#baz~"
1576 (if (tramp--test-smb-or-windows-nt-p) "!foo!bar!baz!" "!foo|bar!baz|")
1577 (if (tramp--test-smb-or-windows-nt-p) ";foo;bar;baz;" ":foo;bar:baz;")
1578 (unless (tramp--test-smb-or-windows-nt-p) "<foo>bar<baz>")
1579 "(foo)bar(baz)"
1580 "[foo]bar[baz]"
1581 "{foo}bar{baz}"))
1583 (ert-deftest tramp-test31-utf8 ()
1584 "Check UTF8 encoding in file names and file contents."
1585 (skip-unless (tramp--test-enabled))
1587 (let ((coding-system-for-read 'utf-8)
1588 (coding-system-for-write 'utf-8)
1589 (file-name-coding-system 'utf-8))
1590 (tramp--test-check-files
1591 "أصبح بوسعك الآن تنزيل نسخة كاملة من موسوعة ويكيبيديا العربية لتصفحها بلا اتصال بالإنترنت"
1592 "银河系漫游指南系列"
1593 "Автостопом по гала́ктике")))
1595 ;; This test is inspired by Bug#16928.
1596 (ert-deftest tramp-test32-asynchronous-requests ()
1597 "Check parallel asynchronous requests.
1598 Such requests could arrive from timers, process filters and
1599 process sentinels. They shall not disturb each other."
1600 ;; Mark as failed until bug has been fixed.
1601 :expected-result :failed
1602 (skip-unless (tramp--test-enabled))
1603 (skip-unless
1605 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory)
1606 'tramp-sh-file-name-handler))
1608 ;; Keep instrumentation verbosity 0 until Tramp bug is fixed. This
1609 ;; has the side effect, that this test fails instead to abort. Good
1610 ;; for hydra.
1611 (tramp--instrument-test-case 0
1612 (let* ((tmp-name (tramp--test-make-temp-name))
1613 (default-directory tmp-name)
1614 (remote-file-name-inhibit-cache t)
1615 timer buffers kill-buffer-query-functions)
1617 (unwind-protect
1618 (progn
1619 (make-directory tmp-name)
1621 ;; Setup a timer in order to raise an ordinary command again
1622 ;; and again. `vc-registered' is well suited, because there
1623 ;; are many checks.
1624 (setq
1625 timer
1626 (run-at-time
1628 (lambda ()
1629 (when buffers
1630 (vc-registered
1631 (buffer-name (nth (random (length buffers)) buffers)))))))
1633 ;; Create temporary buffers. The number of buffers
1634 ;; corresponds to the number of processes; it could be
1635 ;; increased in order to make pressure on Tramp.
1636 (dotimes (i 5)
1637 (add-to-list 'buffers (generate-new-buffer "*temp*")))
1639 ;; Open asynchronous processes. Set process sentinel.
1640 (dolist (buf buffers)
1641 (async-shell-command "read line; touch $line; echo $line" buf)
1642 (set-process-sentinel
1643 (get-buffer-process buf)
1644 (lambda (proc _state)
1645 (delete-file (buffer-name (process-buffer proc))))))
1647 ;; Send a string. Use a random order of the buffers. Mix
1648 ;; with regular operation.
1649 (let ((buffers (copy-sequence buffers))
1650 buf)
1651 (while buffers
1652 (setq buf (nth (random (length buffers)) buffers))
1653 (process-send-string
1654 (get-buffer-process buf) (format "'%s'\n" buf))
1655 (file-attributes (buffer-name buf))
1656 (setq buffers (delq buf buffers))))
1658 ;; Wait until the whole output has been read.
1659 (with-timeout ((* 10 (length buffers))
1660 (ert-fail "`async-shell-command' timed out"))
1661 (let ((buffers (copy-sequence buffers))
1662 buf)
1663 (while buffers
1664 (setq buf (nth (random (length buffers)) buffers))
1665 (if (ignore-errors
1666 (memq (process-status (get-buffer-process buf))
1667 '(run open)))
1668 (accept-process-output (get-buffer-process buf) 0.1)
1669 (setq buffers (delq buf buffers))))))
1671 ;; Check.
1672 (dolist (buf buffers)
1673 (with-current-buffer buf
1674 (should
1675 (string-equal (format "'%s'\n" buf) (buffer-string)))))
1676 (should-not
1677 (directory-files tmp-name nil directory-files-no-dot-files-regexp)))
1679 ;; Cleanup.
1680 (ignore-errors (cancel-timer timer))
1681 (ignore-errors (delete-directory tmp-name 'recursive))
1682 (dolist (buf buffers)
1683 (ignore-errors (kill-buffer buf)))))))
1685 (ert-deftest tramp-test33-recursive-load ()
1686 "Check that Tramp does not fail due to recursive load."
1687 (skip-unless (tramp--test-enabled))
1689 (dolist (code
1690 (list
1691 (format
1692 "(expand-file-name %S)"
1693 tramp-test-temporary-file-directory)
1694 (format
1695 "(let ((default-directory %S)) (expand-file-name %S))"
1696 tramp-test-temporary-file-directory
1697 temporary-file-directory)))
1698 (should-not
1699 (string-match
1700 "Recursive load"
1701 (shell-command-to-string
1702 (format
1703 "%s -batch -Q -L %s --eval %s"
1704 (expand-file-name invocation-name invocation-directory)
1705 (mapconcat 'shell-quote-argument load-path " -L ")
1706 (shell-quote-argument code)))))))
1708 (ert-deftest tramp-test34-unload ()
1709 "Check that Tramp and its subpackages unload completely.
1710 Since it unloads Tramp, it shall be the last test to run."
1711 ;; Mark as failed until all symbols are unbound.
1712 :expected-result (if (featurep 'tramp) :failed :passed)
1713 (when (featurep 'tramp)
1714 (unload-feature 'tramp 'force)
1715 ;; No Tramp feature must be left.
1716 (should-not (featurep 'tramp))
1717 (should-not (all-completions "tramp" (delq 'tramp-tests features)))
1718 ;; `file-name-handler-alist' must be clean.
1719 (should-not (all-completions "tramp" (mapcar 'cdr file-name-handler-alist)))
1720 ;; There shouldn't be left a bound symbol. We do not regard our
1721 ;; test symbols, and the Tramp unload hooks.
1722 (mapatoms
1723 (lambda (x)
1724 (and (or (boundp x) (functionp x))
1725 (string-match "^tramp" (symbol-name x))
1726 (not (string-match "^tramp--?test" (symbol-name x)))
1727 (not (string-match "unload-hook$" (symbol-name x)))
1728 (ert-fail (format "`%s' still bound" x)))))
1729 ; (progn (message "`%s' still bound" x)))))
1730 ;; There shouldn't be left a hook function containing a Tramp
1731 ;; function. We do not regard the Tramp unload hooks.
1732 (mapatoms
1733 (lambda (x)
1734 (and (boundp x)
1735 (string-match "-hooks?$" (symbol-name x))
1736 (not (string-match "unload-hook$" (symbol-name x)))
1737 (consp (symbol-value x))
1738 (ignore-errors (all-completions "tramp" (symbol-value x)))
1739 (ert-fail (format "Hook `%s' still contains Tramp function" x)))))))
1741 ;; TODO:
1743 ;; * dired-compress-file
1744 ;; * dired-uncache
1745 ;; * file-acl
1746 ;; * file-ownership-preserved-p
1747 ;; * file-selinux-context
1748 ;; * find-backup-file-name
1749 ;; * make-auto-save-file-name
1750 ;; * set-file-acl
1751 ;; * set-file-selinux-context
1753 ;; * Work on skipped tests. Make a comment, when it is impossible.
1754 ;; * Fix `tramp-test15-copy-directory' for `smb'. Using tar in a pipe
1755 ;; doesn't work well when an interactive password must be provided.
1756 ;; * Fix `tramp-test27-start-file-process' for `nc' and on MS
1757 ;; Windows (`process-send-eof'?).
1758 ;; * Fix `tramp-test30-special-characters' for `adb' and `nc'.
1759 ;; * Fix `tramp-test31-utf8' for `nc'/`telnet' (when target is a dumb
1760 ;; busybox). Seems to be in `directory-files'.
1761 ;; * Fix Bug#16928. Set expected error of `tramp-test32-asynchronous-requests'.
1762 ;; * Fix `tramp-test34-unload' (Not all symbols are unbound). Set
1763 ;; expected error.
1765 (defun tramp-test-all (&optional interactive)
1766 "Run all tests for \\[tramp]."
1767 (interactive "p")
1768 (funcall
1769 (if interactive 'ert-run-tests-interactively 'ert-run-tests-batch) "^tramp"))
1771 (provide 'tramp-tests)
1772 ;;; tramp-tests.el ends here