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