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