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