]> code.delx.au - gnu-emacs/blob - test/lisp/net/tramp-tests.el
Merge from origin/emacs-25
[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 (unwind-protect
643 ;; Bug#10085.
644 (dolist (n-e '(nil t))
645 (let ((non-essential n-e))
646 (when (getenv "NIX_STORE")
647 (dolist (elt (all-completions "tramp-" obarray 'functionp))
648 (trace-function-background (intern elt))))
649 (dolist (file
650 `(,(file-remote-p tramp-test-temporary-file-directory 'method)
651 ,(file-remote-p tramp-test-temporary-file-directory 'host)))
652 (unless (zerop (length file))
653 (setq file (format "/%s:" file))
654 (should (string-equal (directory-file-name file) file))
655 (when (getenv "NIX_STORE")
656 (message "file %s non-essential %s tramp-completion-mode-p %s"
657 file non-essential (tramp-completion-mode-p)))
658 (should
659 (string-equal
660 (file-name-as-directory file)
661 (if (tramp-completion-mode-p) file (concat file "./"))))
662 (should (string-equal (file-name-directory file) file))
663 (should (string-equal (file-name-nondirectory file) ""))))))
664 (when (getenv "NIX_STORE")
665 (untrace-all)
666 (message "%s" (with-current-buffer trace-buffer (buffer-string))))))
667
668 (ert-deftest tramp-test07-file-exists-p ()
669 "Check `file-exist-p', `write-region' and `delete-file'."
670 (skip-unless (tramp--test-enabled))
671
672 (let ((tmp-name (tramp--test-make-temp-name)))
673 (should-not (file-exists-p tmp-name))
674 (write-region "foo" nil tmp-name)
675 (should (file-exists-p tmp-name))
676 (delete-file tmp-name)
677 (should-not (file-exists-p tmp-name))))
678
679 (ert-deftest tramp-test08-file-local-copy ()
680 "Check `file-local-copy'."
681 (skip-unless (tramp--test-enabled))
682
683 (let ((tmp-name1 (tramp--test-make-temp-name))
684 tmp-name2)
685 (unwind-protect
686 (progn
687 (write-region "foo" nil tmp-name1)
688 (should (setq tmp-name2 (file-local-copy tmp-name1)))
689 (with-temp-buffer
690 (insert-file-contents tmp-name2)
691 (should (string-equal (buffer-string) "foo")))
692 ;; Check also that a file transfer with compression works.
693 (let ((default-directory tramp-test-temporary-file-directory)
694 (tramp-copy-size-limit 4)
695 (tramp-inline-compress-start-size 2))
696 (delete-file tmp-name2)
697 (should (setq tmp-name2 (file-local-copy tmp-name1)))))
698
699 ;; Cleanup.
700 (ignore-errors
701 (delete-file tmp-name1)
702 (delete-file tmp-name2)))))
703
704 (ert-deftest tramp-test09-insert-file-contents ()
705 "Check `insert-file-contents'."
706 (skip-unless (tramp--test-enabled))
707
708 (let ((tmp-name (tramp--test-make-temp-name)))
709 (unwind-protect
710 (progn
711 (write-region "foo" nil tmp-name)
712 (with-temp-buffer
713 (insert-file-contents tmp-name)
714 (should (string-equal (buffer-string) "foo"))
715 (insert-file-contents tmp-name)
716 (should (string-equal (buffer-string) "foofoo"))
717 ;; Insert partly.
718 (insert-file-contents tmp-name nil 1 3)
719 (should (string-equal (buffer-string) "oofoofoo"))
720 ;; Replace.
721 (insert-file-contents tmp-name nil nil nil 'replace)
722 (should (string-equal (buffer-string) "foo"))))
723
724 ;; Cleanup.
725 (ignore-errors (delete-file tmp-name)))))
726
727 (ert-deftest tramp-test10-write-region ()
728 "Check `write-region'."
729 (skip-unless (tramp--test-enabled))
730
731 (let ((tmp-name (tramp--test-make-temp-name)))
732 (unwind-protect
733 (progn
734 (with-temp-buffer
735 (insert "foo")
736 (write-region nil nil tmp-name))
737 (with-temp-buffer
738 (insert-file-contents tmp-name)
739 (should (string-equal (buffer-string) "foo")))
740 ;; Append.
741 (with-temp-buffer
742 (insert "bla")
743 (write-region nil nil tmp-name 'append))
744 (with-temp-buffer
745 (insert-file-contents tmp-name)
746 (should (string-equal (buffer-string) "foobla")))
747 ;; Write string.
748 (write-region "foo" nil tmp-name)
749 (with-temp-buffer
750 (insert-file-contents tmp-name)
751 (should (string-equal (buffer-string) "foo")))
752 ;; Write partly.
753 (with-temp-buffer
754 (insert "123456789")
755 (write-region 3 5 tmp-name))
756 (with-temp-buffer
757 (insert-file-contents tmp-name)
758 (should (string-equal (buffer-string) "34"))))
759
760 ;; Cleanup.
761 (ignore-errors (delete-file tmp-name)))))
762
763 (ert-deftest tramp-test11-copy-file ()
764 "Check `copy-file'."
765 (skip-unless (tramp--test-enabled))
766
767 (let ((tmp-name1 (tramp--test-make-temp-name))
768 (tmp-name2 (tramp--test-make-temp-name))
769 (tmp-name3 (tramp--test-make-temp-name))
770 (tmp-name4 (tramp--test-make-temp-name 'local))
771 (tmp-name5 (tramp--test-make-temp-name 'local)))
772
773 ;; Copy on remote side.
774 (unwind-protect
775 (progn
776 (write-region "foo" nil tmp-name1)
777 (copy-file tmp-name1 tmp-name2)
778 (should (file-exists-p tmp-name2))
779 (with-temp-buffer
780 (insert-file-contents tmp-name2)
781 (should (string-equal (buffer-string) "foo")))
782 (should-error (copy-file tmp-name1 tmp-name2))
783 (copy-file tmp-name1 tmp-name2 'ok)
784 (make-directory tmp-name3)
785 (copy-file tmp-name1 tmp-name3)
786 (should
787 (file-exists-p
788 (expand-file-name (file-name-nondirectory tmp-name1) tmp-name3))))
789
790 ;; Cleanup.
791 (ignore-errors (delete-file tmp-name1))
792 (ignore-errors (delete-file tmp-name2))
793 (ignore-errors (delete-directory tmp-name3 'recursive)))
794
795 ;; Copy from remote side to local side.
796 (unwind-protect
797 (progn
798 (write-region "foo" nil tmp-name1)
799 (copy-file tmp-name1 tmp-name4)
800 (should (file-exists-p tmp-name4))
801 (with-temp-buffer
802 (insert-file-contents tmp-name4)
803 (should (string-equal (buffer-string) "foo")))
804 (should-error (copy-file tmp-name1 tmp-name4))
805 (copy-file tmp-name1 tmp-name4 'ok)
806 (make-directory tmp-name5)
807 (copy-file tmp-name1 tmp-name5)
808 (should
809 (file-exists-p
810 (expand-file-name (file-name-nondirectory tmp-name1) tmp-name5))))
811
812 ;; Cleanup.
813 (ignore-errors (delete-file tmp-name1))
814 (ignore-errors (delete-file tmp-name4))
815 (ignore-errors (delete-directory tmp-name5 'recursive)))
816
817 ;; Copy from local side to remote side.
818 (unwind-protect
819 (progn
820 (write-region "foo" nil tmp-name4 nil 'nomessage)
821 (copy-file tmp-name4 tmp-name1)
822 (should (file-exists-p tmp-name1))
823 (with-temp-buffer
824 (insert-file-contents tmp-name1)
825 (should (string-equal (buffer-string) "foo")))
826 (should-error (copy-file tmp-name4 tmp-name1))
827 (copy-file tmp-name4 tmp-name1 'ok)
828 (make-directory tmp-name3)
829 (copy-file tmp-name4 tmp-name3)
830 (should
831 (file-exists-p
832 (expand-file-name (file-name-nondirectory tmp-name4) tmp-name3))))
833
834 ;; Cleanup.
835 (ignore-errors (delete-file tmp-name1))
836 (ignore-errors (delete-file tmp-name4))
837 (ignore-errors (delete-directory tmp-name3 'recursive)))))
838
839 (ert-deftest tramp-test12-rename-file ()
840 "Check `rename-file'."
841 (skip-unless (tramp--test-enabled))
842
843 (let ((tmp-name1 (tramp--test-make-temp-name))
844 (tmp-name2 (tramp--test-make-temp-name))
845 (tmp-name3 (tramp--test-make-temp-name))
846 (tmp-name4 (tramp--test-make-temp-name 'local))
847 (tmp-name5 (tramp--test-make-temp-name 'local)))
848
849 ;; Rename on remote side.
850 (unwind-protect
851 (progn
852 (write-region "foo" nil tmp-name1)
853 (rename-file tmp-name1 tmp-name2)
854 (should-not (file-exists-p tmp-name1))
855 (should (file-exists-p tmp-name2))
856 (with-temp-buffer
857 (insert-file-contents tmp-name2)
858 (should (string-equal (buffer-string) "foo")))
859 (write-region "foo" nil tmp-name1)
860 (should-error (rename-file tmp-name1 tmp-name2))
861 (rename-file tmp-name1 tmp-name2 'ok)
862 (should-not (file-exists-p tmp-name1))
863 (write-region "foo" nil tmp-name1)
864 (make-directory tmp-name3)
865 (rename-file tmp-name1 tmp-name3)
866 (should-not (file-exists-p tmp-name1))
867 (should
868 (file-exists-p
869 (expand-file-name (file-name-nondirectory tmp-name1) tmp-name3))))
870
871 ;; Cleanup.
872 (ignore-errors (delete-file tmp-name1))
873 (ignore-errors (delete-file tmp-name2))
874 (ignore-errors (delete-directory tmp-name3 'recursive)))
875
876 ;; Rename from remote side to local side.
877 (unwind-protect
878 (progn
879 (write-region "foo" nil tmp-name1)
880 (rename-file tmp-name1 tmp-name4)
881 (should-not (file-exists-p tmp-name1))
882 (should (file-exists-p tmp-name4))
883 (with-temp-buffer
884 (insert-file-contents tmp-name4)
885 (should (string-equal (buffer-string) "foo")))
886 (write-region "foo" nil tmp-name1)
887 (should-error (rename-file tmp-name1 tmp-name4))
888 (rename-file tmp-name1 tmp-name4 'ok)
889 (should-not (file-exists-p tmp-name1))
890 (write-region "foo" nil tmp-name1)
891 (make-directory tmp-name5)
892 (rename-file tmp-name1 tmp-name5)
893 (should-not (file-exists-p tmp-name1))
894 (should
895 (file-exists-p
896 (expand-file-name (file-name-nondirectory tmp-name1) tmp-name5))))
897
898 ;; Cleanup.
899 (ignore-errors (delete-file tmp-name1))
900 (ignore-errors (delete-file tmp-name4))
901 (ignore-errors (delete-directory tmp-name5 'recursive)))
902
903 ;; Rename from local side to remote side.
904 (unwind-protect
905 (progn
906 (write-region "foo" nil tmp-name4 nil 'nomessage)
907 (rename-file tmp-name4 tmp-name1)
908 (should-not (file-exists-p tmp-name4))
909 (should (file-exists-p tmp-name1))
910 (with-temp-buffer
911 (insert-file-contents tmp-name1)
912 (should (string-equal (buffer-string) "foo")))
913 (write-region "foo" nil tmp-name4 nil 'nomessage)
914 (should-error (rename-file tmp-name4 tmp-name1))
915 (rename-file tmp-name4 tmp-name1 'ok)
916 (should-not (file-exists-p tmp-name4))
917 (write-region "foo" nil tmp-name4 nil 'nomessage)
918 (make-directory tmp-name3)
919 (rename-file tmp-name4 tmp-name3)
920 (should-not (file-exists-p tmp-name4))
921 (should
922 (file-exists-p
923 (expand-file-name (file-name-nondirectory tmp-name4) tmp-name3))))
924
925 ;; Cleanup.
926 (ignore-errors (delete-file tmp-name1))
927 (ignore-errors (delete-file tmp-name4))
928 (ignore-errors (delete-directory tmp-name3 'recursive)))))
929
930 (ert-deftest tramp-test13-make-directory ()
931 "Check `make-directory'.
932 This tests also `file-directory-p' and `file-accessible-directory-p'."
933 (skip-unless (tramp--test-enabled))
934
935 (let* ((tmp-name1 (tramp--test-make-temp-name))
936 (tmp-name2 (expand-file-name "foo/bar" tmp-name1)))
937 (unwind-protect
938 (progn
939 (make-directory tmp-name1)
940 (should (file-directory-p tmp-name1))
941 (should (file-accessible-directory-p tmp-name1))
942 (should-error (make-directory tmp-name2) :type 'file-error)
943 (make-directory tmp-name2 'parents)
944 (should (file-directory-p tmp-name2))
945 (should (file-accessible-directory-p tmp-name2)))
946
947 ;; Cleanup.
948 (ignore-errors (delete-directory tmp-name1 'recursive)))))
949
950 (ert-deftest tramp-test14-delete-directory ()
951 "Check `delete-directory'."
952 (skip-unless (tramp--test-enabled))
953
954 (let ((tmp-name (tramp--test-make-temp-name)))
955 ;; Delete empty directory.
956 (make-directory tmp-name)
957 (should (file-directory-p tmp-name))
958 (delete-directory tmp-name)
959 (should-not (file-directory-p tmp-name))
960 ;; Delete non-empty directory.
961 (make-directory tmp-name)
962 (write-region "foo" nil (expand-file-name "bla" tmp-name))
963 (should-error (delete-directory tmp-name) :type 'file-error)
964 (delete-directory tmp-name 'recursive)
965 (should-not (file-directory-p tmp-name))))
966
967 (ert-deftest tramp-test15-copy-directory ()
968 "Check `copy-directory'."
969 (skip-unless (tramp--test-enabled))
970 (skip-unless
971 (not
972 (eq
973 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory)
974 'tramp-smb-file-name-handler)))
975
976 (let* ((tmp-name1 (tramp--test-make-temp-name))
977 (tmp-name2 (tramp--test-make-temp-name))
978 (tmp-name3 (expand-file-name
979 (file-name-nondirectory tmp-name1) tmp-name2))
980 (tmp-name4 (expand-file-name "foo" tmp-name1))
981 (tmp-name5 (expand-file-name "foo" tmp-name2))
982 (tmp-name6 (expand-file-name "foo" tmp-name3)))
983 (unwind-protect
984 (progn
985 ;; Copy empty directory.
986 (make-directory tmp-name1)
987 (write-region "foo" nil tmp-name4)
988 (should (file-directory-p tmp-name1))
989 (should (file-exists-p tmp-name4))
990 (copy-directory tmp-name1 tmp-name2)
991 (should (file-directory-p tmp-name2))
992 (should (file-exists-p tmp-name5))
993 ;; Target directory does exist already.
994 (copy-directory tmp-name1 tmp-name2)
995 (should (file-directory-p tmp-name3))
996 (should (file-exists-p tmp-name6)))
997
998 ;; Cleanup.
999 (ignore-errors
1000 (delete-directory tmp-name1 'recursive)
1001 (delete-directory tmp-name2 'recursive)))))
1002
1003 (ert-deftest tramp-test16-directory-files ()
1004 "Check `directory-files'."
1005 (skip-unless (tramp--test-enabled))
1006
1007 (let* ((tmp-name1 (tramp--test-make-temp-name))
1008 (tmp-name2 (expand-file-name "bla" tmp-name1))
1009 (tmp-name3 (expand-file-name "foo" tmp-name1)))
1010 (unwind-protect
1011 (progn
1012 (make-directory tmp-name1)
1013 (write-region "foo" nil tmp-name2)
1014 (write-region "bla" nil tmp-name3)
1015 (should (file-directory-p tmp-name1))
1016 (should (file-exists-p tmp-name2))
1017 (should (file-exists-p tmp-name3))
1018 (should (equal (directory-files tmp-name1) '("." ".." "bla" "foo")))
1019 (should (equal (directory-files tmp-name1 'full)
1020 `(,(concat tmp-name1 "/.")
1021 ,(concat tmp-name1 "/..")
1022 ,tmp-name2 ,tmp-name3)))
1023 (should (equal (directory-files
1024 tmp-name1 nil directory-files-no-dot-files-regexp)
1025 '("bla" "foo")))
1026 (should (equal (directory-files
1027 tmp-name1 'full directory-files-no-dot-files-regexp)
1028 `(,tmp-name2 ,tmp-name3))))
1029
1030 ;; Cleanup.
1031 (ignore-errors (delete-directory tmp-name1 'recursive)))))
1032
1033 (ert-deftest tramp-test17-insert-directory ()
1034 "Check `insert-directory'."
1035 (skip-unless (tramp--test-enabled))
1036
1037 (let* ((tmp-name1 (tramp--test-make-temp-name))
1038 (tmp-name2 (expand-file-name "foo" tmp-name1))
1039 ;; We test for the summary line. Keyword "total" could be localized.
1040 (process-environment
1041 (append '("LANG=C" "LANGUAGE=C" "LC_ALL=C") process-environment)))
1042 (unwind-protect
1043 (progn
1044 (make-directory tmp-name1)
1045 (write-region "foo" nil tmp-name2)
1046 (should (file-directory-p tmp-name1))
1047 (should (file-exists-p tmp-name2))
1048 (with-temp-buffer
1049 (insert-directory tmp-name1 nil)
1050 (goto-char (point-min))
1051 (should (looking-at-p (regexp-quote tmp-name1))))
1052 (with-temp-buffer
1053 (insert-directory tmp-name1 "-al")
1054 (goto-char (point-min))
1055 (should (looking-at-p (format "^.+ %s$" (regexp-quote tmp-name1)))))
1056 (with-temp-buffer
1057 (insert-directory (file-name-as-directory tmp-name1) "-al")
1058 (goto-char (point-min))
1059 (should
1060 (looking-at-p (format "^.+ %s/$" (regexp-quote tmp-name1)))))
1061 (with-temp-buffer
1062 (insert-directory
1063 (file-name-as-directory tmp-name1) "-al" nil 'full-directory-p)
1064 (goto-char (point-min))
1065 (should
1066 (looking-at-p
1067 (concat
1068 ;; There might be a summary line.
1069 "\\(total.+[[:digit:]]+\n\\)?"
1070 ;; We don't know in which order ".", ".." and "foo" appear.
1071 "\\(.+ \\(\\.?\\.\\|foo\\)\n\\)\\{3\\}")))))
1072
1073 ;; Cleanup.
1074 (ignore-errors (delete-directory tmp-name1 'recursive)))))
1075
1076 (ert-deftest tramp-test18-file-attributes ()
1077 "Check `file-attributes'.
1078 This tests also `file-readable-p' and `file-regular-p'."
1079 (skip-unless (tramp--test-enabled))
1080
1081 ;; We must use `file-truename' for the temporary directory, because
1082 ;; it could be located on a symlinked directory. This would let the
1083 ;; test fail.
1084 (let* ((tramp-test-temporary-file-directory
1085 (file-truename tramp-test-temporary-file-directory))
1086 (tmp-name1 (tramp--test-make-temp-name))
1087 (tmp-name2 (tramp--test-make-temp-name))
1088 ;; File name with "//".
1089 (tmp-name3
1090 (format
1091 "%s%s"
1092 (file-remote-p tmp-name1)
1093 (replace-regexp-in-string
1094 "/" "//" (file-remote-p tmp-name1 'localname))))
1095 attr)
1096 (unwind-protect
1097 (progn
1098 (write-region "foo" nil tmp-name1)
1099 (should (file-exists-p tmp-name1))
1100 (setq attr (file-attributes tmp-name1))
1101 (should (consp attr))
1102 (should (file-exists-p tmp-name1))
1103 (should (file-readable-p tmp-name1))
1104 (should (file-regular-p tmp-name1))
1105 ;; We do not test inodes and device numbers.
1106 (should (null (car attr)))
1107 (should (numberp (nth 1 attr))) ;; Link.
1108 (should (numberp (nth 2 attr))) ;; Uid.
1109 (should (numberp (nth 3 attr))) ;; Gid.
1110 ;; Last access time.
1111 (should (stringp (current-time-string (nth 4 attr))))
1112 ;; Last modification time.
1113 (should (stringp (current-time-string (nth 5 attr))))
1114 ;; Last status change time.
1115 (should (stringp (current-time-string (nth 6 attr))))
1116 (should (numberp (nth 7 attr))) ;; Size.
1117 (should (stringp (nth 8 attr))) ;; Modes.
1118
1119 (setq attr (file-attributes tmp-name1 'string))
1120 (should (stringp (nth 2 attr))) ;; Uid.
1121 (should (stringp (nth 3 attr))) ;; Gid.
1122
1123 (condition-case err
1124 (progn
1125 (make-symbolic-link tmp-name1 tmp-name2)
1126 (should (file-exists-p tmp-name2))
1127 (should (file-symlink-p tmp-name2))
1128 (setq attr (file-attributes tmp-name2))
1129 (should (string-equal
1130 (car attr)
1131 (file-remote-p (file-truename tmp-name1) 'localname)))
1132 (delete-file tmp-name2))
1133 (file-error
1134 (should (string-equal (error-message-string err)
1135 "make-symbolic-link not supported"))))
1136
1137 ;; Check, that "//" in symlinks are handled properly.
1138 (with-temp-buffer
1139 (let ((default-directory tramp-test-temporary-file-directory))
1140 (shell-command
1141 (format
1142 "ln -s %s %s"
1143 (tramp-file-name-localname (tramp-dissect-file-name tmp-name3))
1144 (tramp-file-name-localname (tramp-dissect-file-name tmp-name2)))
1145 t)))
1146 (when (file-symlink-p tmp-name2)
1147 (setq attr (file-attributes tmp-name2))
1148 (should
1149 (string-equal
1150 (car attr)
1151 (tramp-file-name-localname (tramp-dissect-file-name tmp-name3))))
1152 (delete-file tmp-name2))
1153
1154 (delete-file tmp-name1)
1155 (make-directory tmp-name1)
1156 (should (file-exists-p tmp-name1))
1157 (should (file-readable-p tmp-name1))
1158 (should-not (file-regular-p tmp-name1))
1159 (setq attr (file-attributes tmp-name1))
1160 (should (eq (car attr) t)))
1161
1162 ;; Cleanup.
1163 (ignore-errors (delete-directory tmp-name1))
1164 (ignore-errors (delete-file tmp-name1))
1165 (ignore-errors (delete-file tmp-name2)))))
1166
1167 (ert-deftest tramp-test19-directory-files-and-attributes ()
1168 "Check `directory-files-and-attributes'."
1169 (skip-unless (tramp--test-enabled))
1170
1171 ;; `directory-files-and-attributes' contains also values for "../".
1172 ;; Ensure that this doesn't change during tests, for
1173 ;; example due to handling temporary files.
1174 (let* ((tmp-name1 (tramp--test-make-temp-name))
1175 (tmp-name2 (expand-file-name "bla" tmp-name1))
1176 attr)
1177 (unwind-protect
1178 (progn
1179 (make-directory tmp-name1)
1180 (should (file-directory-p tmp-name1))
1181 (make-directory tmp-name2)
1182 (should (file-directory-p tmp-name2))
1183 (write-region "foo" nil (expand-file-name "foo" tmp-name2))
1184 (write-region "bar" nil (expand-file-name "bar" tmp-name2))
1185 (write-region "boz" nil (expand-file-name "boz" tmp-name2))
1186 (setq attr (directory-files-and-attributes tmp-name2))
1187 (should (consp attr))
1188 ;; Dumb remote shells without perl(1) or stat(1) are not
1189 ;; able to return the date correctly. They say "don't know".
1190 (dolist (elt attr)
1191 (unless
1192 (equal
1193 (nth 5
1194 (file-attributes (expand-file-name (car elt) tmp-name2)))
1195 '(0 0))
1196 (should
1197 (equal (file-attributes (expand-file-name (car elt) tmp-name2))
1198 (cdr elt)))))
1199 (setq attr (directory-files-and-attributes tmp-name2 'full))
1200 (dolist (elt attr)
1201 (unless (equal (nth 5 (file-attributes (car elt))) '(0 0))
1202 (should
1203 (equal (file-attributes (car elt)) (cdr elt)))))
1204 (setq attr (directory-files-and-attributes tmp-name2 nil "^b"))
1205 (should (equal (mapcar 'car attr) '("bar" "boz"))))
1206
1207 ;; Cleanup.
1208 (ignore-errors (delete-directory tmp-name1 'recursive)))))
1209
1210 (ert-deftest tramp-test20-file-modes ()
1211 "Check `file-modes'.
1212 This tests also `file-executable-p', `file-writable-p' and `set-file-modes'."
1213 (skip-unless (tramp--test-enabled))
1214 (skip-unless
1215 (not
1216 (memq
1217 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory)
1218 '(tramp-adb-file-name-handler
1219 tramp-gvfs-file-name-handler
1220 tramp-smb-file-name-handler))))
1221
1222 (let ((tmp-name (tramp--test-make-temp-name)))
1223 (unwind-protect
1224 (progn
1225 (write-region "foo" nil tmp-name)
1226 (should (file-exists-p tmp-name))
1227 (set-file-modes tmp-name #o777)
1228 (should (= (file-modes tmp-name) #o777))
1229 (should (file-executable-p tmp-name))
1230 (should (file-writable-p tmp-name))
1231 (set-file-modes tmp-name #o444)
1232 (should (= (file-modes tmp-name) #o444))
1233 (should-not (file-executable-p tmp-name))
1234 ;; A file is always writable for user "root".
1235 (unless (zerop (nth 2 (file-attributes tmp-name)))
1236 (should-not (file-writable-p tmp-name))))
1237
1238 ;; Cleanup.
1239 (ignore-errors (delete-file tmp-name)))))
1240
1241 (ert-deftest tramp-test21-file-links ()
1242 "Check `file-symlink-p'.
1243 This tests also `make-symbolic-link', `file-truename' and `add-name-to-file'."
1244 (skip-unless (tramp--test-enabled))
1245
1246 ;; We must use `file-truename' for the temporary directory, because
1247 ;; it could be located on a symlinked directory. This would let the
1248 ;; test fail.
1249 (let* ((tramp-test-temporary-file-directory
1250 (file-truename tramp-test-temporary-file-directory))
1251 (tmp-name1 (tramp--test-make-temp-name))
1252 (tmp-name2 (tramp--test-make-temp-name))
1253 (tmp-name3 (tramp--test-make-temp-name 'local)))
1254
1255 ;; Check `make-symbolic-link'.
1256 (unwind-protect
1257 (progn
1258 (write-region "foo" nil tmp-name1)
1259 (should (file-exists-p tmp-name1))
1260 ;; Method "smb" supports `make-symbolic-link' only if the
1261 ;; remote host has CIFS capabilities. tramp-adb.el and
1262 ;; tramp-gvfs.el do not support symbolic links at all.
1263 (condition-case err
1264 (make-symbolic-link tmp-name1 tmp-name2)
1265 (file-error
1266 (skip-unless
1267 (not (string-equal (error-message-string err)
1268 "make-symbolic-link not supported")))))
1269 (should (file-symlink-p tmp-name2))
1270 (should-error (make-symbolic-link tmp-name1 tmp-name2))
1271 (make-symbolic-link tmp-name1 tmp-name2 'ok-if-already-exists)
1272 (should (file-symlink-p tmp-name2))
1273 ;; `tmp-name3' is a local file name.
1274 (should-error (make-symbolic-link tmp-name1 tmp-name3)))
1275
1276 ;; Cleanup.
1277 (ignore-errors
1278 (delete-file tmp-name1)
1279 (delete-file tmp-name2)))
1280
1281 ;; Check `add-name-to-file'.
1282 (unwind-protect
1283 (progn
1284 (write-region "foo" nil tmp-name1)
1285 (should (file-exists-p tmp-name1))
1286 (add-name-to-file tmp-name1 tmp-name2)
1287 (should-not (file-symlink-p tmp-name2))
1288 (should-error (add-name-to-file tmp-name1 tmp-name2))
1289 (add-name-to-file tmp-name1 tmp-name2 'ok-if-already-exists)
1290 (should-not (file-symlink-p tmp-name2))
1291 ;; `tmp-name3' is a local file name.
1292 (should-error (add-name-to-file tmp-name1 tmp-name3)))
1293
1294 ;; Cleanup.
1295 (ignore-errors
1296 (delete-file tmp-name1)
1297 (delete-file tmp-name2)))
1298
1299 ;; Check `file-truename'.
1300 (unwind-protect
1301 (progn
1302 (write-region "foo" nil tmp-name1)
1303 (should (file-exists-p tmp-name1))
1304 (make-symbolic-link tmp-name1 tmp-name2)
1305 (should (file-symlink-p tmp-name2))
1306 (should-not (string-equal tmp-name2 (file-truename tmp-name2)))
1307 (should
1308 (string-equal (file-truename tmp-name1) (file-truename tmp-name2)))
1309 (should (file-equal-p tmp-name1 tmp-name2)))
1310 (ignore-errors
1311 (delete-file tmp-name1)
1312 (delete-file tmp-name2)))
1313
1314 ;; `file-truename' shall preserve trailing link of directories.
1315 (unless (file-symlink-p tramp-test-temporary-file-directory)
1316 (let* ((dir1 (directory-file-name tramp-test-temporary-file-directory))
1317 (dir2 (file-name-as-directory dir1)))
1318 (should (string-equal (file-truename dir1) (expand-file-name dir1)))
1319 (should (string-equal (file-truename dir2) (expand-file-name dir2)))))))
1320
1321 (ert-deftest tramp-test22-file-times ()
1322 "Check `set-file-times' and `file-newer-than-file-p'."
1323 (skip-unless (tramp--test-enabled))
1324 (skip-unless
1325 (not
1326 (memq
1327 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory)
1328 '(tramp-gvfs-file-name-handler tramp-smb-file-name-handler))))
1329
1330 (let ((tmp-name1 (tramp--test-make-temp-name))
1331 (tmp-name2 (tramp--test-make-temp-name))
1332 (tmp-name3 (tramp--test-make-temp-name)))
1333 (unwind-protect
1334 (progn
1335 (write-region "foo" nil tmp-name1)
1336 (should (file-exists-p tmp-name1))
1337 (should (consp (nth 5 (file-attributes tmp-name1))))
1338 ;; '(0 0) means don't know, and will be replaced by
1339 ;; `current-time'. Therefore, we use '(0 1).
1340 ;; We skip the test, if the remote handler is not able to
1341 ;; set the correct time.
1342 (skip-unless (set-file-times tmp-name1 '(0 1)))
1343 ;; Dumb remote shells without perl(1) or stat(1) are not
1344 ;; able to return the date correctly. They say "don't know".
1345 (unless (equal (nth 5 (file-attributes tmp-name1)) '(0 0))
1346 (should (equal (nth 5 (file-attributes tmp-name1)) '(0 1)))
1347 (write-region "bla" nil tmp-name2)
1348 (should (file-exists-p tmp-name2))
1349 (should (file-newer-than-file-p tmp-name2 tmp-name1))
1350 ;; `tmp-name3' does not exist.
1351 (should (file-newer-than-file-p tmp-name2 tmp-name3))
1352 (should-not (file-newer-than-file-p tmp-name3 tmp-name1))))
1353
1354 ;; Cleanup.
1355 (ignore-errors
1356 (delete-file tmp-name1)
1357 (delete-file tmp-name2)))))
1358
1359 (ert-deftest tramp-test23-visited-file-modtime ()
1360 "Check `set-visited-file-modtime' and `verify-visited-file-modtime'."
1361 (skip-unless (tramp--test-enabled))
1362
1363 (let ((tmp-name (tramp--test-make-temp-name)))
1364 (unwind-protect
1365 (progn
1366 (write-region "foo" nil tmp-name)
1367 (should (file-exists-p tmp-name))
1368 (with-temp-buffer
1369 (insert-file-contents tmp-name)
1370 (should (verify-visited-file-modtime))
1371 (set-visited-file-modtime '(0 1))
1372 (should (verify-visited-file-modtime))
1373 (should (equal (visited-file-modtime) '(0 1 0 0)))))
1374
1375 ;; Cleanup.
1376 (ignore-errors (delete-file tmp-name)))))
1377
1378 (ert-deftest tramp-test24-file-name-completion ()
1379 "Check `file-name-completion' and `file-name-all-completions'."
1380 (skip-unless (tramp--test-enabled))
1381
1382 (dolist (n-e '(nil t))
1383 (let ((non-essential n-e)
1384 (tmp-name (tramp--test-make-temp-name))
1385 (method (file-remote-p tramp-test-temporary-file-directory 'method))
1386 (host (file-remote-p tramp-test-temporary-file-directory 'host)))
1387
1388 (unwind-protect
1389 (progn
1390 ;; Method and host name in completion mode.
1391 (when (tramp-completion-mode-p)
1392 (unless (zerop (length method))
1393 (should
1394 (member
1395 (format "%s:" method)
1396 (file-name-all-completions (substring method 0 1) "/"))))
1397 (unless (zerop (length host))
1398 (should
1399 (member
1400 (format "%s:" host)
1401 (file-name-all-completions (substring host 0 1) "/"))))
1402 (unless (or (zerop (length method)) (zerop (length host)))
1403 (should
1404 (member
1405 (format "%s:" host)
1406 (file-name-all-completions
1407 (substring host 0 1) (format "/%s:" method))))))
1408
1409 ;; Local files.
1410 (make-directory tmp-name)
1411 (should (file-directory-p tmp-name))
1412 (write-region "foo" nil (expand-file-name "foo" tmp-name))
1413 (write-region "bar" nil (expand-file-name "bold" tmp-name))
1414 (make-directory (expand-file-name "boz" tmp-name))
1415 (should (equal (file-name-completion "fo" tmp-name) "foo"))
1416 (should (equal (file-name-completion "b" tmp-name) "bo"))
1417 (should
1418 (equal
1419 (file-name-completion "b" tmp-name 'file-directory-p) "boz/"))
1420 (should (equal (file-name-all-completions "fo" tmp-name) '("foo")))
1421 (should
1422 (equal
1423 (sort (file-name-all-completions "b" tmp-name) 'string-lessp)
1424 '("bold" "boz/"))))
1425
1426 ;; Cleanup.
1427 (ignore-errors (delete-directory tmp-name 'recursive))))))
1428
1429 (ert-deftest tramp-test25-load ()
1430 "Check `load'."
1431 (skip-unless (tramp--test-enabled))
1432
1433 (let ((tmp-name (tramp--test-make-temp-name)))
1434 (unwind-protect
1435 (progn
1436 (load tmp-name 'noerror 'nomessage)
1437 (should-not (featurep 'tramp-test-load))
1438 (write-region "(provide 'tramp-test-load)" nil tmp-name)
1439 ;; `load' in lread.c does not pass `must-suffix'. Why?
1440 ;(should-error (load tmp-name nil 'nomessage 'nosuffix 'must-suffix))
1441 (load tmp-name nil 'nomessage 'nosuffix)
1442 (should (featurep 'tramp-test-load)))
1443
1444 ;; Cleanup.
1445 (ignore-errors
1446 (and (featurep 'tramp-test-load) (unload-feature 'tramp-test-load))
1447 (delete-file tmp-name)))))
1448
1449 (ert-deftest tramp-test26-process-file ()
1450 "Check `process-file'."
1451 :tags '(:expensive-test)
1452 (skip-unless (tramp--test-enabled))
1453 (skip-unless
1454 (not
1455 (memq
1456 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory)
1457 '(tramp-gvfs-file-name-handler tramp-smb-file-name-handler))))
1458
1459 (let* ((tmp-name (tramp--test-make-temp-name))
1460 (fnnd (file-name-nondirectory tmp-name))
1461 (default-directory tramp-test-temporary-file-directory)
1462 kill-buffer-query-functions)
1463 (unwind-protect
1464 (progn
1465 ;; We cannot use "/bin/true" and "/bin/false"; those paths
1466 ;; do not exist on hydra.
1467 (should (zerop (process-file "true")))
1468 (should-not (zerop (process-file "false")))
1469 (should-not (zerop (process-file "binary-does-not-exist")))
1470 (with-temp-buffer
1471 (write-region "foo" nil tmp-name)
1472 (should (file-exists-p tmp-name))
1473 (should (zerop (process-file "ls" nil t nil fnnd)))
1474 ;; `ls' could produce colorized output.
1475 (goto-char (point-min))
1476 (while (re-search-forward tramp-color-escape-sequence-regexp nil t)
1477 (replace-match "" nil nil))
1478 (should (string-equal (format "%s\n" fnnd) (buffer-string)))
1479 (should-not (get-buffer-window (current-buffer) t))
1480
1481 ;; Second run. The output must be appended.
1482 (goto-char (point-max))
1483 (should (zerop (process-file "ls" nil t t fnnd)))
1484 ;; `ls' could produce colorized output.
1485 (goto-char (point-min))
1486 (while (re-search-forward tramp-color-escape-sequence-regexp nil t)
1487 (replace-match "" nil nil))
1488 (should
1489 (string-equal (format "%s\n%s\n" fnnd fnnd) (buffer-string)))
1490 ;; A non-nil DISPLAY must not raise the buffer.
1491 (should-not (get-buffer-window (current-buffer) t))))
1492
1493 ;; Cleanup.
1494 (ignore-errors (delete-file tmp-name)))))
1495
1496 (ert-deftest tramp-test27-start-file-process ()
1497 "Check `start-file-process'."
1498 :tags '(:expensive-test)
1499 (skip-unless (tramp--test-enabled))
1500 (skip-unless
1501 (not
1502 (memq
1503 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory)
1504 '(tramp-adb-file-name-handler
1505 tramp-gvfs-file-name-handler
1506 tramp-smb-file-name-handler))))
1507
1508 (let ((default-directory tramp-test-temporary-file-directory)
1509 (tmp-name (tramp--test-make-temp-name))
1510 kill-buffer-query-functions proc)
1511 (unwind-protect
1512 (with-temp-buffer
1513 (setq proc (start-file-process "test1" (current-buffer) "cat"))
1514 (should (processp proc))
1515 (should (equal (process-status proc) 'run))
1516 (process-send-string proc "foo")
1517 (process-send-eof proc)
1518 ;; Read output.
1519 (with-timeout (10 (ert-fail "`start-file-process' timed out"))
1520 (while (< (- (point-max) (point-min)) (length "foo"))
1521 (accept-process-output proc 1)))
1522 (should (string-equal (buffer-string) "foo")))
1523
1524 ;; Cleanup.
1525 (ignore-errors (delete-process proc)))
1526
1527 (unwind-protect
1528 (with-temp-buffer
1529 (write-region "foo" nil tmp-name)
1530 (should (file-exists-p tmp-name))
1531 (setq proc
1532 (start-file-process
1533 "test2" (current-buffer)
1534 "cat" (file-name-nondirectory tmp-name)))
1535 (should (processp proc))
1536 ;; Read output.
1537 (with-timeout (10 (ert-fail "`start-file-process' timed out"))
1538 (while (< (- (point-max) (point-min)) (length "foo"))
1539 (accept-process-output proc 1)))
1540 (should (string-equal (buffer-string) "foo")))
1541
1542 ;; Cleanup.
1543 (ignore-errors
1544 (delete-process proc)
1545 (delete-file tmp-name)))
1546
1547 (unwind-protect
1548 (with-temp-buffer
1549 (setq proc (start-file-process "test3" (current-buffer) "cat"))
1550 (should (processp proc))
1551 (should (equal (process-status proc) 'run))
1552 (set-process-filter
1553 proc
1554 (lambda (p s) (with-current-buffer (process-buffer p) (insert s))))
1555 (process-send-string proc "foo")
1556 (process-send-eof proc)
1557 ;; Read output.
1558 (with-timeout (10 (ert-fail "`start-file-process' timed out"))
1559 (while (< (- (point-max) (point-min)) (length "foo"))
1560 (accept-process-output proc 1)))
1561 (should (string-equal (buffer-string) "foo")))
1562
1563 ;; Cleanup.
1564 (ignore-errors (delete-process proc)))))
1565
1566 (ert-deftest tramp-test28-shell-command ()
1567 "Check `shell-command'."
1568 :tags '(:expensive-test)
1569 (skip-unless (tramp--test-enabled))
1570 (skip-unless
1571 (not
1572 (memq
1573 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory)
1574 '(tramp-adb-file-name-handler
1575 tramp-gvfs-file-name-handler
1576 tramp-smb-file-name-handler))))
1577
1578 (let ((tmp-name (tramp--test-make-temp-name))
1579 (default-directory tramp-test-temporary-file-directory)
1580 kill-buffer-query-functions)
1581 (unwind-protect
1582 (with-temp-buffer
1583 (write-region "foo" nil tmp-name)
1584 (should (file-exists-p tmp-name))
1585 (shell-command
1586 (format "ls %s" (file-name-nondirectory tmp-name)) (current-buffer))
1587 ;; `ls' could produce colorized output.
1588 (goto-char (point-min))
1589 (while (re-search-forward tramp-color-escape-sequence-regexp nil t)
1590 (replace-match "" nil nil))
1591 (should
1592 (string-equal
1593 (format "%s\n" (file-name-nondirectory tmp-name)) (buffer-string))))
1594
1595 ;; Cleanup.
1596 (ignore-errors (delete-file tmp-name)))
1597
1598 (unwind-protect
1599 (with-temp-buffer
1600 (write-region "foo" nil tmp-name)
1601 (should (file-exists-p tmp-name))
1602 (async-shell-command
1603 (format "ls %s" (file-name-nondirectory tmp-name)) (current-buffer))
1604 (set-process-sentinel (get-buffer-process (current-buffer)) nil)
1605 ;; Read output.
1606 (with-timeout (10 (ert-fail "`async-shell-command' timed out"))
1607 (while (< (- (point-max) (point-min))
1608 (1+ (length (file-name-nondirectory tmp-name))))
1609 (accept-process-output (get-buffer-process (current-buffer)) 1)))
1610 ;; `ls' could produce colorized output.
1611 (goto-char (point-min))
1612 (while (re-search-forward tramp-color-escape-sequence-regexp nil t)
1613 (replace-match "" nil nil))
1614 ;; There might be a nasty "Process *Async Shell* finished" message.
1615 (goto-char (point-min))
1616 (forward-line)
1617 (narrow-to-region (point-min) (point))
1618 (should
1619 (string-equal
1620 (format "%s\n" (file-name-nondirectory tmp-name)) (buffer-string))))
1621
1622 ;; Cleanup.
1623 (ignore-errors (delete-file tmp-name)))
1624
1625 (unwind-protect
1626 (with-temp-buffer
1627 (write-region "foo" nil tmp-name)
1628 (should (file-exists-p tmp-name))
1629 (async-shell-command "read line; ls $line" (current-buffer))
1630 (set-process-sentinel (get-buffer-process (current-buffer)) nil)
1631 (process-send-string
1632 (get-buffer-process (current-buffer))
1633 (format "%s\n" (file-name-nondirectory tmp-name)))
1634 ;; Read output.
1635 (with-timeout (10 (ert-fail "`async-shell-command' timed out"))
1636 (while (< (- (point-max) (point-min))
1637 (1+ (length (file-name-nondirectory tmp-name))))
1638 (accept-process-output (get-buffer-process (current-buffer)) 1)))
1639 ;; `ls' could produce colorized output.
1640 (goto-char (point-min))
1641 (while (re-search-forward tramp-color-escape-sequence-regexp nil t)
1642 (replace-match "" nil nil))
1643 ;; There might be a nasty "Process *Async Shell* finished" message.
1644 (goto-char (point-min))
1645 (forward-line)
1646 (narrow-to-region (point-min) (point))
1647 (should
1648 (string-equal
1649 (format "%s\n" (file-name-nondirectory tmp-name)) (buffer-string))))
1650
1651 ;; Cleanup.
1652 (ignore-errors (delete-file tmp-name)))))
1653
1654 (ert-deftest tramp-test29-vc-registered ()
1655 "Check `vc-registered'."
1656 :tags '(:expensive-test)
1657 (skip-unless (tramp--test-enabled))
1658 (skip-unless
1659 (eq
1660 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory)
1661 'tramp-sh-file-name-handler))
1662
1663 (let* ((default-directory tramp-test-temporary-file-directory)
1664 (tmp-name1 (tramp--test-make-temp-name))
1665 (tmp-name2 (expand-file-name "foo" tmp-name1))
1666 (tramp-remote-process-environment tramp-remote-process-environment)
1667 (vc-handled-backends
1668 (with-parsed-tramp-file-name tramp-test-temporary-file-directory nil
1669 (cond
1670 ((tramp-find-executable v vc-git-program (tramp-get-remote-path v))
1671 '(Git))
1672 ((tramp-find-executable v vc-hg-program (tramp-get-remote-path v))
1673 '(Hg))
1674 ((tramp-find-executable v vc-bzr-program (tramp-get-remote-path v))
1675 (setq tramp-remote-process-environment
1676 (cons (format "BZR_HOME=%s"
1677 (file-remote-p tmp-name1 'localname))
1678 tramp-remote-process-environment))
1679 ;; We must force a reconnect, in order to activate $BZR_HOME.
1680 (tramp-cleanup-connection
1681 (tramp-dissect-file-name tramp-test-temporary-file-directory)
1682 nil 'keep-password)
1683 '(Bzr))
1684 (t nil)))))
1685 (skip-unless vc-handled-backends)
1686 (message "%s" vc-handled-backends)
1687
1688 (unwind-protect
1689 (progn
1690 (make-directory tmp-name1)
1691 (write-region "foo" nil tmp-name2)
1692 (should (file-directory-p tmp-name1))
1693 (should (file-exists-p tmp-name2))
1694 (should-not (vc-registered tmp-name1))
1695 (should-not (vc-registered tmp-name2))
1696
1697 (let ((default-directory tmp-name1))
1698 ;; Create empty repository, and register the file.
1699 ;; Sometimes, creation of repository fails (bzr!); we skip
1700 ;; the test then.
1701 (condition-case nil
1702 (vc-create-repo (car vc-handled-backends))
1703 (error (skip-unless nil)))
1704 ;; The structure of VC-FILESET is not documented. Let's
1705 ;; hope it won't change.
1706 (condition-case nil
1707 (vc-register
1708 (list (car vc-handled-backends)
1709 (list (file-name-nondirectory tmp-name2))))
1710 ;; `vc-register' has changed its arguments in Emacs 25.1.
1711 (error
1712 (vc-register
1713 nil (list (car vc-handled-backends)
1714 (list (file-name-nondirectory tmp-name2))))))
1715 ;; vc-git uses an own process sentinel, Tramp's sentinel
1716 ;; for flushing the cache isn't used.
1717 (dired-uncache (concat (file-remote-p default-directory) "/"))
1718 (should (vc-registered (file-name-nondirectory tmp-name2)))))
1719
1720 ;; Cleanup.
1721 (ignore-errors (delete-directory tmp-name1 'recursive)))))
1722
1723 (ert-deftest tramp-test30-make-auto-save-file-name ()
1724 "Check `make-auto-save-file-name'."
1725 (skip-unless (tramp--test-enabled))
1726
1727 (let ((tmp-name1 (tramp--test-make-temp-name))
1728 (tmp-name2 (tramp--test-make-temp-name)))
1729
1730 (unwind-protect
1731 (progn
1732 ;; Use default `auto-save-file-name-transforms' mechanism.
1733 (let (tramp-auto-save-directory)
1734 (with-temp-buffer
1735 (setq buffer-file-name tmp-name1)
1736 (should
1737 (string-equal
1738 (make-auto-save-file-name)
1739 ;; This is taken from original `make-auto-save-file-name'.
1740 (expand-file-name
1741 (format
1742 "#%s#"
1743 (subst-char-in-string
1744 ?/ ?! (replace-regexp-in-string "!" "!!" tmp-name1)))
1745 temporary-file-directory)))))
1746
1747 ;; No mapping.
1748 (let (tramp-auto-save-directory auto-save-file-name-transforms)
1749 (with-temp-buffer
1750 (setq buffer-file-name tmp-name1)
1751 (should
1752 (string-equal
1753 (make-auto-save-file-name)
1754 (expand-file-name
1755 (format "#%s#" (file-name-nondirectory tmp-name1))
1756 tramp-test-temporary-file-directory)))))
1757
1758 ;; Use default `tramp-auto-save-directory' mechanism.
1759 (let ((tramp-auto-save-directory tmp-name2))
1760 (with-temp-buffer
1761 (setq buffer-file-name tmp-name1)
1762 (should
1763 (string-equal
1764 (make-auto-save-file-name)
1765 ;; This is taken from Tramp.
1766 (expand-file-name
1767 (format
1768 "#%s#"
1769 (tramp-subst-strs-in-string
1770 '(("_" . "|")
1771 ("/" . "_a")
1772 (":" . "_b")
1773 ("|" . "__")
1774 ("[" . "_l")
1775 ("]" . "_r"))
1776 tmp-name1))
1777 tmp-name2)))
1778 (should (file-directory-p tmp-name2))))
1779
1780 ;; Relative file names shall work, too.
1781 (let ((tramp-auto-save-directory "."))
1782 (with-temp-buffer
1783 (setq buffer-file-name tmp-name1
1784 default-directory tmp-name2)
1785 (should
1786 (string-equal
1787 (make-auto-save-file-name)
1788 ;; This is taken from Tramp.
1789 (expand-file-name
1790 (format
1791 "#%s#"
1792 (tramp-subst-strs-in-string
1793 '(("_" . "|")
1794 ("/" . "_a")
1795 (":" . "_b")
1796 ("|" . "__")
1797 ("[" . "_l")
1798 ("]" . "_r"))
1799 tmp-name1))
1800 tmp-name2)))
1801 (should (file-directory-p tmp-name2)))))
1802
1803 ;; Cleanup.
1804 (ignore-errors (delete-file tmp-name1))
1805 (ignore-errors (delete-directory tmp-name2 'recursive)))))
1806
1807 (defun tramp--test-adb-p ()
1808 "Check, whether the remote host runs Android.
1809 This requires restrictions of file name syntax."
1810 (tramp-adb-file-name-p tramp-test-temporary-file-directory))
1811
1812 (defun tramp--test-ftp-p ()
1813 "Check, whether an FTP-like method is used.
1814 This does not support globbing characters in file names (yet)."
1815 ;; Globbing characters are ??, ?* and ?\[.
1816 (and (eq (tramp-find-foreign-file-name-handler
1817 tramp-test-temporary-file-directory)
1818 'tramp-sh-file-name-handler)
1819 (string-match
1820 "ftp$" (file-remote-p tramp-test-temporary-file-directory 'method))))
1821
1822 (defun tramp--test-gvfs-p ()
1823 "Check, whether the remote host runs a GVFS based method.
1824 This requires restrictions of file name syntax."
1825 (tramp-gvfs-file-name-p tramp-test-temporary-file-directory))
1826
1827 (defun tramp--test-smb-or-windows-nt-p ()
1828 "Check, whether the locale or remote host runs MS Windows.
1829 This requires restrictions of file name syntax."
1830 (or (eq system-type 'windows-nt)
1831 (tramp-smb-file-name-p tramp-test-temporary-file-directory)))
1832
1833 (defun tramp--test-hpux-p ()
1834 "Check, whether the remote host runs HP-UX.
1835 Several special characters do not work properly there."
1836 ;; We must refill the cache. `file-truename' does it.
1837 (with-parsed-tramp-file-name
1838 (file-truename tramp-test-temporary-file-directory) nil
1839 (string-match "^HP-UX" (tramp-get-connection-property v "uname" ""))))
1840
1841 (defun tramp--test-check-files (&rest files)
1842 "Run a simple but comprehensive test over every file in FILES."
1843 ;; We must use `file-truename' for the temporary directory, because
1844 ;; it could be located on a symlinked directory. This would let the
1845 ;; test fail.
1846 (let* ((tramp-test-temporary-file-directory
1847 (file-truename tramp-test-temporary-file-directory))
1848 (tmp-name1 (tramp--test-make-temp-name))
1849 (tmp-name2 (tramp--test-make-temp-name 'local))
1850 (files (delq nil files)))
1851 (unwind-protect
1852 (progn
1853 (make-directory tmp-name1)
1854 (make-directory tmp-name2)
1855 (dolist (elt files)
1856 (let* ((file1 (expand-file-name elt tmp-name1))
1857 (file2 (expand-file-name elt tmp-name2))
1858 (file3 (expand-file-name (concat elt "foo") tmp-name1)))
1859 (write-region elt nil file1)
1860 (should (file-exists-p file1))
1861
1862 ;; Check file contents.
1863 (with-temp-buffer
1864 (insert-file-contents file1)
1865 (should (string-equal (buffer-string) elt)))
1866
1867 ;; Copy file both directions.
1868 (copy-file file1 tmp-name2)
1869 (should (file-exists-p file2))
1870 (delete-file file1)
1871 (should-not (file-exists-p file1))
1872 (copy-file file2 tmp-name1)
1873 (should (file-exists-p file1))
1874
1875 ;; Method "smb" supports `make-symbolic-link' only if the
1876 ;; remote host has CIFS capabilities. tramp-adb.el and
1877 ;; tramp-gvfs.el do not support symbolic links at all.
1878 (condition-case err
1879 (progn
1880 (make-symbolic-link file1 file3)
1881 (should (file-symlink-p file3))
1882 (should
1883 (string-equal
1884 (expand-file-name file1) (file-truename file3)))
1885 (should
1886 (string-equal
1887 (car (file-attributes file3))
1888 (file-remote-p (file-truename file1) 'localname)))
1889 ;; Check file contents.
1890 (with-temp-buffer
1891 (insert-file-contents file3)
1892 (should (string-equal (buffer-string) elt)))
1893 (delete-file file3))
1894 (file-error
1895 (should (string-equal (error-message-string err)
1896 "make-symbolic-link not supported"))))))
1897
1898 ;; Check file names.
1899 (should (equal (directory-files
1900 tmp-name1 nil directory-files-no-dot-files-regexp)
1901 (sort (copy-sequence files) 'string-lessp)))
1902 (should (equal (directory-files
1903 tmp-name2 nil directory-files-no-dot-files-regexp)
1904 (sort (copy-sequence files) 'string-lessp)))
1905
1906 ;; `substitute-in-file-name' could return different values.
1907 ;; For `adb', there could be strange file permissions
1908 ;; preventing overwriting a file. We don't care in this
1909 ;; testcase.
1910 (dolist (elt files)
1911 (let ((file1
1912 (substitute-in-file-name (expand-file-name elt tmp-name1)))
1913 (file2
1914 (substitute-in-file-name (expand-file-name elt tmp-name2))))
1915 (ignore-errors (write-region elt nil file1))
1916 (should (file-exists-p file1))
1917 (ignore-errors (write-region elt nil file2 nil 'nomessage))
1918 (should (file-exists-p file2))))
1919
1920 (should (equal (directory-files
1921 tmp-name1 nil directory-files-no-dot-files-regexp)
1922 (directory-files
1923 tmp-name2 nil directory-files-no-dot-files-regexp)))
1924
1925 ;; Check directory creation. We use a subdirectory "foo"
1926 ;; in order to avoid conflicts with previous file name tests.
1927 (dolist (elt files)
1928 (let* ((elt1 (concat elt "foo"))
1929 (file1 (expand-file-name (concat "foo/" elt) tmp-name1))
1930 (file2 (expand-file-name elt file1))
1931 (file3 (expand-file-name elt1 file1)))
1932 (make-directory file1 'parents)
1933 (should (file-directory-p file1))
1934 (write-region elt nil file2)
1935 (should (file-exists-p file2))
1936 (should
1937 (equal
1938 (directory-files file1 nil directory-files-no-dot-files-regexp)
1939 `(,elt)))
1940 (should
1941 (equal
1942 (caar (directory-files-and-attributes
1943 file1 nil directory-files-no-dot-files-regexp))
1944 elt))
1945
1946 ;; Check symlink in `directory-files-and-attributes'.
1947 (condition-case err
1948 (progn
1949 (make-symbolic-link file2 file3)
1950 (should (file-symlink-p file3))
1951 (should
1952 (string-equal
1953 (caar (directory-files-and-attributes
1954 file1 nil (regexp-quote elt1)))
1955 elt1))
1956 (should
1957 (string-equal
1958 (cadr (car (directory-files-and-attributes
1959 file1 nil (regexp-quote elt1))))
1960 (file-remote-p (file-truename file2) 'localname)))
1961 (delete-file file3)
1962 (should-not (file-exists-p file3)))
1963 (file-error
1964 (should (string-equal (error-message-string err)
1965 "make-symbolic-link not supported"))))
1966
1967 (delete-file file2)
1968 (should-not (file-exists-p file2))
1969 (delete-directory file1)
1970 (should-not (file-exists-p file1)))))
1971
1972 ;; Cleanup.
1973 (ignore-errors (delete-directory tmp-name1 'recursive))
1974 (ignore-errors (delete-directory tmp-name2 'recursive)))))
1975
1976 (defun tramp--test-special-characters ()
1977 "Perform the test in `tramp-test31-special-characters*'."
1978 ;; Newlines, slashes and backslashes in file names are not
1979 ;; supported. So we don't test. And we don't test the tab
1980 ;; character on Windows or Cygwin, because the backslash is
1981 ;; interpreted as a path separator, preventing "\t" from being
1982 ;; expanded to <TAB>.
1983 (tramp--test-check-files
1984 (if (or (tramp--test-gvfs-p) (tramp--test-smb-or-windows-nt-p))
1985 "foo bar baz"
1986 (if (or (tramp--test-adb-p) (eq system-type 'cygwin))
1987 " foo bar baz "
1988 " foo\tbar baz\t"))
1989 "$foo$bar$$baz$"
1990 "-foo-bar-baz-"
1991 "%foo%bar%baz%"
1992 "&foo&bar&baz&"
1993 (unless (or (tramp--test-ftp-p)
1994 (tramp--test-gvfs-p)
1995 (tramp--test-smb-or-windows-nt-p))
1996 "?foo?bar?baz?")
1997 (unless (or (tramp--test-ftp-p)
1998 (tramp--test-gvfs-p)
1999 (tramp--test-smb-or-windows-nt-p))
2000 "*foo*bar*baz*")
2001 (if (or (tramp--test-gvfs-p) (tramp--test-smb-or-windows-nt-p))
2002 "'foo'bar'baz'"
2003 "'foo\"bar'baz\"")
2004 "#foo~bar#baz~"
2005 (if (or (tramp--test-gvfs-p) (tramp--test-smb-or-windows-nt-p))
2006 "!foo!bar!baz!"
2007 "!foo|bar!baz|")
2008 (if (or (tramp--test-gvfs-p) (tramp--test-smb-or-windows-nt-p))
2009 ";foo;bar;baz;"
2010 ":foo;bar:baz;")
2011 (unless (or (tramp--test-gvfs-p) (tramp--test-smb-or-windows-nt-p))
2012 "<foo>bar<baz>")
2013 "(foo)bar(baz)"
2014 (unless (or (tramp--test-ftp-p) (tramp--test-gvfs-p)) "[foo]bar[baz]")
2015 "{foo}bar{baz}"))
2016
2017 ;; These tests are inspired by Bug#17238.
2018 (ert-deftest tramp-test31-special-characters ()
2019 "Check special characters in file names."
2020 (skip-unless (tramp--test-enabled))
2021
2022 (tramp--test-special-characters))
2023
2024 (ert-deftest tramp-test31-special-characters-with-stat ()
2025 "Check special characters in file names.
2026 Use the `stat' command."
2027 :tags '(:expensive-test)
2028 (skip-unless (tramp--test-enabled))
2029 (skip-unless
2030 (eq
2031 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory)
2032 'tramp-sh-file-name-handler))
2033 (with-parsed-tramp-file-name tramp-test-temporary-file-directory nil
2034 (skip-unless (tramp-get-remote-stat v)))
2035
2036 (let ((tramp-connection-properties
2037 (append
2038 `((,(regexp-quote (file-remote-p tramp-test-temporary-file-directory))
2039 "perl" nil))
2040 tramp-connection-properties)))
2041 (tramp--test-special-characters)))
2042
2043 (ert-deftest tramp-test31-special-characters-with-perl ()
2044 "Check special characters in file names.
2045 Use the `perl' command."
2046 :tags '(:expensive-test)
2047 (skip-unless (tramp--test-enabled))
2048 (skip-unless
2049 (eq
2050 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory)
2051 'tramp-sh-file-name-handler))
2052 (with-parsed-tramp-file-name tramp-test-temporary-file-directory nil
2053 (skip-unless (tramp-get-remote-perl v)))
2054
2055 (let ((tramp-connection-properties
2056 (append
2057 `((,(regexp-quote (file-remote-p tramp-test-temporary-file-directory))
2058 "stat" nil)
2059 ;; See `tramp-sh-handle-file-truename'.
2060 (,(regexp-quote (file-remote-p tramp-test-temporary-file-directory))
2061 "readlink" nil))
2062 tramp-connection-properties)))
2063 (tramp--test-special-characters)))
2064
2065 (ert-deftest tramp-test31-special-characters-with-ls ()
2066 "Check special characters in file names.
2067 Use the `ls' command."
2068 :tags '(:expensive-test)
2069 (skip-unless (tramp--test-enabled))
2070 (skip-unless
2071 (eq
2072 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory)
2073 'tramp-sh-file-name-handler))
2074
2075 (let ((tramp-connection-properties
2076 (append
2077 `((,(regexp-quote (file-remote-p tramp-test-temporary-file-directory))
2078 "perl" nil)
2079 (,(regexp-quote (file-remote-p tramp-test-temporary-file-directory))
2080 "stat" nil)
2081 ;; See `tramp-sh-handle-file-truename'.
2082 (,(regexp-quote (file-remote-p tramp-test-temporary-file-directory))
2083 "readlink" nil))
2084 tramp-connection-properties)))
2085 (tramp--test-special-characters)))
2086
2087 (defun tramp--test-utf8 ()
2088 "Perform the test in `tramp-test32-utf8*'."
2089 (let* ((utf8 (if (and (eq system-type 'darwin)
2090 (memq 'utf-8-hfs (coding-system-list)))
2091 'utf-8-hfs 'utf-8))
2092 (coding-system-for-read utf8)
2093 (coding-system-for-write utf8)
2094 (file-name-coding-system utf8))
2095 (tramp--test-check-files
2096 (unless (tramp--test-hpux-p) "Γυρίστε το Γαλαξία με Ώτο Στοπ")
2097 (unless (tramp--test-hpux-p)
2098 "أصبح بوسعك الآن تنزيل نسخة كاملة من موسوعة ويكيبيديا العربية لتصفحها بلا اتصال بالإنترنت")
2099 "银河系漫游指南系列"
2100 "Автостопом по гала́ктике")))
2101
2102 (ert-deftest tramp-test32-utf8 ()
2103 "Check UTF8 encoding in file names and file contents."
2104 (skip-unless (tramp--test-enabled))
2105
2106 (tramp--test-utf8))
2107
2108 (ert-deftest tramp-test32-utf8-with-stat ()
2109 "Check UTF8 encoding in file names and file contents.
2110 Use the `stat' command."
2111 :tags '(:expensive-test)
2112 (skip-unless (tramp--test-enabled))
2113 (skip-unless
2114 (eq
2115 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory)
2116 'tramp-sh-file-name-handler))
2117 (with-parsed-tramp-file-name tramp-test-temporary-file-directory nil
2118 (skip-unless (tramp-get-remote-stat v)))
2119
2120 (let ((tramp-connection-properties
2121 (append
2122 `((,(regexp-quote (file-remote-p tramp-test-temporary-file-directory))
2123 "perl" nil))
2124 tramp-connection-properties)))
2125 (tramp--test-utf8)))
2126
2127 (ert-deftest tramp-test32-utf8-with-perl ()
2128 "Check UTF8 encoding in file names and file contents.
2129 Use the `perl' command."
2130 :tags '(:expensive-test)
2131 (skip-unless (tramp--test-enabled))
2132 (skip-unless
2133 (eq
2134 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory)
2135 'tramp-sh-file-name-handler))
2136 (with-parsed-tramp-file-name tramp-test-temporary-file-directory nil
2137 (skip-unless (tramp-get-remote-perl v)))
2138
2139 (let ((tramp-connection-properties
2140 (append
2141 `((,(regexp-quote (file-remote-p tramp-test-temporary-file-directory))
2142 "stat" nil)
2143 ;; See `tramp-sh-handle-file-truename'.
2144 (,(regexp-quote (file-remote-p tramp-test-temporary-file-directory))
2145 "readlink" nil))
2146 tramp-connection-properties)))
2147 (tramp--test-utf8)))
2148
2149 (ert-deftest tramp-test32-utf8-with-ls ()
2150 "Check UTF8 encoding in file names and file contents.
2151 Use the `ls' command."
2152 :tags '(:expensive-test)
2153 (skip-unless (tramp--test-enabled))
2154 (skip-unless
2155 (eq
2156 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory)
2157 'tramp-sh-file-name-handler))
2158
2159 (let ((tramp-connection-properties
2160 (append
2161 `((,(regexp-quote (file-remote-p tramp-test-temporary-file-directory))
2162 "perl" nil)
2163 (,(regexp-quote (file-remote-p tramp-test-temporary-file-directory))
2164 "stat" nil)
2165 ;; See `tramp-sh-handle-file-truename'.
2166 (,(regexp-quote (file-remote-p tramp-test-temporary-file-directory))
2167 "readlink" nil))
2168 tramp-connection-properties)))
2169 (tramp--test-utf8)))
2170
2171 ;; This test is inspired by Bug#16928.
2172 (ert-deftest tramp-test33-asynchronous-requests ()
2173 "Check parallel asynchronous requests.
2174 Such requests could arrive from timers, process filters and
2175 process sentinels. They shall not disturb each other."
2176 ;; Mark as failed until bug has been fixed.
2177 :expected-result :failed
2178 :tags '(:expensive-test)
2179 (skip-unless (tramp--test-enabled))
2180 (skip-unless
2181 (eq
2182 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory)
2183 'tramp-sh-file-name-handler))
2184
2185 ;; Keep instrumentation verbosity 0 until Tramp bug is fixed. This
2186 ;; has the side effect, that this test fails instead to abort. Good
2187 ;; for hydra.
2188 (tramp--instrument-test-case 0
2189 (let* ((tmp-name (tramp--test-make-temp-name))
2190 (default-directory tmp-name)
2191 (remote-file-name-inhibit-cache t)
2192 timer buffers kill-buffer-query-functions)
2193
2194 (unwind-protect
2195 (progn
2196 (make-directory tmp-name)
2197
2198 ;; Setup a timer in order to raise an ordinary command again
2199 ;; and again. `vc-registered' is well suited, because there
2200 ;; are many checks.
2201 (setq
2202 timer
2203 (run-at-time
2204 0 1
2205 (lambda ()
2206 (when buffers
2207 (vc-registered
2208 (buffer-name (nth (random (length buffers)) buffers)))))))
2209
2210 ;; Create temporary buffers. The number of buffers
2211 ;; corresponds to the number of processes; it could be
2212 ;; increased in order to make pressure on Tramp.
2213 (dotimes (i 5)
2214 (add-to-list 'buffers (generate-new-buffer "*temp*")))
2215
2216 ;; Open asynchronous processes. Set process sentinel.
2217 (dolist (buf buffers)
2218 (async-shell-command "read line; touch $line; echo $line" buf)
2219 (set-process-sentinel
2220 (get-buffer-process buf)
2221 (lambda (proc _state)
2222 (delete-file (buffer-name (process-buffer proc))))))
2223
2224 ;; Send a string. Use a random order of the buffers. Mix
2225 ;; with regular operation.
2226 (let ((buffers (copy-sequence buffers))
2227 buf)
2228 (while buffers
2229 (setq buf (nth (random (length buffers)) buffers))
2230 (process-send-string
2231 (get-buffer-process buf) (format "'%s'\n" buf))
2232 (file-attributes (buffer-name buf))
2233 (setq buffers (delq buf buffers))))
2234
2235 ;; Wait until the whole output has been read.
2236 (with-timeout ((* 10 (length buffers))
2237 (ert-fail "`async-shell-command' timed out"))
2238 (let ((buffers (copy-sequence buffers))
2239 buf)
2240 (while buffers
2241 (setq buf (nth (random (length buffers)) buffers))
2242 (if (ignore-errors
2243 (memq (process-status (get-buffer-process buf))
2244 '(run open)))
2245 (accept-process-output (get-buffer-process buf) 0.1)
2246 (setq buffers (delq buf buffers))))))
2247
2248 ;; Check.
2249 (dolist (buf buffers)
2250 (with-current-buffer buf
2251 (should
2252 (string-equal (format "'%s'\n" buf) (buffer-string)))))
2253 (should-not
2254 (directory-files tmp-name nil directory-files-no-dot-files-regexp)))
2255
2256 ;; Cleanup.
2257 (ignore-errors (cancel-timer timer))
2258 (ignore-errors (delete-directory tmp-name 'recursive))
2259 (dolist (buf buffers)
2260 (ignore-errors (kill-buffer buf)))))))
2261
2262 (ert-deftest tramp-test34-recursive-load ()
2263 "Check that Tramp does not fail due to recursive load."
2264 (skip-unless (tramp--test-enabled))
2265
2266 (dolist (code
2267 (list
2268 (format
2269 "(expand-file-name %S)"
2270 tramp-test-temporary-file-directory)
2271 (format
2272 "(let ((default-directory %S)) (expand-file-name %S))"
2273 tramp-test-temporary-file-directory
2274 temporary-file-directory)))
2275 (should-not
2276 (string-match
2277 "Recursive load"
2278 (shell-command-to-string
2279 (format
2280 "%s -batch -Q -L %s --eval %s"
2281 (expand-file-name invocation-name invocation-directory)
2282 (mapconcat 'shell-quote-argument load-path " -L ")
2283 (shell-quote-argument code)))))))
2284
2285 (ert-deftest tramp-test35-unload ()
2286 "Check that Tramp and its subpackages unload completely.
2287 Since it unloads Tramp, it shall be the last test to run."
2288 ;; Mark as failed until all symbols are unbound.
2289 :expected-result (if (featurep 'tramp) :failed :passed)
2290 :tags '(:expensive-test)
2291 (when (featurep 'tramp)
2292 (unload-feature 'tramp 'force)
2293 ;; No Tramp feature must be left.
2294 (should-not (featurep 'tramp))
2295 (should-not (all-completions "tramp" (delq 'tramp-tests features)))
2296 ;; `file-name-handler-alist' must be clean.
2297 (should-not (all-completions "tramp" (mapcar 'cdr file-name-handler-alist)))
2298 ;; There shouldn't be left a bound symbol. We do not regard our
2299 ;; test symbols, and the Tramp unload hooks.
2300 (mapatoms
2301 (lambda (x)
2302 (and (or (boundp x) (functionp x))
2303 (string-match "^tramp" (symbol-name x))
2304 (not (string-match "^tramp--?test" (symbol-name x)))
2305 (not (string-match "unload-hook$" (symbol-name x)))
2306 (ert-fail (format "`%s' still bound" x)))))
2307 ;; There shouldn't be left a hook function containing a Tramp
2308 ;; function. We do not regard the Tramp unload hooks.
2309 (mapatoms
2310 (lambda (x)
2311 (and (boundp x)
2312 (string-match "-hooks?$" (symbol-name x))
2313 (not (string-match "unload-hook$" (symbol-name x)))
2314 (consp (symbol-value x))
2315 (ignore-errors (all-completions "tramp" (symbol-value x)))
2316 (ert-fail (format "Hook `%s' still contains Tramp function" x)))))))
2317
2318 ;; TODO:
2319
2320 ;; * dired-compress-file
2321 ;; * dired-uncache
2322 ;; * file-acl
2323 ;; * file-ownership-preserved-p
2324 ;; * file-selinux-context
2325 ;; * find-backup-file-name
2326 ;; * set-file-acl
2327 ;; * set-file-selinux-context
2328
2329 ;; * Work on skipped tests. Make a comment, when it is impossible.
2330 ;; * Fix `tramp-test15-copy-directory' for `smb'. Using tar in a pipe
2331 ;; doesn't work well when an interactive password must be provided.
2332 ;; * Fix `tramp-test27-start-file-process' on MS Windows (`process-send-eof'?).
2333 ;; * Fix Bug#16928. Set expected error of `tramp-test33-asynchronous-requests'.
2334 ;; * Fix `tramp-test35-unload' (Not all symbols are unbound). Set
2335 ;; expected error.
2336
2337 (defun tramp-test-all (&optional interactive)
2338 "Run all tests for \\[tramp]."
2339 (interactive "p")
2340 (funcall
2341 (if interactive 'ert-run-tests-interactively 'ert-run-tests-batch) "^tramp"))
2342
2343 (provide 'tramp-tests)
2344 ;;; tramp-tests.el ends here