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