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
)
51 ;; There is no default value on w32 systems, which could work out of the box.
52 (defconst tramp-test-temporary-file-directory
54 ((getenv "REMOTE_TEMPORARY_FILE_DIRECTORY"))
55 ((eq system-type
'windows-nt
) null-device
)
59 (tramp-login-program "sh")
60 (tramp-login-args (("-i")))
61 (tramp-remote-shell "/bin/sh")
62 (tramp-remote-shell-args ("-c"))
63 (tramp-connection-timeout 10)))
64 (format "/mock::%s" temporary-file-directory
)))
65 "Temporary directory for Tramp tests.")
67 (setq password-cache-expiry nil
69 tramp-copy-size-limit nil
70 tramp-message-show-message nil
)
72 ;; This shall happen on hydra only.
73 (when (getenv "NIX_STORE")
74 (add-to-list 'tramp-remote-path
'tramp-own-remote-path
))
76 (defvar tramp--test-enabled-checked nil
77 "Cached result of `tramp--test-enabled'.
78 If the function did run, the value is a cons cell, the `cdr'
81 (defun tramp--test-enabled ()
82 "Whether remote file access is enabled."
83 (unless (consp tramp--test-enabled-checked
)
85 tramp--test-enabled-checked
89 (file-remote-p tramp-test-temporary-file-directory
)
90 (file-directory-p tramp-test-temporary-file-directory
)
91 (file-writable-p tramp-test-temporary-file-directory
))))))
93 (when (cdr tramp--test-enabled-checked
)
94 ;; Cleanup connection.
95 (tramp-cleanup-connection
96 (tramp-dissect-file-name tramp-test-temporary-file-directory
)
100 (cdr tramp--test-enabled-checked
))
102 (defun tramp--test-make-temp-name (&optional local
)
103 "Create a temporary file name for test."
105 (make-temp-name "tramp-test")
106 (if local temporary-file-directory tramp-test-temporary-file-directory
)))
108 (defmacro tramp--instrument-test-case
(verbose &rest body
)
109 "Run BODY with `tramp-verbose' equal VERBOSE.
110 Print the the content of the Tramp debug buffer, if BODY does not
111 eval properly in `should', `should-not' or `should-error'."
112 (declare (indent 1) (debug (natnump body
)))
113 `(let ((tramp-verbose ,verbose
)
114 (tramp-message-show-message t
)
115 (tramp-debug-on-error t
))
117 ;; In general, we cannot use a timeout here: this would
118 ;; prevent traces when the test runs into an error.
119 ; (with-timeout (10 (ert-fail "`tramp--instrument-test-case' timed out"))
123 (signal (car err
) (cdr err
)))
125 (with-parsed-tramp-file-name tramp-test-temporary-file-directory nil
126 (with-current-buffer (tramp-get-connection-buffer v
)
127 (message "%s" (buffer-string)))
128 (with-current-buffer (tramp-get-debug-buffer v
)
129 (message "%s" (buffer-string))))
131 (signal (car err
) (cdr err
))))))
133 (ert-deftest tramp-test00-availability
()
134 "Test availability of Tramp functions."
135 :expected-result
(if (tramp--test-enabled) :passed
:failed
)
136 (message "Remote directory: `%s'" tramp-test-temporary-file-directory
)
137 (should (ignore-errors
139 (file-remote-p tramp-test-temporary-file-directory
)
140 (file-directory-p tramp-test-temporary-file-directory
)
141 (file-writable-p tramp-test-temporary-file-directory
)))))
143 (ert-deftest tramp-test01-file-name-syntax
()
144 "Check remote file name syntax."
146 (should (tramp-tramp-file-p "/method::"))
147 (should (tramp-tramp-file-p "/host:"))
148 (should (tramp-tramp-file-p "/user@:"))
149 (should (tramp-tramp-file-p "/user@host:"))
150 (should (tramp-tramp-file-p "/method:host:"))
151 (should (tramp-tramp-file-p "/method:user@:"))
152 (should (tramp-tramp-file-p "/method:user@host:"))
153 (should (tramp-tramp-file-p "/method:user@email@host:"))
156 (should (tramp-tramp-file-p "/host#1234:"))
157 (should (tramp-tramp-file-p "/user@host#1234:"))
158 (should (tramp-tramp-file-p "/method:host#1234:"))
159 (should (tramp-tramp-file-p "/method:user@host#1234:"))
161 ;; Using an IPv4 address.
162 (should (tramp-tramp-file-p "/1.2.3.4:"))
163 (should (tramp-tramp-file-p "/user@1.2.3.4:"))
164 (should (tramp-tramp-file-p "/method:1.2.3.4:"))
165 (should (tramp-tramp-file-p "/method:user@1.2.3.4:"))
167 ;; Using an IPv6 address.
168 (should (tramp-tramp-file-p "/[]:"))
169 (should (tramp-tramp-file-p "/[::1]:"))
170 (should (tramp-tramp-file-p "/user@[::1]:"))
171 (should (tramp-tramp-file-p "/method:[::1]:"))
172 (should (tramp-tramp-file-p "/method:user@[::1]:"))
174 ;; Local file name part.
175 (should (tramp-tramp-file-p "/host:/:"))
176 (should (tramp-tramp-file-p "/method:::"))
177 (should (tramp-tramp-file-p "/method::/path/to/file"))
178 (should (tramp-tramp-file-p "/method::file"))
181 (should (tramp-tramp-file-p "/method1:|method2::"))
182 (should (tramp-tramp-file-p "/method1:host1|host2:"))
183 (should (tramp-tramp-file-p "/method1:host1|method2:host2:"))
184 (should (tramp-tramp-file-p "/method1:user1@host1|method2:user2@host2:"))
185 (should (tramp-tramp-file-p
186 "/method1:user1@host1|method2:user2@host2|method3:user3@host3:"))
189 (should-not (tramp-tramp-file-p nil
))
190 (should-not (tramp-tramp-file-p 'symbol
))
191 ;; "/:" suppresses file name handlers.
192 (should-not (tramp-tramp-file-p "/::"))
193 (should-not (tramp-tramp-file-p "/:@:"))
194 (should-not (tramp-tramp-file-p "/:[]:"))
195 ;; Multihops require a method.
196 (should-not (tramp-tramp-file-p "/host1|host2:"))
197 ;; Methods or hostnames shall be at least two characters on MS Windows.
198 (when (memq system-type
'(cygwin windows-nt
))
199 (should-not (tramp-tramp-file-p "/c:/path/to/file"))
200 (should-not (tramp-tramp-file-p "/c::/path/to/file"))))
202 (ert-deftest tramp-test02-file-name-dissect
()
203 "Check remote file name components."
204 (let ((tramp-default-method "default-method")
205 (tramp-default-user "default-user")
206 (tramp-default-host "default-host"))
207 ;; Expand `tramp-default-user' and `tramp-default-host'.
208 (should (string-equal
209 (file-remote-p "/method::")
210 (format "/%s:%s@%s:" "method" "default-user" "default-host")))
211 (should (string-equal (file-remote-p "/method::" 'method
) "method"))
212 (should (string-equal (file-remote-p "/method::" 'user
) "default-user"))
213 (should (string-equal (file-remote-p "/method::" 'host
) "default-host"))
214 (should (string-equal (file-remote-p "/method::" 'localname
) ""))
216 ;; Expand `tramp-default-method' and `tramp-default-user'.
217 (should (string-equal
218 (file-remote-p "/host:")
219 (format "/%s:%s@%s:" "default-method" "default-user" "host")))
220 (should (string-equal (file-remote-p "/host:" 'method
) "default-method"))
221 (should (string-equal (file-remote-p "/host:" 'user
) "default-user"))
222 (should (string-equal (file-remote-p "/host:" 'host
) "host"))
223 (should (string-equal (file-remote-p "/host:" 'localname
) ""))
225 ;; Expand `tramp-default-method' and `tramp-default-host'.
226 (should (string-equal
227 (file-remote-p "/user@:")
228 (format "/%s:%s@%s:" "default-method""user" "default-host")))
229 (should (string-equal (file-remote-p "/user@:" 'method
) "default-method"))
230 (should (string-equal (file-remote-p "/user@:" 'user
) "user"))
231 (should (string-equal (file-remote-p "/user@:" 'host
) "default-host"))
232 (should (string-equal (file-remote-p "/user@:" 'localname
) ""))
234 ;; Expand `tramp-default-method'.
235 (should (string-equal
236 (file-remote-p "/user@host:")
237 (format "/%s:%s@%s:" "default-method" "user" "host")))
238 (should (string-equal
239 (file-remote-p "/user@host:" 'method
) "default-method"))
240 (should (string-equal (file-remote-p "/user@host:" 'user
) "user"))
241 (should (string-equal (file-remote-p "/user@host:" 'host
) "host"))
242 (should (string-equal (file-remote-p "/user@host:" 'localname
) ""))
244 ;; Expand `tramp-default-user'.
245 (should (string-equal
246 (file-remote-p "/method:host:")
247 (format "/%s:%s@%s:" "method" "default-user" "host")))
248 (should (string-equal (file-remote-p "/method:host:" 'method
) "method"))
249 (should (string-equal (file-remote-p "/method:host:" 'user
) "default-user"))
250 (should (string-equal (file-remote-p "/method:host:" 'host
) "host"))
251 (should (string-equal (file-remote-p "/method:host:" 'localname
) ""))
253 ;; Expand `tramp-default-host'.
254 (should (string-equal
255 (file-remote-p "/method:user@:")
256 (format "/%s:%s@%s:" "method" "user" "default-host")))
257 (should (string-equal (file-remote-p "/method:user@:" 'method
) "method"))
258 (should (string-equal (file-remote-p "/method:user@:" 'user
) "user"))
259 (should (string-equal (file-remote-p "/method:user@:" 'host
)
261 (should (string-equal (file-remote-p "/method:user@:" 'localname
) ""))
264 (should (string-equal
265 (file-remote-p "/method:user@host:")
266 (format "/%s:%s@%s:" "method" "user" "host")))
267 (should (string-equal
268 (file-remote-p "/method:user@host:" 'method
) "method"))
269 (should (string-equal (file-remote-p "/method:user@host:" 'user
) "user"))
270 (should (string-equal (file-remote-p "/method:user@host:" 'host
) "host"))
271 (should (string-equal (file-remote-p "/method:user@host:" 'localname
) ""))
274 (should (string-equal
275 (file-remote-p "/method:user@email@host:")
276 (format "/%s:%s@%s:" "method" "user@email" "host")))
277 (should (string-equal
278 (file-remote-p "/method:user@email@host:" 'method
) "method"))
279 (should (string-equal
280 (file-remote-p "/method:user@email@host:" 'user
) "user@email"))
281 (should (string-equal
282 (file-remote-p "/method:user@email@host:" 'host
) "host"))
283 (should (string-equal
284 (file-remote-p "/method:user@email@host:" 'localname
) ""))
286 ;; Expand `tramp-default-method' and `tramp-default-user'.
287 (should (string-equal
288 (file-remote-p "/host#1234:")
289 (format "/%s:%s@%s:" "default-method" "default-user" "host#1234")))
290 (should (string-equal
291 (file-remote-p "/host#1234:" 'method
) "default-method"))
292 (should (string-equal (file-remote-p "/host#1234:" 'user
) "default-user"))
293 (should (string-equal (file-remote-p "/host#1234:" 'host
) "host#1234"))
294 (should (string-equal (file-remote-p "/host#1234:" 'localname
) ""))
296 ;; Expand `tramp-default-method'.
297 (should (string-equal
298 (file-remote-p "/user@host#1234:")
299 (format "/%s:%s@%s:" "default-method" "user" "host#1234")))
300 (should (string-equal
301 (file-remote-p "/user@host#1234:" 'method
) "default-method"))
302 (should (string-equal (file-remote-p "/user@host#1234:" 'user
) "user"))
303 (should (string-equal (file-remote-p "/user@host#1234:" 'host
) "host#1234"))
304 (should (string-equal (file-remote-p "/user@host#1234:" 'localname
) ""))
306 ;; Expand `tramp-default-user'.
307 (should (string-equal
308 (file-remote-p "/method:host#1234:")
309 (format "/%s:%s@%s:" "method" "default-user" "host#1234")))
310 (should (string-equal
311 (file-remote-p "/method:host#1234:" 'method
) "method"))
312 (should (string-equal
313 (file-remote-p "/method:host#1234:" 'user
) "default-user"))
314 (should (string-equal
315 (file-remote-p "/method:host#1234:" 'host
) "host#1234"))
316 (should (string-equal (file-remote-p "/method:host#1234:" 'localname
) ""))
319 (should (string-equal
320 (file-remote-p "/method:user@host#1234:")
321 (format "/%s:%s@%s:" "method" "user" "host#1234")))
322 (should (string-equal
323 (file-remote-p "/method:user@host#1234:" 'method
) "method"))
324 (should (string-equal
325 (file-remote-p "/method:user@host#1234:" 'user
) "user"))
326 (should (string-equal
327 (file-remote-p "/method:user@host#1234:" 'host
) "host#1234"))
328 (should (string-equal
329 (file-remote-p "/method:user@host#1234:" 'localname
) ""))
331 ;; Expand `tramp-default-method' and `tramp-default-user'.
332 (should (string-equal
333 (file-remote-p "/1.2.3.4:")
334 (format "/%s:%s@%s:" "default-method" "default-user" "1.2.3.4")))
335 (should (string-equal (file-remote-p "/1.2.3.4:" 'method
) "default-method"))
336 (should (string-equal (file-remote-p "/1.2.3.4:" 'user
) "default-user"))
337 (should (string-equal (file-remote-p "/1.2.3.4:" 'host
) "1.2.3.4"))
338 (should (string-equal (file-remote-p "/1.2.3.4:" 'localname
) ""))
340 ;; Expand `tramp-default-method'.
341 (should (string-equal
342 (file-remote-p "/user@1.2.3.4:")
343 (format "/%s:%s@%s:" "default-method" "user" "1.2.3.4")))
344 (should (string-equal
345 (file-remote-p "/user@1.2.3.4:" 'method
) "default-method"))
346 (should (string-equal (file-remote-p "/user@1.2.3.4:" 'user
) "user"))
347 (should (string-equal (file-remote-p "/user@1.2.3.4:" 'host
) "1.2.3.4"))
348 (should (string-equal (file-remote-p "/user@1.2.3.4:" 'localname
) ""))
350 ;; Expand `tramp-default-user'.
351 (should (string-equal
352 (file-remote-p "/method:1.2.3.4:")
353 (format "/%s:%s@%s:" "method" "default-user" "1.2.3.4")))
354 (should (string-equal (file-remote-p "/method:1.2.3.4:" 'method
) "method"))
355 (should (string-equal
356 (file-remote-p "/method:1.2.3.4:" 'user
) "default-user"))
357 (should (string-equal (file-remote-p "/method:1.2.3.4:" 'host
) "1.2.3.4"))
358 (should (string-equal (file-remote-p "/method:1.2.3.4:" 'localname
) ""))
361 (should (string-equal
362 (file-remote-p "/method:user@1.2.3.4:")
363 (format "/%s:%s@%s:" "method" "user" "1.2.3.4")))
364 (should (string-equal
365 (file-remote-p "/method:user@1.2.3.4:" 'method
) "method"))
366 (should (string-equal (file-remote-p "/method:user@1.2.3.4:" 'user
) "user"))
367 (should (string-equal
368 (file-remote-p "/method:user@1.2.3.4:" 'host
) "1.2.3.4"))
369 (should (string-equal
370 (file-remote-p "/method:user@1.2.3.4:" 'localname
) ""))
372 ;; Expand `tramp-default-method', `tramp-default-user' and
373 ;; `tramp-default-host'.
374 (should (string-equal
375 (file-remote-p "/[]:")
377 "/%s:%s@%s:" "default-method" "default-user" "default-host")))
378 (should (string-equal (file-remote-p "/[]:" 'method
) "default-method"))
379 (should (string-equal (file-remote-p "/[]:" 'user
) "default-user"))
380 (should (string-equal (file-remote-p "/[]:" 'host
) "default-host"))
381 (should (string-equal (file-remote-p "/[]:" 'localname
) ""))
383 ;; Expand `tramp-default-method' and `tramp-default-user'.
384 (let ((tramp-default-host "::1"))
385 (should (string-equal
386 (file-remote-p "/[]:")
387 (format "/%s:%s@%s:" "default-method" "default-user" "[::1]")))
388 (should (string-equal (file-remote-p "/[]:" 'method
) "default-method"))
389 (should (string-equal (file-remote-p "/[]:" 'user
) "default-user"))
390 (should (string-equal (file-remote-p "/[]:" 'host
) "::1"))
391 (should (string-equal (file-remote-p "/[]:" 'localname
) "")))
393 ;; Expand `tramp-default-method' and `tramp-default-user'.
394 (should (string-equal
395 (file-remote-p "/[::1]:")
396 (format "/%s:%s@%s:" "default-method" "default-user" "[::1]")))
397 (should (string-equal (file-remote-p "/[::1]:" 'method
) "default-method"))
398 (should (string-equal (file-remote-p "/[::1]:" 'user
) "default-user"))
399 (should (string-equal (file-remote-p "/[::1]:" 'host
) "::1"))
400 (should (string-equal (file-remote-p "/[::1]:" 'localname
) ""))
402 ;; Expand `tramp-default-method'.
403 (should (string-equal
404 (file-remote-p "/user@[::1]:")
405 (format "/%s:%s@%s:" "default-method" "user" "[::1]")))
406 (should (string-equal
407 (file-remote-p "/user@[::1]:" 'method
) "default-method"))
408 (should (string-equal (file-remote-p "/user@[::1]:" 'user
) "user"))
409 (should (string-equal (file-remote-p "/user@[::1]:" 'host
) "::1"))
410 (should (string-equal (file-remote-p "/user@[::1]:" 'localname
) ""))
412 ;; Expand `tramp-default-user'.
413 (should (string-equal
414 (file-remote-p "/method:[::1]:")
415 (format "/%s:%s@%s:" "method" "default-user" "[::1]")))
416 (should (string-equal (file-remote-p "/method:[::1]:" 'method
) "method"))
417 (should (string-equal
418 (file-remote-p "/method:[::1]:" 'user
) "default-user"))
419 (should (string-equal (file-remote-p "/method:[::1]:" 'host
) "::1"))
420 (should (string-equal (file-remote-p "/method:[::1]:" 'localname
) ""))
423 (should (string-equal
424 (file-remote-p "/method:user@[::1]:")
425 (format "/%s:%s@%s:" "method" "user" "[::1]")))
426 (should (string-equal
427 (file-remote-p "/method:user@[::1]:" 'method
) "method"))
428 (should (string-equal (file-remote-p "/method:user@[::1]:" 'user
) "user"))
429 (should (string-equal (file-remote-p "/method:user@[::1]:" 'host
) "::1"))
430 (should (string-equal
431 (file-remote-p "/method:user@[::1]:" 'localname
) ""))
433 ;; Local file name part.
434 (should (string-equal (file-remote-p "/host:/:" 'localname
) "/:"))
435 (should (string-equal (file-remote-p "/method:::" 'localname
) ":"))
436 (should (string-equal (file-remote-p "/method:: " 'localname
) " "))
437 (should (string-equal (file-remote-p "/method::file" 'localname
) "file"))
438 (should (string-equal
439 (file-remote-p "/method::/path/to/file" 'localname
)
445 (file-remote-p "/method1:user1@host1|method2:user2@host2:/path/to/file")
446 (format "/%s:%s@%s:" "method2" "user2" "host2")))
450 "/method1:user1@host1|method2:user2@host2:/path/to/file" 'method
)
455 "/method1:user1@host1|method2:user2@host2:/path/to/file" 'user
)
460 "/method1:user1@host1|method2:user2@host2:/path/to/file" 'host
)
465 "/method1:user1@host1|method2:user2@host2:/path/to/file" 'localname
)
471 "/method1:user1@host1|method2:user2@host2|method3:user3@host3:/path/to/file")
472 (format "/%s:%s@%s:" "method3" "user3" "host3")))
476 "/method1:user1@host1|method2:user2@host2|method3:user3@host3:/path/to/file"
482 "/method1:user1@host1|method2:user2@host2|method3:user3@host3:/path/to/file"
488 "/method1:user1@host1|method2:user2@host2|method3:user3@host3:/path/to/file"
494 "/method1:user1@host1|method2:user2@host2|method3:user3@host3:/path/to/file"
498 (ert-deftest tramp-test03-file-name-defaults
()
499 "Check default values for some methods."
500 ;; Default values in tramp-adb.el.
501 (should (string-equal (file-remote-p "/adb::" 'host
) ""))
502 ;; Default values in tramp-ftp.el.
503 (should (string-equal (file-remote-p "/ftp.host:" 'method
) "ftp"))
504 (dolist (u '("ftp" "anonymous"))
505 (should (string-equal (file-remote-p (format "/%s@:" u
) 'method
) "ftp")))
506 ;; Default values in tramp-gvfs.el.
507 (when (and (load "tramp-gvfs" 'noerror
'nomessage
)
508 (symbol-value 'tramp-gvfs-enabled
))
509 (should (string-equal (file-remote-p "/synce::" 'user
) nil
)))
510 ;; Default values in tramp-gw.el.
511 (dolist (m '("tunnel" "socks"))
513 (string-equal (file-remote-p (format "/%s::" m
) 'user
) (user-login-name))))
514 ;; Default values in tramp-sh.el.
515 (dolist (h `("127.0.0.1" "[::1]" "localhost" "localhost6" ,(system-name)))
516 (should (string-equal (file-remote-p (format "/root@%s:" h
) 'method
) "su")))
517 (dolist (m '("su" "sudo" "ksu"))
518 (should (string-equal (file-remote-p (format "/%s::" m
) 'user
) "root")))
519 (dolist (m '("rcp" "remcp" "rsh" "telnet" "krlogin" "fcp"))
521 (string-equal (file-remote-p (format "/%s::" m
) 'user
) (user-login-name))))
522 ;; Default values in tramp-smb.el.
523 (should (string-equal (file-remote-p "/user%domain@host:" 'method
) "smb"))
524 (should (string-equal (file-remote-p "/smb::" 'user
) nil
)))
526 (ert-deftest tramp-test04-substitute-in-file-name
()
527 "Check `substitute-in-file-name'."
528 (should (string-equal (substitute-in-file-name "/method:host://foo") "/foo"))
531 (substitute-in-file-name "/method:host:/path//foo") "/method:host:/foo"))
533 (string-equal (substitute-in-file-name "/method:host:/path///foo") "/foo"))
536 (substitute-in-file-name "/method:host:/path/~/foo") "/method:host:~/foo"))
538 (string-equal (substitute-in-file-name "/method:host:/path//~/foo") "~/foo"))
539 (let (process-environment)
542 (substitute-in-file-name "/method:host:/path/$FOO")
543 "/method:host:/path/$FOO"))
547 (substitute-in-file-name "/method:host:/path/$FOO")
548 "/method:host:/path/bla"))
551 (substitute-in-file-name "/method:host:/path/$$FOO")
552 "/method:host:/path/$FOO"))))
554 (ert-deftest tramp-test05-expand-file-name
()
555 "Check `expand-file-name'."
558 (expand-file-name "/method:host:/path/./file") "/method:host:/path/file"))
561 (expand-file-name "/method:host:/path/../file") "/method:host:/file")))
563 (ert-deftest tramp-test06-directory-file-name
()
564 "Check `directory-file-name'.
565 This checks also `file-name-as-directory', `file-name-directory'
566 and `file-name-nondirectory'."
569 (directory-file-name "/method:host:/path/to/file")
570 "/method:host:/path/to/file"))
573 (directory-file-name "/method:host:/path/to/file/")
574 "/method:host:/path/to/file"))
577 (file-name-as-directory "/method:host:/path/to/file")
578 "/method:host:/path/to/file/"))
581 (file-name-as-directory "/method:host:/path/to/file/")
582 "/method:host:/path/to/file/"))
585 (file-name-directory "/method:host:/path/to/file")
586 "/method:host:/path/to/"))
589 (file-name-directory "/method:host:/path/to/file/")
590 "/method:host:/path/to/file/"))
592 (string-equal (file-name-nondirectory "/method:host:/path/to/file") "file"))
594 (string-equal (file-name-nondirectory "/method:host:/path/to/file/") ""))
597 (unhandled-file-name-directory "/method:host:/path/to/file"))))
599 (ert-deftest tramp-test07-file-exists-p
()
600 "Check `file-exist-p', `write-region' and `delete-file'."
601 (skip-unless (tramp--test-enabled))
603 (let ((tmp-name (tramp--test-make-temp-name)))
604 (should-not (file-exists-p tmp-name
))
605 (write-region "foo" nil tmp-name
)
606 (should (file-exists-p tmp-name
))
607 (delete-file tmp-name
)
608 (should-not (file-exists-p tmp-name
))))
610 (ert-deftest tramp-test08-file-local-copy
()
611 "Check `file-local-copy'."
612 (skip-unless (tramp--test-enabled))
614 (let ((tmp-name1 (tramp--test-make-temp-name))
618 (write-region "foo" nil tmp-name1
)
619 (should (setq tmp-name2
(file-local-copy tmp-name1
)))
621 (insert-file-contents tmp-name2
)
622 (should (string-equal (buffer-string) "foo"))))
624 (delete-file tmp-name1
)
625 (delete-file tmp-name2
)))))
627 (ert-deftest tramp-test09-insert-file-contents
()
628 "Check `insert-file-contents'."
629 (skip-unless (tramp--test-enabled))
631 (let ((tmp-name (tramp--test-make-temp-name)))
634 (write-region "foo" nil tmp-name
)
636 (insert-file-contents tmp-name
)
637 (should (string-equal (buffer-string) "foo"))
638 (insert-file-contents tmp-name
)
639 (should (string-equal (buffer-string) "foofoo"))
641 (insert-file-contents tmp-name nil
1 3)
642 (should (string-equal (buffer-string) "oofoofoo"))
644 (insert-file-contents tmp-name nil nil nil
'replace
)
645 (should (string-equal (buffer-string) "foo"))))
646 (ignore-errors (delete-file tmp-name
)))))
648 (ert-deftest tramp-test10-write-region
()
649 "Check `write-region'."
650 (skip-unless (tramp--test-enabled))
652 (let ((tmp-name (tramp--test-make-temp-name)))
657 (write-region nil nil tmp-name
))
659 (insert-file-contents tmp-name
)
660 (should (string-equal (buffer-string) "foo")))
664 (write-region nil nil tmp-name
'append
))
666 (insert-file-contents tmp-name
)
667 (should (string-equal (buffer-string) "foobla")))
669 (write-region "foo" nil tmp-name
)
671 (insert-file-contents tmp-name
)
672 (should (string-equal (buffer-string) "foo")))
676 (write-region 3 5 tmp-name
))
678 (insert-file-contents tmp-name
)
679 (should (string-equal (buffer-string) "34"))))
680 (ignore-errors (delete-file tmp-name
)))))
682 (ert-deftest tramp-test11-copy-file
()
684 (skip-unless (tramp--test-enabled))
686 (let ((tmp-name1 (tramp--test-make-temp-name))
687 (tmp-name2 (tramp--test-make-temp-name))
688 (tmp-name3 (tramp--test-make-temp-name))
689 (tmp-name4 (tramp--test-make-temp-name 'local
))
690 (tmp-name5 (tramp--test-make-temp-name 'local
)))
692 ;; Copy on remote side.
695 (write-region "foo" nil tmp-name1
)
696 (copy-file tmp-name1 tmp-name2
)
697 (should (file-exists-p tmp-name2
))
699 (insert-file-contents tmp-name2
)
700 (should (string-equal (buffer-string) "foo")))
701 (should-error (copy-file tmp-name1 tmp-name2
))
702 (copy-file tmp-name1 tmp-name2
'ok
)
703 (make-directory tmp-name3
)
704 (copy-file tmp-name1 tmp-name3
)
707 (expand-file-name (file-name-nondirectory tmp-name1
) tmp-name3
))))
708 (ignore-errors (delete-file tmp-name1
))
709 (ignore-errors (delete-file tmp-name2
))
710 (ignore-errors (delete-directory tmp-name3
'recursive
)))
712 ;; Copy from remote side to local side.
715 (write-region "foo" nil tmp-name1
)
716 (copy-file tmp-name1 tmp-name4
)
717 (should (file-exists-p tmp-name4
))
719 (insert-file-contents tmp-name4
)
720 (should (string-equal (buffer-string) "foo")))
721 (should-error (copy-file tmp-name1 tmp-name4
))
722 (copy-file tmp-name1 tmp-name4
'ok
)
723 (make-directory tmp-name5
)
724 (copy-file tmp-name1 tmp-name5
)
727 (expand-file-name (file-name-nondirectory tmp-name1
) tmp-name5
))))
728 (ignore-errors (delete-file tmp-name1
))
729 (ignore-errors (delete-file tmp-name4
))
730 (ignore-errors (delete-directory tmp-name5
'recursive
)))
732 ;; Copy from local side to remote side.
735 (write-region "foo" nil tmp-name4 nil
'nomessage
)
736 (copy-file tmp-name4 tmp-name1
)
737 (should (file-exists-p tmp-name1
))
739 (insert-file-contents tmp-name1
)
740 (should (string-equal (buffer-string) "foo")))
741 (should-error (copy-file tmp-name4 tmp-name1
))
742 (copy-file tmp-name4 tmp-name1
'ok
)
743 (make-directory tmp-name3
)
744 (copy-file tmp-name4 tmp-name3
)
747 (expand-file-name (file-name-nondirectory tmp-name4
) tmp-name3
))))
748 (ignore-errors (delete-file tmp-name1
))
749 (ignore-errors (delete-file tmp-name4
))
750 (ignore-errors (delete-directory tmp-name3
'recursive
)))))
752 (ert-deftest tramp-test12-rename-file
()
753 "Check `rename-file'."
754 (skip-unless (tramp--test-enabled))
756 (let ((tmp-name1 (tramp--test-make-temp-name))
757 (tmp-name2 (tramp--test-make-temp-name))
758 (tmp-name3 (tramp--test-make-temp-name))
759 (tmp-name4 (tramp--test-make-temp-name 'local
))
760 (tmp-name5 (tramp--test-make-temp-name 'local
)))
762 ;; Rename on remote side.
765 (write-region "foo" nil tmp-name1
)
766 (rename-file tmp-name1 tmp-name2
)
767 (should-not (file-exists-p tmp-name1
))
768 (should (file-exists-p tmp-name2
))
770 (insert-file-contents tmp-name2
)
771 (should (string-equal (buffer-string) "foo")))
772 (write-region "foo" nil tmp-name1
)
773 (should-error (rename-file tmp-name1 tmp-name2
))
774 (rename-file tmp-name1 tmp-name2
'ok
)
775 (should-not (file-exists-p tmp-name1
))
776 (write-region "foo" nil tmp-name1
)
777 (make-directory tmp-name3
)
778 (rename-file tmp-name1 tmp-name3
)
779 (should-not (file-exists-p tmp-name1
))
782 (expand-file-name (file-name-nondirectory tmp-name1
) tmp-name3
))))
783 (ignore-errors (delete-file tmp-name1
))
784 (ignore-errors (delete-file tmp-name2
))
785 (ignore-errors (delete-directory tmp-name3
'recursive
)))
787 ;; Rename from remote side to local side.
790 (write-region "foo" nil tmp-name1
)
791 (rename-file tmp-name1 tmp-name4
)
792 (should-not (file-exists-p tmp-name1
))
793 (should (file-exists-p tmp-name4
))
795 (insert-file-contents tmp-name4
)
796 (should (string-equal (buffer-string) "foo")))
797 (write-region "foo" nil tmp-name1
)
798 (should-error (rename-file tmp-name1 tmp-name4
))
799 (rename-file tmp-name1 tmp-name4
'ok
)
800 (should-not (file-exists-p tmp-name1
))
801 (write-region "foo" nil tmp-name1
)
802 (make-directory tmp-name5
)
803 (rename-file tmp-name1 tmp-name5
)
804 (should-not (file-exists-p tmp-name1
))
807 (expand-file-name (file-name-nondirectory tmp-name1
) tmp-name5
))))
808 (ignore-errors (delete-file tmp-name1
))
809 (ignore-errors (delete-file tmp-name4
))
810 (ignore-errors (delete-directory tmp-name5
'recursive
)))
812 ;; Rename from local side to remote side.
815 (write-region "foo" nil tmp-name4 nil
'nomessage
)
816 (rename-file tmp-name4 tmp-name1
)
817 (should-not (file-exists-p tmp-name4
))
818 (should (file-exists-p tmp-name1
))
820 (insert-file-contents tmp-name1
)
821 (should (string-equal (buffer-string) "foo")))
822 (write-region "foo" nil tmp-name4 nil
'nomessage
)
823 (should-error (rename-file tmp-name4 tmp-name1
))
824 (rename-file tmp-name4 tmp-name1
'ok
)
825 (should-not (file-exists-p tmp-name4
))
826 (write-region "foo" nil tmp-name4 nil
'nomessage
)
827 (make-directory tmp-name3
)
828 (rename-file tmp-name4 tmp-name3
)
829 (should-not (file-exists-p tmp-name4
))
832 (expand-file-name (file-name-nondirectory tmp-name4
) tmp-name3
))))
833 (ignore-errors (delete-file tmp-name1
))
834 (ignore-errors (delete-file tmp-name4
))
835 (ignore-errors (delete-directory tmp-name3
'recursive
)))))
837 (ert-deftest tramp-test13-make-directory
()
838 "Check `make-directory'.
839 This tests also `file-directory-p' and `file-accessible-directory-p'."
840 (skip-unless (tramp--test-enabled))
842 (let ((tmp-name (tramp--test-make-temp-name)))
845 (make-directory tmp-name
)
846 (should (file-directory-p tmp-name
))
847 (should (file-accessible-directory-p tmp-name
)))
848 (ignore-errors (delete-directory tmp-name
)))))
850 (ert-deftest tramp-test14-delete-directory
()
851 "Check `delete-directory'."
852 (skip-unless (tramp--test-enabled))
854 (let ((tmp-name (tramp--test-make-temp-name)))
855 ;; Delete empty directory.
856 (make-directory tmp-name
)
857 (should (file-directory-p tmp-name
))
858 (delete-directory tmp-name
)
859 (should-not (file-directory-p tmp-name
))
860 ;; Delete non-empty directory.
861 (make-directory tmp-name
)
862 (write-region "foo" nil
(expand-file-name "bla" tmp-name
))
863 (should-error (delete-directory tmp-name
) :type
'file-error
)
864 (delete-directory tmp-name
'recursive
)
865 (should-not (file-directory-p tmp-name
))))
867 (ert-deftest tramp-test15-copy-directory
()
868 "Check `copy-directory'."
869 (skip-unless (tramp--test-enabled))
871 (let* ((tmp-name1 (tramp--test-make-temp-name))
872 (tmp-name2 (tramp--test-make-temp-name))
873 (tmp-name3 (expand-file-name
874 (file-name-nondirectory tmp-name1
) tmp-name2
))
875 (tmp-name4 (expand-file-name "foo" tmp-name1
))
876 (tmp-name5 (expand-file-name "foo" tmp-name2
))
877 (tmp-name6 (expand-file-name "foo" tmp-name3
)))
880 ;; Copy empty directory.
881 (make-directory tmp-name1
)
882 (write-region "foo" nil tmp-name4
)
883 (should (file-directory-p tmp-name1
))
884 (should (file-exists-p tmp-name4
))
885 (copy-directory tmp-name1 tmp-name2
)
886 (should (file-directory-p tmp-name2
))
887 (should (file-exists-p tmp-name5
))
888 ;; Target directory does exist already.
889 (copy-directory tmp-name1 tmp-name2
)
890 (should (file-directory-p tmp-name3
))
891 (should (file-exists-p tmp-name6
)))
893 (delete-directory tmp-name1
'recursive
)
894 (delete-directory tmp-name2
'recursive
)))))
896 (ert-deftest tramp-test16-directory-files
()
897 "Check `directory-files'."
898 (skip-unless (tramp--test-enabled))
900 (let* ((tmp-name1 (tramp--test-make-temp-name))
901 (tmp-name2 (expand-file-name "bla" tmp-name1
))
902 (tmp-name3 (expand-file-name "foo" tmp-name1
)))
905 (make-directory tmp-name1
)
906 (write-region "foo" nil tmp-name2
)
907 (write-region "bla" nil tmp-name3
)
908 (should (file-directory-p tmp-name1
))
909 (should (file-exists-p tmp-name2
))
910 (should (file-exists-p tmp-name3
))
911 (should (equal (directory-files tmp-name1
) '("." ".." "bla" "foo")))
912 (should (equal (directory-files tmp-name1
'full
)
913 `(,(concat tmp-name1
"/.")
914 ,(concat tmp-name1
"/..")
915 ,tmp-name2
,tmp-name3
)))
916 (should (equal (directory-files
917 tmp-name1 nil directory-files-no-dot-files-regexp
)
919 (should (equal (directory-files
920 tmp-name1
'full directory-files-no-dot-files-regexp
)
921 `(,tmp-name2
,tmp-name3
))))
922 (ignore-errors (delete-directory tmp-name1
'recursive
)))))
924 (ert-deftest tramp-test17-insert-directory
()
925 "Check `insert-directory'."
926 (skip-unless (tramp--test-enabled))
928 (let* ((tmp-name1 (tramp--test-make-temp-name))
929 (tmp-name2 (expand-file-name "foo" tmp-name1
)))
932 (make-directory tmp-name1
)
933 (write-region "foo" nil tmp-name2
)
934 (should (file-directory-p tmp-name1
))
935 (should (file-exists-p tmp-name2
))
937 (insert-directory tmp-name1 nil
)
938 (goto-char (point-min))
939 (should (looking-at-p (regexp-quote tmp-name1
))))
941 (insert-directory tmp-name1
"-al")
942 (goto-char (point-min))
943 (should (looking-at-p (format "^.+ %s$" (regexp-quote tmp-name1
)))))
945 (insert-directory (file-name-as-directory tmp-name1
) "-al")
946 (goto-char (point-min))
948 (looking-at-p (format "^.+ %s/$" (regexp-quote tmp-name1
)))))
951 (file-name-as-directory tmp-name1
) "-al" nil
'full-directory-p
)
952 (goto-char (point-min))
955 "\\(total.+[[:digit:]]+\n\\)?.+ \\.\n.+ \\.\\.\n.+ foo$"))))
956 (ignore-errors (delete-directory tmp-name1
'recursive
)))))
958 (ert-deftest tramp-test18-file-attributes
()
959 "Check `file-attributes'.
960 This tests also `file-readable-p' and `file-regular-p'."
961 (skip-unless (tramp--test-enabled))
963 (let ((tmp-name (tramp--test-make-temp-name))
967 (write-region "foo" nil tmp-name
)
968 (should (file-exists-p tmp-name
))
969 (setq attr
(file-attributes tmp-name
))
970 (should (consp attr
))
971 (should (file-exists-p tmp-name
))
972 (should (file-readable-p tmp-name
))
973 (should (file-regular-p tmp-name
))
974 ;; We do not test inodes and device numbers.
975 (should (null (car attr
)))
976 (should (numberp (nth 1 attr
))) ;; Link.
977 (should (numberp (nth 2 attr
))) ;; Uid.
978 (should (numberp (nth 3 attr
))) ;; Gid.
980 (should (stringp (current-time-string (nth 4 attr
))))
981 ;; Last modification time.
982 (should (stringp (current-time-string (nth 5 attr
))))
983 ;; Last status change time.
984 (should (stringp (current-time-string (nth 6 attr
))))
985 (should (numberp (nth 7 attr
))) ;; Size.
986 (should (stringp (nth 8 attr
))) ;; Modes.
988 (setq attr
(file-attributes tmp-name
'string
))
989 (should (stringp (nth 2 attr
))) ;; Uid.
990 (should (stringp (nth 3 attr
))) ;; Gid.
991 (delete-file tmp-name
)
993 (make-directory tmp-name
)
994 (should (file-exists-p tmp-name
))
995 (should (file-readable-p tmp-name
))
996 (should-not (file-regular-p tmp-name
))
997 (setq attr
(file-attributes tmp-name
))
998 (should (eq (car attr
) t
)))
999 (ignore-errors (delete-directory tmp-name
)))))
1001 (ert-deftest tramp-test19-directory-files-and-attributes
()
1002 "Check `directory-files-and-attributes'."
1003 (skip-unless (tramp--test-enabled))
1005 ;; `directory-files-and-attributes' contains also values for "../".
1006 ;; Ensure that this doesn't change during tests, for
1007 ;; example due to handling temporary files.
1008 (let* ((tmp-name1 (tramp--test-make-temp-name))
1009 (tmp-name2 (expand-file-name "bla" tmp-name1
))
1013 (make-directory tmp-name1
)
1014 (should (file-directory-p tmp-name1
))
1015 (make-directory tmp-name2
)
1016 (should (file-directory-p tmp-name2
))
1017 (write-region "foo" nil
(expand-file-name "foo" tmp-name2
))
1018 (write-region "bar" nil
(expand-file-name "bar" tmp-name2
))
1019 (write-region "boz" nil
(expand-file-name "boz" tmp-name2
))
1020 (setq attr
(directory-files-and-attributes tmp-name2
))
1021 (should (consp attr
))
1022 ;; Dumb remote shells without perl(1) or stat(1) are not
1023 ;; able to return the date correctly. They say "don't know".
1028 (file-attributes (expand-file-name (car elt
) tmp-name2
)))
1031 (equal (file-attributes (expand-file-name (car elt
) tmp-name2
))
1033 (setq attr
(directory-files-and-attributes tmp-name2
'full
))
1035 (unless (equal (nth 5 (file-attributes (car elt
))) '(0 0))
1037 (equal (file-attributes (car elt
)) (cdr elt
)))))
1038 (setq attr
(directory-files-and-attributes tmp-name2 nil
"^b"))
1039 (should (equal (mapcar 'car attr
) '("bar" "boz"))))
1040 (ignore-errors (delete-directory tmp-name1
'recursive
)))))
1042 (ert-deftest tramp-test20-file-modes
()
1043 "Check `file-modes'.
1044 This tests also `file-executable-p', `file-writable-p' and `set-file-modes'."
1045 (skip-unless (tramp--test-enabled))
1049 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory
)
1050 '(tramp-adb-file-name-handler
1051 tramp-gvfs-file-name-handler
1052 tramp-smb-file-name-handler
))))
1054 (let ((tmp-name (tramp--test-make-temp-name)))
1057 (write-region "foo" nil tmp-name
)
1058 (should (file-exists-p tmp-name
))
1059 (set-file-modes tmp-name
#o777
)
1060 (should (= (file-modes tmp-name
) #o777
))
1061 (should (file-executable-p tmp-name
))
1062 (should (file-writable-p tmp-name
))
1063 (set-file-modes tmp-name
#o444
)
1064 (should (= (file-modes tmp-name
) #o444
))
1065 (should-not (file-executable-p tmp-name
))
1066 ;; A file is always writable for user "root".
1067 (unless (zerop (nth 2 (file-attributes tmp-name
)))
1068 (should-not (file-writable-p tmp-name
))))
1069 (ignore-errors (delete-file tmp-name
)))))
1071 (ert-deftest tramp-test21-file-links
()
1072 "Check `file-symlink-p'.
1073 This tests also `make-symbolic-link', `file-truename' and `add-name-to-file'."
1074 (skip-unless (tramp--test-enabled))
1076 (let ((tmp-name1 (tramp--test-make-temp-name))
1077 (tmp-name2 (tramp--test-make-temp-name))
1078 (tmp-name3 (tramp--test-make-temp-name 'local
)))
1081 (write-region "foo" nil tmp-name1
)
1082 (should (file-exists-p tmp-name1
))
1083 ;; Method "smb" supports `make-symbolic-link' only if the
1084 ;; remote host has CIFS capabilities. tramp-adb.el and
1085 ;; tramp-gvfs.el do not support symbolic links at all.
1087 (make-symbolic-link tmp-name1 tmp-name2
)
1090 (not (string-equal (error-message-string err
)
1091 "make-symbolic-link not supported")))))
1092 (should (file-symlink-p tmp-name2
))
1093 (should-error (make-symbolic-link tmp-name1 tmp-name2
))
1094 (make-symbolic-link tmp-name1 tmp-name2
'ok-if-already-exists
)
1095 (should (file-symlink-p tmp-name2
))
1096 ;; `tmp-name3' is a local file name.
1097 (should-error (make-symbolic-link tmp-name1 tmp-name3
)))
1099 (delete-file tmp-name1
)
1100 (delete-file tmp-name2
)))
1104 (write-region "foo" nil tmp-name1
)
1105 (should (file-exists-p tmp-name1
))
1106 (add-name-to-file tmp-name1 tmp-name2
)
1107 (should-not (file-symlink-p tmp-name2
))
1108 (should-error (add-name-to-file tmp-name1 tmp-name2
))
1109 (add-name-to-file tmp-name1 tmp-name2
'ok-if-already-exists
)
1110 (should-not (file-symlink-p tmp-name2
))
1111 ;; `tmp-name3' is a local file name.
1112 (should-error (add-name-to-file tmp-name1 tmp-name3
)))
1114 (delete-file tmp-name1
)
1115 (delete-file tmp-name2
)))
1119 (write-region "foo" nil tmp-name1
)
1120 (should (file-exists-p tmp-name1
))
1121 (make-symbolic-link tmp-name1 tmp-name2
)
1122 (should (file-symlink-p tmp-name2
))
1123 (should-not (string-equal tmp-name2
(file-truename tmp-name2
)))
1125 (string-equal (file-truename tmp-name1
) (file-truename tmp-name2
)))
1126 (should (file-equal-p tmp-name1 tmp-name2
)))
1128 (delete-file tmp-name1
)
1129 (delete-file tmp-name2
)))
1131 ;; `file-truename' shall preserve trailing link of directories.
1132 (unless (file-symlink-p tramp-test-temporary-file-directory
)
1133 (let* ((dir1 (directory-file-name tramp-test-temporary-file-directory
))
1134 (dir2 (file-name-as-directory dir1
)))
1135 (should (string-equal (file-truename dir1
) (expand-file-name dir1
)))
1136 (should (string-equal (file-truename dir2
) (expand-file-name dir2
)))))))
1138 (ert-deftest tramp-test22-file-times
()
1139 "Check `set-file-times' and `file-newer-than-file-p'."
1140 (skip-unless (tramp--test-enabled))
1144 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory
)
1145 '(tramp-gvfs-file-name-handler tramp-smb-file-name-handler
))))
1147 (let ((tmp-name1 (tramp--test-make-temp-name))
1148 (tmp-name2 (tramp--test-make-temp-name))
1149 (tmp-name3 (tramp--test-make-temp-name)))
1152 (write-region "foo" nil tmp-name1
)
1153 (should (file-exists-p tmp-name1
))
1154 (should (consp (nth 5 (file-attributes tmp-name1
))))
1155 ;; '(0 0) means don't know, and will be replaced by
1156 ;; `current-time'. Therefore, we use '(0 1).
1157 ;; We skip the test, if the remote handler is not able to
1158 ;; set the correct time.
1159 (skip-unless (set-file-times tmp-name1
'(0 1)))
1160 ;; Dumb remote shells without perl(1) or stat(1) are not
1161 ;; able to return the date correctly. They say "don't know".
1162 (unless (equal (nth 5 (file-attributes tmp-name1
)) '(0 0))
1163 (should (equal (nth 5 (file-attributes tmp-name1
)) '(0 1)))
1164 (write-region "bla" nil tmp-name2
)
1165 (should (file-exists-p tmp-name2
))
1166 (should (file-newer-than-file-p tmp-name2 tmp-name1
))
1167 ;; `tmp-name3' does not exist.
1168 (should (file-newer-than-file-p tmp-name2 tmp-name3
))
1169 (should-not (file-newer-than-file-p tmp-name3 tmp-name1
))))
1171 (delete-file tmp-name1
)
1172 (delete-file tmp-name2
)))))
1174 (ert-deftest tramp-test23-visited-file-modtime
()
1175 "Check `set-visited-file-modtime' and `verify-visited-file-modtime'."
1176 (skip-unless (tramp--test-enabled))
1178 (let ((tmp-name (tramp--test-make-temp-name)))
1181 (write-region "foo" nil tmp-name
)
1182 (should (file-exists-p tmp-name
))
1184 (insert-file-contents tmp-name
)
1185 (should (verify-visited-file-modtime))
1186 (set-visited-file-modtime '(0 1))
1187 (should (verify-visited-file-modtime))
1188 (should (equal (visited-file-modtime) '(0 1 0 0)))))
1189 (ignore-errors (delete-file tmp-name
)))))
1191 (ert-deftest tramp-test24-file-name-completion
()
1192 "Check `file-name-completion' and `file-name-all-completions'."
1193 (skip-unless (tramp--test-enabled))
1195 (let ((tmp-name (tramp--test-make-temp-name)))
1198 (make-directory tmp-name
)
1199 (should (file-directory-p tmp-name
))
1200 (write-region "foo" nil
(expand-file-name "foo" tmp-name
))
1201 (write-region "bar" nil
(expand-file-name "bold" tmp-name
))
1202 (make-directory (expand-file-name "boz" tmp-name
))
1203 (should (equal (file-name-completion "fo" tmp-name
) "foo"))
1204 (should (equal (file-name-completion "b" tmp-name
) "bo"))
1206 (equal (file-name-completion "b" tmp-name
'file-directory-p
) "boz/"))
1207 (should (equal (file-name-all-completions "fo" tmp-name
) '("foo")))
1209 (equal (sort (file-name-all-completions "b" tmp-name
) 'string-lessp
)
1211 (ignore-errors (delete-directory tmp-name
'recursive
)))))
1213 (ert-deftest tramp-test25-load
()
1215 (skip-unless (tramp--test-enabled))
1217 (let ((tmp-name (tramp--test-make-temp-name)))
1220 (load tmp-name
'noerror
'nomessage
)
1221 (should-not (featurep 'tramp-test-load
))
1222 (write-region "(provide 'tramp-test-load)" nil tmp-name
)
1223 ;; `load' in lread.c does not pass `must-suffix'. Why?
1224 ;(should-error (load tmp-name nil 'nomessage 'nosuffix 'must-suffix))
1225 (load tmp-name nil
'nomessage
'nosuffix
)
1226 (should (featurep 'tramp-test-load
)))
1228 (and (featurep 'tramp-test-load
) (unload-feature 'tramp-test-load
))
1229 (delete-file tmp-name
)))))
1231 (ert-deftest tramp-test26-process-file
()
1232 "Check `process-file'."
1233 (skip-unless (tramp--test-enabled))
1237 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory
)
1238 '(tramp-gvfs-file-name-handler tramp-smb-file-name-handler
))))
1240 (let ((tmp-name (tramp--test-make-temp-name))
1241 (default-directory tramp-test-temporary-file-directory
)
1242 kill-buffer-query-functions
)
1245 ;; We cannot use "/bin/true" and "/bin/false"; those paths
1246 ;; do not exist on hydra.
1247 (should (zerop (process-file "true")))
1248 (should-not (zerop (process-file "false")))
1249 (should-not (zerop (process-file "binary-does-not-exist")))
1251 (write-region "foo" nil tmp-name
)
1252 (should (file-exists-p tmp-name
))
1255 (process-file "ls" nil t nil
(file-name-nondirectory tmp-name
))))
1256 ;; `ls' could produce colorized output.
1257 (goto-char (point-min))
1258 (while (re-search-forward tramp-color-escape-sequence-regexp nil t
)
1259 (replace-match "" nil nil
))
1262 (format "%s\n" (file-name-nondirectory tmp-name
))
1264 (ignore-errors (delete-file tmp-name
)))))
1266 (ert-deftest tramp-test27-start-file-process
()
1267 "Check `start-file-process'."
1268 (skip-unless (tramp--test-enabled))
1272 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory
)
1273 '(tramp-adb-file-name-handler
1274 tramp-gvfs-file-name-handler
1275 tramp-smb-file-name-handler
))))
1277 (let ((default-directory tramp-test-temporary-file-directory
)
1278 (tmp-name (tramp--test-make-temp-name))
1279 kill-buffer-query-functions proc
)
1282 (setq proc
(start-file-process "test1" (current-buffer) "cat"))
1283 (should (processp proc
))
1284 (should (equal (process-status proc
) 'run
))
1285 (process-send-string proc
"foo")
1286 (process-send-eof proc
)
1287 (accept-process-output proc
1)
1288 (should (string-equal (buffer-string) "foo")))
1289 (ignore-errors (delete-process proc
)))
1293 (write-region "foo" nil tmp-name
)
1294 (should (file-exists-p tmp-name
))
1297 "test2" (current-buffer)
1298 "cat" (file-name-nondirectory tmp-name
)))
1299 (should (processp proc
))
1300 (accept-process-output proc
1)
1301 (should (string-equal (buffer-string) "foo")))
1303 (delete-process proc
)
1304 (delete-file tmp-name
)))
1308 (setq proc
(start-file-process "test3" nil
"cat"))
1309 (should (processp proc
))
1310 (should (equal (process-status proc
) 'run
))
1312 proc
(lambda (_p s
) (should (string-equal s
"foo"))))
1313 (process-send-string proc
"foo")
1314 (process-send-eof proc
)
1315 (accept-process-output proc
1))
1316 (ignore-errors (delete-process proc
)))))
1318 (ert-deftest tramp-test28-shell-command
()
1319 "Check `shell-command'."
1320 (skip-unless (tramp--test-enabled))
1324 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory
)
1325 '(tramp-adb-file-name-handler
1326 tramp-gvfs-file-name-handler
1327 tramp-smb-file-name-handler
))))
1329 (let ((tmp-name (tramp--test-make-temp-name))
1330 (default-directory tramp-test-temporary-file-directory
)
1331 kill-buffer-query-functions
)
1334 (write-region "foo" nil tmp-name
)
1335 (should (file-exists-p tmp-name
))
1337 (format "ls %s" (file-name-nondirectory tmp-name
)) (current-buffer))
1338 ;; `ls' could produce colorized output.
1339 (goto-char (point-min))
1340 (while (re-search-forward tramp-color-escape-sequence-regexp nil t
)
1341 (replace-match "" nil nil
))
1344 (format "%s\n" (file-name-nondirectory tmp-name
)) (buffer-string))))
1345 (ignore-errors (delete-file tmp-name
)))
1349 (write-region "foo" nil tmp-name
)
1350 (should (file-exists-p tmp-name
))
1351 (async-shell-command
1352 (format "ls %s" (file-name-nondirectory tmp-name
)) (current-buffer))
1353 (accept-process-output (get-buffer-process (current-buffer)) 1)
1354 (with-timeout (10 (ert-fail "`async-shell-command' timed out"))
1357 (memq (process-status (get-buffer-process (current-buffer)))
1359 (accept-process-output (get-buffer-process (current-buffer)) 1)))
1360 ;; `ls' could produce colorized output.
1361 (goto-char (point-min))
1362 (while (re-search-forward tramp-color-escape-sequence-regexp nil t
)
1363 (replace-match "" nil nil
))
1366 (format "%s\n" (file-name-nondirectory tmp-name
)) (buffer-string))))
1367 (ignore-errors (delete-file tmp-name
)))
1371 (write-region "foo" nil tmp-name
)
1372 (should (file-exists-p tmp-name
))
1373 (async-shell-command "read line; ls $line" (current-buffer))
1374 (process-send-string
1375 (get-buffer-process (current-buffer))
1376 (format "%s\n" (file-name-nondirectory tmp-name
)))
1377 (accept-process-output (get-buffer-process (current-buffer)) 1)
1378 (with-timeout (10 (ert-fail "`async-shell-command' timed out"))
1381 (memq (process-status (get-buffer-process (current-buffer)))
1383 (accept-process-output (get-buffer-process (current-buffer)) 1)))
1386 (format "%s\n" (file-name-nondirectory tmp-name
)) (buffer-string))))
1387 (ignore-errors (delete-file tmp-name
)))))
1389 (ert-deftest tramp-test29-vc-registered
()
1390 "Check `vc-registered'."
1391 (skip-unless (tramp--test-enabled))
1394 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory
)
1395 'tramp-sh-file-name-handler
))
1397 (let* ((default-directory tramp-test-temporary-file-directory
)
1398 (tmp-name1 (tramp--test-make-temp-name))
1399 (tmp-name2 (expand-file-name "foo" tmp-name1
))
1400 (vc-handled-backends
1401 (with-parsed-tramp-file-name tramp-test-temporary-file-directory nil
1403 ((tramp-find-executable v vc-bzr-program
(tramp-get-remote-path v
))
1405 ((tramp-find-executable v vc-git-program
(tramp-get-remote-path v
))
1407 ((tramp-find-executable v vc-hg-program
(tramp-get-remote-path v
))
1410 (skip-unless vc-handled-backends
)
1411 (message "%s" vc-handled-backends
)
1415 (make-directory tmp-name1
)
1416 (write-region "foo" nil tmp-name2
)
1417 (should (file-directory-p tmp-name1
))
1418 (should (file-exists-p tmp-name2
))
1419 (should-not (vc-registered tmp-name1
))
1420 (should-not (vc-registered tmp-name2
))
1422 (let ((default-directory tmp-name1
))
1423 ;; Create empty repository, and register the file.
1424 (vc-create-repo (car vc-handled-backends
))
1425 ;; The structure of VC-FILESET is not documented. Let's
1426 ;; hope it won't change.
1428 nil
(list (car vc-handled-backends
)
1429 (list (file-name-nondirectory tmp-name2
)))))
1430 (should (vc-registered tmp-name2
)))
1432 (ignore-errors (delete-directory tmp-name1
'recursive
)))))
1434 (defun tramp--test-check-files (&rest files
)
1435 "Runs a simple but comprehensive test over every file in FILES."
1436 (let ((tmp-name1 (tramp--test-make-temp-name))
1437 (tmp-name2 (tramp--test-make-temp-name 'local
)))
1440 (make-directory tmp-name1
)
1441 (make-directory tmp-name2
)
1443 (let ((file1 (expand-file-name elt tmp-name1
))
1444 (file2 (expand-file-name elt tmp-name2
)))
1445 (write-region elt nil file1
)
1446 (should (file-exists-p file1
))
1447 ;; Check file contents.
1449 (insert-file-contents file1
)
1450 (should (string-equal (buffer-string) elt
)))
1451 ;; Copy file both directions.
1452 (copy-file file1 tmp-name2
)
1453 (should (file-exists-p file2
))
1455 (should-not (file-exists-p file1
))
1456 (copy-file file2 tmp-name1
)
1457 (should (file-exists-p file1
))))
1458 ;; Check file names.
1459 (should (equal (directory-files
1460 tmp-name1 nil directory-files-no-dot-files-regexp
)
1461 (sort (copy-sequence files
) 'string-lessp
)))
1462 (should (equal (directory-files
1463 tmp-name2 nil directory-files-no-dot-files-regexp
)
1464 (sort files
'string-lessp
))))
1465 (ignore-errors (delete-directory tmp-name1
'recursive
))
1466 (ignore-errors (delete-directory tmp-name2
'recursive
)))))
1468 ;; This test is inspired by Bug#17238.
1469 (ert-deftest tramp-test30-special-characters
()
1470 "Check special characters in file names."
1471 (skip-unless (tramp--test-enabled))
1473 ;; Newlines, slashes and backslashes in file names are not supported.
1474 ;; So we don't test.
1475 (tramp--test-check-files
1490 (ert-deftest tramp-test31-utf8
()
1491 "Check UTF8 encoding in file names and file contents."
1492 (skip-unless (tramp--test-enabled))
1494 (let ((coding-system-for-read 'utf-8
)
1495 (coding-system-for-write 'utf-8
)
1496 (file-name-coding-system 'utf-8
))
1497 (tramp--test-check-files
1498 "أصبح بوسعك الآن تنزيل نسخة كاملة من موسوعة ويكيبيديا العربية لتصفحها بلا اتصال بالإنترنت"
1500 "Автостопом по гала́ктике")))
1502 ;; This test is inspired by Bug#16928.
1503 (ert-deftest tramp-test32-asynchronous-requests
()
1504 "Check parallel asynchronous requests.
1505 Such requests could arrive from timers, process filters and
1506 process sentinels. They shall not disturb each other."
1507 ;; Mark as failed until bug has been fixed.
1508 :expected-result
:failed
1509 (skip-unless (tramp--test-enabled))
1512 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory
)
1513 'tramp-sh-file-name-handler
))
1515 ;; Keep instrumentation verbosity 0 until Tramp bug is fixed. This
1516 ;; has the side effect, that this test fails instead to abort. Good
1518 (tramp--instrument-test-case 0
1519 (let* ((tmp-name (tramp--test-make-temp-name))
1520 (default-directory tmp-name
)
1521 (remote-file-name-inhibit-cache t
)
1522 timer buffers kill-buffer-query-functions
)
1526 (make-directory tmp-name
)
1528 ;; Setup a timer in order to raise an ordinary command again
1529 ;; and again. `vc-registered' is well suited, because there
1538 (buffer-name (nth (random (length buffers
)) buffers
)))))))
1540 ;; Create temporary buffers. The number of buffers
1541 ;; corresponds to the number of processes; it could be
1542 ;; increased in order to make pressure on Tramp.
1544 (add-to-list 'buffers
(generate-new-buffer "*temp*")))
1546 ;; Open asynchronous processes. Set process sentinel.
1547 (dolist (buf buffers
)
1548 (async-shell-command "read line; touch $line; echo $line" buf
)
1549 (set-process-sentinel
1550 (get-buffer-process buf
)
1551 (lambda (proc _state
)
1552 (delete-file (buffer-name (process-buffer proc
))))))
1554 ;; Send a string. Use a random order of the buffers. Mix
1555 ;; with regular operation.
1556 (let ((buffers (copy-sequence buffers
))
1559 (setq buf
(nth (random (length buffers
)) buffers
))
1560 (process-send-string
1561 (get-buffer-process buf
) (format "'%s'\n" buf
))
1562 (file-attributes (buffer-name buf
))
1563 (setq buffers
(delq buf buffers
))))
1565 ;; Wait until the whole output has been read.
1566 (with-timeout ((* 10 (length buffers
))
1567 (ert-fail "`async-shell-command' timed out"))
1568 (let ((buffers (copy-sequence buffers
))
1571 (setq buf
(nth (random (length buffers
)) buffers
))
1573 (memq (process-status (get-buffer-process buf
))
1575 (accept-process-output (get-buffer-process buf
) 0.1)
1576 (setq buffers
(delq buf buffers
))))))
1579 (dolist (buf buffers
)
1580 (with-current-buffer buf
1582 (string-equal (format "'%s'\n" buf
) (buffer-string)))))
1584 (directory-files tmp-name nil directory-files-no-dot-files-regexp
)))
1587 (ignore-errors (cancel-timer timer
))
1588 (ignore-errors (delete-directory tmp-name
'recursive
))
1589 (dolist (buf buffers
)
1590 (ignore-errors (kill-buffer buf
)))))))
1592 (ert-deftest tramp-test33-recursive-load
()
1593 "Check that Tramp does not fail due to recursive load."
1594 (skip-unless (tramp--test-enabled))
1599 "(expand-file-name %S)"
1600 tramp-test-temporary-file-directory
)
1602 "(let ((default-directory %S)) (expand-file-name %S))"
1603 tramp-test-temporary-file-directory
1604 temporary-file-directory
)))
1608 (shell-command-to-string
1610 "%s -batch -Q -L %s --eval %s"
1611 (expand-file-name invocation-name invocation-directory
)
1612 (mapconcat 'shell-quote-argument load-path
" -L ")
1613 (shell-quote-argument code
)))))))
1615 (ert-deftest tramp-test34-unload
()
1616 "Check that Tramp and its subpackages unload completely.
1617 Since it unloads Tramp, it shall be the last test to run."
1618 ;; Mark as failed until all symbols are unbound.
1619 :expected-result
(if (featurep 'tramp
) :failed
:passed
)
1620 (when (featurep 'tramp
)
1621 (unload-feature 'tramp
'force
)
1622 ;; No Tramp feature must be left.
1623 (should-not (featurep 'tramp
))
1624 (should-not (all-completions "tramp" (delq 'tramp-tests features
)))
1625 ;; `file-name-handler-alist' must be clean.
1626 (should-not (all-completions "tramp" (mapcar 'cdr file-name-handler-alist
)))
1627 ;; There shouldn't be left a bound symbol. We do not regard our
1628 ;; test symbols, and the Tramp unload hooks.
1631 (and (or (boundp x
) (functionp x
))
1632 (string-match "^tramp" (symbol-name x
))
1633 (not (string-match "^tramp--?test" (symbol-name x
)))
1634 (not (string-match "unload-hook$" (symbol-name x
)))
1635 (ert-fail (format "`%s' still bound" x
)))))
1636 ; (progn (message "`%s' still bound" x)))))
1637 ;; There shouldn't be left a hook function containing a Tramp
1638 ;; function. We do not regard the Tramp unload hooks.
1642 (string-match "-hooks?$" (symbol-name x
))
1643 (not (string-match "unload-hook$" (symbol-name x
)))
1644 (consp (symbol-value x
))
1645 (ignore-errors (all-completions "tramp" (symbol-value x
)))
1646 (ert-fail (format "Hook `%s' still contains Tramp function" x
)))))))
1650 ;; * dired-compress-file
1653 ;; * file-ownership-preserved-p
1654 ;; * file-selinux-context
1655 ;; * find-backup-file-name
1656 ;; * make-auto-save-file-name
1658 ;; * set-file-selinux-context
1660 ;; * Fix `tramp-test27-start-file-process' on MS Windows (`process-send-eof'?).
1661 ;; * Fix `tramp-test28-shell-command' on MS Windows (nasty plink message).
1662 ;; * Fix `tramp-test31-utf8' for MS Windows and `nc'/`telnet' (when
1663 ;; target is a dumb busybox). Seems to be in `directory-files'.
1664 ;; * Fix Bug#16928. Set expected error of `tramp-test32-asynchronous-requests'.
1665 ;; * Fix `tramp-test34-unload' (Not all symbols are unbound). Set
1668 (defun tramp-test-all (&optional interactive
)
1669 "Run all tests for \\[tramp]."
1672 (if interactive
'ert-run-tests-interactively
'ert-run-tests-batch
) "^tramp"))
1674 (provide 'tramp-tests
)
1675 ;;; tramp-tests.el ends here