* lisp/emacs-lisp/cl-macs.el (cl-defstruct): Fix debug spec (Bug#24430).
[emacs.git] / test / lisp / net / tramp-tests.el
blob5d9d3a039b6f42028cc16453aa27bcfbc44f24ed
1 ;;; tramp-tests.el --- Tests of remote file access
3 ;; Copyright (C) 2013-2016 Free Software Foundation, Inc.
5 ;; Author: Michael Albinus <michael.albinus@gmx.de>
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.
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.
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/'.
20 ;;; Commentary:
22 ;; The tests require a recent ert.el from Emacs 24.4.
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.
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.
36 ;; A whole test run can be performed calling the command `tramp-test-all'.
38 ;;; Code:
40 (require 'ert)
41 (require 'tramp)
42 (require 'vc)
43 (require 'vc-bzr)
44 (require 'vc-git)
45 (require 'vc-hg)
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)
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.")
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)
78 ;; This shall happen on hydra only.
79 (when (getenv "NIX_STORE")
80 (add-to-list 'tramp-remote-path 'tramp-own-remote-path))
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.")
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))))))
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)))
106 ;; Return result.
107 (cdr tramp--test-enabled-checked))
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)))
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))))))))
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)))))
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:"))
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:"))
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:"))
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]:"))
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"))
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:"))
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"))))
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))
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))
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))
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))
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))
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))
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))
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))
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))
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))
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))
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))
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))
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))
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))
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))
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))
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)))
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))
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))
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))
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))
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"))
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")))
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")))))
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)))
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"))))
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")))
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"))
641 ;; Bug#10085.
642 (when (tramp--test-enabled) ;; Packages like tramp-gvfs.el might be disabled.
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) ""))))))))
661 (ert-deftest tramp-test07-file-exists-p ()
662 "Check `file-exist-p', `write-region' and `delete-file'."
663 (skip-unless (tramp--test-enabled))
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))))
672 (ert-deftest tramp-test08-file-local-copy ()
673 "Check `file-local-copy'."
674 (skip-unless (tramp--test-enabled))
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)))))
692 ;; Cleanup.
693 (ignore-errors
694 (delete-file tmp-name1)
695 (delete-file tmp-name2)))))
697 (ert-deftest tramp-test09-insert-file-contents ()
698 "Check `insert-file-contents'."
699 (skip-unless (tramp--test-enabled))
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"))))
717 ;; Cleanup.
718 (ignore-errors (delete-file tmp-name)))))
720 (ert-deftest tramp-test10-write-region ()
721 "Check `write-region'."
722 (skip-unless (tramp--test-enabled))
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"))))
753 ;; Cleanup.
754 (ignore-errors (delete-file tmp-name)))))
756 (ert-deftest tramp-test11-copy-file ()
757 "Check `copy-file'."
758 (skip-unless (tramp--test-enabled))
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)))
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))))
783 ;; Cleanup.
784 (ignore-errors (delete-file tmp-name1))
785 (ignore-errors (delete-file tmp-name2))
786 (ignore-errors (delete-directory tmp-name3 'recursive)))
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))))
805 ;; Cleanup.
806 (ignore-errors (delete-file tmp-name1))
807 (ignore-errors (delete-file tmp-name4))
808 (ignore-errors (delete-directory tmp-name5 'recursive)))
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))))
827 ;; Cleanup.
828 (ignore-errors (delete-file tmp-name1))
829 (ignore-errors (delete-file tmp-name4))
830 (ignore-errors (delete-directory tmp-name3 'recursive)))))
832 (ert-deftest tramp-test12-rename-file ()
833 "Check `rename-file'."
834 (skip-unless (tramp--test-enabled))
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)))
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))))
864 ;; Cleanup.
865 (ignore-errors (delete-file tmp-name1))
866 (ignore-errors (delete-file tmp-name2))
867 (ignore-errors (delete-directory tmp-name3 'recursive)))
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))))
891 ;; Cleanup.
892 (ignore-errors (delete-file tmp-name1))
893 (ignore-errors (delete-file tmp-name4))
894 (ignore-errors (delete-directory tmp-name5 'recursive)))
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))))
918 ;; Cleanup.
919 (ignore-errors (delete-file tmp-name1))
920 (ignore-errors (delete-file tmp-name4))
921 (ignore-errors (delete-directory tmp-name3 'recursive)))))
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))
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))
936 (make-directory tmp-name2 'parents)
937 (should (file-directory-p tmp-name2))
938 (should (file-accessible-directory-p tmp-name2)))
940 ;; Cleanup.
941 (ignore-errors (delete-directory tmp-name1 'recursive)))))
943 (ert-deftest tramp-test14-delete-directory ()
944 "Check `delete-directory'."
945 (skip-unless (tramp--test-enabled))
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 (should (file-directory-p tmp-name))
956 (write-region "foo" nil (expand-file-name "bla" tmp-name))
957 (should (file-exists-p (expand-file-name "bla" tmp-name)))
958 (should-error (delete-directory tmp-name))
959 (delete-directory tmp-name 'recursive)
960 (should-not (file-directory-p tmp-name))))
962 (ert-deftest tramp-test15-copy-directory ()
963 "Check `copy-directory'."
964 (skip-unless (tramp--test-enabled))
966 (let* ((tmp-name1 (tramp--test-make-temp-name))
967 (tmp-name2 (tramp--test-make-temp-name))
968 (tmp-name3 (expand-file-name
969 (file-name-nondirectory tmp-name1) tmp-name2))
970 (tmp-name4 (expand-file-name "foo" tmp-name1))
971 (tmp-name5 (expand-file-name "foo" tmp-name2))
972 (tmp-name6 (expand-file-name "foo" tmp-name3)))
974 ;; Copy complete directory.
975 (unwind-protect
976 (progn
977 ;; Copy empty directory.
978 (make-directory tmp-name1)
979 (write-region "foo" nil tmp-name4)
980 (should (file-directory-p tmp-name1))
981 (should (file-exists-p tmp-name4))
982 (copy-directory tmp-name1 tmp-name2)
983 (should (file-directory-p tmp-name2))
984 (should (file-exists-p tmp-name5))
985 ;; Target directory does exist already.
986 (copy-directory tmp-name1 tmp-name2)
987 (should (file-directory-p tmp-name3))
988 (should (file-exists-p tmp-name6)))
990 ;; Cleanup.
991 (ignore-errors
992 (delete-directory tmp-name1 'recursive)
993 (delete-directory tmp-name2 'recursive)))
995 ;; Copy directory contents.
996 (unwind-protect
997 (progn
998 ;; Copy empty directory.
999 (make-directory tmp-name1)
1000 (write-region "foo" nil tmp-name4)
1001 (should (file-directory-p tmp-name1))
1002 (should (file-exists-p tmp-name4))
1003 (copy-directory tmp-name1 tmp-name2 nil 'parents 'contents)
1004 (should (file-directory-p tmp-name2))
1005 (should (file-exists-p tmp-name5))
1006 ;; Target directory does exist already.
1007 (delete-file tmp-name5)
1008 (should-not (file-exists-p tmp-name5))
1009 (copy-directory tmp-name1 tmp-name2 nil 'parents 'contents)
1010 (should (file-directory-p tmp-name2))
1011 (should (file-exists-p tmp-name5))
1012 (should-not (file-directory-p tmp-name3))
1013 (should-not (file-exists-p tmp-name6)))
1015 ;; Cleanup.
1016 (ignore-errors
1017 (delete-directory tmp-name1 'recursive)
1018 (delete-directory tmp-name2 'recursive)))))
1020 (ert-deftest tramp-test16-directory-files ()
1021 "Check `directory-files'."
1022 (skip-unless (tramp--test-enabled))
1024 (let* ((tmp-name1 (tramp--test-make-temp-name))
1025 (tmp-name2 (expand-file-name "bla" tmp-name1))
1026 (tmp-name3 (expand-file-name "foo" tmp-name1)))
1027 (unwind-protect
1028 (progn
1029 (make-directory tmp-name1)
1030 (write-region "foo" nil tmp-name2)
1031 (write-region "bla" nil tmp-name3)
1032 (should (file-directory-p tmp-name1))
1033 (should (file-exists-p tmp-name2))
1034 (should (file-exists-p tmp-name3))
1035 (should (equal (directory-files tmp-name1) '("." ".." "bla" "foo")))
1036 (should (equal (directory-files tmp-name1 'full)
1037 `(,(concat tmp-name1 "/.")
1038 ,(concat tmp-name1 "/..")
1039 ,tmp-name2 ,tmp-name3)))
1040 (should (equal (directory-files
1041 tmp-name1 nil directory-files-no-dot-files-regexp)
1042 '("bla" "foo")))
1043 (should (equal (directory-files
1044 tmp-name1 'full directory-files-no-dot-files-regexp)
1045 `(,tmp-name2 ,tmp-name3))))
1047 ;; Cleanup.
1048 (ignore-errors (delete-directory tmp-name1 'recursive)))))
1050 (ert-deftest tramp-test17-insert-directory ()
1051 "Check `insert-directory'."
1052 (skip-unless (tramp--test-enabled))
1054 (let* ((tmp-name1 (tramp--test-make-temp-name))
1055 (tmp-name2 (expand-file-name "foo" tmp-name1))
1056 ;; We test for the summary line. Keyword "total" could be localized.
1057 (process-environment
1058 (append '("LANG=C" "LANGUAGE=C" "LC_ALL=C") process-environment)))
1059 (unwind-protect
1060 (progn
1061 (make-directory tmp-name1)
1062 (write-region "foo" nil tmp-name2)
1063 (should (file-directory-p tmp-name1))
1064 (should (file-exists-p tmp-name2))
1065 (with-temp-buffer
1066 (insert-directory tmp-name1 nil)
1067 (goto-char (point-min))
1068 (should (looking-at-p (regexp-quote tmp-name1))))
1069 (with-temp-buffer
1070 (insert-directory tmp-name1 "-al")
1071 (goto-char (point-min))
1072 (should (looking-at-p (format "^.+ %s$" (regexp-quote tmp-name1)))))
1073 (with-temp-buffer
1074 (insert-directory (file-name-as-directory tmp-name1) "-al")
1075 (goto-char (point-min))
1076 (should
1077 (looking-at-p (format "^.+ %s/$" (regexp-quote tmp-name1)))))
1078 (with-temp-buffer
1079 (insert-directory
1080 (file-name-as-directory tmp-name1) "-al" nil 'full-directory-p)
1081 (goto-char (point-min))
1082 (should
1083 (looking-at-p
1084 (concat
1085 ;; There might be a summary line.
1086 "\\(total.+[[:digit:]]+\n\\)?"
1087 ;; We don't know in which order ".", ".." and "foo" appear.
1088 "\\(.+ \\(\\.?\\.\\|foo\\)\n\\)\\{3\\}")))))
1090 ;; Cleanup.
1091 (ignore-errors (delete-directory tmp-name1 'recursive)))))
1093 (ert-deftest tramp-test18-file-attributes ()
1094 "Check `file-attributes'.
1095 This tests also `file-readable-p', `file-regular-p' and
1096 `file-ownership-preserved-p'."
1097 (skip-unless (tramp--test-enabled))
1099 ;; We must use `file-truename' for the temporary directory, because
1100 ;; it could be located on a symlinked directory. This would let the
1101 ;; test fail.
1102 (let* ((tramp-test-temporary-file-directory
1103 (file-truename tramp-test-temporary-file-directory))
1104 (tmp-name1 (tramp--test-make-temp-name))
1105 (tmp-name2 (tramp--test-make-temp-name))
1106 ;; File name with "//".
1107 (tmp-name3
1108 (format
1109 "%s%s"
1110 (file-remote-p tmp-name1)
1111 (replace-regexp-in-string
1112 "/" "//" (file-remote-p tmp-name1 'localname))))
1113 attr)
1114 (unwind-protect
1115 (progn
1116 ;; `file-ownership-preserved-p' should return t for
1117 ;; non-existing files. It is implemented only in tramp-sh.el.
1118 (when (tramp--test-sh-p)
1119 (should (file-ownership-preserved-p tmp-name1 'group)))
1120 (write-region "foo" nil tmp-name1)
1121 (should (file-exists-p tmp-name1))
1122 (should (file-readable-p tmp-name1))
1123 (should (file-regular-p tmp-name1))
1124 (when (tramp--test-sh-p)
1125 (should (file-ownership-preserved-p tmp-name1 'group)))
1127 ;; We do not test inodes and device numbers.
1128 (setq attr (file-attributes tmp-name1))
1129 (should (consp attr))
1130 (should (null (car attr)))
1131 (should (numberp (nth 1 attr))) ;; Link.
1132 (should (numberp (nth 2 attr))) ;; Uid.
1133 (should (numberp (nth 3 attr))) ;; Gid.
1134 ;; Last access time.
1135 (should (stringp (current-time-string (nth 4 attr))))
1136 ;; Last modification time.
1137 (should (stringp (current-time-string (nth 5 attr))))
1138 ;; Last status change time.
1139 (should (stringp (current-time-string (nth 6 attr))))
1140 (should (numberp (nth 7 attr))) ;; Size.
1141 (should (stringp (nth 8 attr))) ;; Modes.
1143 (setq attr (file-attributes tmp-name1 'string))
1144 (should (stringp (nth 2 attr))) ;; Uid.
1145 (should (stringp (nth 3 attr))) ;; Gid.
1147 (condition-case err
1148 (progn
1149 (when (tramp--test-sh-p)
1150 (should (file-ownership-preserved-p tmp-name2 'group)))
1151 (make-symbolic-link tmp-name1 tmp-name2)
1152 (should (file-exists-p tmp-name2))
1153 (should (file-symlink-p tmp-name2))
1154 (when (tramp--test-sh-p)
1155 (should (file-ownership-preserved-p tmp-name2 'group)))
1156 (setq attr (file-attributes tmp-name2))
1157 (should (string-equal
1158 (car attr)
1159 (file-remote-p (file-truename tmp-name1) 'localname)))
1160 (delete-file tmp-name2))
1161 (file-error
1162 (should (string-equal (error-message-string err)
1163 "make-symbolic-link not supported"))))
1165 ;; Check, that "//" in symlinks are handled properly.
1166 (with-temp-buffer
1167 (let ((default-directory tramp-test-temporary-file-directory))
1168 (shell-command
1169 (format
1170 "ln -s %s %s"
1171 (tramp-file-name-localname (tramp-dissect-file-name tmp-name3))
1172 (tramp-file-name-localname (tramp-dissect-file-name tmp-name2)))
1173 t)))
1174 (when (file-symlink-p tmp-name2)
1175 (setq attr (file-attributes tmp-name2))
1176 (should
1177 (string-equal
1178 (car attr)
1179 (tramp-file-name-localname (tramp-dissect-file-name tmp-name3))))
1180 (delete-file tmp-name2))
1182 (when (tramp--test-sh-p)
1183 (should (file-ownership-preserved-p tmp-name1 'group)))
1184 (delete-file tmp-name1)
1185 (make-directory tmp-name1)
1186 (should (file-exists-p tmp-name1))
1187 (should (file-readable-p tmp-name1))
1188 (should-not (file-regular-p tmp-name1))
1189 (when (tramp--test-sh-p)
1190 (should (file-ownership-preserved-p tmp-name1 'group)))
1191 (setq attr (file-attributes tmp-name1))
1192 (should (eq (car attr) t)))
1194 ;; Cleanup.
1195 (ignore-errors (delete-directory tmp-name1))
1196 (ignore-errors (delete-file tmp-name1))
1197 (ignore-errors (delete-file tmp-name2)))))
1199 (ert-deftest tramp-test19-directory-files-and-attributes ()
1200 "Check `directory-files-and-attributes'."
1201 (skip-unless (tramp--test-enabled))
1203 ;; `directory-files-and-attributes' contains also values for "../".
1204 ;; Ensure that this doesn't change during tests, for
1205 ;; example due to handling temporary files.
1206 (let* ((tmp-name1 (tramp--test-make-temp-name))
1207 (tmp-name2 (expand-file-name "bla" tmp-name1))
1208 attr)
1209 (unwind-protect
1210 (progn
1211 (make-directory tmp-name1)
1212 (should (file-directory-p tmp-name1))
1213 (make-directory tmp-name2)
1214 (should (file-directory-p tmp-name2))
1215 (write-region "foo" nil (expand-file-name "foo" tmp-name2))
1216 (write-region "bar" nil (expand-file-name "bar" tmp-name2))
1217 (write-region "boz" nil (expand-file-name "boz" tmp-name2))
1218 (setq attr (directory-files-and-attributes tmp-name2))
1219 (should (consp attr))
1220 ;; Dumb remote shells without perl(1) or stat(1) are not
1221 ;; able to return the date correctly. They say "don't know".
1222 (dolist (elt attr)
1223 (unless
1224 (equal
1225 (nth 5
1226 (file-attributes (expand-file-name (car elt) tmp-name2)))
1227 '(0 0))
1228 (should
1229 (equal (file-attributes (expand-file-name (car elt) tmp-name2))
1230 (cdr elt)))))
1231 (setq attr (directory-files-and-attributes tmp-name2 'full))
1232 (dolist (elt attr)
1233 (unless (equal (nth 5 (file-attributes (car elt))) '(0 0))
1234 (should
1235 (equal (file-attributes (car elt)) (cdr elt)))))
1236 (setq attr (directory-files-and-attributes tmp-name2 nil "^b"))
1237 (should (equal (mapcar 'car attr) '("bar" "boz"))))
1239 ;; Cleanup.
1240 (ignore-errors (delete-directory tmp-name1 'recursive)))))
1242 (ert-deftest tramp-test20-file-modes ()
1243 "Check `file-modes'.
1244 This tests also `file-executable-p', `file-writable-p' and `set-file-modes'."
1245 (skip-unless (tramp--test-enabled))
1246 (skip-unless (tramp--test-sh-p))
1248 (let ((tmp-name (tramp--test-make-temp-name)))
1249 (unwind-protect
1250 (progn
1251 (write-region "foo" nil tmp-name)
1252 (should (file-exists-p tmp-name))
1253 (set-file-modes tmp-name #o777)
1254 (should (= (file-modes tmp-name) #o777))
1255 (should (file-executable-p tmp-name))
1256 (should (file-writable-p tmp-name))
1257 (set-file-modes tmp-name #o444)
1258 (should (= (file-modes tmp-name) #o444))
1259 (should-not (file-executable-p tmp-name))
1260 ;; A file is always writable for user "root".
1261 (unless (zerop (nth 2 (file-attributes tmp-name)))
1262 (should-not (file-writable-p tmp-name))))
1264 ;; Cleanup.
1265 (ignore-errors (delete-file tmp-name)))))
1267 (ert-deftest tramp-test21-file-links ()
1268 "Check `file-symlink-p'.
1269 This tests also `make-symbolic-link', `file-truename' and `add-name-to-file'."
1270 (skip-unless (tramp--test-enabled))
1272 ;; We must use `file-truename' for the temporary directory, because
1273 ;; it could be located on a symlinked directory. This would let the
1274 ;; test fail.
1275 (let* ((tramp-test-temporary-file-directory
1276 (file-truename tramp-test-temporary-file-directory))
1277 (tmp-name1 (tramp--test-make-temp-name))
1278 (tmp-name2 (tramp--test-make-temp-name))
1279 (tmp-name3 (tramp--test-make-temp-name 'local)))
1281 ;; Check `make-symbolic-link'.
1282 (unwind-protect
1283 (progn
1284 (write-region "foo" nil tmp-name1)
1285 (should (file-exists-p tmp-name1))
1286 ;; Method "smb" supports `make-symbolic-link' only if the
1287 ;; remote host has CIFS capabilities. tramp-adb.el and
1288 ;; tramp-gvfs.el do not support symbolic links at all.
1289 (condition-case err
1290 (make-symbolic-link tmp-name1 tmp-name2)
1291 (file-error
1292 (skip-unless
1293 (not (string-equal (error-message-string err)
1294 "make-symbolic-link not supported")))))
1295 (should (file-symlink-p tmp-name2))
1296 (should-error (make-symbolic-link tmp-name1 tmp-name2))
1297 (make-symbolic-link tmp-name1 tmp-name2 'ok-if-already-exists)
1298 (should (file-symlink-p tmp-name2))
1299 ;; `tmp-name3' is a local file name.
1300 (should-error (make-symbolic-link tmp-name1 tmp-name3)))
1302 ;; Cleanup.
1303 (ignore-errors
1304 (delete-file tmp-name1)
1305 (delete-file tmp-name2)))
1307 ;; Check `add-name-to-file'.
1308 (unwind-protect
1309 (progn
1310 (write-region "foo" nil tmp-name1)
1311 (should (file-exists-p tmp-name1))
1312 (add-name-to-file tmp-name1 tmp-name2)
1313 (should-not (file-symlink-p tmp-name2))
1314 (should-error (add-name-to-file tmp-name1 tmp-name2))
1315 (add-name-to-file tmp-name1 tmp-name2 'ok-if-already-exists)
1316 (should-not (file-symlink-p tmp-name2))
1317 ;; `tmp-name3' is a local file name.
1318 (should-error (add-name-to-file tmp-name1 tmp-name3)))
1320 ;; Cleanup.
1321 (ignore-errors
1322 (delete-file tmp-name1)
1323 (delete-file tmp-name2)))
1325 ;; Check `file-truename'.
1326 (unwind-protect
1327 (progn
1328 (write-region "foo" nil tmp-name1)
1329 (should (file-exists-p tmp-name1))
1330 (make-symbolic-link tmp-name1 tmp-name2)
1331 (should (file-symlink-p tmp-name2))
1332 (should-not (string-equal tmp-name2 (file-truename tmp-name2)))
1333 (should
1334 (string-equal (file-truename tmp-name1) (file-truename tmp-name2)))
1335 (should (file-equal-p tmp-name1 tmp-name2)))
1336 (ignore-errors
1337 (delete-file tmp-name1)
1338 (delete-file tmp-name2)))
1340 ;; `file-truename' shall preserve trailing link of directories.
1341 (unless (file-symlink-p tramp-test-temporary-file-directory)
1342 (let* ((dir1 (directory-file-name tramp-test-temporary-file-directory))
1343 (dir2 (file-name-as-directory dir1)))
1344 (should (string-equal (file-truename dir1) (expand-file-name dir1)))
1345 (should (string-equal (file-truename dir2) (expand-file-name dir2)))))))
1347 (ert-deftest tramp-test22-file-times ()
1348 "Check `set-file-times' and `file-newer-than-file-p'."
1349 (skip-unless (tramp--test-enabled))
1350 (skip-unless (or (tramp--test-adb-p) (tramp--test-sh-p)))
1352 (let ((tmp-name1 (tramp--test-make-temp-name))
1353 (tmp-name2 (tramp--test-make-temp-name))
1354 (tmp-name3 (tramp--test-make-temp-name)))
1355 (unwind-protect
1356 (progn
1357 (write-region "foo" nil tmp-name1)
1358 (should (file-exists-p tmp-name1))
1359 (should (consp (nth 5 (file-attributes tmp-name1))))
1360 ;; '(0 0) means don't know, and will be replaced by
1361 ;; `current-time'. Therefore, we use '(0 1).
1362 ;; We skip the test, if the remote handler is not able to
1363 ;; set the correct time.
1364 (skip-unless (set-file-times tmp-name1 '(0 1)))
1365 ;; Dumb remote shells without perl(1) or stat(1) are not
1366 ;; able to return the date correctly. They say "don't know".
1367 (unless (equal (nth 5 (file-attributes tmp-name1)) '(0 0))
1368 (should (equal (nth 5 (file-attributes tmp-name1)) '(0 1)))
1369 (write-region "bla" nil tmp-name2)
1370 (should (file-exists-p tmp-name2))
1371 (should (file-newer-than-file-p tmp-name2 tmp-name1))
1372 ;; `tmp-name3' does not exist.
1373 (should (file-newer-than-file-p tmp-name2 tmp-name3))
1374 (should-not (file-newer-than-file-p tmp-name3 tmp-name1))))
1376 ;; Cleanup.
1377 (ignore-errors
1378 (delete-file tmp-name1)
1379 (delete-file tmp-name2)))))
1381 (ert-deftest tramp-test23-visited-file-modtime ()
1382 "Check `set-visited-file-modtime' and `verify-visited-file-modtime'."
1383 (skip-unless (tramp--test-enabled))
1385 (let ((tmp-name (tramp--test-make-temp-name)))
1386 (unwind-protect
1387 (progn
1388 (write-region "foo" nil tmp-name)
1389 (should (file-exists-p tmp-name))
1390 (with-temp-buffer
1391 (insert-file-contents tmp-name)
1392 (should (verify-visited-file-modtime))
1393 (set-visited-file-modtime '(0 1))
1394 (should (verify-visited-file-modtime))
1395 (should (equal (visited-file-modtime) '(0 1 0 0)))))
1397 ;; Cleanup.
1398 (ignore-errors (delete-file tmp-name)))))
1400 (ert-deftest tramp-test24-file-name-completion ()
1401 "Check `file-name-completion' and `file-name-all-completions'."
1402 (skip-unless (tramp--test-enabled))
1404 (dolist (n-e '(nil t))
1405 (let ((non-essential n-e)
1406 (tmp-name (tramp--test-make-temp-name))
1407 (method (file-remote-p tramp-test-temporary-file-directory 'method))
1408 (host (file-remote-p tramp-test-temporary-file-directory 'host)))
1410 (unwind-protect
1411 (progn
1412 ;; Method and host name in completion mode. This kind of
1413 ;; completion does not work on MS Windows.
1414 (when (and (tramp-completion-mode-p)
1415 (not (memq system-type '(cygwin windows-nt))))
1416 (unless (zerop (length method))
1417 (should
1418 (member
1419 (format "%s:" method)
1420 (file-name-all-completions (substring method 0 1) "/"))))
1421 (unless (zerop (length host))
1422 (let ((tramp-default-method (or method tramp-default-method)))
1423 (should
1424 (member
1425 (format "%s:" host)
1426 (file-name-all-completions (substring host 0 1) "/")))))
1427 (unless (or (zerop (length method)) (zerop (length host)))
1428 (should
1429 (member
1430 (format "%s:" host)
1431 (file-name-all-completions
1432 (substring host 0 1) (format "/%s:" method))))))
1434 ;; Local files.
1435 (make-directory tmp-name)
1436 (should (file-directory-p tmp-name))
1437 (write-region "foo" nil (expand-file-name "foo" tmp-name))
1438 (should (file-exists-p (expand-file-name "foo" tmp-name)))
1439 (write-region "bar" nil (expand-file-name "bold" tmp-name))
1440 (should (file-exists-p (expand-file-name "bold" tmp-name)))
1441 (make-directory (expand-file-name "boz" tmp-name))
1442 (should (file-directory-p (expand-file-name "boz" tmp-name)))
1443 (should (equal (file-name-completion "fo" tmp-name) "foo"))
1444 (should (equal (file-name-completion "foo" tmp-name) t))
1445 (should (equal (file-name-completion "b" tmp-name) "bo"))
1446 (should-not (file-name-completion "a" tmp-name))
1447 (should
1448 (equal
1449 (file-name-completion "b" tmp-name 'file-directory-p) "boz/"))
1450 (should (equal (file-name-all-completions "fo" tmp-name) '("foo")))
1451 (should
1452 (equal
1453 (sort (file-name-all-completions "b" tmp-name) 'string-lessp)
1454 '("bold" "boz/")))
1455 (should-not (file-name-all-completions "a" tmp-name))
1456 ;; `completion-regexp-list' restricts the completion to
1457 ;; files which match all expressions in this list.
1458 (let ((completion-regexp-list
1459 `(,directory-files-no-dot-files-regexp "b")))
1460 (should
1461 (equal (file-name-completion "" tmp-name) "bo"))
1462 (should
1463 (equal
1464 (sort (file-name-all-completions "" tmp-name) 'string-lessp)
1465 '("bold" "boz/"))))
1466 ;; `file-name-completion' ignores file names that end in
1467 ;; any string in `completion-ignored-extensions'.
1468 (let ((completion-ignored-extensions '(".ext")))
1469 (write-region "foo" nil (expand-file-name "foo.ext" tmp-name))
1470 (should (file-exists-p (expand-file-name "foo.ext" tmp-name)))
1471 (should (equal (file-name-completion "fo" tmp-name) "foo"))
1472 (should (equal (file-name-completion "foo" tmp-name) t))
1473 (should (equal (file-name-completion "foo." tmp-name) "foo.ext"))
1474 (should (equal (file-name-completion "foo.ext" tmp-name) t))
1475 ;; `file-name-all-completions' is not affected.
1476 (should
1477 (equal
1478 (sort (file-name-all-completions "" tmp-name) 'string-lessp)
1479 '("../" "./" "bold" "boz/" "foo" "foo.ext")))))
1481 ;; Cleanup.
1482 (ignore-errors (delete-directory tmp-name 'recursive))))))
1484 (ert-deftest tramp-test25-load ()
1485 "Check `load'."
1486 (skip-unless (tramp--test-enabled))
1488 (let ((tmp-name (tramp--test-make-temp-name)))
1489 (unwind-protect
1490 (progn
1491 (load tmp-name 'noerror 'nomessage)
1492 (should-not (featurep 'tramp-test-load))
1493 (write-region "(provide 'tramp-test-load)" nil tmp-name)
1494 ;; `load' in lread.c does not pass `must-suffix'. Why?
1495 ;(should-error (load tmp-name nil 'nomessage 'nosuffix 'must-suffix))
1496 (load tmp-name nil 'nomessage 'nosuffix)
1497 (should (featurep 'tramp-test-load)))
1499 ;; Cleanup.
1500 (ignore-errors
1501 (and (featurep 'tramp-test-load) (unload-feature 'tramp-test-load))
1502 (delete-file tmp-name)))))
1504 (ert-deftest tramp-test26-process-file ()
1505 "Check `process-file'."
1506 :tags '(:expensive-test)
1507 (skip-unless (tramp--test-enabled))
1508 (skip-unless (or (tramp--test-adb-p) (tramp--test-sh-p)))
1510 (let* ((tmp-name (tramp--test-make-temp-name))
1511 (fnnd (file-name-nondirectory tmp-name))
1512 (default-directory tramp-test-temporary-file-directory)
1513 kill-buffer-query-functions)
1514 (unwind-protect
1515 (progn
1516 ;; We cannot use "/bin/true" and "/bin/false"; those paths
1517 ;; do not exist on hydra.
1518 (should (zerop (process-file "true")))
1519 (should-not (zerop (process-file "false")))
1520 (should-not (zerop (process-file "binary-does-not-exist")))
1521 (with-temp-buffer
1522 (write-region "foo" nil tmp-name)
1523 (should (file-exists-p tmp-name))
1524 (should (zerop (process-file "ls" nil t nil fnnd)))
1525 ;; `ls' could produce colorized output.
1526 (goto-char (point-min))
1527 (while
1528 (re-search-forward tramp-display-escape-sequence-regexp nil t)
1529 (replace-match "" nil nil))
1530 (should (string-equal (format "%s\n" fnnd) (buffer-string)))
1531 (should-not (get-buffer-window (current-buffer) t))
1533 ;; Second run. The output must be appended.
1534 (goto-char (point-max))
1535 (should (zerop (process-file "ls" nil t t fnnd)))
1536 ;; `ls' could produce colorized output.
1537 (goto-char (point-min))
1538 (while
1539 (re-search-forward tramp-display-escape-sequence-regexp nil t)
1540 (replace-match "" nil nil))
1541 (should
1542 (string-equal (format "%s\n%s\n" fnnd fnnd) (buffer-string)))
1543 ;; A non-nil DISPLAY must not raise the buffer.
1544 (should-not (get-buffer-window (current-buffer) t))))
1546 ;; Cleanup.
1547 (ignore-errors (delete-file tmp-name)))))
1549 (ert-deftest tramp-test27-start-file-process ()
1550 "Check `start-file-process'."
1551 :tags '(:expensive-test)
1552 (skip-unless (tramp--test-enabled))
1553 (skip-unless (tramp--test-sh-p))
1555 (let ((default-directory tramp-test-temporary-file-directory)
1556 (tmp-name (tramp--test-make-temp-name))
1557 kill-buffer-query-functions proc)
1558 (unwind-protect
1559 (with-temp-buffer
1560 (setq proc (start-file-process "test1" (current-buffer) "cat"))
1561 (should (processp proc))
1562 (should (equal (process-status proc) 'run))
1563 (process-send-string proc "foo")
1564 (process-send-eof proc)
1565 ;; Read output.
1566 (with-timeout (10 (ert-fail "`start-file-process' timed out"))
1567 (while (< (- (point-max) (point-min)) (length "foo"))
1568 (accept-process-output proc 0.1)))
1569 (should (string-equal (buffer-string) "foo")))
1571 ;; Cleanup.
1572 (ignore-errors (delete-process proc)))
1574 (unwind-protect
1575 (with-temp-buffer
1576 (write-region "foo" nil tmp-name)
1577 (should (file-exists-p tmp-name))
1578 (setq proc
1579 (start-file-process
1580 "test2" (current-buffer)
1581 "cat" (file-name-nondirectory tmp-name)))
1582 (should (processp proc))
1583 ;; Read output.
1584 (with-timeout (10 (ert-fail "`start-file-process' timed out"))
1585 (while (< (- (point-max) (point-min)) (length "foo"))
1586 (accept-process-output proc 0.1)))
1587 (should (string-equal (buffer-string) "foo")))
1589 ;; Cleanup.
1590 (ignore-errors
1591 (delete-process proc)
1592 (delete-file tmp-name)))
1594 (unwind-protect
1595 (with-temp-buffer
1596 (setq proc (start-file-process "test3" (current-buffer) "cat"))
1597 (should (processp proc))
1598 (should (equal (process-status proc) 'run))
1599 (set-process-filter
1600 proc
1601 (lambda (p s) (with-current-buffer (process-buffer p) (insert s))))
1602 (process-send-string proc "foo")
1603 (process-send-eof proc)
1604 ;; Read output.
1605 (with-timeout (10 (ert-fail "`start-file-process' timed out"))
1606 (while (< (- (point-max) (point-min)) (length "foo"))
1607 (accept-process-output proc 0.1)))
1608 (should (string-equal (buffer-string) "foo")))
1610 ;; Cleanup.
1611 (ignore-errors (delete-process proc)))))
1613 (ert-deftest tramp-test28-shell-command ()
1614 "Check `shell-command'."
1615 :tags '(:expensive-test)
1616 (skip-unless (tramp--test-enabled))
1617 (skip-unless (tramp--test-sh-p))
1619 (let ((tmp-name (tramp--test-make-temp-name))
1620 (default-directory tramp-test-temporary-file-directory)
1621 kill-buffer-query-functions)
1622 (unwind-protect
1623 (with-temp-buffer
1624 (write-region "foo" nil tmp-name)
1625 (should (file-exists-p tmp-name))
1626 (shell-command
1627 (format "ls %s" (file-name-nondirectory tmp-name)) (current-buffer))
1628 ;; `ls' could produce colorized output.
1629 (goto-char (point-min))
1630 (while (re-search-forward tramp-display-escape-sequence-regexp nil t)
1631 (replace-match "" nil nil))
1632 (should
1633 (string-equal
1634 (format "%s\n" (file-name-nondirectory tmp-name)) (buffer-string))))
1636 ;; Cleanup.
1637 (ignore-errors (delete-file tmp-name)))
1639 (unwind-protect
1640 (with-temp-buffer
1641 (write-region "foo" nil tmp-name)
1642 (should (file-exists-p tmp-name))
1643 (async-shell-command
1644 (format "ls %s" (file-name-nondirectory tmp-name)) (current-buffer))
1645 (set-process-sentinel (get-buffer-process (current-buffer)) nil)
1646 ;; Read output.
1647 (with-timeout (10 (ert-fail "`async-shell-command' timed out"))
1648 (while (< (- (point-max) (point-min))
1649 (1+ (length (file-name-nondirectory tmp-name))))
1650 (accept-process-output
1651 (get-buffer-process (current-buffer)) 0.1)))
1652 ;; `ls' could produce colorized output.
1653 (goto-char (point-min))
1654 (while (re-search-forward tramp-display-escape-sequence-regexp nil t)
1655 (replace-match "" nil nil))
1656 ;; There might be a nasty "Process *Async Shell* finished" message.
1657 (goto-char (point-min))
1658 (forward-line)
1659 (narrow-to-region (point-min) (point))
1660 (should
1661 (string-equal
1662 (format "%s\n" (file-name-nondirectory tmp-name)) (buffer-string))))
1664 ;; Cleanup.
1665 (ignore-errors (delete-file tmp-name)))
1667 (unwind-protect
1668 (with-temp-buffer
1669 (write-region "foo" nil tmp-name)
1670 (should (file-exists-p tmp-name))
1671 (async-shell-command "read line; ls $line" (current-buffer))
1672 (set-process-sentinel (get-buffer-process (current-buffer)) nil)
1673 (process-send-string
1674 (get-buffer-process (current-buffer))
1675 (format "%s\n" (file-name-nondirectory tmp-name)))
1676 ;; Read output.
1677 (with-timeout (10 (ert-fail "`async-shell-command' timed out"))
1678 (while (< (- (point-max) (point-min))
1679 (1+ (length (file-name-nondirectory tmp-name))))
1680 (accept-process-output
1681 (get-buffer-process (current-buffer)) 0.1)))
1682 ;; `ls' could produce colorized output.
1683 (goto-char (point-min))
1684 (while (re-search-forward tramp-display-escape-sequence-regexp nil t)
1685 (replace-match "" nil nil))
1686 ;; There might be a nasty "Process *Async Shell* finished" message.
1687 (goto-char (point-min))
1688 (forward-line)
1689 (narrow-to-region (point-min) (point))
1690 (should
1691 (string-equal
1692 (format "%s\n" (file-name-nondirectory tmp-name)) (buffer-string))))
1694 ;; Cleanup.
1695 (ignore-errors (delete-file tmp-name)))))
1697 (defun tramp-test--shell-command-to-string-asynchronously (command)
1698 "Like `shell-command-to-string', but for asynchronous processes."
1699 (with-temp-buffer
1700 (async-shell-command command (current-buffer))
1701 ;; Suppress nasty messages.
1702 (set-process-sentinel (get-buffer-process (current-buffer)) nil)
1703 (with-timeout (10)
1704 (while (get-buffer-process (current-buffer))
1705 (accept-process-output (get-buffer-process (current-buffer)) 0.1)))
1706 (accept-process-output nil 0.1)
1707 (buffer-substring-no-properties (point-min) (point-max))))
1709 ;; This test is inspired by Bug#23952.
1710 (ert-deftest tramp-test29-environment-variables ()
1711 "Check that remote processes set / unset environment variables properly."
1712 :tags '(:expensive-test)
1713 (skip-unless (tramp--test-enabled))
1714 (skip-unless (tramp--test-sh-p))
1716 (dolist (this-shell-command-to-string
1717 '(;; Synchronously.
1718 shell-command-to-string
1719 ;; Asynchronously.
1720 tramp-test--shell-command-to-string-asynchronously))
1722 (let ((default-directory tramp-test-temporary-file-directory)
1723 (shell-file-name "/bin/sh")
1724 (envvar (concat "VAR_" (upcase (md5 (current-time-string)))))
1725 kill-buffer-query-functions)
1727 (unwind-protect
1728 ;; Set a value.
1729 (let ((process-environment
1730 (cons (concat envvar "=foo") process-environment)))
1731 ;; Default value.
1732 (should
1733 (string-match
1734 "foo"
1735 (funcall
1736 this-shell-command-to-string
1737 (format "echo -n ${%s:?bla}" envvar))))))
1739 (unwind-protect
1740 ;; Set the empty value.
1741 (let ((process-environment
1742 (cons (concat envvar "=") process-environment)))
1743 ;; Value is null.
1744 (should
1745 (string-match
1746 "bla"
1747 (funcall
1748 this-shell-command-to-string
1749 (format "echo -n ${%s:?bla}" envvar))))
1750 ;; Variable is set.
1751 (should
1752 (string-match
1753 (regexp-quote envvar)
1754 (funcall this-shell-command-to-string "set")))))
1756 ;; We force a reconnect, in order to have a clean environment.
1757 (tramp-cleanup-connection
1758 (tramp-dissect-file-name tramp-test-temporary-file-directory)
1759 'keep-debug 'keep-password)
1760 (unwind-protect
1761 ;; Unset the variable.
1762 (let ((tramp-remote-process-environment
1763 (cons (concat envvar "=foo")
1764 tramp-remote-process-environment)))
1765 ;; Set the initial value, we want to unset below.
1766 (should
1767 (string-match
1768 "foo"
1769 (funcall
1770 this-shell-command-to-string
1771 (format "echo -n ${%s:?bla}" envvar))))
1772 (let ((process-environment
1773 (cons envvar process-environment)))
1774 ;; Variable is unset.
1775 (should
1776 (string-match
1777 "bla"
1778 (funcall
1779 this-shell-command-to-string
1780 (format "echo -n ${%s:?bla}" envvar))))
1781 ;; Variable is unset.
1782 (should-not
1783 (string-match
1784 (regexp-quote envvar)
1785 (funcall this-shell-command-to-string "set")))))))))
1787 (ert-deftest tramp-test30-vc-registered ()
1788 "Check `vc-registered'."
1789 :tags '(:expensive-test)
1790 (skip-unless (tramp--test-enabled))
1791 (skip-unless (tramp--test-sh-p))
1793 (let* ((default-directory tramp-test-temporary-file-directory)
1794 (tmp-name1 (tramp--test-make-temp-name))
1795 (tmp-name2 (expand-file-name "foo" tmp-name1))
1796 (tramp-remote-process-environment tramp-remote-process-environment)
1797 (vc-handled-backends
1798 (with-parsed-tramp-file-name tramp-test-temporary-file-directory nil
1799 (cond
1800 ((tramp-find-executable v vc-git-program (tramp-get-remote-path v))
1801 '(Git))
1802 ((tramp-find-executable v vc-hg-program (tramp-get-remote-path v))
1803 '(Hg))
1804 ((tramp-find-executable v vc-bzr-program (tramp-get-remote-path v))
1805 (setq tramp-remote-process-environment
1806 (cons (format "BZR_HOME=%s"
1807 (file-remote-p tmp-name1 'localname))
1808 tramp-remote-process-environment))
1809 ;; We must force a reconnect, in order to activate $BZR_HOME.
1810 (tramp-cleanup-connection
1811 (tramp-dissect-file-name tramp-test-temporary-file-directory)
1812 nil 'keep-password)
1813 '(Bzr))
1814 (t nil)))))
1815 (skip-unless vc-handled-backends)
1816 (message "%s" vc-handled-backends)
1818 (unwind-protect
1819 (progn
1820 (make-directory tmp-name1)
1821 (write-region "foo" nil tmp-name2)
1822 (should (file-directory-p tmp-name1))
1823 (should (file-exists-p tmp-name2))
1824 (should-not (vc-registered tmp-name1))
1825 (should-not (vc-registered tmp-name2))
1827 (let ((default-directory tmp-name1))
1828 ;; Create empty repository, and register the file.
1829 ;; Sometimes, creation of repository fails (bzr!); we skip
1830 ;; the test then.
1831 (condition-case nil
1832 (vc-create-repo (car vc-handled-backends))
1833 (error (skip-unless nil)))
1834 ;; The structure of VC-FILESET is not documented. Let's
1835 ;; hope it won't change.
1836 (condition-case nil
1837 (vc-register
1838 (list (car vc-handled-backends)
1839 (list (file-name-nondirectory tmp-name2))))
1840 ;; `vc-register' has changed its arguments in Emacs 25.1.
1841 (error
1842 (vc-register
1843 nil (list (car vc-handled-backends)
1844 (list (file-name-nondirectory tmp-name2))))))
1845 ;; vc-git uses an own process sentinel, Tramp's sentinel
1846 ;; for flushing the cache isn't used.
1847 (dired-uncache (concat (file-remote-p default-directory) "/"))
1848 (should (vc-registered (file-name-nondirectory tmp-name2)))))
1850 ;; Cleanup.
1851 (ignore-errors (delete-directory tmp-name1 'recursive)))))
1853 (ert-deftest tramp-test31-make-auto-save-file-name ()
1854 "Check `make-auto-save-file-name'."
1855 (skip-unless (tramp--test-enabled))
1857 (let ((tmp-name1 (tramp--test-make-temp-name))
1858 (tmp-name2 (tramp--test-make-temp-name)))
1860 (unwind-protect
1861 (progn
1862 ;; Use default `auto-save-file-name-transforms' mechanism.
1863 (let (tramp-auto-save-directory)
1864 (with-temp-buffer
1865 (setq buffer-file-name tmp-name1)
1866 (should
1867 (string-equal
1868 (make-auto-save-file-name)
1869 ;; This is taken from original `make-auto-save-file-name'.
1870 (expand-file-name
1871 (format
1872 "#%s#"
1873 (subst-char-in-string
1874 ?/ ?! (replace-regexp-in-string "!" "!!" tmp-name1)))
1875 temporary-file-directory)))))
1877 ;; No mapping.
1878 (let (tramp-auto-save-directory auto-save-file-name-transforms)
1879 (with-temp-buffer
1880 (setq buffer-file-name tmp-name1)
1881 (should
1882 (string-equal
1883 (make-auto-save-file-name)
1884 (expand-file-name
1885 (format "#%s#" (file-name-nondirectory tmp-name1))
1886 tramp-test-temporary-file-directory)))))
1888 ;; Use default `tramp-auto-save-directory' mechanism.
1889 (let ((tramp-auto-save-directory tmp-name2))
1890 (with-temp-buffer
1891 (setq buffer-file-name tmp-name1)
1892 (should
1893 (string-equal
1894 (make-auto-save-file-name)
1895 ;; This is taken from Tramp.
1896 (expand-file-name
1897 (format
1898 "#%s#"
1899 (tramp-subst-strs-in-string
1900 '(("_" . "|")
1901 ("/" . "_a")
1902 (":" . "_b")
1903 ("|" . "__")
1904 ("[" . "_l")
1905 ("]" . "_r"))
1906 tmp-name1))
1907 tmp-name2)))
1908 (should (file-directory-p tmp-name2))))
1910 ;; Relative file names shall work, too.
1911 (let ((tramp-auto-save-directory "."))
1912 (with-temp-buffer
1913 (setq buffer-file-name tmp-name1
1914 default-directory tmp-name2)
1915 (should
1916 (string-equal
1917 (make-auto-save-file-name)
1918 ;; This is taken from Tramp.
1919 (expand-file-name
1920 (format
1921 "#%s#"
1922 (tramp-subst-strs-in-string
1923 '(("_" . "|")
1924 ("/" . "_a")
1925 (":" . "_b")
1926 ("|" . "__")
1927 ("[" . "_l")
1928 ("]" . "_r"))
1929 tmp-name1))
1930 tmp-name2)))
1931 (should (file-directory-p tmp-name2)))))
1933 ;; Cleanup.
1934 (ignore-errors (delete-file tmp-name1))
1935 (ignore-errors (delete-directory tmp-name2 'recursive)))))
1937 ;; The functions have been introduced in Emacs 25.2.
1938 (ert-deftest tramp-test32-make-nearby-temp-file ()
1939 "Check `make-nearby-temp-file' and `temporary-file-directory'."
1940 (skip-unless (tramp--test-enabled))
1941 (skip-unless
1942 (and (fboundp 'make-nearby-temp-file) (fboundp 'temporary-file-directory)))
1944 (let ((default-directory tramp-test-temporary-file-directory)
1945 tmp-file)
1946 ;; The remote host shall know a temporary file directory.
1947 (should (stringp (temporary-file-directory)))
1948 (should
1949 (string-equal
1950 (file-remote-p default-directory)
1951 (file-remote-p (temporary-file-directory))))
1953 ;; The temporary file shall be located on the remote host.
1954 (setq tmp-file (make-nearby-temp-file "tramp-test"))
1955 (should (file-exists-p tmp-file))
1956 (should (file-regular-p tmp-file))
1957 (should
1958 (string-equal
1959 (file-remote-p default-directory)
1960 (file-remote-p tmp-file)))
1961 (delete-file tmp-file)
1962 (should-not (file-exists-p tmp-file))
1964 (setq tmp-file (make-nearby-temp-file "tramp-test" 'dir))
1965 (should (file-exists-p tmp-file))
1966 (should (file-directory-p tmp-file))
1967 (delete-directory tmp-file)
1968 (should-not (file-exists-p tmp-file))))
1970 (defun tramp--test-adb-p ()
1971 "Check, whether the remote host runs Android.
1972 This requires restrictions of file name syntax."
1973 (tramp-adb-file-name-p tramp-test-temporary-file-directory))
1975 (defun tramp--test-ftp-p ()
1976 "Check, whether an FTP-like method is used.
1977 This does not support globbing characters in file names (yet)."
1978 ;; Globbing characters are ??, ?* and ?\[.
1979 (string-match
1980 "ftp$" (file-remote-p tramp-test-temporary-file-directory 'method)))
1982 (defun tramp--test-gvfs-p ()
1983 "Check, whether the remote host runs a GVFS based method.
1984 This requires restrictions of file name syntax."
1985 (tramp-gvfs-file-name-p tramp-test-temporary-file-directory))
1987 (defun tramp--test-rsync-p ()
1988 "Check, whether the rsync method is used.
1989 This does not support special file names."
1990 (string-equal
1991 "rsync" (file-remote-p tramp-test-temporary-file-directory 'method)))
1993 (defun tramp--test-sh-p ()
1994 "Check, whether the remote host runs a based method from tramp-sh.el."
1996 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory)
1997 'tramp-sh-file-name-handler))
1999 (defun tramp--test-smb-or-windows-nt-p ()
2000 "Check, whether the locale or remote host runs MS Windows.
2001 This requires restrictions of file name syntax."
2002 (or (eq system-type 'windows-nt)
2003 (tramp-smb-file-name-p tramp-test-temporary-file-directory)))
2005 (defun tramp--test-hpux-p ()
2006 "Check, whether the remote host runs HP-UX.
2007 Several special characters do not work properly there."
2008 ;; We must refill the cache. `file-truename' does it.
2009 (with-parsed-tramp-file-name
2010 (file-truename tramp-test-temporary-file-directory) nil
2011 (string-match "^HP-UX" (tramp-get-connection-property v "uname" ""))))
2013 (defun tramp--test-check-files (&rest files)
2014 "Run a simple but comprehensive test over every file in FILES."
2015 ;; We must use `file-truename' for the temporary directory, because
2016 ;; it could be located on a symlinked directory. This would let the
2017 ;; test fail.
2018 (let* ((tramp-test-temporary-file-directory
2019 (file-truename tramp-test-temporary-file-directory))
2020 (tmp-name1 (tramp--test-make-temp-name))
2021 (tmp-name2 (tramp--test-make-temp-name 'local))
2022 (files (delq nil files)))
2023 (unwind-protect
2024 (progn
2025 (make-directory tmp-name1)
2026 (make-directory tmp-name2)
2027 (dolist (elt files)
2028 (let* ((file1 (expand-file-name elt tmp-name1))
2029 (file2 (expand-file-name elt tmp-name2))
2030 (file3 (expand-file-name (concat elt "foo") tmp-name1)))
2031 (write-region elt nil file1)
2032 (should (file-exists-p file1))
2034 ;; Check file contents.
2035 (with-temp-buffer
2036 (insert-file-contents file1)
2037 (should (string-equal (buffer-string) elt)))
2039 ;; Copy file both directions.
2040 (copy-file file1 tmp-name2)
2041 (should (file-exists-p file2))
2042 (delete-file file1)
2043 (should-not (file-exists-p file1))
2044 (copy-file file2 tmp-name1)
2045 (should (file-exists-p file1))
2047 ;; Method "smb" supports `make-symbolic-link' only if the
2048 ;; remote host has CIFS capabilities. tramp-adb.el and
2049 ;; tramp-gvfs.el do not support symbolic links at all.
2050 (condition-case err
2051 (progn
2052 (make-symbolic-link file1 file3)
2053 (should (file-symlink-p file3))
2054 (should
2055 (string-equal
2056 (expand-file-name file1) (file-truename file3)))
2057 (should
2058 (string-equal
2059 (car (file-attributes file3))
2060 (file-remote-p (file-truename file1) 'localname)))
2061 ;; Check file contents.
2062 (with-temp-buffer
2063 (insert-file-contents file3)
2064 (should (string-equal (buffer-string) elt)))
2065 (delete-file file3))
2066 (file-error
2067 (should (string-equal (error-message-string err)
2068 "make-symbolic-link not supported"))))))
2070 ;; Check file names.
2071 (should (equal (directory-files
2072 tmp-name1 nil directory-files-no-dot-files-regexp)
2073 (sort (copy-sequence files) 'string-lessp)))
2074 (should (equal (directory-files
2075 tmp-name2 nil directory-files-no-dot-files-regexp)
2076 (sort (copy-sequence files) 'string-lessp)))
2078 ;; `substitute-in-file-name' could return different values.
2079 ;; For `adb', there could be strange file permissions
2080 ;; preventing overwriting a file. We don't care in this
2081 ;; testcase.
2082 (dolist (elt files)
2083 (let ((file1
2084 (substitute-in-file-name (expand-file-name elt tmp-name1)))
2085 (file2
2086 (substitute-in-file-name (expand-file-name elt tmp-name2))))
2087 (ignore-errors (write-region elt nil file1))
2088 (should (file-exists-p file1))
2089 (ignore-errors (write-region elt nil file2 nil 'nomessage))
2090 (should (file-exists-p file2))))
2092 (should (equal (directory-files
2093 tmp-name1 nil directory-files-no-dot-files-regexp)
2094 (directory-files
2095 tmp-name2 nil directory-files-no-dot-files-regexp)))
2097 ;; Check directory creation. We use a subdirectory "foo"
2098 ;; in order to avoid conflicts with previous file name tests.
2099 (dolist (elt files)
2100 (let* ((elt1 (concat elt "foo"))
2101 (file1 (expand-file-name (concat "foo/" elt) tmp-name1))
2102 (file2 (expand-file-name elt file1))
2103 (file3 (expand-file-name elt1 file1)))
2104 (make-directory file1 'parents)
2105 (should (file-directory-p file1))
2106 (write-region elt nil file2)
2107 (should (file-exists-p file2))
2108 (should
2109 (equal
2110 (directory-files file1 nil directory-files-no-dot-files-regexp)
2111 `(,elt)))
2112 (should
2113 (equal
2114 (caar (directory-files-and-attributes
2115 file1 nil directory-files-no-dot-files-regexp))
2116 elt))
2118 ;; Check symlink in `directory-files-and-attributes'.
2119 (condition-case err
2120 (progn
2121 (make-symbolic-link file2 file3)
2122 (should (file-symlink-p file3))
2123 (should
2124 (string-equal
2125 (caar (directory-files-and-attributes
2126 file1 nil (regexp-quote elt1)))
2127 elt1))
2128 (should
2129 (string-equal
2130 (cadr (car (directory-files-and-attributes
2131 file1 nil (regexp-quote elt1))))
2132 (file-remote-p (file-truename file2) 'localname)))
2133 (delete-file file3)
2134 (should-not (file-exists-p file3)))
2135 (file-error
2136 (should (string-equal (error-message-string err)
2137 "make-symbolic-link not supported"))))
2139 (delete-file file2)
2140 (should-not (file-exists-p file2))
2141 (delete-directory file1)
2142 (should-not (file-exists-p file1)))))
2144 ;; Cleanup.
2145 (ignore-errors (delete-directory tmp-name1 'recursive))
2146 (ignore-errors (delete-directory tmp-name2 'recursive)))))
2148 (defun tramp--test-special-characters ()
2149 "Perform the test in `tramp-test33-special-characters*'."
2150 ;; Newlines, slashes and backslashes in file names are not
2151 ;; supported. So we don't test. And we don't test the tab
2152 ;; character on Windows or Cygwin, because the backslash is
2153 ;; interpreted as a path separator, preventing "\t" from being
2154 ;; expanded to <TAB>.
2155 (tramp--test-check-files
2156 (if (or (tramp--test-gvfs-p) (tramp--test-smb-or-windows-nt-p))
2157 "foo bar baz"
2158 (if (or (tramp--test-adb-p) (eq system-type 'cygwin))
2159 " foo bar baz "
2160 " foo\tbar baz\t"))
2161 "$foo$bar$$baz$"
2162 "-foo-bar-baz-"
2163 "%foo%bar%baz%"
2164 "&foo&bar&baz&"
2165 (unless (or (tramp--test-ftp-p)
2166 (tramp--test-gvfs-p)
2167 (tramp--test-smb-or-windows-nt-p))
2168 "?foo?bar?baz?")
2169 (unless (or (tramp--test-ftp-p)
2170 (tramp--test-gvfs-p)
2171 (tramp--test-smb-or-windows-nt-p))
2172 "*foo*bar*baz*")
2173 (if (or (tramp--test-gvfs-p) (tramp--test-smb-or-windows-nt-p))
2174 "'foo'bar'baz'"
2175 "'foo\"bar'baz\"")
2176 "#foo~bar#baz~"
2177 (if (or (tramp--test-gvfs-p) (tramp--test-smb-or-windows-nt-p))
2178 "!foo!bar!baz!"
2179 "!foo|bar!baz|")
2180 (if (or (tramp--test-gvfs-p) (tramp--test-smb-or-windows-nt-p))
2181 ";foo;bar;baz;"
2182 ":foo;bar:baz;")
2183 (unless (or (tramp--test-gvfs-p) (tramp--test-smb-or-windows-nt-p))
2184 "<foo>bar<baz>")
2185 "(foo)bar(baz)"
2186 (unless (or (tramp--test-ftp-p) (tramp--test-gvfs-p)) "[foo]bar[baz]")
2187 "{foo}bar{baz}"))
2189 ;; These tests are inspired by Bug#17238.
2190 (ert-deftest tramp-test33-special-characters ()
2191 "Check special characters in file names."
2192 (skip-unless (tramp--test-enabled))
2193 (skip-unless (not (tramp--test-rsync-p)))
2195 (tramp--test-special-characters))
2197 (ert-deftest tramp-test33-special-characters-with-stat ()
2198 "Check special characters in file names.
2199 Use the `stat' command."
2200 :tags '(:expensive-test)
2201 (skip-unless (tramp--test-enabled))
2202 (skip-unless (and (tramp--test-sh-p) (not (tramp--test-rsync-p))))
2203 (with-parsed-tramp-file-name tramp-test-temporary-file-directory nil
2204 (skip-unless (tramp-get-remote-stat v)))
2206 (let ((tramp-connection-properties
2207 (append
2208 `((,(regexp-quote (file-remote-p tramp-test-temporary-file-directory))
2209 "perl" nil))
2210 tramp-connection-properties)))
2211 (tramp--test-special-characters)))
2213 (ert-deftest tramp-test33-special-characters-with-perl ()
2214 "Check special characters in file names.
2215 Use the `perl' command."
2216 :tags '(:expensive-test)
2217 (skip-unless (tramp--test-enabled))
2218 (skip-unless (and (tramp--test-sh-p) (not (tramp--test-rsync-p))))
2219 (with-parsed-tramp-file-name tramp-test-temporary-file-directory nil
2220 (skip-unless (tramp-get-remote-perl v)))
2222 (let ((tramp-connection-properties
2223 (append
2224 `((,(regexp-quote (file-remote-p tramp-test-temporary-file-directory))
2225 "stat" nil)
2226 ;; See `tramp-sh-handle-file-truename'.
2227 (,(regexp-quote (file-remote-p tramp-test-temporary-file-directory))
2228 "readlink" nil))
2229 tramp-connection-properties)))
2230 (tramp--test-special-characters)))
2232 (ert-deftest tramp-test33-special-characters-with-ls ()
2233 "Check special characters in file names.
2234 Use the `ls' command."
2235 :tags '(:expensive-test)
2236 (skip-unless (tramp--test-enabled))
2237 (skip-unless (and (tramp--test-sh-p) (not (tramp--test-rsync-p))))
2239 (let ((tramp-connection-properties
2240 (append
2241 `((,(regexp-quote (file-remote-p tramp-test-temporary-file-directory))
2242 "perl" nil)
2243 (,(regexp-quote (file-remote-p tramp-test-temporary-file-directory))
2244 "stat" nil)
2245 ;; See `tramp-sh-handle-file-truename'.
2246 (,(regexp-quote (file-remote-p tramp-test-temporary-file-directory))
2247 "readlink" nil))
2248 tramp-connection-properties)))
2249 (tramp--test-special-characters)))
2251 (defun tramp--test-utf8 ()
2252 "Perform the test in `tramp-test34-utf8*'."
2253 (let* ((utf8 (if (and (eq system-type 'darwin)
2254 (memq 'utf-8-hfs (coding-system-list)))
2255 'utf-8-hfs 'utf-8))
2256 (coding-system-for-read utf8)
2257 (coding-system-for-write utf8)
2258 (file-name-coding-system utf8))
2259 (tramp--test-check-files
2260 (unless (tramp--test-hpux-p) "Γυρίστε το Γαλαξία με Ώτο Στοπ")
2261 (unless (tramp--test-hpux-p)
2262 "أصبح بوسعك الآن تنزيل نسخة كاملة من موسوعة ويكيبيديا العربية لتصفحها بلا اتصال بالإنترنت")
2263 "银河系漫游指南系列"
2264 "Автостопом по гала́ктике")))
2266 (ert-deftest tramp-test34-utf8 ()
2267 "Check UTF8 encoding in file names and file contents."
2268 (skip-unless (tramp--test-enabled))
2269 (skip-unless (not (tramp--test-rsync-p)))
2271 (tramp--test-utf8))
2273 (ert-deftest tramp-test34-utf8-with-stat ()
2274 "Check UTF8 encoding in file names and file contents.
2275 Use the `stat' command."
2276 :tags '(:expensive-test)
2277 (skip-unless (tramp--test-enabled))
2278 (skip-unless (and (tramp--test-sh-p) (not (tramp--test-rsync-p))))
2279 (with-parsed-tramp-file-name tramp-test-temporary-file-directory nil
2280 (skip-unless (tramp-get-remote-stat v)))
2282 (let ((tramp-connection-properties
2283 (append
2284 `((,(regexp-quote (file-remote-p tramp-test-temporary-file-directory))
2285 "perl" nil))
2286 tramp-connection-properties)))
2287 (tramp--test-utf8)))
2289 (ert-deftest tramp-test34-utf8-with-perl ()
2290 "Check UTF8 encoding in file names and file contents.
2291 Use the `perl' command."
2292 :tags '(:expensive-test)
2293 (skip-unless (tramp--test-enabled))
2294 (skip-unless (and (tramp--test-sh-p) (not (tramp--test-rsync-p))))
2295 (with-parsed-tramp-file-name tramp-test-temporary-file-directory nil
2296 (skip-unless (tramp-get-remote-perl v)))
2298 (let ((tramp-connection-properties
2299 (append
2300 `((,(regexp-quote (file-remote-p tramp-test-temporary-file-directory))
2301 "stat" nil)
2302 ;; See `tramp-sh-handle-file-truename'.
2303 (,(regexp-quote (file-remote-p tramp-test-temporary-file-directory))
2304 "readlink" nil))
2305 tramp-connection-properties)))
2306 (tramp--test-utf8)))
2308 (ert-deftest tramp-test34-utf8-with-ls ()
2309 "Check UTF8 encoding in file names and file contents.
2310 Use the `ls' command."
2311 :tags '(:expensive-test)
2312 (skip-unless (tramp--test-enabled))
2313 (skip-unless (and (tramp--test-sh-p) (not (tramp--test-rsync-p))))
2315 (let ((tramp-connection-properties
2316 (append
2317 `((,(regexp-quote (file-remote-p tramp-test-temporary-file-directory))
2318 "perl" nil)
2319 (,(regexp-quote (file-remote-p tramp-test-temporary-file-directory))
2320 "stat" nil)
2321 ;; See `tramp-sh-handle-file-truename'.
2322 (,(regexp-quote (file-remote-p tramp-test-temporary-file-directory))
2323 "readlink" nil))
2324 tramp-connection-properties)))
2325 (tramp--test-utf8)))
2327 ;; This test is inspired by Bug#16928.
2328 (ert-deftest tramp-test35-asynchronous-requests ()
2329 "Check parallel asynchronous requests.
2330 Such requests could arrive from timers, process filters and
2331 process sentinels. They shall not disturb each other."
2332 ;; Mark as failed until bug has been fixed.
2333 :expected-result :failed
2334 :tags '(:expensive-test)
2335 (skip-unless (tramp--test-enabled))
2336 (skip-unless (tramp--test-sh-p))
2338 ;; Keep instrumentation verbosity 0 until Tramp bug is fixed. This
2339 ;; has the side effect, that this test fails instead to abort. Good
2340 ;; for hydra.
2341 (tramp--instrument-test-case 0
2342 (let* ((tmp-name (tramp--test-make-temp-name))
2343 (default-directory tmp-name)
2344 (remote-file-name-inhibit-cache t)
2345 timer buffers kill-buffer-query-functions)
2347 (unwind-protect
2348 (progn
2349 (make-directory tmp-name)
2351 ;; Setup a timer in order to raise an ordinary command again
2352 ;; and again. `vc-registered' is well suited, because there
2353 ;; are many checks.
2354 (setq
2355 timer
2356 (run-at-time
2358 (lambda ()
2359 (when buffers
2360 (vc-registered
2361 (buffer-name (nth (random (length buffers)) buffers)))))))
2363 ;; Create temporary buffers. The number of buffers
2364 ;; corresponds to the number of processes; it could be
2365 ;; increased in order to make pressure on Tramp.
2366 (dotimes (i 5)
2367 (add-to-list 'buffers (generate-new-buffer "*temp*")))
2369 ;; Open asynchronous processes. Set process sentinel.
2370 (dolist (buf buffers)
2371 (async-shell-command "read line; touch $line; echo $line" buf)
2372 (set-process-sentinel
2373 (get-buffer-process buf)
2374 (lambda (proc _state)
2375 (delete-file (buffer-name (process-buffer proc))))))
2377 ;; Send a string. Use a random order of the buffers. Mix
2378 ;; with regular operation.
2379 (let ((buffers (copy-sequence buffers))
2380 buf)
2381 (while buffers
2382 (setq buf (nth (random (length buffers)) buffers))
2383 (process-send-string
2384 (get-buffer-process buf) (format "'%s'\n" buf))
2385 (file-attributes (buffer-name buf))
2386 (setq buffers (delq buf buffers))))
2388 ;; Wait until the whole output has been read.
2389 (with-timeout ((* 10 (length buffers))
2390 (ert-fail "`async-shell-command' timed out"))
2391 (let ((buffers (copy-sequence buffers))
2392 buf)
2393 (while buffers
2394 (setq buf (nth (random (length buffers)) buffers))
2395 (if (ignore-errors
2396 (memq (process-status (get-buffer-process buf))
2397 '(run open)))
2398 (accept-process-output (get-buffer-process buf) 0.1)
2399 (setq buffers (delq buf buffers))))))
2401 ;; Check.
2402 (dolist (buf buffers)
2403 (with-current-buffer buf
2404 (should
2405 (string-equal (format "'%s'\n" buf) (buffer-string)))))
2406 (should-not
2407 (directory-files tmp-name nil directory-files-no-dot-files-regexp)))
2409 ;; Cleanup.
2410 (ignore-errors (cancel-timer timer))
2411 (ignore-errors (delete-directory tmp-name 'recursive))
2412 (dolist (buf buffers)
2413 (ignore-errors (kill-buffer buf)))))))
2415 (ert-deftest tramp-test36-recursive-load ()
2416 "Check that Tramp does not fail due to recursive load."
2417 (skip-unless (tramp--test-enabled))
2419 (dolist (code
2420 (list
2421 (format
2422 "(expand-file-name %S)"
2423 tramp-test-temporary-file-directory)
2424 (format
2425 "(let ((default-directory %S)) (expand-file-name %S))"
2426 tramp-test-temporary-file-directory
2427 temporary-file-directory)))
2428 (should-not
2429 (string-match
2430 "Recursive load"
2431 (shell-command-to-string
2432 (format
2433 "%s -batch -Q -L %s --eval %s"
2434 (expand-file-name invocation-name invocation-directory)
2435 (mapconcat 'shell-quote-argument load-path " -L ")
2436 (shell-quote-argument code)))))))
2438 (ert-deftest tramp-test37-unload ()
2439 "Check that Tramp and its subpackages unload completely.
2440 Since it unloads Tramp, it shall be the last test to run."
2441 ;; Mark as failed until all symbols are unbound.
2442 :expected-result (if (featurep 'tramp) :failed :passed)
2443 :tags '(:expensive-test)
2444 (when (featurep 'tramp)
2445 (unload-feature 'tramp 'force)
2446 ;; No Tramp feature must be left.
2447 (should-not (featurep 'tramp))
2448 (should-not (all-completions "tramp" (delq 'tramp-tests features)))
2449 ;; `file-name-handler-alist' must be clean.
2450 (should-not (all-completions "tramp" (mapcar 'cdr file-name-handler-alist)))
2451 ;; There shouldn't be left a bound symbol. We do not regard our
2452 ;; test symbols, and the Tramp unload hooks.
2453 (mapatoms
2454 (lambda (x)
2455 (and (or (boundp x) (functionp x))
2456 (string-match "^tramp" (symbol-name x))
2457 (not (string-match "^tramp--?test" (symbol-name x)))
2458 (not (string-match "unload-hook$" (symbol-name x)))
2459 (ert-fail (format "`%s' still bound" x)))))
2460 ;; There shouldn't be left a hook function containing a Tramp
2461 ;; function. We do not regard the Tramp unload hooks.
2462 (mapatoms
2463 (lambda (x)
2464 (and (boundp x)
2465 (string-match "-hooks?$" (symbol-name x))
2466 (not (string-match "unload-hook$" (symbol-name x)))
2467 (consp (symbol-value x))
2468 (ignore-errors (all-completions "tramp" (symbol-value x)))
2469 (ert-fail (format "Hook `%s' still contains Tramp function" x)))))))
2471 ;; TODO:
2473 ;; * dired-compress-file
2474 ;; * dired-uncache
2475 ;; * file-acl
2476 ;; * file-selinux-context
2477 ;; * find-backup-file-name
2478 ;; * set-file-acl
2479 ;; * set-file-selinux-context
2481 ;; * Work on skipped tests. Make a comment, when it is impossible.
2482 ;; * Fix `tramp-test06-directory-file-name' for `ftp'.
2483 ;; * Fix `tramp-test27-start-file-process' on MS Windows (`process-send-eof'?).
2484 ;; * Fix Bug#16928. Set expected error of `tramp-test35-asynchronous-requests'.
2485 ;; * Fix `tramp-test37-unload' (Not all symbols are unbound). Set
2486 ;; expected error.
2488 (defun tramp-test-all (&optional interactive)
2489 "Run all tests for \\[tramp]."
2490 (interactive "p")
2491 (funcall
2492 (if interactive 'ert-run-tests-interactively 'ert-run-tests-batch) "^tramp"))
2494 (provide 'tramp-tests)
2495 ;;; tramp-tests.el ends here