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