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