declare smobs in alloc.c
[bpt/emacs.git] / test / automated / tramp-tests.el
1 ;;; tramp-tests.el --- Tests of remote file access
2
3 ;; Copyright (C) 2013-2014 Free Software Foundation, Inc.
4
5 ;; Author: Michael Albinus <michael.albinus@gmx.de>
6
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.
11 ;;
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.
16 ;;
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/'.
19
20 ;;; Commentary:
21
22 ;; The tests require a recent ert.el from Emacs 24.4.
23
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.
29
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.
35
36 ;; A whole test run can be performed calling the command `tramp-test-all'.
37
38 ;;; Code:
39
40 (require 'ert)
41 (require 'tramp)
42 (require 'vc)
43 (require 'vc-bzr)
44 (require 'vc-git)
45 (require 'vc-hg)
46
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)
51
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.")
67
68 (setq password-cache-expiry nil
69 tramp-verbose 0
70 tramp-copy-size-limit nil
71 tramp-message-show-message nil)
72
73 ;; This shall happen on hydra only.
74 (when (getenv "NIX_STORE")
75 (add-to-list 'tramp-remote-path 'tramp-own-remote-path))
76
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.")
81
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))))))
93
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)))
100
101 ;; Return result.
102 (cdr tramp--test-enabled-checked))
103
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)))
109
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))))))))
128
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)))))
138
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:"))
150
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:"))
156
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:"))
162
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]:"))
169
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"))
175
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:"))
183
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"))))
197
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) ""))
211
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) ""))
220
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) ""))
229
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) ""))
239
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) ""))
248
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) ""))
258
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) ""))
268
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) ""))
281
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) ""))
291
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) ""))
301
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) ""))
313
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) ""))
326
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) ""))
335
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) ""))
345
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) ""))
355
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) ""))
367
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) ""))
378
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) "")))
388
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) ""))
397
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) ""))
407
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) ""))
417
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) ""))
428
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"))
437
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"))
463
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"))))
493
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)))
521
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"))))
549
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")))
558
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"))))
594
595 (ert-deftest tramp-test07-file-exists-p ()
596 "Check `file-exist-p', `write-region' and `delete-file'."
597 (skip-unless (tramp--test-enabled))
598
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))))
605
606 (ert-deftest tramp-test08-file-local-copy ()
607 "Check `file-local-copy'."
608 (skip-unless (tramp--test-enabled))
609
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)))))
622
623 (ert-deftest tramp-test09-insert-file-contents ()
624 "Check `insert-file-contents'."
625 (skip-unless (tramp--test-enabled))
626
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)))))
643
644 (ert-deftest tramp-test10-write-region ()
645 "Check `write-region'."
646 (skip-unless (tramp--test-enabled))
647
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)))))
677
678 (ert-deftest tramp-test11-copy-file ()
679 "Check `copy-file'."
680 (skip-unless (tramp--test-enabled))
681
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)))
687
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)))
707
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)))
727
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)))))
747
748 (ert-deftest tramp-test12-rename-file ()
749 "Check `rename-file'."
750 (skip-unless (tramp--test-enabled))
751
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)))
757
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)))
782
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)))
807
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)))))
832
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))
837
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)))))
845
846 (ert-deftest tramp-test14-delete-directory ()
847 "Check `delete-directory'."
848 (skip-unless (tramp--test-enabled))
849
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))))
862
863 (ert-deftest tramp-test15-copy-directory ()
864 "Check `copy-directory'."
865 (skip-unless (tramp--test-enabled))
866 (skip-unless
867 (not
868 (eq
869 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory)
870 'tramp-smb-file-name-handler)))
871
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)))))
896
897 (ert-deftest tramp-test16-directory-files ()
898 "Check `directory-files'."
899 (skip-unless (tramp--test-enabled))
900
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)))))
924
925 (ert-deftest tramp-test17-insert-directory ()
926 "Check `insert-directory'."
927 (skip-unless (tramp--test-enabled))
928
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 "\\(total.+[[:digit:]]+\n\\)?.+ \\.\n.+ \\.\\.\n.+ foo$"))))
957 (ignore-errors (delete-directory tmp-name1 'recursive)))))
958
959 (ert-deftest tramp-test18-file-attributes ()
960 "Check `file-attributes'.
961 This tests also `file-readable-p' and `file-regular-p'."
962 (skip-unless (tramp--test-enabled))
963
964 (let ((tmp-name (tramp--test-make-temp-name))
965 attr)
966 (unwind-protect
967 (progn
968 (write-region "foo" nil tmp-name)
969 (should (file-exists-p tmp-name))
970 (setq attr (file-attributes tmp-name))
971 (should (consp attr))
972 (should (file-exists-p tmp-name))
973 (should (file-readable-p tmp-name))
974 (should (file-regular-p tmp-name))
975 ;; We do not test inodes and device numbers.
976 (should (null (car attr)))
977 (should (numberp (nth 1 attr))) ;; Link.
978 (should (numberp (nth 2 attr))) ;; Uid.
979 (should (numberp (nth 3 attr))) ;; Gid.
980 ;; Last access time.
981 (should (stringp (current-time-string (nth 4 attr))))
982 ;; Last modification time.
983 (should (stringp (current-time-string (nth 5 attr))))
984 ;; Last status change time.
985 (should (stringp (current-time-string (nth 6 attr))))
986 (should (numberp (nth 7 attr))) ;; Size.
987 (should (stringp (nth 8 attr))) ;; Modes.
988
989 (setq attr (file-attributes tmp-name 'string))
990 (should (stringp (nth 2 attr))) ;; Uid.
991 (should (stringp (nth 3 attr))) ;; Gid.
992 (delete-file tmp-name)
993
994 (make-directory tmp-name)
995 (should (file-exists-p tmp-name))
996 (should (file-readable-p tmp-name))
997 (should-not (file-regular-p tmp-name))
998 (setq attr (file-attributes tmp-name))
999 (should (eq (car attr) t)))
1000 (ignore-errors (delete-directory tmp-name)))))
1001
1002 (ert-deftest tramp-test19-directory-files-and-attributes ()
1003 "Check `directory-files-and-attributes'."
1004 (skip-unless (tramp--test-enabled))
1005
1006 ;; `directory-files-and-attributes' contains also values for "../".
1007 ;; Ensure that this doesn't change during tests, for
1008 ;; example due to handling temporary files.
1009 (let* ((tmp-name1 (tramp--test-make-temp-name))
1010 (tmp-name2 (expand-file-name "bla" tmp-name1))
1011 attr)
1012 (unwind-protect
1013 (progn
1014 (make-directory tmp-name1)
1015 (should (file-directory-p tmp-name1))
1016 (make-directory tmp-name2)
1017 (should (file-directory-p tmp-name2))
1018 (write-region "foo" nil (expand-file-name "foo" tmp-name2))
1019 (write-region "bar" nil (expand-file-name "bar" tmp-name2))
1020 (write-region "boz" nil (expand-file-name "boz" tmp-name2))
1021 (setq attr (directory-files-and-attributes tmp-name2))
1022 (should (consp attr))
1023 ;; Dumb remote shells without perl(1) or stat(1) are not
1024 ;; able to return the date correctly. They say "don't know".
1025 (dolist (elt attr)
1026 (unless
1027 (equal
1028 (nth 5
1029 (file-attributes (expand-file-name (car elt) tmp-name2)))
1030 '(0 0))
1031 (should
1032 (equal (file-attributes (expand-file-name (car elt) tmp-name2))
1033 (cdr elt)))))
1034 (setq attr (directory-files-and-attributes tmp-name2 'full))
1035 (dolist (elt attr)
1036 (unless (equal (nth 5 (file-attributes (car elt))) '(0 0))
1037 (should
1038 (equal (file-attributes (car elt)) (cdr elt)))))
1039 (setq attr (directory-files-and-attributes tmp-name2 nil "^b"))
1040 (should (equal (mapcar 'car attr) '("bar" "boz"))))
1041 (ignore-errors (delete-directory tmp-name1 'recursive)))))
1042
1043 (ert-deftest tramp-test20-file-modes ()
1044 "Check `file-modes'.
1045 This tests also `file-executable-p', `file-writable-p' and `set-file-modes'."
1046 (skip-unless (tramp--test-enabled))
1047 (skip-unless
1048 (not
1049 (memq
1050 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory)
1051 '(tramp-adb-file-name-handler
1052 tramp-gvfs-file-name-handler
1053 tramp-smb-file-name-handler))))
1054
1055 (let ((tmp-name (tramp--test-make-temp-name)))
1056 (unwind-protect
1057 (progn
1058 (write-region "foo" nil tmp-name)
1059 (should (file-exists-p tmp-name))
1060 (set-file-modes tmp-name #o777)
1061 (should (= (file-modes tmp-name) #o777))
1062 (should (file-executable-p tmp-name))
1063 (should (file-writable-p tmp-name))
1064 (set-file-modes tmp-name #o444)
1065 (should (= (file-modes tmp-name) #o444))
1066 (should-not (file-executable-p tmp-name))
1067 ;; A file is always writable for user "root".
1068 (unless (zerop (nth 2 (file-attributes tmp-name)))
1069 (should-not (file-writable-p tmp-name))))
1070 (ignore-errors (delete-file tmp-name)))))
1071
1072 (ert-deftest tramp-test21-file-links ()
1073 "Check `file-symlink-p'.
1074 This tests also `make-symbolic-link', `file-truename' and `add-name-to-file'."
1075 (skip-unless (tramp--test-enabled))
1076
1077 ;; We must use `file-truename' for the temporary directory, because
1078 ;; it could be located on a symlinked directory. This would let the
1079 ;; test fail.
1080 (let* ((tramp-test-temporary-file-directory
1081 (file-truename tramp-test-temporary-file-directory))
1082 (tmp-name1 (tramp--test-make-temp-name))
1083 (tmp-name2 (tramp--test-make-temp-name))
1084 (tmp-name3 (tramp--test-make-temp-name 'local)))
1085 (unwind-protect
1086 (progn
1087 (write-region "foo" nil tmp-name1)
1088 (should (file-exists-p tmp-name1))
1089 ;; Method "smb" supports `make-symbolic-link' only if the
1090 ;; remote host has CIFS capabilities. tramp-adb.el and
1091 ;; tramp-gvfs.el do not support symbolic links at all.
1092 (condition-case err
1093 (make-symbolic-link tmp-name1 tmp-name2)
1094 (file-error
1095 (skip-unless
1096 (not (string-equal (error-message-string err)
1097 "make-symbolic-link not supported")))))
1098 (should (file-symlink-p tmp-name2))
1099 (should-error (make-symbolic-link tmp-name1 tmp-name2))
1100 (make-symbolic-link tmp-name1 tmp-name2 'ok-if-already-exists)
1101 (should (file-symlink-p tmp-name2))
1102 ;; `tmp-name3' is a local file name.
1103 (should-error (make-symbolic-link tmp-name1 tmp-name3)))
1104 (ignore-errors
1105 (delete-file tmp-name1)
1106 (delete-file tmp-name2)))
1107
1108 (unwind-protect
1109 (progn
1110 (write-region "foo" nil tmp-name1)
1111 (should (file-exists-p tmp-name1))
1112 (add-name-to-file tmp-name1 tmp-name2)
1113 (should-not (file-symlink-p tmp-name2))
1114 (should-error (add-name-to-file tmp-name1 tmp-name2))
1115 (add-name-to-file tmp-name1 tmp-name2 'ok-if-already-exists)
1116 (should-not (file-symlink-p tmp-name2))
1117 ;; `tmp-name3' is a local file name.
1118 (should-error (add-name-to-file tmp-name1 tmp-name3)))
1119 (ignore-errors
1120 (delete-file tmp-name1)
1121 (delete-file tmp-name2)))
1122
1123 (unwind-protect
1124 (progn
1125 (write-region "foo" nil tmp-name1)
1126 (should (file-exists-p tmp-name1))
1127 (make-symbolic-link tmp-name1 tmp-name2)
1128 (should (file-symlink-p tmp-name2))
1129 (should-not (string-equal tmp-name2 (file-truename tmp-name2)))
1130 (should
1131 (string-equal (file-truename tmp-name1) (file-truename tmp-name2)))
1132 (should (file-equal-p tmp-name1 tmp-name2)))
1133 (ignore-errors
1134 (delete-file tmp-name1)
1135 (delete-file tmp-name2)))
1136
1137 ;; `file-truename' shall preserve trailing link of directories.
1138 (unless (file-symlink-p tramp-test-temporary-file-directory)
1139 (let* ((dir1 (directory-file-name tramp-test-temporary-file-directory))
1140 (dir2 (file-name-as-directory dir1)))
1141 (should (string-equal (file-truename dir1) (expand-file-name dir1)))
1142 (should (string-equal (file-truename dir2) (expand-file-name dir2)))))))
1143
1144 (ert-deftest tramp-test22-file-times ()
1145 "Check `set-file-times' and `file-newer-than-file-p'."
1146 (skip-unless (tramp--test-enabled))
1147 (skip-unless
1148 (not
1149 (memq
1150 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory)
1151 '(tramp-gvfs-file-name-handler tramp-smb-file-name-handler))))
1152
1153 (let ((tmp-name1 (tramp--test-make-temp-name))
1154 (tmp-name2 (tramp--test-make-temp-name))
1155 (tmp-name3 (tramp--test-make-temp-name)))
1156 (unwind-protect
1157 (progn
1158 (write-region "foo" nil tmp-name1)
1159 (should (file-exists-p tmp-name1))
1160 (should (consp (nth 5 (file-attributes tmp-name1))))
1161 ;; '(0 0) means don't know, and will be replaced by
1162 ;; `current-time'. Therefore, we use '(0 1).
1163 ;; We skip the test, if the remote handler is not able to
1164 ;; set the correct time.
1165 (skip-unless (set-file-times tmp-name1 '(0 1)))
1166 ;; Dumb remote shells without perl(1) or stat(1) are not
1167 ;; able to return the date correctly. They say "don't know".
1168 (unless (equal (nth 5 (file-attributes tmp-name1)) '(0 0))
1169 (should (equal (nth 5 (file-attributes tmp-name1)) '(0 1)))
1170 (write-region "bla" nil tmp-name2)
1171 (should (file-exists-p tmp-name2))
1172 (should (file-newer-than-file-p tmp-name2 tmp-name1))
1173 ;; `tmp-name3' does not exist.
1174 (should (file-newer-than-file-p tmp-name2 tmp-name3))
1175 (should-not (file-newer-than-file-p tmp-name3 tmp-name1))))
1176 (ignore-errors
1177 (delete-file tmp-name1)
1178 (delete-file tmp-name2)))))
1179
1180 (ert-deftest tramp-test23-visited-file-modtime ()
1181 "Check `set-visited-file-modtime' and `verify-visited-file-modtime'."
1182 (skip-unless (tramp--test-enabled))
1183
1184 (let ((tmp-name (tramp--test-make-temp-name)))
1185 (unwind-protect
1186 (progn
1187 (write-region "foo" nil tmp-name)
1188 (should (file-exists-p tmp-name))
1189 (with-temp-buffer
1190 (insert-file-contents tmp-name)
1191 (should (verify-visited-file-modtime))
1192 (set-visited-file-modtime '(0 1))
1193 (should (verify-visited-file-modtime))
1194 (should (equal (visited-file-modtime) '(0 1 0 0)))))
1195 (ignore-errors (delete-file tmp-name)))))
1196
1197 (ert-deftest tramp-test24-file-name-completion ()
1198 "Check `file-name-completion' and `file-name-all-completions'."
1199 (skip-unless (tramp--test-enabled))
1200
1201 (let ((tmp-name (tramp--test-make-temp-name)))
1202 (unwind-protect
1203 (progn
1204 (make-directory tmp-name)
1205 (should (file-directory-p tmp-name))
1206 (write-region "foo" nil (expand-file-name "foo" tmp-name))
1207 (write-region "bar" nil (expand-file-name "bold" tmp-name))
1208 (make-directory (expand-file-name "boz" tmp-name))
1209 (should (equal (file-name-completion "fo" tmp-name) "foo"))
1210 (should (equal (file-name-completion "b" tmp-name) "bo"))
1211 (should
1212 (equal (file-name-completion "b" tmp-name 'file-directory-p) "boz/"))
1213 (should (equal (file-name-all-completions "fo" tmp-name) '("foo")))
1214 (should
1215 (equal (sort (file-name-all-completions "b" tmp-name) 'string-lessp)
1216 '("bold" "boz/"))))
1217 (ignore-errors (delete-directory tmp-name 'recursive)))))
1218
1219 (ert-deftest tramp-test25-load ()
1220 "Check `load'."
1221 (skip-unless (tramp--test-enabled))
1222
1223 (let ((tmp-name (tramp--test-make-temp-name)))
1224 (unwind-protect
1225 (progn
1226 (load tmp-name 'noerror 'nomessage)
1227 (should-not (featurep 'tramp-test-load))
1228 (write-region "(provide 'tramp-test-load)" nil tmp-name)
1229 ;; `load' in lread.c does not pass `must-suffix'. Why?
1230 ;(should-error (load tmp-name nil 'nomessage 'nosuffix 'must-suffix))
1231 (load tmp-name nil 'nomessage 'nosuffix)
1232 (should (featurep 'tramp-test-load)))
1233 (ignore-errors
1234 (and (featurep 'tramp-test-load) (unload-feature 'tramp-test-load))
1235 (delete-file tmp-name)))))
1236
1237 (ert-deftest tramp-test26-process-file ()
1238 "Check `process-file'."
1239 (skip-unless (tramp--test-enabled))
1240 (skip-unless
1241 (not
1242 (memq
1243 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory)
1244 '(tramp-gvfs-file-name-handler tramp-smb-file-name-handler))))
1245
1246 (let* ((tmp-name (tramp--test-make-temp-name))
1247 (fnnd (file-name-nondirectory tmp-name))
1248 (default-directory tramp-test-temporary-file-directory)
1249 kill-buffer-query-functions)
1250 (unwind-protect
1251 (progn
1252 ;; We cannot use "/bin/true" and "/bin/false"; those paths
1253 ;; do not exist on hydra.
1254 (should (zerop (process-file "true")))
1255 (should-not (zerop (process-file "false")))
1256 (should-not (zerop (process-file "binary-does-not-exist")))
1257 (with-temp-buffer
1258 (write-region "foo" nil tmp-name)
1259 (should (file-exists-p tmp-name))
1260 (should (zerop (process-file "ls" nil t nil fnnd)))
1261 ;; `ls' could produce colorized output.
1262 (goto-char (point-min))
1263 (while (re-search-forward tramp-color-escape-sequence-regexp nil t)
1264 (replace-match "" nil nil))
1265 (should (string-equal (format "%s\n" fnnd) (buffer-string)))
1266 (should-not (get-buffer-window (current-buffer) t))
1267
1268 ;; Second run. The output must be appended.
1269 (should (zerop (process-file "ls" nil t t fnnd)))
1270 ;; `ls' could produce colorized output.
1271 (goto-char (point-min))
1272 (while (re-search-forward tramp-color-escape-sequence-regexp nil t)
1273 (replace-match "" nil nil))
1274 (should
1275 (string-equal (format "%s\n%s\n" fnnd fnnd) (buffer-string)))
1276 ;; A non-nil DISPLAY must not raise the buffer.
1277 (should-not (get-buffer-window (current-buffer) t))))
1278
1279 (ignore-errors (delete-file tmp-name)))))
1280
1281 (ert-deftest tramp-test27-start-file-process ()
1282 "Check `start-file-process'."
1283 (skip-unless (tramp--test-enabled))
1284 (skip-unless
1285 (not
1286 (memq
1287 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory)
1288 '(tramp-adb-file-name-handler
1289 tramp-gvfs-file-name-handler
1290 tramp-smb-file-name-handler))))
1291
1292 (let ((default-directory tramp-test-temporary-file-directory)
1293 (tmp-name (tramp--test-make-temp-name))
1294 kill-buffer-query-functions proc)
1295 (unwind-protect
1296 (with-temp-buffer
1297 (setq proc (start-file-process "test1" (current-buffer) "cat"))
1298 (should (processp proc))
1299 (should (equal (process-status proc) 'run))
1300 (process-send-string proc "foo")
1301 (process-send-eof proc)
1302 ;; Read output.
1303 (with-timeout (10 (ert-fail "`start-file-process' timed out"))
1304 (while (< (- (point-max) (point-min)) (length "foo"))
1305 (accept-process-output proc 1)))
1306 (should (string-equal (buffer-string) "foo")))
1307 (ignore-errors (delete-process proc)))
1308
1309 (unwind-protect
1310 (with-temp-buffer
1311 (write-region "foo" nil tmp-name)
1312 (should (file-exists-p tmp-name))
1313 (setq proc
1314 (start-file-process
1315 "test2" (current-buffer)
1316 "cat" (file-name-nondirectory tmp-name)))
1317 (should (processp proc))
1318 ;; Read output.
1319 (with-timeout (10 (ert-fail "`start-file-process' timed out"))
1320 (while (< (- (point-max) (point-min)) (length "foo"))
1321 (accept-process-output proc 1)))
1322 (should (string-equal (buffer-string) "foo")))
1323 (ignore-errors
1324 (delete-process proc)
1325 (delete-file tmp-name)))
1326
1327 (unwind-protect
1328 (with-temp-buffer
1329 (setq proc (start-file-process "test3" (current-buffer) "cat"))
1330 (should (processp proc))
1331 (should (equal (process-status proc) 'run))
1332 (set-process-filter
1333 proc
1334 (lambda (p s) (with-current-buffer (process-buffer p) (insert s))))
1335 (process-send-string proc "foo")
1336 (process-send-eof proc)
1337 ;; Read output.
1338 (with-timeout (10 (ert-fail "`start-file-process' timed out"))
1339 (while (< (- (point-max) (point-min)) (length "foo"))
1340 (accept-process-output proc 1)))
1341 (should (string-equal (buffer-string) "foo")))
1342 (ignore-errors (delete-process proc)))))
1343
1344 (ert-deftest tramp-test28-shell-command ()
1345 "Check `shell-command'."
1346 (skip-unless (tramp--test-enabled))
1347 (skip-unless
1348 (not
1349 (memq
1350 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory)
1351 '(tramp-adb-file-name-handler
1352 tramp-gvfs-file-name-handler
1353 tramp-smb-file-name-handler))))
1354
1355 (let ((tmp-name (tramp--test-make-temp-name))
1356 (default-directory tramp-test-temporary-file-directory)
1357 kill-buffer-query-functions)
1358 (unwind-protect
1359 (with-temp-buffer
1360 (write-region "foo" nil tmp-name)
1361 (should (file-exists-p tmp-name))
1362 (shell-command
1363 (format "ls %s" (file-name-nondirectory tmp-name)) (current-buffer))
1364 ;; `ls' could produce colorized output.
1365 (goto-char (point-min))
1366 (while (re-search-forward tramp-color-escape-sequence-regexp nil t)
1367 (replace-match "" nil nil))
1368 (should
1369 (string-equal
1370 (format "%s\n" (file-name-nondirectory tmp-name)) (buffer-string))))
1371 (ignore-errors (delete-file tmp-name)))
1372
1373 (unwind-protect
1374 (with-temp-buffer
1375 (write-region "foo" nil tmp-name)
1376 (should (file-exists-p tmp-name))
1377 (async-shell-command
1378 (format "ls %s" (file-name-nondirectory tmp-name)) (current-buffer))
1379 (set-process-sentinel (get-buffer-process (current-buffer)) nil)
1380 ;; Read output.
1381 (with-timeout (10 (ert-fail "`async-shell-command' timed out"))
1382 (while (< (- (point-max) (point-min))
1383 (1+ (length (file-name-nondirectory tmp-name))))
1384 (accept-process-output (get-buffer-process (current-buffer)) 1)))
1385 ;; `ls' could produce colorized output.
1386 (goto-char (point-min))
1387 (while (re-search-forward tramp-color-escape-sequence-regexp nil t)
1388 (replace-match "" nil nil))
1389 ;; There might be a nasty "Process *Async Shell* finished" message.
1390 (goto-char (point-min))
1391 (forward-line)
1392 (narrow-to-region (point-min) (point))
1393 (should
1394 (string-equal
1395 (format "%s\n" (file-name-nondirectory tmp-name)) (buffer-string))))
1396 (ignore-errors (delete-file tmp-name)))
1397
1398 (unwind-protect
1399 (with-temp-buffer
1400 (write-region "foo" nil tmp-name)
1401 (should (file-exists-p tmp-name))
1402 (async-shell-command "read line; ls $line" (current-buffer))
1403 (set-process-sentinel (get-buffer-process (current-buffer)) nil)
1404 (process-send-string
1405 (get-buffer-process (current-buffer))
1406 (format "%s\n" (file-name-nondirectory tmp-name)))
1407 ;; Read output.
1408 (with-timeout (10 (ert-fail "`async-shell-command' timed out"))
1409 (while (< (- (point-max) (point-min))
1410 (1+ (length (file-name-nondirectory tmp-name))))
1411 (accept-process-output (get-buffer-process (current-buffer)) 1)))
1412 ;; `ls' could produce colorized output.
1413 (goto-char (point-min))
1414 (while (re-search-forward tramp-color-escape-sequence-regexp nil t)
1415 (replace-match "" nil nil))
1416 ;; There might be a nasty "Process *Async Shell* finished" message.
1417 (goto-char (point-min))
1418 (forward-line)
1419 (narrow-to-region (point-min) (point))
1420 (should
1421 (string-equal
1422 (format "%s\n" (file-name-nondirectory tmp-name)) (buffer-string))))
1423 (ignore-errors (delete-file tmp-name)))))
1424
1425 (ert-deftest tramp-test29-vc-registered ()
1426 "Check `vc-registered'."
1427 (skip-unless (tramp--test-enabled))
1428 (skip-unless
1429 (eq
1430 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory)
1431 'tramp-sh-file-name-handler))
1432
1433 (let* ((default-directory tramp-test-temporary-file-directory)
1434 (tmp-name1 (tramp--test-make-temp-name))
1435 (tmp-name2 (expand-file-name "foo" tmp-name1))
1436 (tramp-remote-process-environment tramp-remote-process-environment)
1437 (vc-handled-backends
1438 (with-parsed-tramp-file-name tramp-test-temporary-file-directory nil
1439 (cond
1440 ((tramp-find-executable v vc-bzr-program (tramp-get-remote-path v))
1441 (setq tramp-remote-process-environment
1442 (cons (format "BZR_HOME=%s"
1443 (file-remote-p tmp-name1 'localname))
1444 tramp-remote-process-environment))
1445 ;; We must force a reconnect, in order to activate $BZR_HOME.
1446 (tramp-cleanup-connection
1447 (tramp-dissect-file-name tramp-test-temporary-file-directory)
1448 nil 'keep-password)
1449 '(Bzr))
1450 ((tramp-find-executable v vc-git-program (tramp-get-remote-path v))
1451 '(Git))
1452 ((tramp-find-executable v vc-hg-program (tramp-get-remote-path v))
1453 '(Hg))
1454 (t nil)))))
1455 (skip-unless vc-handled-backends)
1456 (message "%s" vc-handled-backends)
1457
1458 (unwind-protect
1459 (progn
1460 (make-directory tmp-name1)
1461 (write-region "foo" nil tmp-name2)
1462 (should (file-directory-p tmp-name1))
1463 (should (file-exists-p tmp-name2))
1464 (should-not (vc-registered tmp-name1))
1465 (should-not (vc-registered tmp-name2))
1466
1467 (let ((default-directory tmp-name1))
1468 ;; Create empty repository, and register the file.
1469 (vc-create-repo (car vc-handled-backends))
1470 ;; The structure of VC-FILESET is not documented. Let's
1471 ;; hope it won't change.
1472 (vc-register
1473 nil (list (car vc-handled-backends)
1474 (list (file-name-nondirectory tmp-name2)))))
1475 (should (vc-registered tmp-name2)))
1476
1477 (ignore-errors (delete-directory tmp-name1 'recursive)))))
1478
1479 (defun tramp--test-check-files (&rest files)
1480 "Runs a simple but comprehensive test over every file in FILES."
1481 (let ((tmp-name1 (tramp--test-make-temp-name))
1482 (tmp-name2 (tramp--test-make-temp-name 'local)))
1483 (unwind-protect
1484 (progn
1485 (make-directory tmp-name1)
1486 (make-directory tmp-name2)
1487 (dolist (elt files)
1488 (let ((file1 (expand-file-name elt tmp-name1))
1489 (file2 (expand-file-name elt tmp-name2)))
1490 (write-region elt nil file1)
1491 (should (file-exists-p file1))
1492 ;; Check file contents.
1493 (with-temp-buffer
1494 (insert-file-contents file1)
1495 (should (string-equal (buffer-string) elt)))
1496 ;; Copy file both directions.
1497 (copy-file file1 tmp-name2)
1498 (should (file-exists-p file2))
1499 (delete-file file1)
1500 (should-not (file-exists-p file1))
1501 (copy-file file2 tmp-name1)
1502 (should (file-exists-p file1))))
1503
1504 ;; Check file names.
1505 (should (equal (directory-files
1506 tmp-name1 nil directory-files-no-dot-files-regexp)
1507 (sort (copy-sequence files) 'string-lessp)))
1508 (should (equal (directory-files
1509 tmp-name2 nil directory-files-no-dot-files-regexp)
1510 (sort (copy-sequence files) 'string-lessp)))
1511
1512 ;; `substitute-in-file-name' could return different values.
1513 ;; For `adb', there could be strange file permissions
1514 ;; preventing overwriting a file. We don't care in this
1515 ;; testcase.
1516 (dolist (elt files)
1517 (let ((file1
1518 (substitute-in-file-name (expand-file-name elt tmp-name1)))
1519 (file2
1520 (substitute-in-file-name (expand-file-name elt tmp-name2))))
1521 (ignore-errors (write-region elt nil file1))
1522 (should (file-exists-p file1))
1523 (ignore-errors (write-region elt nil file2 nil 'nomessage))
1524 (should (file-exists-p file2))))
1525
1526 (should (equal (directory-files
1527 tmp-name1 nil directory-files-no-dot-files-regexp)
1528 (directory-files
1529 tmp-name2 nil directory-files-no-dot-files-regexp))))
1530
1531 (ignore-errors (delete-directory tmp-name1 'recursive))
1532 (ignore-errors (delete-directory tmp-name2 'recursive)))))
1533
1534 ;; This test is inspired by Bug#17238.
1535 (ert-deftest tramp-test30-special-characters ()
1536 "Check special characters in file names."
1537 (skip-unless (tramp--test-enabled))
1538 (skip-unless
1539 (not
1540 (memq
1541 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory)
1542 '(tramp-adb-file-name-handler
1543 tramp-gvfs-file-name-handler
1544 tramp-smb-file-name-handler))))
1545
1546 ;; Newlines, slashes and backslashes in file names are not supported.
1547 ;; So we don't test.
1548 (tramp--test-check-files
1549 " foo\tbar baz\t"
1550 "$foo$bar$$baz$"
1551 "-foo-bar-baz-"
1552 "%foo%bar%baz%"
1553 "&foo&bar&baz&"
1554 "?foo?bar?baz?"
1555 "*foo*bar*baz*"
1556 "'foo\"bar'baz\""
1557 "#foo~bar#baz~"
1558 "!foo|bar!baz|"
1559 ":foo;bar:baz;"
1560 "<foo>bar<baz>"
1561 "(foo)bar(baz)"
1562 "[foo]bar[baz]"
1563 "{foo}bar{baz}"))
1564
1565 (ert-deftest tramp-test31-utf8 ()
1566 "Check UTF8 encoding in file names and file contents."
1567 (skip-unless (tramp--test-enabled))
1568
1569 (let ((coding-system-for-read 'utf-8)
1570 (coding-system-for-write 'utf-8)
1571 (file-name-coding-system 'utf-8))
1572 (tramp--test-check-files
1573 "أصبح بوسعك الآن تنزيل نسخة كاملة من موسوعة ويكيبيديا العربية لتصفحها بلا اتصال بالإنترنت"
1574 "银河系漫游指南系列"
1575 "Автостопом по гала́ктике")))
1576
1577 ;; This test is inspired by Bug#16928.
1578 (ert-deftest tramp-test32-asynchronous-requests ()
1579 "Check parallel asynchronous requests.
1580 Such requests could arrive from timers, process filters and
1581 process sentinels. They shall not disturb each other."
1582 ;; Mark as failed until bug has been fixed.
1583 :expected-result :failed
1584 (skip-unless (tramp--test-enabled))
1585 (skip-unless
1586 (eq
1587 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory)
1588 'tramp-sh-file-name-handler))
1589
1590 ;; Keep instrumentation verbosity 0 until Tramp bug is fixed. This
1591 ;; has the side effect, that this test fails instead to abort. Good
1592 ;; for hydra.
1593 (tramp--instrument-test-case 0
1594 (let* ((tmp-name (tramp--test-make-temp-name))
1595 (default-directory tmp-name)
1596 (remote-file-name-inhibit-cache t)
1597 timer buffers kill-buffer-query-functions)
1598
1599 (unwind-protect
1600 (progn
1601 (make-directory tmp-name)
1602
1603 ;; Setup a timer in order to raise an ordinary command again
1604 ;; and again. `vc-registered' is well suited, because there
1605 ;; are many checks.
1606 (setq
1607 timer
1608 (run-at-time
1609 0 1
1610 (lambda ()
1611 (when buffers
1612 (vc-registered
1613 (buffer-name (nth (random (length buffers)) buffers)))))))
1614
1615 ;; Create temporary buffers. The number of buffers
1616 ;; corresponds to the number of processes; it could be
1617 ;; increased in order to make pressure on Tramp.
1618 (dotimes (i 5)
1619 (add-to-list 'buffers (generate-new-buffer "*temp*")))
1620
1621 ;; Open asynchronous processes. Set process sentinel.
1622 (dolist (buf buffers)
1623 (async-shell-command "read line; touch $line; echo $line" buf)
1624 (set-process-sentinel
1625 (get-buffer-process buf)
1626 (lambda (proc _state)
1627 (delete-file (buffer-name (process-buffer proc))))))
1628
1629 ;; Send a string. Use a random order of the buffers. Mix
1630 ;; with regular operation.
1631 (let ((buffers (copy-sequence buffers))
1632 buf)
1633 (while buffers
1634 (setq buf (nth (random (length buffers)) buffers))
1635 (process-send-string
1636 (get-buffer-process buf) (format "'%s'\n" buf))
1637 (file-attributes (buffer-name buf))
1638 (setq buffers (delq buf buffers))))
1639
1640 ;; Wait until the whole output has been read.
1641 (with-timeout ((* 10 (length buffers))
1642 (ert-fail "`async-shell-command' timed out"))
1643 (let ((buffers (copy-sequence buffers))
1644 buf)
1645 (while buffers
1646 (setq buf (nth (random (length buffers)) buffers))
1647 (if (ignore-errors
1648 (memq (process-status (get-buffer-process buf))
1649 '(run open)))
1650 (accept-process-output (get-buffer-process buf) 0.1)
1651 (setq buffers (delq buf buffers))))))
1652
1653 ;; Check.
1654 (dolist (buf buffers)
1655 (with-current-buffer buf
1656 (should
1657 (string-equal (format "'%s'\n" buf) (buffer-string)))))
1658 (should-not
1659 (directory-files tmp-name nil directory-files-no-dot-files-regexp)))
1660
1661 ;; Cleanup.
1662 (ignore-errors (cancel-timer timer))
1663 (ignore-errors (delete-directory tmp-name 'recursive))
1664 (dolist (buf buffers)
1665 (ignore-errors (kill-buffer buf)))))))
1666
1667 (ert-deftest tramp-test33-recursive-load ()
1668 "Check that Tramp does not fail due to recursive load."
1669 (skip-unless (tramp--test-enabled))
1670
1671 (dolist (code
1672 (list
1673 (format
1674 "(expand-file-name %S)"
1675 tramp-test-temporary-file-directory)
1676 (format
1677 "(let ((default-directory %S)) (expand-file-name %S))"
1678 tramp-test-temporary-file-directory
1679 temporary-file-directory)))
1680 (should-not
1681 (string-match
1682 "Recursive load"
1683 (shell-command-to-string
1684 (format
1685 "%s -batch -Q -L %s --eval %s"
1686 (expand-file-name invocation-name invocation-directory)
1687 (mapconcat 'shell-quote-argument load-path " -L ")
1688 (shell-quote-argument code)))))))
1689
1690 (ert-deftest tramp-test34-unload ()
1691 "Check that Tramp and its subpackages unload completely.
1692 Since it unloads Tramp, it shall be the last test to run."
1693 ;; Mark as failed until all symbols are unbound.
1694 :expected-result (if (featurep 'tramp) :failed :passed)
1695 (when (featurep 'tramp)
1696 (unload-feature 'tramp 'force)
1697 ;; No Tramp feature must be left.
1698 (should-not (featurep 'tramp))
1699 (should-not (all-completions "tramp" (delq 'tramp-tests features)))
1700 ;; `file-name-handler-alist' must be clean.
1701 (should-not (all-completions "tramp" (mapcar 'cdr file-name-handler-alist)))
1702 ;; There shouldn't be left a bound symbol. We do not regard our
1703 ;; test symbols, and the Tramp unload hooks.
1704 (mapatoms
1705 (lambda (x)
1706 (and (or (boundp x) (functionp x))
1707 (string-match "^tramp" (symbol-name x))
1708 (not (string-match "^tramp--?test" (symbol-name x)))
1709 (not (string-match "unload-hook$" (symbol-name x)))
1710 (ert-fail (format "`%s' still bound" x)))))
1711 ; (progn (message "`%s' still bound" x)))))
1712 ;; There shouldn't be left a hook function containing a Tramp
1713 ;; function. We do not regard the Tramp unload hooks.
1714 (mapatoms
1715 (lambda (x)
1716 (and (boundp x)
1717 (string-match "-hooks?$" (symbol-name x))
1718 (not (string-match "unload-hook$" (symbol-name x)))
1719 (consp (symbol-value x))
1720 (ignore-errors (all-completions "tramp" (symbol-value x)))
1721 (ert-fail (format "Hook `%s' still contains Tramp function" x)))))))
1722
1723 ;; TODO:
1724
1725 ;; * dired-compress-file
1726 ;; * dired-uncache
1727 ;; * file-acl
1728 ;; * file-ownership-preserved-p
1729 ;; * file-selinux-context
1730 ;; * find-backup-file-name
1731 ;; * make-auto-save-file-name
1732 ;; * set-file-acl
1733 ;; * set-file-selinux-context
1734
1735 ;; * Work on skipped tests. Make a comment, when it is impossible.
1736 ;; * Fix `tramp-test15-copy-directory' for `smb'. Using tar in a pipe
1737 ;; doesn't work well when an interactive password must be provided.
1738 ;; * Fix `tramp-test27-start-file-process' for `nc' and on MS
1739 ;; Windows (`process-send-eof'?).
1740 ;; * Fix `tramp-test28-shell-command' on MS Windows (nasty plink message).
1741 ;; * Fix `tramp-test30-special-characters' for `adb', `nc' and `smb'.
1742 ;; * Fix `tramp-test31-utf8' for MS Windows and `nc'/`telnet' (when
1743 ;; target is a dumb busybox). Seems to be in `directory-files'.
1744 ;; * Fix Bug#16928. Set expected error of `tramp-test32-asynchronous-requests'.
1745 ;; * Fix `tramp-test34-unload' (Not all symbols are unbound). Set
1746 ;; expected error.
1747
1748 (defun tramp-test-all (&optional interactive)
1749 "Run all tests for \\[tramp]."
1750 (interactive "p")
1751 (funcall
1752 (if interactive 'ert-run-tests-interactively 'ert-run-tests-batch) "^tramp"))
1753
1754 (provide 'tramp-tests)
1755 ;;; tramp-tests.el ends here