1 ;;; tramp-tests.el --- Tests of remote file access
3 ;; Copyright (C) 2013-2014 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/'.
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'.
47 (declare-function tramp-find-executable "tramp-sh")
48 (declare-function tramp-get-remote-path "tramp-sh")
49 (defvar tramp-copy-size-limit)
50 (defvar tramp-remote-process-environment)
52 ;; There is no default value on w32 systems, which could work out of the box.
53 (defconst tramp-test-temporary-file-directory
55 ((getenv "REMOTE_TEMPORARY_FILE_DIRECTORY"))
56 ((eq system-type 'windows-nt) null-device)
60 (tramp-login-program "sh")
61 (tramp-login-args (("-i")))
62 (tramp-remote-shell "/bin/sh")
63 (tramp-remote-shell-args ("-c"))
64 (tramp-connection-timeout 10)))
65 (format "/mock::%s" temporary-file-directory)))
66 "Temporary directory for Tramp tests.")
68 (setq password-cache-expiry nil
70 tramp-copy-size-limit nil
71 tramp-message-show-message nil)
73 ;; This shall happen on hydra only.
74 (when (getenv "NIX_STORE")
75 (add-to-list 'tramp-remote-path 'tramp-own-remote-path))
77 (defvar tramp--test-enabled-checked nil
78 "Cached result of `tramp--test-enabled'.
79 If the function did run, the value is a cons cell, the `cdr'
82 (defun tramp--test-enabled ()
83 "Whether remote file access is enabled."
84 (unless (consp tramp--test-enabled-checked)
86 tramp--test-enabled-checked
90 (file-remote-p tramp-test-temporary-file-directory)
91 (file-directory-p tramp-test-temporary-file-directory)
92 (file-writable-p tramp-test-temporary-file-directory))))))
94 (when (cdr tramp--test-enabled-checked)
95 ;; Cleanup connection.
97 (tramp-cleanup-connection
98 (tramp-dissect-file-name tramp-test-temporary-file-directory)
102 (cdr tramp--test-enabled-checked))
104 (defun tramp--test-make-temp-name (&optional local)
105 "Create a temporary file name for test."
107 (make-temp-name "tramp-test")
108 (if local temporary-file-directory tramp-test-temporary-file-directory)))
110 (defmacro tramp--instrument-test-case (verbose &rest body)
111 "Run BODY with `tramp-verbose' equal VERBOSE.
112 Print the the content of the Tramp debug buffer, if BODY does not
113 eval properly in `should', `should-not' or `should-error'. BODY
114 shall not contain a timeout."
115 (declare (indent 1) (debug (natnump body)))
116 `(let ((tramp-verbose ,verbose)
117 (tramp-message-show-message t)
118 (tramp-debug-on-error t))
122 (signal (car err) (cdr err)))
124 (with-parsed-tramp-file-name tramp-test-temporary-file-directory nil
125 (with-current-buffer (tramp-get-connection-buffer v)
126 (message "%s" (buffer-string)))
127 (with-current-buffer (tramp-get-debug-buffer v)
128 (message "%s" (buffer-string))))
130 (signal (car err) (cdr err))))))
132 (ert-deftest tramp-test00-availability ()
133 "Test availability of Tramp functions."
134 :expected-result (if (tramp--test-enabled) :passed :failed)
135 (message "Remote directory: `%s'" tramp-test-temporary-file-directory)
136 (should (ignore-errors
138 (file-remote-p tramp-test-temporary-file-directory)
139 (file-directory-p tramp-test-temporary-file-directory)
140 (file-writable-p tramp-test-temporary-file-directory)))))
142 (ert-deftest tramp-test01-file-name-syntax ()
143 "Check remote file name syntax."
145 (should (tramp-tramp-file-p "/method::"))
146 (should (tramp-tramp-file-p "/host:"))
147 (should (tramp-tramp-file-p "/user@:"))
148 (should (tramp-tramp-file-p "/user@host:"))
149 (should (tramp-tramp-file-p "/method:host:"))
150 (should (tramp-tramp-file-p "/method:user@:"))
151 (should (tramp-tramp-file-p "/method:user@host:"))
152 (should (tramp-tramp-file-p "/method:user@email@host:"))
155 (should (tramp-tramp-file-p "/host#1234:"))
156 (should (tramp-tramp-file-p "/user@host#1234:"))
157 (should (tramp-tramp-file-p "/method:host#1234:"))
158 (should (tramp-tramp-file-p "/method:user@host#1234:"))
160 ;; Using an IPv4 address.
161 (should (tramp-tramp-file-p "/1.2.3.4:"))
162 (should (tramp-tramp-file-p "/user@1.2.3.4:"))
163 (should (tramp-tramp-file-p "/method:1.2.3.4:"))
164 (should (tramp-tramp-file-p "/method:user@1.2.3.4:"))
166 ;; Using an IPv6 address.
167 (should (tramp-tramp-file-p "/[]:"))
168 (should (tramp-tramp-file-p "/[::1]:"))
169 (should (tramp-tramp-file-p "/user@[::1]:"))
170 (should (tramp-tramp-file-p "/method:[::1]:"))
171 (should (tramp-tramp-file-p "/method:user@[::1]:"))
173 ;; Local file name part.
174 (should (tramp-tramp-file-p "/host:/:"))
175 (should (tramp-tramp-file-p "/method:::"))
176 (should (tramp-tramp-file-p "/method::/path/to/file"))
177 (should (tramp-tramp-file-p "/method::file"))
180 (should (tramp-tramp-file-p "/method1:|method2::"))
181 (should (tramp-tramp-file-p "/method1:host1|host2:"))
182 (should (tramp-tramp-file-p "/method1:host1|method2:host2:"))
183 (should (tramp-tramp-file-p "/method1:user1@host1|method2:user2@host2:"))
184 (should (tramp-tramp-file-p
185 "/method1:user1@host1|method2:user2@host2|method3:user3@host3:"))
188 (should-not (tramp-tramp-file-p nil))
189 (should-not (tramp-tramp-file-p 'symbol))
190 ;; "/:" suppresses file name handlers.
191 (should-not (tramp-tramp-file-p "/::"))
192 (should-not (tramp-tramp-file-p "/:@:"))
193 (should-not (tramp-tramp-file-p "/:[]:"))
194 ;; Multihops require a method.
195 (should-not (tramp-tramp-file-p "/host1|host2:"))
196 ;; Methods or hostnames shall be at least two characters on MS Windows.
197 (when (memq system-type '(cygwin windows-nt))
198 (should-not (tramp-tramp-file-p "/c:/path/to/file"))
199 (should-not (tramp-tramp-file-p "/c::/path/to/file"))))
201 (ert-deftest tramp-test02-file-name-dissect ()
202 "Check remote file name components."
203 (let ((tramp-default-method "default-method")
204 (tramp-default-user "default-user")
205 (tramp-default-host "default-host"))
206 ;; Expand `tramp-default-user' and `tramp-default-host'.
207 (should (string-equal
208 (file-remote-p "/method::")
209 (format "/%s:%s@%s:" "method" "default-user" "default-host")))
210 (should (string-equal (file-remote-p "/method::" 'method) "method"))
211 (should (string-equal (file-remote-p "/method::" 'user) "default-user"))
212 (should (string-equal (file-remote-p "/method::" 'host) "default-host"))
213 (should (string-equal (file-remote-p "/method::" 'localname) ""))
215 ;; Expand `tramp-default-method' and `tramp-default-user'.
216 (should (string-equal
217 (file-remote-p "/host:")
218 (format "/%s:%s@%s:" "default-method" "default-user" "host")))
219 (should (string-equal (file-remote-p "/host:" 'method) "default-method"))
220 (should (string-equal (file-remote-p "/host:" 'user) "default-user"))
221 (should (string-equal (file-remote-p "/host:" 'host) "host"))
222 (should (string-equal (file-remote-p "/host:" 'localname) ""))
224 ;; Expand `tramp-default-method' and `tramp-default-host'.
225 (should (string-equal
226 (file-remote-p "/user@:")
227 (format "/%s:%s@%s:" "default-method""user" "default-host")))
228 (should (string-equal (file-remote-p "/user@:" 'method) "default-method"))
229 (should (string-equal (file-remote-p "/user@:" 'user) "user"))
230 (should (string-equal (file-remote-p "/user@:" 'host) "default-host"))
231 (should (string-equal (file-remote-p "/user@:" 'localname) ""))
233 ;; Expand `tramp-default-method'.
234 (should (string-equal
235 (file-remote-p "/user@host:")
236 (format "/%s:%s@%s:" "default-method" "user" "host")))
237 (should (string-equal
238 (file-remote-p "/user@host:" 'method) "default-method"))
239 (should (string-equal (file-remote-p "/user@host:" 'user) "user"))
240 (should (string-equal (file-remote-p "/user@host:" 'host) "host"))
241 (should (string-equal (file-remote-p "/user@host:" 'localname) ""))
243 ;; Expand `tramp-default-user'.
244 (should (string-equal
245 (file-remote-p "/method:host:")
246 (format "/%s:%s@%s:" "method" "default-user" "host")))
247 (should (string-equal (file-remote-p "/method:host:" 'method) "method"))
248 (should (string-equal (file-remote-p "/method:host:" 'user) "default-user"))
249 (should (string-equal (file-remote-p "/method:host:" 'host) "host"))
250 (should (string-equal (file-remote-p "/method:host:" 'localname) ""))
252 ;; Expand `tramp-default-host'.
253 (should (string-equal
254 (file-remote-p "/method:user@:")
255 (format "/%s:%s@%s:" "method" "user" "default-host")))
256 (should (string-equal (file-remote-p "/method:user@:" 'method) "method"))
257 (should (string-equal (file-remote-p "/method:user@:" 'user) "user"))
258 (should (string-equal (file-remote-p "/method:user@:" 'host)
260 (should (string-equal (file-remote-p "/method:user@:" 'localname) ""))
263 (should (string-equal
264 (file-remote-p "/method:user@host:")
265 (format "/%s:%s@%s:" "method" "user" "host")))
266 (should (string-equal
267 (file-remote-p "/method:user@host:" 'method) "method"))
268 (should (string-equal (file-remote-p "/method:user@host:" 'user) "user"))
269 (should (string-equal (file-remote-p "/method:user@host:" 'host) "host"))
270 (should (string-equal (file-remote-p "/method:user@host:" 'localname) ""))
273 (should (string-equal
274 (file-remote-p "/method:user@email@host:")
275 (format "/%s:%s@%s:" "method" "user@email" "host")))
276 (should (string-equal
277 (file-remote-p "/method:user@email@host:" 'method) "method"))
278 (should (string-equal
279 (file-remote-p "/method:user@email@host:" 'user) "user@email"))
280 (should (string-equal
281 (file-remote-p "/method:user@email@host:" 'host) "host"))
282 (should (string-equal
283 (file-remote-p "/method:user@email@host:" 'localname) ""))
285 ;; Expand `tramp-default-method' and `tramp-default-user'.
286 (should (string-equal
287 (file-remote-p "/host#1234:")
288 (format "/%s:%s@%s:" "default-method" "default-user" "host#1234")))
289 (should (string-equal
290 (file-remote-p "/host#1234:" 'method) "default-method"))
291 (should (string-equal (file-remote-p "/host#1234:" 'user) "default-user"))
292 (should (string-equal (file-remote-p "/host#1234:" 'host) "host#1234"))
293 (should (string-equal (file-remote-p "/host#1234:" 'localname) ""))
295 ;; Expand `tramp-default-method'.
296 (should (string-equal
297 (file-remote-p "/user@host#1234:")
298 (format "/%s:%s@%s:" "default-method" "user" "host#1234")))
299 (should (string-equal
300 (file-remote-p "/user@host#1234:" 'method) "default-method"))
301 (should (string-equal (file-remote-p "/user@host#1234:" 'user) "user"))
302 (should (string-equal (file-remote-p "/user@host#1234:" 'host) "host#1234"))
303 (should (string-equal (file-remote-p "/user@host#1234:" 'localname) ""))
305 ;; Expand `tramp-default-user'.
306 (should (string-equal
307 (file-remote-p "/method:host#1234:")
308 (format "/%s:%s@%s:" "method" "default-user" "host#1234")))
309 (should (string-equal
310 (file-remote-p "/method:host#1234:" 'method) "method"))
311 (should (string-equal
312 (file-remote-p "/method:host#1234:" 'user) "default-user"))
313 (should (string-equal
314 (file-remote-p "/method:host#1234:" 'host) "host#1234"))
315 (should (string-equal (file-remote-p "/method:host#1234:" 'localname) ""))
318 (should (string-equal
319 (file-remote-p "/method:user@host#1234:")
320 (format "/%s:%s@%s:" "method" "user" "host#1234")))
321 (should (string-equal
322 (file-remote-p "/method:user@host#1234:" 'method) "method"))
323 (should (string-equal
324 (file-remote-p "/method:user@host#1234:" 'user) "user"))
325 (should (string-equal
326 (file-remote-p "/method:user@host#1234:" 'host) "host#1234"))
327 (should (string-equal
328 (file-remote-p "/method:user@host#1234:" 'localname) ""))
330 ;; Expand `tramp-default-method' and `tramp-default-user'.
331 (should (string-equal
332 (file-remote-p "/1.2.3.4:")
333 (format "/%s:%s@%s:" "default-method" "default-user" "1.2.3.4")))
334 (should (string-equal (file-remote-p "/1.2.3.4:" 'method) "default-method"))
335 (should (string-equal (file-remote-p "/1.2.3.4:" 'user) "default-user"))
336 (should (string-equal (file-remote-p "/1.2.3.4:" 'host) "1.2.3.4"))
337 (should (string-equal (file-remote-p "/1.2.3.4:" 'localname) ""))
339 ;; Expand `tramp-default-method'.
340 (should (string-equal
341 (file-remote-p "/user@1.2.3.4:")
342 (format "/%s:%s@%s:" "default-method" "user" "1.2.3.4")))
343 (should (string-equal
344 (file-remote-p "/user@1.2.3.4:" 'method) "default-method"))
345 (should (string-equal (file-remote-p "/user@1.2.3.4:" 'user) "user"))
346 (should (string-equal (file-remote-p "/user@1.2.3.4:" 'host) "1.2.3.4"))
347 (should (string-equal (file-remote-p "/user@1.2.3.4:" 'localname) ""))
349 ;; Expand `tramp-default-user'.
350 (should (string-equal
351 (file-remote-p "/method:1.2.3.4:")
352 (format "/%s:%s@%s:" "method" "default-user" "1.2.3.4")))
353 (should (string-equal (file-remote-p "/method:1.2.3.4:" 'method) "method"))
354 (should (string-equal
355 (file-remote-p "/method:1.2.3.4:" 'user) "default-user"))
356 (should (string-equal (file-remote-p "/method:1.2.3.4:" 'host) "1.2.3.4"))
357 (should (string-equal (file-remote-p "/method:1.2.3.4:" 'localname) ""))
360 (should (string-equal
361 (file-remote-p "/method:user@1.2.3.4:")
362 (format "/%s:%s@%s:" "method" "user" "1.2.3.4")))
363 (should (string-equal
364 (file-remote-p "/method:user@1.2.3.4:" 'method) "method"))
365 (should (string-equal (file-remote-p "/method:user@1.2.3.4:" 'user) "user"))
366 (should (string-equal
367 (file-remote-p "/method:user@1.2.3.4:" 'host) "1.2.3.4"))
368 (should (string-equal
369 (file-remote-p "/method:user@1.2.3.4:" 'localname) ""))
371 ;; Expand `tramp-default-method', `tramp-default-user' and
372 ;; `tramp-default-host'.
373 (should (string-equal
374 (file-remote-p "/[]:")
376 "/%s:%s@%s:" "default-method" "default-user" "default-host")))
377 (should (string-equal (file-remote-p "/[]:" 'method) "default-method"))
378 (should (string-equal (file-remote-p "/[]:" 'user) "default-user"))
379 (should (string-equal (file-remote-p "/[]:" 'host) "default-host"))
380 (should (string-equal (file-remote-p "/[]:" 'localname) ""))
382 ;; Expand `tramp-default-method' and `tramp-default-user'.
383 (let ((tramp-default-host "::1"))
384 (should (string-equal
385 (file-remote-p "/[]:")
386 (format "/%s:%s@%s:" "default-method" "default-user" "[::1]")))
387 (should (string-equal (file-remote-p "/[]:" 'method) "default-method"))
388 (should (string-equal (file-remote-p "/[]:" 'user) "default-user"))
389 (should (string-equal (file-remote-p "/[]:" 'host) "::1"))
390 (should (string-equal (file-remote-p "/[]:" 'localname) "")))
392 ;; Expand `tramp-default-method' and `tramp-default-user'.
393 (should (string-equal
394 (file-remote-p "/[::1]:")
395 (format "/%s:%s@%s:" "default-method" "default-user" "[::1]")))
396 (should (string-equal (file-remote-p "/[::1]:" 'method) "default-method"))
397 (should (string-equal (file-remote-p "/[::1]:" 'user) "default-user"))
398 (should (string-equal (file-remote-p "/[::1]:" 'host) "::1"))
399 (should (string-equal (file-remote-p "/[::1]:" 'localname) ""))
401 ;; Expand `tramp-default-method'.
402 (should (string-equal
403 (file-remote-p "/user@[::1]:")
404 (format "/%s:%s@%s:" "default-method" "user" "[::1]")))
405 (should (string-equal
406 (file-remote-p "/user@[::1]:" 'method) "default-method"))
407 (should (string-equal (file-remote-p "/user@[::1]:" 'user) "user"))
408 (should (string-equal (file-remote-p "/user@[::1]:" 'host) "::1"))
409 (should (string-equal (file-remote-p "/user@[::1]:" 'localname) ""))
411 ;; Expand `tramp-default-user'.
412 (should (string-equal
413 (file-remote-p "/method:[::1]:")
414 (format "/%s:%s@%s:" "method" "default-user" "[::1]")))
415 (should (string-equal (file-remote-p "/method:[::1]:" 'method) "method"))
416 (should (string-equal
417 (file-remote-p "/method:[::1]:" 'user) "default-user"))
418 (should (string-equal (file-remote-p "/method:[::1]:" 'host) "::1"))
419 (should (string-equal (file-remote-p "/method:[::1]:" 'localname) ""))
422 (should (string-equal
423 (file-remote-p "/method:user@[::1]:")
424 (format "/%s:%s@%s:" "method" "user" "[::1]")))
425 (should (string-equal
426 (file-remote-p "/method:user@[::1]:" 'method) "method"))
427 (should (string-equal (file-remote-p "/method:user@[::1]:" 'user) "user"))
428 (should (string-equal (file-remote-p "/method:user@[::1]:" 'host) "::1"))
429 (should (string-equal
430 (file-remote-p "/method:user@[::1]:" 'localname) ""))
432 ;; Local file name part.
433 (should (string-equal (file-remote-p "/host:/:" 'localname) "/:"))
434 (should (string-equal (file-remote-p "/method:::" 'localname) ":"))
435 (should (string-equal (file-remote-p "/method:: " 'localname) " "))
436 (should (string-equal (file-remote-p "/method::file" 'localname) "file"))
437 (should (string-equal
438 (file-remote-p "/method::/path/to/file" 'localname)
444 (file-remote-p "/method1:user1@host1|method2:user2@host2:/path/to/file")
445 (format "/%s:%s@%s:" "method2" "user2" "host2")))
449 "/method1:user1@host1|method2:user2@host2:/path/to/file" 'method)
454 "/method1:user1@host1|method2:user2@host2:/path/to/file" 'user)
459 "/method1:user1@host1|method2:user2@host2:/path/to/file" 'host)
464 "/method1:user1@host1|method2:user2@host2:/path/to/file" 'localname)
470 "/method1:user1@host1|method2:user2@host2|method3:user3@host3:/path/to/file")
471 (format "/%s:%s@%s:" "method3" "user3" "host3")))
475 "/method1:user1@host1|method2:user2@host2|method3:user3@host3:/path/to/file"
481 "/method1:user1@host1|method2:user2@host2|method3:user3@host3:/path/to/file"
487 "/method1:user1@host1|method2:user2@host2|method3:user3@host3:/path/to/file"
493 "/method1:user1@host1|method2:user2@host2|method3:user3@host3:/path/to/file"
497 (ert-deftest tramp-test03-file-name-defaults ()
498 "Check default values for some methods."
499 ;; Default values in tramp-adb.el.
500 (should (string-equal (file-remote-p "/adb::" 'host) ""))
501 ;; Default values in tramp-ftp.el.
502 (should (string-equal (file-remote-p "/ftp.host:" 'method) "ftp"))
503 (dolist (u '("ftp" "anonymous"))
504 (should (string-equal (file-remote-p (format "/%s@:" u) 'method) "ftp")))
505 ;; Default values in tramp-gvfs.el.
506 (when (and (load "tramp-gvfs" 'noerror 'nomessage)
507 (symbol-value 'tramp-gvfs-enabled))
508 (should (string-equal (file-remote-p "/synce::" 'user) nil)))
509 ;; Default values in tramp-gw.el.
510 (dolist (m '("tunnel" "socks"))
512 (string-equal (file-remote-p (format "/%s::" m) 'user) (user-login-name))))
513 ;; Default values in tramp-sh.el.
514 (dolist (h `("127.0.0.1" "[::1]" "localhost" "localhost6" ,(system-name)))
515 (should (string-equal (file-remote-p (format "/root@%s:" h) 'method) "su")))
516 (dolist (m '("su" "sudo" "ksu"))
517 (should (string-equal (file-remote-p (format "/%s::" m) 'user) "root")))
518 (dolist (m '("rcp" "remcp" "rsh" "telnet" "krlogin" "fcp"))
520 (string-equal (file-remote-p (format "/%s::" m) 'user) (user-login-name))))
521 ;; Default values in tramp-smb.el.
522 (should (string-equal (file-remote-p "/user%domain@host:" 'method) "smb"))
523 (should (string-equal (file-remote-p "/smb::" 'user) nil)))
525 (ert-deftest tramp-test04-substitute-in-file-name ()
526 "Check `substitute-in-file-name'."
527 (should (string-equal (substitute-in-file-name "/method:host://foo") "/foo"))
530 (substitute-in-file-name "/method:host:/path//foo") "/method:host:/foo"))
532 (string-equal (substitute-in-file-name "/method:host:/path///foo") "/foo"))
535 (substitute-in-file-name "/method:host:/path/~/foo") "/method:host:~/foo"))
537 (string-equal (substitute-in-file-name "/method:host:/path//~/foo") "~/foo"))
538 (let (process-environment)
541 (substitute-in-file-name "/method:host:/path/$FOO")
542 "/method:host:/path/$FOO"))
546 (substitute-in-file-name "/method:host:/path/$FOO")
547 "/method:host:/path/bla"))
550 (substitute-in-file-name "/method:host:/path/$$FOO")
551 "/method:host:/path/$FOO"))))
553 (ert-deftest tramp-test05-expand-file-name ()
554 "Check `expand-file-name'."
557 (expand-file-name "/method:host:/path/./file") "/method:host:/path/file"))
560 (expand-file-name "/method:host:/path/../file") "/method:host:/file")))
562 (ert-deftest tramp-test06-directory-file-name ()
563 "Check `directory-file-name'.
564 This checks also `file-name-as-directory', `file-name-directory'
565 and `file-name-nondirectory'."
568 (directory-file-name "/method:host:/path/to/file")
569 "/method:host:/path/to/file"))
572 (directory-file-name "/method:host:/path/to/file/")
573 "/method:host:/path/to/file"))
576 (file-name-as-directory "/method:host:/path/to/file")
577 "/method:host:/path/to/file/"))
580 (file-name-as-directory "/method:host:/path/to/file/")
581 "/method:host:/path/to/file/"))
584 (file-name-directory "/method:host:/path/to/file")
585 "/method:host:/path/to/"))
588 (file-name-directory "/method:host:/path/to/file/")
589 "/method:host:/path/to/file/"))
591 (string-equal (file-name-nondirectory "/method:host:/path/to/file") "file"))
593 (string-equal (file-name-nondirectory "/method:host:/path/to/file/") ""))
596 (unhandled-file-name-directory "/method:host:/path/to/file"))))
598 (ert-deftest tramp-test07-file-exists-p ()
599 "Check `file-exist-p', `write-region' and `delete-file'."
600 (skip-unless (tramp--test-enabled))
602 (let ((tmp-name (tramp--test-make-temp-name)))
603 (should-not (file-exists-p tmp-name))
604 (write-region "foo" nil tmp-name)
605 (should (file-exists-p tmp-name))
606 (delete-file tmp-name)
607 (should-not (file-exists-p tmp-name))))
609 (ert-deftest tramp-test08-file-local-copy ()
610 "Check `file-local-copy'."
611 (skip-unless (tramp--test-enabled))
613 (let ((tmp-name1 (tramp--test-make-temp-name))
617 (write-region "foo" nil tmp-name1)
618 (should (setq tmp-name2 (file-local-copy tmp-name1)))
620 (insert-file-contents tmp-name2)
621 (should (string-equal (buffer-string) "foo"))))
623 (delete-file tmp-name1)
624 (delete-file tmp-name2)))))
626 (ert-deftest tramp-test09-insert-file-contents ()
627 "Check `insert-file-contents'."
628 (skip-unless (tramp--test-enabled))
630 (let ((tmp-name (tramp--test-make-temp-name)))
633 (write-region "foo" nil tmp-name)
635 (insert-file-contents tmp-name)
636 (should (string-equal (buffer-string) "foo"))
637 (insert-file-contents tmp-name)
638 (should (string-equal (buffer-string) "foofoo"))
640 (insert-file-contents tmp-name nil 1 3)
641 (should (string-equal (buffer-string) "oofoofoo"))
643 (insert-file-contents tmp-name nil nil nil 'replace)
644 (should (string-equal (buffer-string) "foo"))))
645 (ignore-errors (delete-file tmp-name)))))
647 (ert-deftest tramp-test10-write-region ()
648 "Check `write-region'."
649 (skip-unless (tramp--test-enabled))
651 (let ((tmp-name (tramp--test-make-temp-name)))
656 (write-region nil nil tmp-name))
658 (insert-file-contents tmp-name)
659 (should (string-equal (buffer-string) "foo")))
663 (write-region nil nil tmp-name 'append))
665 (insert-file-contents tmp-name)
666 (should (string-equal (buffer-string) "foobla")))
668 (write-region "foo" nil tmp-name)
670 (insert-file-contents tmp-name)
671 (should (string-equal (buffer-string) "foo")))
675 (write-region 3 5 tmp-name))
677 (insert-file-contents tmp-name)
678 (should (string-equal (buffer-string) "34"))))
679 (ignore-errors (delete-file tmp-name)))))
681 (ert-deftest tramp-test11-copy-file ()
683 (skip-unless (tramp--test-enabled))
685 (let ((tmp-name1 (tramp--test-make-temp-name))
686 (tmp-name2 (tramp--test-make-temp-name))
687 (tmp-name3 (tramp--test-make-temp-name))
688 (tmp-name4 (tramp--test-make-temp-name 'local))
689 (tmp-name5 (tramp--test-make-temp-name 'local)))
691 ;; Copy on remote side.
694 (write-region "foo" nil tmp-name1)
695 (copy-file tmp-name1 tmp-name2)
696 (should (file-exists-p tmp-name2))
698 (insert-file-contents tmp-name2)
699 (should (string-equal (buffer-string) "foo")))
700 (should-error (copy-file tmp-name1 tmp-name2))
701 (copy-file tmp-name1 tmp-name2 'ok)
702 (make-directory tmp-name3)
703 (copy-file tmp-name1 tmp-name3)
706 (expand-file-name (file-name-nondirectory tmp-name1) tmp-name3))))
707 (ignore-errors (delete-file tmp-name1))
708 (ignore-errors (delete-file tmp-name2))
709 (ignore-errors (delete-directory tmp-name3 'recursive)))
711 ;; Copy from remote side to local side.
714 (write-region "foo" nil tmp-name1)
715 (copy-file tmp-name1 tmp-name4)
716 (should (file-exists-p tmp-name4))
718 (insert-file-contents tmp-name4)
719 (should (string-equal (buffer-string) "foo")))
720 (should-error (copy-file tmp-name1 tmp-name4))
721 (copy-file tmp-name1 tmp-name4 'ok)
722 (make-directory tmp-name5)
723 (copy-file tmp-name1 tmp-name5)
726 (expand-file-name (file-name-nondirectory tmp-name1) tmp-name5))))
727 (ignore-errors (delete-file tmp-name1))
728 (ignore-errors (delete-file tmp-name4))
729 (ignore-errors (delete-directory tmp-name5 'recursive)))
731 ;; Copy from local side to remote side.
734 (write-region "foo" nil tmp-name4 nil 'nomessage)
735 (copy-file tmp-name4 tmp-name1)
736 (should (file-exists-p tmp-name1))
738 (insert-file-contents tmp-name1)
739 (should (string-equal (buffer-string) "foo")))
740 (should-error (copy-file tmp-name4 tmp-name1))
741 (copy-file tmp-name4 tmp-name1 'ok)
742 (make-directory tmp-name3)
743 (copy-file tmp-name4 tmp-name3)
746 (expand-file-name (file-name-nondirectory tmp-name4) tmp-name3))))
747 (ignore-errors (delete-file tmp-name1))
748 (ignore-errors (delete-file tmp-name4))
749 (ignore-errors (delete-directory tmp-name3 'recursive)))))
751 (ert-deftest tramp-test12-rename-file ()
752 "Check `rename-file'."
753 (skip-unless (tramp--test-enabled))
755 (let ((tmp-name1 (tramp--test-make-temp-name))
756 (tmp-name2 (tramp--test-make-temp-name))
757 (tmp-name3 (tramp--test-make-temp-name))
758 (tmp-name4 (tramp--test-make-temp-name 'local))
759 (tmp-name5 (tramp--test-make-temp-name 'local)))
761 ;; Rename on remote side.
764 (write-region "foo" nil tmp-name1)
765 (rename-file tmp-name1 tmp-name2)
766 (should-not (file-exists-p tmp-name1))
767 (should (file-exists-p tmp-name2))
769 (insert-file-contents tmp-name2)
770 (should (string-equal (buffer-string) "foo")))
771 (write-region "foo" nil tmp-name1)
772 (should-error (rename-file tmp-name1 tmp-name2))
773 (rename-file tmp-name1 tmp-name2 'ok)
774 (should-not (file-exists-p tmp-name1))
775 (write-region "foo" nil tmp-name1)
776 (make-directory tmp-name3)
777 (rename-file tmp-name1 tmp-name3)
778 (should-not (file-exists-p tmp-name1))
781 (expand-file-name (file-name-nondirectory tmp-name1) tmp-name3))))
782 (ignore-errors (delete-file tmp-name1))
783 (ignore-errors (delete-file tmp-name2))
784 (ignore-errors (delete-directory tmp-name3 'recursive)))
786 ;; Rename from remote side to local side.
789 (write-region "foo" nil tmp-name1)
790 (rename-file tmp-name1 tmp-name4)
791 (should-not (file-exists-p tmp-name1))
792 (should (file-exists-p tmp-name4))
794 (insert-file-contents tmp-name4)
795 (should (string-equal (buffer-string) "foo")))
796 (write-region "foo" nil tmp-name1)
797 (should-error (rename-file tmp-name1 tmp-name4))
798 (rename-file tmp-name1 tmp-name4 'ok)
799 (should-not (file-exists-p tmp-name1))
800 (write-region "foo" nil tmp-name1)
801 (make-directory tmp-name5)
802 (rename-file tmp-name1 tmp-name5)
803 (should-not (file-exists-p tmp-name1))
806 (expand-file-name (file-name-nondirectory tmp-name1) tmp-name5))))
807 (ignore-errors (delete-file tmp-name1))
808 (ignore-errors (delete-file tmp-name4))
809 (ignore-errors (delete-directory tmp-name5 'recursive)))
811 ;; Rename from local side to remote side.
814 (write-region "foo" nil tmp-name4 nil 'nomessage)
815 (rename-file tmp-name4 tmp-name1)
816 (should-not (file-exists-p tmp-name4))
817 (should (file-exists-p tmp-name1))
819 (insert-file-contents tmp-name1)
820 (should (string-equal (buffer-string) "foo")))
821 (write-region "foo" nil tmp-name4 nil 'nomessage)
822 (should-error (rename-file tmp-name4 tmp-name1))
823 (rename-file tmp-name4 tmp-name1 'ok)
824 (should-not (file-exists-p tmp-name4))
825 (write-region "foo" nil tmp-name4 nil 'nomessage)
826 (make-directory tmp-name3)
827 (rename-file tmp-name4 tmp-name3)
828 (should-not (file-exists-p tmp-name4))
831 (expand-file-name (file-name-nondirectory tmp-name4) tmp-name3))))
832 (ignore-errors (delete-file tmp-name1))
833 (ignore-errors (delete-file tmp-name4))
834 (ignore-errors (delete-directory tmp-name3 'recursive)))))
836 (ert-deftest tramp-test13-make-directory ()
837 "Check `make-directory'.
838 This tests also `file-directory-p' and `file-accessible-directory-p'."
839 (skip-unless (tramp--test-enabled))
841 (let ((tmp-name (tramp--test-make-temp-name)))
844 (make-directory tmp-name)
845 (should (file-directory-p tmp-name))
846 (should (file-accessible-directory-p tmp-name)))
847 (ignore-errors (delete-directory tmp-name)))))
849 (ert-deftest tramp-test14-delete-directory ()
850 "Check `delete-directory'."
851 (skip-unless (tramp--test-enabled))
853 (let ((tmp-name (tramp--test-make-temp-name)))
854 ;; Delete empty directory.
855 (make-directory tmp-name)
856 (should (file-directory-p tmp-name))
857 (delete-directory tmp-name)
858 (should-not (file-directory-p tmp-name))
859 ;; Delete non-empty directory.
860 (make-directory tmp-name)
861 (write-region "foo" nil (expand-file-name "bla" tmp-name))
862 (should-error (delete-directory tmp-name) :type 'file-error)
863 (delete-directory tmp-name 'recursive)
864 (should-not (file-directory-p tmp-name))))
866 (ert-deftest tramp-test15-copy-directory ()
867 "Check `copy-directory'."
868 (skip-unless (tramp--test-enabled))
872 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory)
873 'tramp-smb-file-name-handler)))
875 (let* ((tmp-name1 (tramp--test-make-temp-name))
876 (tmp-name2 (tramp--test-make-temp-name))
877 (tmp-name3 (expand-file-name
878 (file-name-nondirectory tmp-name1) tmp-name2))
879 (tmp-name4 (expand-file-name "foo" tmp-name1))
880 (tmp-name5 (expand-file-name "foo" tmp-name2))
881 (tmp-name6 (expand-file-name "foo" tmp-name3)))
884 ;; Copy empty directory.
885 (make-directory tmp-name1)
886 (write-region "foo" nil tmp-name4)
887 (should (file-directory-p tmp-name1))
888 (should (file-exists-p tmp-name4))
889 (copy-directory tmp-name1 tmp-name2)
890 (should (file-directory-p tmp-name2))
891 (should (file-exists-p tmp-name5))
892 ;; Target directory does exist already.
893 (copy-directory tmp-name1 tmp-name2)
894 (should (file-directory-p tmp-name3))
895 (should (file-exists-p tmp-name6)))
897 (delete-directory tmp-name1 'recursive)
898 (delete-directory tmp-name2 'recursive)))))
900 (ert-deftest tramp-test16-directory-files ()
901 "Check `directory-files'."
902 (skip-unless (tramp--test-enabled))
904 (let* ((tmp-name1 (tramp--test-make-temp-name))
905 (tmp-name2 (expand-file-name "bla" tmp-name1))
906 (tmp-name3 (expand-file-name "foo" tmp-name1)))
909 (make-directory tmp-name1)
910 (write-region "foo" nil tmp-name2)
911 (write-region "bla" nil tmp-name3)
912 (should (file-directory-p tmp-name1))
913 (should (file-exists-p tmp-name2))
914 (should (file-exists-p tmp-name3))
915 (should (equal (directory-files tmp-name1) '("." ".." "bla" "foo")))
916 (should (equal (directory-files tmp-name1 'full)
917 `(,(concat tmp-name1 "/.")
918 ,(concat tmp-name1 "/..")
919 ,tmp-name2 ,tmp-name3)))
920 (should (equal (directory-files
921 tmp-name1 nil directory-files-no-dot-files-regexp)
923 (should (equal (directory-files
924 tmp-name1 'full directory-files-no-dot-files-regexp)
925 `(,tmp-name2 ,tmp-name3))))
926 (ignore-errors (delete-directory tmp-name1 'recursive)))))
928 (ert-deftest tramp-test17-insert-directory ()
929 "Check `insert-directory'."
930 (skip-unless (tramp--test-enabled))
932 (let* ((tmp-name1 (tramp--test-make-temp-name))
933 (tmp-name2 (expand-file-name "foo" tmp-name1)))
936 (make-directory tmp-name1)
937 (write-region "foo" nil tmp-name2)
938 (should (file-directory-p tmp-name1))
939 (should (file-exists-p tmp-name2))
941 (insert-directory tmp-name1 nil)
942 (goto-char (point-min))
943 (should (looking-at-p (regexp-quote tmp-name1))))
945 (insert-directory tmp-name1 "-al")
946 (goto-char (point-min))
947 (should (looking-at-p (format "^.+ %s$" (regexp-quote tmp-name1)))))
949 (insert-directory (file-name-as-directory tmp-name1) "-al")
950 (goto-char (point-min))
952 (looking-at-p (format "^.+ %s/$" (regexp-quote tmp-name1)))))
955 (file-name-as-directory tmp-name1) "-al" nil 'full-directory-p)
956 (goto-char (point-min))
959 "\\(total.+[[:digit:]]+\n\\)?.+ \\.\n.+ \\.\\.\n.+ foo$"))))
960 (ignore-errors (delete-directory tmp-name1 'recursive)))))
962 (ert-deftest tramp-test18-file-attributes ()
963 "Check `file-attributes'.
964 This tests also `file-readable-p' and `file-regular-p'."
965 (skip-unless (tramp--test-enabled))
967 (let ((tmp-name (tramp--test-make-temp-name))
971 (write-region "foo" nil tmp-name)
972 (should (file-exists-p tmp-name))
973 (setq attr (file-attributes tmp-name))
974 (should (consp attr))
975 (should (file-exists-p tmp-name))
976 (should (file-readable-p tmp-name))
977 (should (file-regular-p tmp-name))
978 ;; We do not test inodes and device numbers.
979 (should (null (car attr)))
980 (should (numberp (nth 1 attr))) ;; Link.
981 (should (numberp (nth 2 attr))) ;; Uid.
982 (should (numberp (nth 3 attr))) ;; Gid.
984 (should (stringp (current-time-string (nth 4 attr))))
985 ;; Last modification time.
986 (should (stringp (current-time-string (nth 5 attr))))
987 ;; Last status change time.
988 (should (stringp (current-time-string (nth 6 attr))))
989 (should (numberp (nth 7 attr))) ;; Size.
990 (should (stringp (nth 8 attr))) ;; Modes.
992 (setq attr (file-attributes tmp-name 'string))
993 (should (stringp (nth 2 attr))) ;; Uid.
994 (should (stringp (nth 3 attr))) ;; Gid.
995 (delete-file tmp-name)
997 (make-directory tmp-name)
998 (should (file-exists-p tmp-name))
999 (should (file-readable-p tmp-name))
1000 (should-not (file-regular-p tmp-name))
1001 (setq attr (file-attributes tmp-name))
1002 (should (eq (car attr) t)))
1003 (ignore-errors (delete-directory tmp-name)))))
1005 (ert-deftest tramp-test19-directory-files-and-attributes ()
1006 "Check `directory-files-and-attributes'."
1007 (skip-unless (tramp--test-enabled))
1009 ;; `directory-files-and-attributes' contains also values for "../".
1010 ;; Ensure that this doesn't change during tests, for
1011 ;; example due to handling temporary files.
1012 (let* ((tmp-name1 (tramp--test-make-temp-name))
1013 (tmp-name2 (expand-file-name "bla" tmp-name1))
1017 (make-directory tmp-name1)
1018 (should (file-directory-p tmp-name1))
1019 (make-directory tmp-name2)
1020 (should (file-directory-p tmp-name2))
1021 (write-region "foo" nil (expand-file-name "foo" tmp-name2))
1022 (write-region "bar" nil (expand-file-name "bar" tmp-name2))
1023 (write-region "boz" nil (expand-file-name "boz" tmp-name2))
1024 (setq attr (directory-files-and-attributes tmp-name2))
1025 (should (consp attr))
1026 ;; Dumb remote shells without perl(1) or stat(1) are not
1027 ;; able to return the date correctly. They say "don't know".
1032 (file-attributes (expand-file-name (car elt) tmp-name2)))
1035 (equal (file-attributes (expand-file-name (car elt) tmp-name2))
1037 (setq attr (directory-files-and-attributes tmp-name2 'full))
1039 (unless (equal (nth 5 (file-attributes (car elt))) '(0 0))
1041 (equal (file-attributes (car elt)) (cdr elt)))))
1042 (setq attr (directory-files-and-attributes tmp-name2 nil "^b"))
1043 (should (equal (mapcar 'car attr) '("bar" "boz"))))
1044 (ignore-errors (delete-directory tmp-name1 'recursive)))))
1046 (ert-deftest tramp-test20-file-modes ()
1047 "Check `file-modes'.
1048 This tests also `file-executable-p', `file-writable-p' and `set-file-modes'."
1049 (skip-unless (tramp--test-enabled))
1053 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory)
1054 '(tramp-adb-file-name-handler
1055 tramp-gvfs-file-name-handler
1056 tramp-smb-file-name-handler))))
1058 (let ((tmp-name (tramp--test-make-temp-name)))
1061 (write-region "foo" nil tmp-name)
1062 (should (file-exists-p tmp-name))
1063 (set-file-modes tmp-name #o777)
1064 (should (= (file-modes tmp-name) #o777))
1065 (should (file-executable-p tmp-name))
1066 (should (file-writable-p tmp-name))
1067 (set-file-modes tmp-name #o444)
1068 (should (= (file-modes tmp-name) #o444))
1069 (should-not (file-executable-p tmp-name))
1070 ;; A file is always writable for user "root".
1071 (unless (zerop (nth 2 (file-attributes tmp-name)))
1072 (should-not (file-writable-p tmp-name))))
1073 (ignore-errors (delete-file tmp-name)))))
1075 (ert-deftest tramp-test21-file-links ()
1076 "Check `file-symlink-p'.
1077 This tests also `make-symbolic-link', `file-truename' and `add-name-to-file'."
1078 (skip-unless (tramp--test-enabled))
1080 ;; We must use `file-truename' for the temporary directory, because
1081 ;; it could be located on a symlinked directory. This would let the
1083 (let* ((tramp-test-temporary-file-directory
1084 (file-truename tramp-test-temporary-file-directory))
1085 (tmp-name1 (tramp--test-make-temp-name))
1086 (tmp-name2 (tramp--test-make-temp-name))
1087 (tmp-name3 (tramp--test-make-temp-name 'local)))
1090 (write-region "foo" nil tmp-name1)
1091 (should (file-exists-p tmp-name1))
1092 ;; Method "smb" supports `make-symbolic-link' only if the
1093 ;; remote host has CIFS capabilities. tramp-adb.el and
1094 ;; tramp-gvfs.el do not support symbolic links at all.
1096 (make-symbolic-link tmp-name1 tmp-name2)
1099 (not (string-equal (error-message-string err)
1100 "make-symbolic-link not supported")))))
1101 (should (file-symlink-p tmp-name2))
1102 (should-error (make-symbolic-link tmp-name1 tmp-name2))
1103 (make-symbolic-link tmp-name1 tmp-name2 'ok-if-already-exists)
1104 (should (file-symlink-p tmp-name2))
1105 ;; `tmp-name3' is a local file name.
1106 (should-error (make-symbolic-link tmp-name1 tmp-name3)))
1108 (delete-file tmp-name1)
1109 (delete-file tmp-name2)))
1113 (write-region "foo" nil tmp-name1)
1114 (should (file-exists-p tmp-name1))
1115 (add-name-to-file tmp-name1 tmp-name2)
1116 (should-not (file-symlink-p tmp-name2))
1117 (should-error (add-name-to-file tmp-name1 tmp-name2))
1118 (add-name-to-file tmp-name1 tmp-name2 'ok-if-already-exists)
1119 (should-not (file-symlink-p tmp-name2))
1120 ;; `tmp-name3' is a local file name.
1121 (should-error (add-name-to-file tmp-name1 tmp-name3)))
1123 (delete-file tmp-name1)
1124 (delete-file tmp-name2)))
1128 (write-region "foo" nil tmp-name1)
1129 (should (file-exists-p tmp-name1))
1130 (make-symbolic-link tmp-name1 tmp-name2)
1131 (should (file-symlink-p tmp-name2))
1132 (should-not (string-equal tmp-name2 (file-truename tmp-name2)))
1134 (string-equal (file-truename tmp-name1) (file-truename tmp-name2)))
1135 (should (file-equal-p tmp-name1 tmp-name2)))
1137 (delete-file tmp-name1)
1138 (delete-file tmp-name2)))
1140 ;; `file-truename' shall preserve trailing link of directories.
1141 (unless (file-symlink-p tramp-test-temporary-file-directory)
1142 (let* ((dir1 (directory-file-name tramp-test-temporary-file-directory))
1143 (dir2 (file-name-as-directory dir1)))
1144 (should (string-equal (file-truename dir1) (expand-file-name dir1)))
1145 (should (string-equal (file-truename dir2) (expand-file-name dir2)))))))
1147 (ert-deftest tramp-test22-file-times ()
1148 "Check `set-file-times' and `file-newer-than-file-p'."
1149 (skip-unless (tramp--test-enabled))
1153 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory)
1154 '(tramp-gvfs-file-name-handler tramp-smb-file-name-handler))))
1156 (let ((tmp-name1 (tramp--test-make-temp-name))
1157 (tmp-name2 (tramp--test-make-temp-name))
1158 (tmp-name3 (tramp--test-make-temp-name)))
1161 (write-region "foo" nil tmp-name1)
1162 (should (file-exists-p tmp-name1))
1163 (should (consp (nth 5 (file-attributes tmp-name1))))
1164 ;; '(0 0) means don't know, and will be replaced by
1165 ;; `current-time'. Therefore, we use '(0 1).
1166 ;; We skip the test, if the remote handler is not able to
1167 ;; set the correct time.
1168 (skip-unless (set-file-times tmp-name1 '(0 1)))
1169 ;; Dumb remote shells without perl(1) or stat(1) are not
1170 ;; able to return the date correctly. They say "don't know".
1171 (unless (equal (nth 5 (file-attributes tmp-name1)) '(0 0))
1172 (should (equal (nth 5 (file-attributes tmp-name1)) '(0 1)))
1173 (write-region "bla" nil tmp-name2)
1174 (should (file-exists-p tmp-name2))
1175 (should (file-newer-than-file-p tmp-name2 tmp-name1))
1176 ;; `tmp-name3' does not exist.
1177 (should (file-newer-than-file-p tmp-name2 tmp-name3))
1178 (should-not (file-newer-than-file-p tmp-name3 tmp-name1))))
1180 (delete-file tmp-name1)
1181 (delete-file tmp-name2)))))
1183 (ert-deftest tramp-test23-visited-file-modtime ()
1184 "Check `set-visited-file-modtime' and `verify-visited-file-modtime'."
1185 (skip-unless (tramp--test-enabled))
1187 (let ((tmp-name (tramp--test-make-temp-name)))
1190 (write-region "foo" nil tmp-name)
1191 (should (file-exists-p tmp-name))
1193 (insert-file-contents tmp-name)
1194 (should (verify-visited-file-modtime))
1195 (set-visited-file-modtime '(0 1))
1196 (should (verify-visited-file-modtime))
1197 (should (equal (visited-file-modtime) '(0 1 0 0)))))
1198 (ignore-errors (delete-file tmp-name)))))
1200 (ert-deftest tramp-test24-file-name-completion ()
1201 "Check `file-name-completion' and `file-name-all-completions'."
1202 (skip-unless (tramp--test-enabled))
1204 (let ((tmp-name (tramp--test-make-temp-name)))
1207 (make-directory tmp-name)
1208 (should (file-directory-p tmp-name))
1209 (write-region "foo" nil (expand-file-name "foo" tmp-name))
1210 (write-region "bar" nil (expand-file-name "bold" tmp-name))
1211 (make-directory (expand-file-name "boz" tmp-name))
1212 (should (equal (file-name-completion "fo" tmp-name) "foo"))
1213 (should (equal (file-name-completion "b" tmp-name) "bo"))
1215 (equal (file-name-completion "b" tmp-name 'file-directory-p) "boz/"))
1216 (should (equal (file-name-all-completions "fo" tmp-name) '("foo")))
1218 (equal (sort (file-name-all-completions "b" tmp-name) 'string-lessp)
1220 (ignore-errors (delete-directory tmp-name 'recursive)))))
1222 (ert-deftest tramp-test25-load ()
1224 (skip-unless (tramp--test-enabled))
1226 (let ((tmp-name (tramp--test-make-temp-name)))
1229 (load tmp-name 'noerror 'nomessage)
1230 (should-not (featurep 'tramp-test-load))
1231 (write-region "(provide 'tramp-test-load)" nil tmp-name)
1232 ;; `load' in lread.c does not pass `must-suffix'. Why?
1233 ;(should-error (load tmp-name nil 'nomessage 'nosuffix 'must-suffix))
1234 (load tmp-name nil 'nomessage 'nosuffix)
1235 (should (featurep 'tramp-test-load)))
1237 (and (featurep 'tramp-test-load) (unload-feature 'tramp-test-load))
1238 (delete-file tmp-name)))))
1240 (ert-deftest tramp-test26-process-file ()
1241 "Check `process-file'."
1242 (skip-unless (tramp--test-enabled))
1246 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory)
1247 '(tramp-gvfs-file-name-handler tramp-smb-file-name-handler))))
1249 (let* ((tmp-name (tramp--test-make-temp-name))
1250 (fnnd (file-name-nondirectory tmp-name))
1251 (default-directory tramp-test-temporary-file-directory)
1252 kill-buffer-query-functions)
1255 ;; We cannot use "/bin/true" and "/bin/false"; those paths
1256 ;; do not exist on hydra.
1257 (should (zerop (process-file "true")))
1258 (should-not (zerop (process-file "false")))
1259 (should-not (zerop (process-file "binary-does-not-exist")))
1261 (write-region "foo" nil tmp-name)
1262 (should (file-exists-p tmp-name))
1263 (should (zerop (process-file "ls" nil t nil fnnd)))
1264 ;; `ls' could produce colorized output.
1265 (goto-char (point-min))
1266 (while (re-search-forward tramp-color-escape-sequence-regexp nil t)
1267 (replace-match "" nil nil))
1268 (should (string-equal (format "%s\n" fnnd) (buffer-string)))
1269 (should-not (get-buffer-window (current-buffer) t))
1271 ;; Second run. The output must be appended.
1272 (should (zerop (process-file "ls" nil t t fnnd)))
1273 ;; `ls' could produce colorized output.
1274 (goto-char (point-min))
1275 (while (re-search-forward tramp-color-escape-sequence-regexp nil t)
1276 (replace-match "" nil nil))
1278 (string-equal (format "%s\n%s\n" fnnd fnnd) (buffer-string)))
1279 ;; A non-nil DISPLAY must not raise the buffer.
1280 (should-not (get-buffer-window (current-buffer) t))))
1282 (ignore-errors (delete-file tmp-name)))))
1284 (ert-deftest tramp-test27-start-file-process ()
1285 "Check `start-file-process'."
1286 (skip-unless (tramp--test-enabled))
1290 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory)
1291 '(tramp-adb-file-name-handler
1292 tramp-gvfs-file-name-handler
1293 tramp-smb-file-name-handler))))
1295 (let ((default-directory tramp-test-temporary-file-directory)
1296 (tmp-name (tramp--test-make-temp-name))
1297 kill-buffer-query-functions proc)
1300 (setq proc (start-file-process "test1" (current-buffer) "cat"))
1301 (should (processp proc))
1302 (should (equal (process-status proc) 'run))
1303 (process-send-string proc "foo")
1304 (process-send-eof proc)
1306 (with-timeout (10 (ert-fail "`start-file-process' timed out"))
1307 (while (< (- (point-max) (point-min)) (length "foo"))
1308 (accept-process-output proc 1)))
1309 (should (string-equal (buffer-string) "foo")))
1310 (ignore-errors (delete-process proc)))
1314 (write-region "foo" nil tmp-name)
1315 (should (file-exists-p tmp-name))
1318 "test2" (current-buffer)
1319 "cat" (file-name-nondirectory tmp-name)))
1320 (should (processp proc))
1322 (with-timeout (10 (ert-fail "`start-file-process' timed out"))
1323 (while (< (- (point-max) (point-min)) (length "foo"))
1324 (accept-process-output proc 1)))
1325 (should (string-equal (buffer-string) "foo")))
1327 (delete-process proc)
1328 (delete-file tmp-name)))
1332 (setq proc (start-file-process "test3" (current-buffer) "cat"))
1333 (should (processp proc))
1334 (should (equal (process-status proc) 'run))
1337 (lambda (p s) (with-current-buffer (process-buffer p) (insert s))))
1338 (process-send-string proc "foo")
1339 (process-send-eof proc)
1341 (with-timeout (10 (ert-fail "`start-file-process' timed out"))
1342 (while (< (- (point-max) (point-min)) (length "foo"))
1343 (accept-process-output proc 1)))
1344 (should (string-equal (buffer-string) "foo")))
1345 (ignore-errors (delete-process proc)))))
1347 (ert-deftest tramp-test28-shell-command ()
1348 "Check `shell-command'."
1349 (skip-unless (tramp--test-enabled))
1353 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory)
1354 '(tramp-adb-file-name-handler
1355 tramp-gvfs-file-name-handler
1356 tramp-smb-file-name-handler))))
1358 (let ((tmp-name (tramp--test-make-temp-name))
1359 (default-directory tramp-test-temporary-file-directory)
1360 kill-buffer-query-functions)
1363 (write-region "foo" nil tmp-name)
1364 (should (file-exists-p tmp-name))
1366 (format "ls %s" (file-name-nondirectory tmp-name)) (current-buffer))
1367 ;; `ls' could produce colorized output.
1368 (goto-char (point-min))
1369 (while (re-search-forward tramp-color-escape-sequence-regexp nil t)
1370 (replace-match "" nil nil))
1373 (format "%s\n" (file-name-nondirectory tmp-name)) (buffer-string))))
1374 (ignore-errors (delete-file tmp-name)))
1378 (write-region "foo" nil tmp-name)
1379 (should (file-exists-p tmp-name))
1380 (async-shell-command
1381 (format "ls %s" (file-name-nondirectory tmp-name)) (current-buffer))
1382 (set-process-sentinel (get-buffer-process (current-buffer)) nil)
1384 (with-timeout (10 (ert-fail "`async-shell-command' timed out"))
1385 (while (< (- (point-max) (point-min))
1386 (1+ (length (file-name-nondirectory tmp-name))))
1387 (accept-process-output (get-buffer-process (current-buffer)) 1)))
1388 ;; `ls' could produce colorized output.
1389 (goto-char (point-min))
1390 (while (re-search-forward tramp-color-escape-sequence-regexp nil t)
1391 (replace-match "" nil nil))
1392 ;; There might be a nasty "Process *Async Shell* finished" message.
1393 (goto-char (point-min))
1395 (narrow-to-region (point-min) (point))
1398 (format "%s\n" (file-name-nondirectory tmp-name)) (buffer-string))))
1399 (ignore-errors (delete-file tmp-name)))
1403 (write-region "foo" nil tmp-name)
1404 (should (file-exists-p tmp-name))
1405 (async-shell-command "read line; ls $line" (current-buffer))
1406 (set-process-sentinel (get-buffer-process (current-buffer)) nil)
1407 (process-send-string
1408 (get-buffer-process (current-buffer))
1409 (format "%s\n" (file-name-nondirectory tmp-name)))
1411 (with-timeout (10 (ert-fail "`async-shell-command' timed out"))
1412 (while (< (- (point-max) (point-min))
1413 (1+ (length (file-name-nondirectory tmp-name))))
1414 (accept-process-output (get-buffer-process (current-buffer)) 1)))
1415 ;; `ls' could produce colorized output.
1416 (goto-char (point-min))
1417 (while (re-search-forward tramp-color-escape-sequence-regexp nil t)
1418 (replace-match "" nil nil))
1419 ;; There might be a nasty "Process *Async Shell* finished" message.
1420 (goto-char (point-min))
1422 (narrow-to-region (point-min) (point))
1425 (format "%s\n" (file-name-nondirectory tmp-name)) (buffer-string))))
1426 (ignore-errors (delete-file tmp-name)))))
1428 (ert-deftest tramp-test29-vc-registered ()
1429 "Check `vc-registered'."
1430 (skip-unless (tramp--test-enabled))
1433 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory)
1434 'tramp-sh-file-name-handler))
1436 (let* ((default-directory tramp-test-temporary-file-directory)
1437 (tmp-name1 (tramp--test-make-temp-name))
1438 (tmp-name2 (expand-file-name "foo" tmp-name1))
1439 (tramp-remote-process-environment tramp-remote-process-environment)
1440 (vc-handled-backends
1441 (with-parsed-tramp-file-name tramp-test-temporary-file-directory nil
1443 ((tramp-find-executable v vc-bzr-program (tramp-get-remote-path v))
1444 (setq tramp-remote-process-environment
1445 (cons (format "BZR_HOME=%s"
1446 (file-remote-p tmp-name1 'localname))
1447 tramp-remote-process-environment))
1448 ;; We must force a reconnect, in order to activate $BZR_HOME.
1449 (tramp-cleanup-connection
1450 (tramp-dissect-file-name tramp-test-temporary-file-directory)
1453 ((tramp-find-executable v vc-git-program (tramp-get-remote-path v))
1455 ((tramp-find-executable v vc-hg-program (tramp-get-remote-path v))
1458 (skip-unless vc-handled-backends)
1459 (message "%s" vc-handled-backends)
1463 (make-directory tmp-name1)
1464 (write-region "foo" nil tmp-name2)
1465 (should (file-directory-p tmp-name1))
1466 (should (file-exists-p tmp-name2))
1467 (should-not (vc-registered tmp-name1))
1468 (should-not (vc-registered tmp-name2))
1470 (let ((default-directory tmp-name1))
1471 ;; Create empty repository, and register the file.
1472 (vc-create-repo (car vc-handled-backends))
1473 ;; The structure of VC-FILESET is not documented. Let's
1474 ;; hope it won't change.
1476 nil (list (car vc-handled-backends)
1477 (list (file-name-nondirectory tmp-name2)))))
1478 (should (vc-registered tmp-name2)))
1480 (ignore-errors (delete-directory tmp-name1 'recursive)))))
1482 (defun tramp--test-check-files (&rest files)
1483 "Runs a simple but comprehensive test over every file in FILES."
1484 (let ((tmp-name1 (tramp--test-make-temp-name))
1485 (tmp-name2 (tramp--test-make-temp-name 'local)))
1488 (make-directory tmp-name1)
1489 (make-directory tmp-name2)
1491 (let ((file1 (expand-file-name elt tmp-name1))
1492 (file2 (expand-file-name elt tmp-name2)))
1493 (write-region elt nil file1)
1494 (should (file-exists-p file1))
1495 ;; Check file contents.
1497 (insert-file-contents file1)
1498 (should (string-equal (buffer-string) elt)))
1499 ;; Copy file both directions.
1500 (copy-file file1 tmp-name2)
1501 (should (file-exists-p file2))
1503 (should-not (file-exists-p file1))
1504 (copy-file file2 tmp-name1)
1505 (should (file-exists-p file1))))
1507 ;; Check file names.
1508 (should (equal (directory-files
1509 tmp-name1 nil directory-files-no-dot-files-regexp)
1510 (sort (copy-sequence files) 'string-lessp)))
1511 (should (equal (directory-files
1512 tmp-name2 nil directory-files-no-dot-files-regexp)
1513 (sort (copy-sequence files) 'string-lessp)))
1515 ;; `substitute-in-file-name' could return different values.
1516 ;; For `adb', there could be strange file permissions
1517 ;; preventing overwriting a file. We don't care in this
1521 (substitute-in-file-name (expand-file-name elt tmp-name1)))
1523 (substitute-in-file-name (expand-file-name elt tmp-name2))))
1524 (ignore-errors (write-region elt nil file1))
1525 (should (file-exists-p file1))
1526 (ignore-errors (write-region elt nil file2 nil 'nomessage))
1527 (should (file-exists-p file2))))
1529 (should (equal (directory-files
1530 tmp-name1 nil directory-files-no-dot-files-regexp)
1532 tmp-name2 nil directory-files-no-dot-files-regexp))))
1534 (ignore-errors (delete-directory tmp-name1 'recursive))
1535 (ignore-errors (delete-directory tmp-name2 'recursive)))))
1537 ;; This test is inspired by Bug#17238.
1538 (ert-deftest tramp-test30-special-characters ()
1539 "Check special characters in file names."
1540 (skip-unless (tramp--test-enabled))
1544 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory)
1545 '(tramp-adb-file-name-handler
1546 tramp-gvfs-file-name-handler
1547 tramp-smb-file-name-handler))))
1549 ;; Newlines, slashes and backslashes in file names are not supported.
1550 ;; So we don't test.
1551 (tramp--test-check-files
1568 (ert-deftest tramp-test31-utf8 ()
1569 "Check UTF8 encoding in file names and file contents."
1570 (skip-unless (tramp--test-enabled))
1572 (let ((coding-system-for-read 'utf-8)
1573 (coding-system-for-write 'utf-8)
1574 (file-name-coding-system 'utf-8))
1575 (tramp--test-check-files
1576 "أصبح بوسعك الآن تنزيل نسخة كاملة من موسوعة ويكيبيديا العربية لتصفحها بلا اتصال بالإنترنت"
1578 "Автостопом по гала́ктике")))
1580 ;; This test is inspired by Bug#16928.
1581 (ert-deftest tramp-test32-asynchronous-requests ()
1582 "Check parallel asynchronous requests.
1583 Such requests could arrive from timers, process filters and
1584 process sentinels. They shall not disturb each other."
1585 ;; Mark as failed until bug has been fixed.
1586 :expected-result :failed
1587 (skip-unless (tramp--test-enabled))
1590 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory)
1591 'tramp-sh-file-name-handler))
1593 ;; Keep instrumentation verbosity 0 until Tramp bug is fixed. This
1594 ;; has the side effect, that this test fails instead to abort. Good
1596 (tramp--instrument-test-case 0
1597 (let* ((tmp-name (tramp--test-make-temp-name))
1598 (default-directory tmp-name)
1599 (remote-file-name-inhibit-cache t)
1600 timer buffers kill-buffer-query-functions)
1604 (make-directory tmp-name)
1606 ;; Setup a timer in order to raise an ordinary command again
1607 ;; and again. `vc-registered' is well suited, because there
1616 (buffer-name (nth (random (length buffers)) buffers)))))))
1618 ;; Create temporary buffers. The number of buffers
1619 ;; corresponds to the number of processes; it could be
1620 ;; increased in order to make pressure on Tramp.
1622 (add-to-list 'buffers (generate-new-buffer "*temp*")))
1624 ;; Open asynchronous processes. Set process sentinel.
1625 (dolist (buf buffers)
1626 (async-shell-command "read line; touch $line; echo $line" buf)
1627 (set-process-sentinel
1628 (get-buffer-process buf)
1629 (lambda (proc _state)
1630 (delete-file (buffer-name (process-buffer proc))))))
1632 ;; Send a string. Use a random order of the buffers. Mix
1633 ;; with regular operation.
1634 (let ((buffers (copy-sequence buffers))
1637 (setq buf (nth (random (length buffers)) buffers))
1638 (process-send-string
1639 (get-buffer-process buf) (format "'%s'\n" buf))
1640 (file-attributes (buffer-name buf))
1641 (setq buffers (delq buf buffers))))
1643 ;; Wait until the whole output has been read.
1644 (with-timeout ((* 10 (length buffers))
1645 (ert-fail "`async-shell-command' timed out"))
1646 (let ((buffers (copy-sequence buffers))
1649 (setq buf (nth (random (length buffers)) buffers))
1651 (memq (process-status (get-buffer-process buf))
1653 (accept-process-output (get-buffer-process buf) 0.1)
1654 (setq buffers (delq buf buffers))))))
1657 (dolist (buf buffers)
1658 (with-current-buffer buf
1660 (string-equal (format "'%s'\n" buf) (buffer-string)))))
1662 (directory-files tmp-name nil directory-files-no-dot-files-regexp)))
1665 (ignore-errors (cancel-timer timer))
1666 (ignore-errors (delete-directory tmp-name 'recursive))
1667 (dolist (buf buffers)
1668 (ignore-errors (kill-buffer buf)))))))
1670 (ert-deftest tramp-test33-recursive-load ()
1671 "Check that Tramp does not fail due to recursive load."
1672 (skip-unless (tramp--test-enabled))
1677 "(expand-file-name %S)"
1678 tramp-test-temporary-file-directory)
1680 "(let ((default-directory %S)) (expand-file-name %S))"
1681 tramp-test-temporary-file-directory
1682 temporary-file-directory)))
1686 (shell-command-to-string
1688 "%s -batch -Q -L %s --eval %s"
1689 (expand-file-name invocation-name invocation-directory)
1690 (mapconcat 'shell-quote-argument load-path " -L ")
1691 (shell-quote-argument code)))))))
1693 (ert-deftest tramp-test34-unload ()
1694 "Check that Tramp and its subpackages unload completely.
1695 Since it unloads Tramp, it shall be the last test to run."
1696 ;; Mark as failed until all symbols are unbound.
1697 :expected-result (if (featurep 'tramp) :failed :passed)
1698 (when (featurep 'tramp)
1699 (unload-feature 'tramp 'force)
1700 ;; No Tramp feature must be left.
1701 (should-not (featurep 'tramp))
1702 (should-not (all-completions "tramp" (delq 'tramp-tests features)))
1703 ;; `file-name-handler-alist' must be clean.
1704 (should-not (all-completions "tramp" (mapcar 'cdr file-name-handler-alist)))
1705 ;; There shouldn't be left a bound symbol. We do not regard our
1706 ;; test symbols, and the Tramp unload hooks.
1709 (and (or (boundp x) (functionp x))
1710 (string-match "^tramp" (symbol-name x))
1711 (not (string-match "^tramp--?test" (symbol-name x)))
1712 (not (string-match "unload-hook$" (symbol-name x)))
1713 (ert-fail (format "`%s' still bound" x)))))
1714 ; (progn (message "`%s' still bound" x)))))
1715 ;; There shouldn't be left a hook function containing a Tramp
1716 ;; function. We do not regard the Tramp unload hooks.
1720 (string-match "-hooks?$" (symbol-name x))
1721 (not (string-match "unload-hook$" (symbol-name x)))
1722 (consp (symbol-value x))
1723 (ignore-errors (all-completions "tramp" (symbol-value x)))
1724 (ert-fail (format "Hook `%s' still contains Tramp function" x)))))))
1728 ;; * dired-compress-file
1731 ;; * file-ownership-preserved-p
1732 ;; * file-selinux-context
1733 ;; * find-backup-file-name
1734 ;; * make-auto-save-file-name
1736 ;; * set-file-selinux-context
1738 ;; * Work on skipped tests. Make a comment, when it is impossible.
1739 ;; * Fix `tramp-test15-copy-directory' for `smb'. Using tar in a pipe
1740 ;; doesn't work well when an interactive password must be provided.
1741 ;; * Fix `tramp-test27-start-file-process' for `nc' and on MS
1742 ;; Windows (`process-send-eof'?).
1743 ;; * Fix `tramp-test28-shell-command' on MS Windows (nasty plink message).
1744 ;; * Fix `tramp-test30-special-characters' for `adb', `nc' and `smb'.
1745 ;; * Fix `tramp-test31-utf8' for MS Windows and `nc'/`telnet' (when
1746 ;; target is a dumb busybox). Seems to be in `directory-files'.
1747 ;; * Fix Bug#16928. Set expected error of `tramp-test32-asynchronous-requests'.
1748 ;; * Fix `tramp-test34-unload' (Not all symbols are unbound). Set
1751 (defun tramp-test-all (&optional interactive)
1752 "Run all tests for \\[tramp]."
1755 (if interactive 'ert-run-tests-interactively 'ert-run-tests-batch) "^tramp"))
1757 (provide 'tramp-tests)
1758 ;;; tramp-tests.el ends here