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