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 handler 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 ;; When running the tests in batch mode, it must NOT require an
37 ;; interactive password prompt unless the environment variable
38 ;; $REMOTE_ALLOW_PASSWORD is set.
40 ;; A whole test run can be performed calling the command `tramp-test-all'.
51 (declare-function tramp-find-executable
"tramp-sh")
52 (declare-function tramp-get-remote-path
"tramp-sh")
53 (defvar tramp-copy-size-limit
)
55 ;; There is no default value on w32 systems, which could work out of the box.
56 (defconst tramp-test-temporary-file-directory
58 ((getenv "REMOTE_TEMPORARY_FILE_DIRECTORY"))
59 ((eq system-type
'windows-nt
) null-device
)
63 (tramp-login-program "sh")
64 (tramp-login-args (("-i")))
65 (tramp-remote-shell "/bin/sh")
66 (tramp-remote-shell-args ("-c"))
67 (tramp-connection-timeout 10)))
68 (format "/mock::%s" temporary-file-directory
)))
69 "Temporary directory for Tramp tests.")
71 (setq password-cache-expiry nil
73 tramp-copy-size-limit nil
74 tramp-message-show-message nil
)
76 ;; Disable interactive passwords in batch mode.
77 (when (and noninteractive
(not (getenv "REMOTE_ALLOW_PASSWORD")))
78 (defalias 'tramp-read-passwd
'ignore
))
80 ;; This shall happen on hydra only.
81 (when (getenv "NIX_STORE")
82 (add-to-list 'tramp-remote-path
'tramp-own-remote-path
))
84 (defvar tramp--test-enabled-checked nil
85 "Cached result of `tramp--test-enabled'.
86 If the function did run, the value is a cons cell, the `cdr'
89 (defun tramp--test-enabled ()
90 "Whether remote file access is enabled."
91 (unless (consp tramp--test-enabled-checked
)
93 tramp--test-enabled-checked
97 (file-remote-p tramp-test-temporary-file-directory
)
98 (file-directory-p tramp-test-temporary-file-directory
)
99 (file-writable-p tramp-test-temporary-file-directory
))))))
101 (when (cdr tramp--test-enabled-checked
)
102 ;; Cleanup connection.
103 (tramp-cleanup-connection
104 (tramp-dissect-file-name tramp-test-temporary-file-directory
)
108 (cdr tramp--test-enabled-checked
))
110 (defun tramp--test-make-temp-name (&optional local
)
111 "Create a temporary file name for test."
113 (make-temp-name "tramp-test")
114 (if local temporary-file-directory tramp-test-temporary-file-directory
)))
116 (defmacro tramp--instrument-test-case
(verbose &rest body
)
117 "Run BODY with `tramp-verbose' equal VERBOSE.
118 Print the the content of the Tramp debug buffer, if BODY does not
119 eval properly in `should', `should-not' or `should-error'."
120 (declare (indent 1) (debug (natnump body
)))
121 `(let ((tramp-verbose ,verbose
)
122 (tramp-message-show-message t
)
123 (tramp-debug-on-error t
))
125 ;; In general, we cannot use a timeout here: this would
126 ;; prevent traces when the test runs into an error.
127 ; (with-timeout (10 (ert-fail "`tramp--instrument-test-case' timed out"))
131 (signal (car err
) (cdr err
)))
133 (with-parsed-tramp-file-name tramp-test-temporary-file-directory nil
134 (with-current-buffer (tramp-get-connection-buffer v
)
135 (message "%s" (buffer-string)))
136 (with-current-buffer (tramp-get-debug-buffer v
)
137 (message "%s" (buffer-string))))
139 (signal (car err
) (cdr err
))))))
141 (ert-deftest tramp-test00-availability
()
142 "Test availability of Tramp functions."
143 :expected-result
(if (tramp--test-enabled) :passed
:failed
)
144 (message "Remote directory: `%s'" tramp-test-temporary-file-directory
)
145 (should (ignore-errors
147 (file-remote-p tramp-test-temporary-file-directory
)
148 (file-directory-p tramp-test-temporary-file-directory
)
149 (file-writable-p tramp-test-temporary-file-directory
)))))
151 (ert-deftest tramp-test01-file-name-syntax
()
152 "Check remote file name syntax."
154 (should (tramp-tramp-file-p "/method::"))
155 (should (tramp-tramp-file-p "/host:"))
156 (should (tramp-tramp-file-p "/user@:"))
157 (should (tramp-tramp-file-p "/user@host:"))
158 (should (tramp-tramp-file-p "/method:host:"))
159 (should (tramp-tramp-file-p "/method:user@:"))
160 (should (tramp-tramp-file-p "/method:user@host:"))
161 (should (tramp-tramp-file-p "/method:user@email@host:"))
164 (should (tramp-tramp-file-p "/host#1234:"))
165 (should (tramp-tramp-file-p "/user@host#1234:"))
166 (should (tramp-tramp-file-p "/method:host#1234:"))
167 (should (tramp-tramp-file-p "/method:user@host#1234:"))
169 ;; Using an IPv4 address.
170 (should (tramp-tramp-file-p "/1.2.3.4:"))
171 (should (tramp-tramp-file-p "/user@1.2.3.4:"))
172 (should (tramp-tramp-file-p "/method:1.2.3.4:"))
173 (should (tramp-tramp-file-p "/method:user@1.2.3.4:"))
175 ;; Using an IPv6 address.
176 (should (tramp-tramp-file-p "/[]:"))
177 (should (tramp-tramp-file-p "/[::1]:"))
178 (should (tramp-tramp-file-p "/user@[::1]:"))
179 (should (tramp-tramp-file-p "/method:[::1]:"))
180 (should (tramp-tramp-file-p "/method:user@[::1]:"))
182 ;; Local file name part.
183 (should (tramp-tramp-file-p "/host:/:"))
184 (should (tramp-tramp-file-p "/method:::"))
185 (should (tramp-tramp-file-p "/method::/path/to/file"))
186 (should (tramp-tramp-file-p "/method::file"))
189 (should (tramp-tramp-file-p "/method1:|method2::"))
190 (should (tramp-tramp-file-p "/method1:host1|host2:"))
191 (should (tramp-tramp-file-p "/method1:host1|method2:host2:"))
192 (should (tramp-tramp-file-p "/method1:user1@host1|method2:user2@host2:"))
193 (should (tramp-tramp-file-p
194 "/method1:user1@host1|method2:user2@host2|method3:user3@host3:"))
197 (should-not (tramp-tramp-file-p nil
))
198 (should-not (tramp-tramp-file-p 'symbol
))
199 ;; "/:" suppresses file name handlers.
200 (should-not (tramp-tramp-file-p "/::"))
201 (should-not (tramp-tramp-file-p "/:@:"))
202 (should-not (tramp-tramp-file-p "/:[]:"))
203 ;; Multihops require a method.
204 (should-not (tramp-tramp-file-p "/host1|host2:"))
205 ;; Methods or hostnames shall be at least two characters on MS Windows.
206 (when (memq system-type
'(cygwin windows-nt
))
207 (should-not (tramp-tramp-file-p "/c:/path/to/file"))
208 (should-not (tramp-tramp-file-p "/c::/path/to/file"))))
210 (ert-deftest tramp-test02-file-name-dissect
()
211 "Check remote file name components."
212 (let ((tramp-default-method "default-method")
213 (tramp-default-user "default-user")
214 (tramp-default-host "default-host"))
215 ;; Expand `tramp-default-user' and `tramp-default-host'.
216 (should (string-equal
217 (file-remote-p "/method::")
218 (format "/%s:%s@%s:" "method" "default-user" "default-host")))
219 (should (string-equal (file-remote-p "/method::" 'method
) "method"))
220 (should (string-equal (file-remote-p "/method::" 'user
) "default-user"))
221 (should (string-equal (file-remote-p "/method::" 'host
) "default-host"))
222 (should (string-equal (file-remote-p "/method::" 'localname
) ""))
224 ;; Expand `tramp-default-method' and `tramp-default-user'.
225 (should (string-equal
226 (file-remote-p "/host:")
227 (format "/%s:%s@%s:" "default-method" "default-user" "host")))
228 (should (string-equal (file-remote-p "/host:" 'method
) "default-method"))
229 (should (string-equal (file-remote-p "/host:" 'user
) "default-user"))
230 (should (string-equal (file-remote-p "/host:" 'host
) "host"))
231 (should (string-equal (file-remote-p "/host:" 'localname
) ""))
233 ;; Expand `tramp-default-method' and `tramp-default-host'.
234 (should (string-equal
235 (file-remote-p "/user@:")
236 (format "/%s:%s@%s:" "default-method""user" "default-host")))
237 (should (string-equal (file-remote-p "/user@:" 'method
) "default-method"))
238 (should (string-equal (file-remote-p "/user@:" 'user
) "user"))
239 (should (string-equal (file-remote-p "/user@:" 'host
) "default-host"))
240 (should (string-equal (file-remote-p "/user@:" 'localname
) ""))
242 ;; Expand `tramp-default-method'.
243 (should (string-equal
244 (file-remote-p "/user@host:")
245 (format "/%s:%s@%s:" "default-method" "user" "host")))
246 (should (string-equal
247 (file-remote-p "/user@host:" 'method
) "default-method"))
248 (should (string-equal (file-remote-p "/user@host:" 'user
) "user"))
249 (should (string-equal (file-remote-p "/user@host:" 'host
) "host"))
250 (should (string-equal (file-remote-p "/user@host:" 'localname
) ""))
252 ;; Expand `tramp-default-user'.
253 (should (string-equal
254 (file-remote-p "/method:host:")
255 (format "/%s:%s@%s:" "method" "default-user" "host")))
256 (should (string-equal (file-remote-p "/method:host:" 'method
) "method"))
257 (should (string-equal (file-remote-p "/method:host:" 'user
) "default-user"))
258 (should (string-equal (file-remote-p "/method:host:" 'host
) "host"))
259 (should (string-equal (file-remote-p "/method:host:" 'localname
) ""))
261 ;; Expand `tramp-default-host'.
262 (should (string-equal
263 (file-remote-p "/method:user@:")
264 (format "/%s:%s@%s:" "method" "user" "default-host")))
265 (should (string-equal (file-remote-p "/method:user@:" 'method
) "method"))
266 (should (string-equal (file-remote-p "/method:user@:" 'user
) "user"))
267 (should (string-equal (file-remote-p "/method:user@:" 'host
)
269 (should (string-equal (file-remote-p "/method:user@:" 'localname
) ""))
272 (should (string-equal
273 (file-remote-p "/method:user@host:")
274 (format "/%s:%s@%s:" "method" "user" "host")))
275 (should (string-equal
276 (file-remote-p "/method:user@host:" 'method
) "method"))
277 (should (string-equal (file-remote-p "/method:user@host:" 'user
) "user"))
278 (should (string-equal (file-remote-p "/method:user@host:" 'host
) "host"))
279 (should (string-equal (file-remote-p "/method:user@host:" 'localname
) ""))
282 (should (string-equal
283 (file-remote-p "/method:user@email@host:")
284 (format "/%s:%s@%s:" "method" "user@email" "host")))
285 (should (string-equal
286 (file-remote-p "/method:user@email@host:" 'method
) "method"))
287 (should (string-equal
288 (file-remote-p "/method:user@email@host:" 'user
) "user@email"))
289 (should (string-equal
290 (file-remote-p "/method:user@email@host:" 'host
) "host"))
291 (should (string-equal
292 (file-remote-p "/method:user@email@host:" 'localname
) ""))
294 ;; Expand `tramp-default-method' and `tramp-default-user'.
295 (should (string-equal
296 (file-remote-p "/host#1234:")
297 (format "/%s:%s@%s:" "default-method" "default-user" "host#1234")))
298 (should (string-equal
299 (file-remote-p "/host#1234:" 'method
) "default-method"))
300 (should (string-equal (file-remote-p "/host#1234:" 'user
) "default-user"))
301 (should (string-equal (file-remote-p "/host#1234:" 'host
) "host#1234"))
302 (should (string-equal (file-remote-p "/host#1234:" 'localname
) ""))
304 ;; Expand `tramp-default-method'.
305 (should (string-equal
306 (file-remote-p "/user@host#1234:")
307 (format "/%s:%s@%s:" "default-method" "user" "host#1234")))
308 (should (string-equal
309 (file-remote-p "/user@host#1234:" 'method
) "default-method"))
310 (should (string-equal (file-remote-p "/user@host#1234:" 'user
) "user"))
311 (should (string-equal (file-remote-p "/user@host#1234:" 'host
) "host#1234"))
312 (should (string-equal (file-remote-p "/user@host#1234:" 'localname
) ""))
314 ;; Expand `tramp-default-user'.
315 (should (string-equal
316 (file-remote-p "/method:host#1234:")
317 (format "/%s:%s@%s:" "method" "default-user" "host#1234")))
318 (should (string-equal
319 (file-remote-p "/method:host#1234:" 'method
) "method"))
320 (should (string-equal
321 (file-remote-p "/method:host#1234:" 'user
) "default-user"))
322 (should (string-equal
323 (file-remote-p "/method:host#1234:" 'host
) "host#1234"))
324 (should (string-equal (file-remote-p "/method:host#1234:" 'localname
) ""))
327 (should (string-equal
328 (file-remote-p "/method:user@host#1234:")
329 (format "/%s:%s@%s:" "method" "user" "host#1234")))
330 (should (string-equal
331 (file-remote-p "/method:user@host#1234:" 'method
) "method"))
332 (should (string-equal
333 (file-remote-p "/method:user@host#1234:" 'user
) "user"))
334 (should (string-equal
335 (file-remote-p "/method:user@host#1234:" 'host
) "host#1234"))
336 (should (string-equal
337 (file-remote-p "/method:user@host#1234:" 'localname
) ""))
339 ;; Expand `tramp-default-method' and `tramp-default-user'.
340 (should (string-equal
341 (file-remote-p "/1.2.3.4:")
342 (format "/%s:%s@%s:" "default-method" "default-user" "1.2.3.4")))
343 (should (string-equal (file-remote-p "/1.2.3.4:" 'method
) "default-method"))
344 (should (string-equal (file-remote-p "/1.2.3.4:" 'user
) "default-user"))
345 (should (string-equal (file-remote-p "/1.2.3.4:" 'host
) "1.2.3.4"))
346 (should (string-equal (file-remote-p "/1.2.3.4:" 'localname
) ""))
348 ;; Expand `tramp-default-method'.
349 (should (string-equal
350 (file-remote-p "/user@1.2.3.4:")
351 (format "/%s:%s@%s:" "default-method" "user" "1.2.3.4")))
352 (should (string-equal
353 (file-remote-p "/user@1.2.3.4:" 'method
) "default-method"))
354 (should (string-equal (file-remote-p "/user@1.2.3.4:" 'user
) "user"))
355 (should (string-equal (file-remote-p "/user@1.2.3.4:" 'host
) "1.2.3.4"))
356 (should (string-equal (file-remote-p "/user@1.2.3.4:" 'localname
) ""))
358 ;; Expand `tramp-default-user'.
359 (should (string-equal
360 (file-remote-p "/method:1.2.3.4:")
361 (format "/%s:%s@%s:" "method" "default-user" "1.2.3.4")))
362 (should (string-equal (file-remote-p "/method:1.2.3.4:" 'method
) "method"))
363 (should (string-equal
364 (file-remote-p "/method:1.2.3.4:" 'user
) "default-user"))
365 (should (string-equal (file-remote-p "/method:1.2.3.4:" 'host
) "1.2.3.4"))
366 (should (string-equal (file-remote-p "/method:1.2.3.4:" 'localname
) ""))
369 (should (string-equal
370 (file-remote-p "/method:user@1.2.3.4:")
371 (format "/%s:%s@%s:" "method" "user" "1.2.3.4")))
372 (should (string-equal
373 (file-remote-p "/method:user@1.2.3.4:" 'method
) "method"))
374 (should (string-equal (file-remote-p "/method:user@1.2.3.4:" 'user
) "user"))
375 (should (string-equal
376 (file-remote-p "/method:user@1.2.3.4:" 'host
) "1.2.3.4"))
377 (should (string-equal
378 (file-remote-p "/method:user@1.2.3.4:" 'localname
) ""))
380 ;; Expand `tramp-default-method', `tramp-default-user' and
381 ;; `tramp-default-host'.
382 (should (string-equal
383 (file-remote-p "/[]:")
385 "/%s:%s@%s:" "default-method" "default-user" "default-host")))
386 (should (string-equal (file-remote-p "/[]:" 'method
) "default-method"))
387 (should (string-equal (file-remote-p "/[]:" 'user
) "default-user"))
388 (should (string-equal (file-remote-p "/[]:" 'host
) "default-host"))
389 (should (string-equal (file-remote-p "/[]:" 'localname
) ""))
391 ;; Expand `tramp-default-method' and `tramp-default-user'.
392 (let ((tramp-default-host "::1"))
393 (should (string-equal
394 (file-remote-p "/[]:")
395 (format "/%s:%s@%s:" "default-method" "default-user" "[::1]")))
396 (should (string-equal (file-remote-p "/[]:" 'method
) "default-method"))
397 (should (string-equal (file-remote-p "/[]:" 'user
) "default-user"))
398 (should (string-equal (file-remote-p "/[]:" 'host
) "::1"))
399 (should (string-equal (file-remote-p "/[]:" 'localname
) "")))
401 ;; Expand `tramp-default-method' and `tramp-default-user'.
402 (should (string-equal
403 (file-remote-p "/[::1]:")
404 (format "/%s:%s@%s:" "default-method" "default-user" "[::1]")))
405 (should (string-equal (file-remote-p "/[::1]:" 'method
) "default-method"))
406 (should (string-equal (file-remote-p "/[::1]:" 'user
) "default-user"))
407 (should (string-equal (file-remote-p "/[::1]:" 'host
) "::1"))
408 (should (string-equal (file-remote-p "/[::1]:" 'localname
) ""))
410 ;; Expand `tramp-default-method'.
411 (should (string-equal
412 (file-remote-p "/user@[::1]:")
413 (format "/%s:%s@%s:" "default-method" "user" "[::1]")))
414 (should (string-equal
415 (file-remote-p "/user@[::1]:" 'method
) "default-method"))
416 (should (string-equal (file-remote-p "/user@[::1]:" 'user
) "user"))
417 (should (string-equal (file-remote-p "/user@[::1]:" 'host
) "::1"))
418 (should (string-equal (file-remote-p "/user@[::1]:" 'localname
) ""))
420 ;; Expand `tramp-default-user'.
421 (should (string-equal
422 (file-remote-p "/method:[::1]:")
423 (format "/%s:%s@%s:" "method" "default-user" "[::1]")))
424 (should (string-equal (file-remote-p "/method:[::1]:" 'method
) "method"))
425 (should (string-equal
426 (file-remote-p "/method:[::1]:" 'user
) "default-user"))
427 (should (string-equal (file-remote-p "/method:[::1]:" 'host
) "::1"))
428 (should (string-equal (file-remote-p "/method:[::1]:" 'localname
) ""))
431 (should (string-equal
432 (file-remote-p "/method:user@[::1]:")
433 (format "/%s:%s@%s:" "method" "user" "[::1]")))
434 (should (string-equal
435 (file-remote-p "/method:user@[::1]:" 'method
) "method"))
436 (should (string-equal (file-remote-p "/method:user@[::1]:" 'user
) "user"))
437 (should (string-equal (file-remote-p "/method:user@[::1]:" 'host
) "::1"))
438 (should (string-equal
439 (file-remote-p "/method:user@[::1]:" 'localname
) ""))
441 ;; Local file name part.
442 (should (string-equal (file-remote-p "/host:/:" 'localname
) "/:"))
443 (should (string-equal (file-remote-p "/method:::" 'localname
) ":"))
444 (should (string-equal (file-remote-p "/method:: " 'localname
) " "))
445 (should (string-equal (file-remote-p "/method::file" 'localname
) "file"))
446 (should (string-equal
447 (file-remote-p "/method::/path/to/file" 'localname
)
453 (file-remote-p "/method1:user1@host1|method2:user2@host2:/path/to/file")
454 (format "/%s:%s@%s:" "method2" "user2" "host2")))
458 "/method1:user1@host1|method2:user2@host2:/path/to/file" 'method
)
463 "/method1:user1@host1|method2:user2@host2:/path/to/file" 'user
)
468 "/method1:user1@host1|method2:user2@host2:/path/to/file" 'host
)
473 "/method1:user1@host1|method2:user2@host2:/path/to/file" 'localname
)
479 "/method1:user1@host1|method2:user2@host2|method3:user3@host3:/path/to/file")
480 (format "/%s:%s@%s:" "method3" "user3" "host3")))
484 "/method1:user1@host1|method2:user2@host2|method3:user3@host3:/path/to/file"
490 "/method1:user1@host1|method2:user2@host2|method3:user3@host3:/path/to/file"
496 "/method1:user1@host1|method2:user2@host2|method3:user3@host3:/path/to/file"
502 "/method1:user1@host1|method2:user2@host2|method3:user3@host3:/path/to/file"
506 (ert-deftest tramp-test03-file-name-defaults
()
507 "Check default values for some methods."
508 ;; Default values in tramp-adb.el.
509 (should (string-equal (file-remote-p "/adb::" 'host
) ""))
510 ;; Default values in tramp-ftp.el.
511 (should (string-equal (file-remote-p "/ftp.host:" 'method
) "ftp"))
512 (dolist (u '("ftp" "anonymous"))
513 (should (string-equal (file-remote-p (format "/%s@:" u
) 'method
) "ftp")))
514 ;; Default values in tramp-gvfs.el.
515 (when (and (load "tramp-gvfs" 'noerror
'nomessage
)
516 (symbol-value 'tramp-gvfs-enabled
))
517 (should (string-equal (file-remote-p "/synce::" 'user
) nil
)))
518 ;; Default values in tramp-gw.el.
519 (dolist (m '("tunnel" "socks"))
521 (string-equal (file-remote-p (format "/%s::" m
) 'user
) (user-login-name))))
522 ;; Default values in tramp-sh.el.
523 (dolist (h `("127.0.0.1" "[::1]" "localhost" "localhost6" ,(system-name)))
524 (should (string-equal (file-remote-p (format "/root@%s:" h
) 'method
) "su")))
525 (dolist (m '("su" "sudo" "ksu"))
526 (should (string-equal (file-remote-p (format "/%s::" m
) 'user
) "root")))
527 (dolist (m '("rcp" "remcp" "rsh" "telnet" "krlogin" "fcp"))
529 (string-equal (file-remote-p (format "/%s::" m
) 'user
) (user-login-name))))
530 ;; Default values in tramp-smb.el.
531 (should (string-equal (file-remote-p "/user%domain@host:" 'method
) "smb"))
532 (should (string-equal (file-remote-p "/smb::" 'user
) nil
)))
534 (ert-deftest tramp-test04-substitute-in-file-name
()
535 "Check `substitute-in-file-name'."
536 (should (string-equal (substitute-in-file-name "/method:host://foo") "/foo"))
539 (substitute-in-file-name "/method:host:/path//foo") "/method:host:/foo"))
541 (string-equal (substitute-in-file-name "/method:host:/path///foo") "/foo"))
544 (substitute-in-file-name "/method:host:/path/~/foo") "/method:host:~/foo"))
546 (string-equal (substitute-in-file-name "/method:host:/path//~/foo") "~/foo"))
547 (let (process-environment)
550 (substitute-in-file-name "/method:host:/path/$FOO")
551 "/method:host:/path/$FOO"))
555 (substitute-in-file-name "/method:host:/path/$FOO")
556 "/method:host:/path/bla"))
559 (substitute-in-file-name "/method:host:/path/$$FOO")
560 "/method:host:/path/$FOO"))))
562 (ert-deftest tramp-test05-expand-file-name
()
563 "Check `expand-file-name'."
566 (expand-file-name "/method:host:/path/./file") "/method:host:/path/file"))
569 (expand-file-name "/method:host:/path/../file") "/method:host:/file")))
571 (ert-deftest tramp-test06-directory-file-name
()
572 "Check `directory-file-name'.
573 This checks also `file-name-as-directory', `file-name-directory'
574 and `file-name-nondirectory'."
577 (directory-file-name "/method:host:/path/to/file")
578 "/method:host:/path/to/file"))
581 (directory-file-name "/method:host:/path/to/file/")
582 "/method:host:/path/to/file"))
585 (file-name-as-directory "/method:host:/path/to/file")
586 "/method:host:/path/to/file/"))
589 (file-name-as-directory "/method:host:/path/to/file/")
590 "/method:host:/path/to/file/"))
593 (file-name-directory "/method:host:/path/to/file")
594 "/method:host:/path/to/"))
597 (file-name-directory "/method:host:/path/to/file/")
598 "/method:host:/path/to/file/"))
600 (string-equal (file-name-nondirectory "/method:host:/path/to/file") "file"))
602 (string-equal (file-name-nondirectory "/method:host:/path/to/file/") ""))
605 (unhandled-file-name-directory "/method:host:/path/to/file"))))
607 (ert-deftest tramp-test07-file-exists-p
()
608 "Check `file-exist-p', `write-region' and `delete-file'."
609 (skip-unless (tramp--test-enabled))
611 (let ((tmp-name (tramp--test-make-temp-name)))
612 (should-not (file-exists-p tmp-name
))
613 (write-region "foo" nil tmp-name
)
614 (should (file-exists-p tmp-name
))
615 (delete-file tmp-name
)
616 (should-not (file-exists-p tmp-name
))))
618 (ert-deftest tramp-test08-file-local-copy
()
619 "Check `file-local-copy'."
620 (skip-unless (tramp--test-enabled))
622 (let ((tmp-name1 (tramp--test-make-temp-name))
626 (write-region "foo" nil tmp-name1
)
627 (should (setq tmp-name2
(file-local-copy tmp-name1
)))
629 (insert-file-contents tmp-name2
)
630 (should (string-equal (buffer-string) "foo"))))
632 (delete-file tmp-name1
)
633 (delete-file tmp-name2
)))))
635 (ert-deftest tramp-test09-insert-file-contents
()
636 "Check `insert-file-contents'."
637 (skip-unless (tramp--test-enabled))
639 (let ((tmp-name (tramp--test-make-temp-name)))
642 (write-region "foo" nil tmp-name
)
644 (insert-file-contents tmp-name
)
645 (should (string-equal (buffer-string) "foo"))
646 (insert-file-contents tmp-name
)
647 (should (string-equal (buffer-string) "foofoo"))
649 (insert-file-contents tmp-name nil
1 3)
650 (should (string-equal (buffer-string) "oofoofoo"))
652 (insert-file-contents tmp-name nil nil nil
'replace
)
653 (should (string-equal (buffer-string) "foo"))))
654 (ignore-errors (delete-file tmp-name
)))))
656 (ert-deftest tramp-test10-write-region
()
657 "Check `write-region'."
658 (skip-unless (tramp--test-enabled))
660 (let ((tmp-name (tramp--test-make-temp-name)))
665 (write-region nil nil tmp-name
))
667 (insert-file-contents tmp-name
)
668 (should (string-equal (buffer-string) "foo")))
672 (write-region nil nil tmp-name
'append
))
674 (insert-file-contents tmp-name
)
675 (should (string-equal (buffer-string) "foobla")))
677 (write-region "foo" nil tmp-name
)
679 (insert-file-contents tmp-name
)
680 (should (string-equal (buffer-string) "foo")))
684 (write-region 3 5 tmp-name
))
686 (insert-file-contents tmp-name
)
687 (should (string-equal (buffer-string) "34"))))
688 (ignore-errors (delete-file tmp-name
)))))
690 (ert-deftest tramp-test11-copy-file
()
692 (skip-unless (tramp--test-enabled))
694 (let ((tmp-name1 (tramp--test-make-temp-name))
695 (tmp-name2 (tramp--test-make-temp-name))
696 (tmp-name3 (tramp--test-make-temp-name))
697 (tmp-name4 (tramp--test-make-temp-name 'local
))
698 (tmp-name5 (tramp--test-make-temp-name 'local
)))
700 ;; Copy on remote side.
703 (write-region "foo" nil tmp-name1
)
704 (copy-file tmp-name1 tmp-name2
)
705 (should (file-exists-p tmp-name2
))
707 (insert-file-contents tmp-name2
)
708 (should (string-equal (buffer-string) "foo")))
709 (should-error (copy-file tmp-name1 tmp-name2
))
710 (copy-file tmp-name1 tmp-name2
'ok
)
711 (make-directory tmp-name3
)
712 (copy-file tmp-name1 tmp-name3
)
715 (expand-file-name (file-name-nondirectory tmp-name1
) tmp-name3
))))
716 (ignore-errors (delete-file tmp-name1
))
717 (ignore-errors (delete-file tmp-name2
))
718 (ignore-errors (delete-directory tmp-name3
'recursive
)))
720 ;; Copy from remote side to local side.
723 (write-region "foo" nil tmp-name1
)
724 (copy-file tmp-name1 tmp-name4
)
725 (should (file-exists-p tmp-name4
))
727 (insert-file-contents tmp-name4
)
728 (should (string-equal (buffer-string) "foo")))
729 (should-error (copy-file tmp-name1 tmp-name4
))
730 (copy-file tmp-name1 tmp-name4
'ok
)
731 (make-directory tmp-name5
)
732 (copy-file tmp-name1 tmp-name5
)
735 (expand-file-name (file-name-nondirectory tmp-name1
) tmp-name5
))))
736 (ignore-errors (delete-file tmp-name1
))
737 (ignore-errors (delete-file tmp-name4
))
738 (ignore-errors (delete-directory tmp-name5
'recursive
)))
740 ;; Copy from local side to remote side.
743 (write-region "foo" nil tmp-name4 nil
'nomessage
)
744 (copy-file tmp-name4 tmp-name1
)
745 (should (file-exists-p tmp-name1
))
747 (insert-file-contents tmp-name1
)
748 (should (string-equal (buffer-string) "foo")))
749 (should-error (copy-file tmp-name4 tmp-name1
))
750 (copy-file tmp-name4 tmp-name1
'ok
)
751 (make-directory tmp-name3
)
752 (copy-file tmp-name4 tmp-name3
)
755 (expand-file-name (file-name-nondirectory tmp-name4
) tmp-name3
))))
756 (ignore-errors (delete-file tmp-name1
))
757 (ignore-errors (delete-file tmp-name4
))
758 (ignore-errors (delete-directory tmp-name3
'recursive
)))))
760 (ert-deftest tramp-test12-rename-file
()
761 "Check `rename-file'."
762 (skip-unless (tramp--test-enabled))
764 (let ((tmp-name1 (tramp--test-make-temp-name))
765 (tmp-name2 (tramp--test-make-temp-name))
766 (tmp-name3 (tramp--test-make-temp-name))
767 (tmp-name4 (tramp--test-make-temp-name 'local
))
768 (tmp-name5 (tramp--test-make-temp-name 'local
)))
770 ;; Rename on remote side.
773 (write-region "foo" nil tmp-name1
)
774 (rename-file tmp-name1 tmp-name2
)
775 (should-not (file-exists-p tmp-name1
))
776 (should (file-exists-p tmp-name2
))
778 (insert-file-contents tmp-name2
)
779 (should (string-equal (buffer-string) "foo")))
780 (write-region "foo" nil tmp-name1
)
781 (should-error (rename-file tmp-name1 tmp-name2
))
782 (rename-file tmp-name1 tmp-name2
'ok
)
783 (should-not (file-exists-p tmp-name1
))
784 (write-region "foo" nil tmp-name1
)
785 (make-directory tmp-name3
)
786 (rename-file tmp-name1 tmp-name3
)
787 (should-not (file-exists-p tmp-name1
))
790 (expand-file-name (file-name-nondirectory tmp-name1
) tmp-name3
))))
791 (ignore-errors (delete-file tmp-name1
))
792 (ignore-errors (delete-file tmp-name2
))
793 (ignore-errors (delete-directory tmp-name3
'recursive
)))
795 ;; Rename from remote side to local side.
798 (write-region "foo" nil tmp-name1
)
799 (rename-file tmp-name1 tmp-name4
)
800 (should-not (file-exists-p tmp-name1
))
801 (should (file-exists-p tmp-name4
))
803 (insert-file-contents tmp-name4
)
804 (should (string-equal (buffer-string) "foo")))
805 (write-region "foo" nil tmp-name1
)
806 (should-error (rename-file tmp-name1 tmp-name4
))
807 (rename-file tmp-name1 tmp-name4
'ok
)
808 (should-not (file-exists-p tmp-name1
))
809 (write-region "foo" nil tmp-name1
)
810 (make-directory tmp-name5
)
811 (rename-file tmp-name1 tmp-name5
)
812 (should-not (file-exists-p tmp-name1
))
815 (expand-file-name (file-name-nondirectory tmp-name1
) tmp-name5
))))
816 (ignore-errors (delete-file tmp-name1
))
817 (ignore-errors (delete-file tmp-name4
))
818 (ignore-errors (delete-directory tmp-name5
'recursive
)))
820 ;; Rename from local side to remote side.
823 (write-region "foo" nil tmp-name4 nil
'nomessage
)
824 (rename-file tmp-name4 tmp-name1
)
825 (should-not (file-exists-p tmp-name4
))
826 (should (file-exists-p tmp-name1
))
828 (insert-file-contents tmp-name1
)
829 (should (string-equal (buffer-string) "foo")))
830 (write-region "foo" nil tmp-name4 nil
'nomessage
)
831 (should-error (rename-file tmp-name4 tmp-name1
))
832 (rename-file tmp-name4 tmp-name1
'ok
)
833 (should-not (file-exists-p tmp-name4
))
834 (write-region "foo" nil tmp-name4 nil
'nomessage
)
835 (make-directory tmp-name3
)
836 (rename-file tmp-name4 tmp-name3
)
837 (should-not (file-exists-p tmp-name4
))
840 (expand-file-name (file-name-nondirectory tmp-name4
) tmp-name3
))))
841 (ignore-errors (delete-file tmp-name1
))
842 (ignore-errors (delete-file tmp-name4
))
843 (ignore-errors (delete-directory tmp-name3
'recursive
)))))
845 (ert-deftest tramp-test13-make-directory
()
846 "Check `make-directory'.
847 This tests also `file-directory-p' and `file-accessible-directory-p'."
848 (skip-unless (tramp--test-enabled))
850 (let ((tmp-name (tramp--test-make-temp-name)))
853 (make-directory tmp-name
)
854 (should (file-directory-p tmp-name
))
855 (should (file-accessible-directory-p tmp-name
)))
856 (ignore-errors (delete-directory tmp-name
)))))
858 (ert-deftest tramp-test14-delete-directory
()
859 "Check `delete-directory'."
860 (skip-unless (tramp--test-enabled))
862 (let ((tmp-name (tramp--test-make-temp-name)))
863 ;; Delete empty directory.
864 (make-directory tmp-name
)
865 (should (file-directory-p tmp-name
))
866 (delete-directory tmp-name
)
867 (should-not (file-directory-p tmp-name
))
868 ;; Delete non-empty directory.
869 (make-directory tmp-name
)
870 (write-region "foo" nil
(expand-file-name "bla" tmp-name
))
871 (should-error (delete-directory tmp-name
) :type
'file-error
)
872 (delete-directory tmp-name
'recursive
)
873 (should-not (file-directory-p tmp-name
))))
875 (ert-deftest tramp-test15-copy-directory
()
876 "Check `copy-directory'."
877 (skip-unless (tramp--test-enabled))
879 (let* ((tmp-name1 (tramp--test-make-temp-name))
880 (tmp-name2 (tramp--test-make-temp-name))
881 (tmp-name3 (expand-file-name
882 (file-name-nondirectory tmp-name1
) tmp-name2
))
883 (tmp-name4 (expand-file-name "foo" tmp-name1
))
884 (tmp-name5 (expand-file-name "foo" tmp-name2
))
885 (tmp-name6 (expand-file-name "foo" tmp-name3
)))
888 ;; Copy empty directory.
889 (make-directory tmp-name1
)
890 (write-region "foo" nil tmp-name4
)
891 (should (file-directory-p tmp-name1
))
892 (should (file-exists-p tmp-name4
))
893 (copy-directory tmp-name1 tmp-name2
)
894 (should (file-directory-p tmp-name2
))
895 (should (file-exists-p tmp-name5
))
896 ;; Target directory does exist already.
897 (copy-directory tmp-name1 tmp-name2
)
898 (should (file-directory-p tmp-name3
))
899 (should (file-exists-p tmp-name6
)))
901 (delete-directory tmp-name1
'recursive
)
902 (delete-directory tmp-name2
'recursive
)))))
904 (ert-deftest tramp-test16-directory-files
()
905 "Check `directory-files'."
906 (skip-unless (tramp--test-enabled))
908 (let* ((tmp-name1 (tramp--test-make-temp-name))
909 (tmp-name2 (expand-file-name "bla" tmp-name1
))
910 (tmp-name3 (expand-file-name "foo" tmp-name1
)))
913 (make-directory tmp-name1
)
914 (write-region "foo" nil tmp-name2
)
915 (write-region "bla" nil tmp-name3
)
916 (should (file-directory-p tmp-name1
))
917 (should (file-exists-p tmp-name2
))
918 (should (file-exists-p tmp-name3
))
919 (should (equal (directory-files tmp-name1
) '("." ".." "bla" "foo")))
920 (should (equal (directory-files tmp-name1
'full
)
921 `(,(concat tmp-name1
"/.")
922 ,(concat tmp-name1
"/..")
923 ,tmp-name2
,tmp-name3
)))
924 (should (equal (directory-files
925 tmp-name1 nil directory-files-no-dot-files-regexp
)
927 (should (equal (directory-files
928 tmp-name1
'full directory-files-no-dot-files-regexp
)
929 `(,tmp-name2
,tmp-name3
))))
930 (ignore-errors (delete-directory tmp-name1
'recursive
)))))
932 (ert-deftest tramp-test17-insert-directory
()
933 "Check `insert-directory'."
934 (skip-unless (tramp--test-enabled))
936 (let* ((tmp-name1 (tramp--test-make-temp-name))
937 (tmp-name2 (expand-file-name "foo" tmp-name1
)))
940 (make-directory tmp-name1
)
941 (write-region "foo" nil tmp-name2
)
942 (should (file-directory-p tmp-name1
))
943 (should (file-exists-p tmp-name2
))
945 (insert-directory tmp-name1 nil
)
946 (goto-char (point-min))
947 (should (looking-at-p (regexp-quote tmp-name1
))))
949 (insert-directory tmp-name1
"-al")
950 (goto-char (point-min))
951 (should (looking-at-p (format "^.+ %s$" (regexp-quote tmp-name1
)))))
953 (insert-directory (file-name-as-directory tmp-name1
) "-al")
954 (goto-char (point-min))
956 (looking-at-p (format "^.+ %s/$" (regexp-quote tmp-name1
)))))
959 (file-name-as-directory tmp-name1
) "-al" nil
'full-directory-p
)
960 (goto-char (point-min))
963 "\\(total.+[[:digit:]]+\n\\)?.+ \\.\n.+ \\.\\.\n.+ foo$"))))
964 (ignore-errors (delete-directory tmp-name1
'recursive
)))))
966 (ert-deftest tramp-test18-file-attributes
()
967 "Check `file-attributes'.
968 This tests also `file-readable-p' and `file-regular-p'."
969 (skip-unless (tramp--test-enabled))
971 (let ((tmp-name (tramp--test-make-temp-name))
975 (write-region "foo" nil tmp-name
)
976 (should (file-exists-p tmp-name
))
977 (setq attr
(file-attributes tmp-name
))
978 (should (consp attr
))
979 (should (file-exists-p tmp-name
))
980 (should (file-readable-p tmp-name
))
981 (should (file-regular-p tmp-name
))
982 ;; We do not test inodes and device numbers.
983 (should (null (car attr
)))
984 (should (numberp (nth 1 attr
))) ;; Link.
985 (should (numberp (nth 2 attr
))) ;; Uid.
986 (should (numberp (nth 3 attr
))) ;; Gid.
988 (should (stringp (current-time-string (nth 4 attr
))))
989 ;; Last modification time.
990 (should (stringp (current-time-string (nth 5 attr
))))
991 ;; Last status change time.
992 (should (stringp (current-time-string (nth 6 attr
))))
993 (should (numberp (nth 7 attr
))) ;; Size.
994 (should (stringp (nth 8 attr
))) ;; Modes.
996 (setq attr
(file-attributes tmp-name
'string
))
997 (should (stringp (nth 2 attr
))) ;; Uid.
998 (should (stringp (nth 3 attr
))) ;; Gid.
999 (delete-file tmp-name
)
1001 (make-directory tmp-name
)
1002 (should (file-exists-p tmp-name
))
1003 (should (file-readable-p tmp-name
))
1004 (should-not (file-regular-p tmp-name
))
1005 (setq attr
(file-attributes tmp-name
))
1006 (should (eq (car attr
) t
)))
1007 (ignore-errors (delete-directory tmp-name
)))))
1009 (ert-deftest tramp-test19-directory-files-and-attributes
()
1010 "Check `directory-files-and-attributes'."
1011 (skip-unless (tramp--test-enabled))
1013 ;; `directory-files-and-attributes' contains also values for "../".
1014 ;; Ensure that this doesn't change during tests, for
1015 ;; example due to handling temporary files.
1016 (let* ((tmp-name1 (tramp--test-make-temp-name))
1017 (tmp-name2 (expand-file-name "bla" tmp-name1
))
1021 (make-directory tmp-name1
)
1022 (should (file-directory-p tmp-name1
))
1023 (make-directory tmp-name2
)
1024 (should (file-directory-p tmp-name2
))
1025 (write-region "foo" nil
(expand-file-name "foo" tmp-name2
))
1026 (write-region "bar" nil
(expand-file-name "bar" tmp-name2
))
1027 (write-region "boz" nil
(expand-file-name "boz" tmp-name2
))
1028 (setq attr
(directory-files-and-attributes tmp-name2
))
1029 (should (consp attr
))
1030 ;; Dumb remote shells without perl(1) or stat(1) are not
1031 ;; able to return the date correctly. They say "don't know".
1036 (file-attributes (expand-file-name (car elt
) tmp-name2
)))
1039 (equal (file-attributes (expand-file-name (car elt
) tmp-name2
))
1041 (setq attr
(directory-files-and-attributes tmp-name2
'full
))
1043 (unless (equal (nth 5 (file-attributes (car elt
))) '(0 0))
1045 (equal (file-attributes (car elt
)) (cdr elt
)))))
1046 (setq attr
(directory-files-and-attributes tmp-name2 nil
"^b"))
1047 (should (equal (mapcar 'car attr
) '("bar" "boz"))))
1048 (ignore-errors (delete-directory tmp-name1
'recursive
)))))
1050 (ert-deftest tramp-test20-file-modes
()
1051 "Check `file-modes'.
1052 This tests also `file-executable-p', `file-writable-p' and `set-file-modes'."
1053 (skip-unless (tramp--test-enabled))
1057 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory
)
1058 '(tramp-adb-file-name-handler
1059 tramp-gvfs-file-name-handler
1060 tramp-smb-file-name-handler
))))
1062 (let ((tmp-name (tramp--test-make-temp-name)))
1065 (write-region "foo" nil tmp-name
)
1066 (should (file-exists-p tmp-name
))
1067 (set-file-modes tmp-name
#o777
)
1068 (should (= (file-modes tmp-name
) #o777
))
1069 (should (file-executable-p tmp-name
))
1070 (should (file-writable-p tmp-name
))
1071 (set-file-modes tmp-name
#o444
)
1072 (should (= (file-modes tmp-name
) #o444
))
1073 (should-not (file-executable-p tmp-name
))
1074 ;; A file is always writable for user "root".
1075 (unless (zerop (nth 2 (file-attributes tmp-name
)))
1076 (should-not (file-writable-p tmp-name
))))
1077 (ignore-errors (delete-file tmp-name
)))))
1079 (ert-deftest tramp-test21-file-links
()
1080 "Check `file-symlink-p'.
1081 This tests also `make-symbolic-link', `file-truename' and `add-name-to-file'."
1082 (skip-unless (tramp--test-enabled))
1084 (let ((tmp-name1 (tramp--test-make-temp-name))
1085 (tmp-name2 (tramp--test-make-temp-name))
1086 (tmp-name3 (tramp--test-make-temp-name 'local
)))
1089 (write-region "foo" nil tmp-name1
)
1090 (should (file-exists-p tmp-name1
))
1091 ;; Method "smb" supports `make-symbolic-link' only if the
1092 ;; remote host has CIFS capabilities. tramp-adb.el and
1093 ;; tramp-gvfs.el do not support symbolic links at all.
1095 (make-symbolic-link tmp-name1 tmp-name2
)
1098 (not (string-equal (error-message-string err
)
1099 "make-symbolic-link not supported")))))
1100 (should (file-symlink-p tmp-name2
))
1101 (should-error (make-symbolic-link tmp-name1 tmp-name2
))
1102 (make-symbolic-link tmp-name1 tmp-name2
'ok-if-already-exists
)
1103 (should (file-symlink-p tmp-name2
))
1104 ;; `tmp-name3' is a local file name.
1105 (should-error (make-symbolic-link tmp-name1 tmp-name3
)))
1107 (delete-file tmp-name1
)
1108 (delete-file tmp-name2
)))
1112 (write-region "foo" nil tmp-name1
)
1113 (should (file-exists-p tmp-name1
))
1114 (add-name-to-file tmp-name1 tmp-name2
)
1115 (should-not (file-symlink-p tmp-name2
))
1116 (should-error (add-name-to-file tmp-name1 tmp-name2
))
1117 (add-name-to-file tmp-name1 tmp-name2
'ok-if-already-exists
)
1118 (should-not (file-symlink-p tmp-name2
))
1119 ;; `tmp-name3' is a local file name.
1120 (should-error (add-name-to-file tmp-name1 tmp-name3
)))
1122 (delete-file tmp-name1
)
1123 (delete-file tmp-name2
)))
1127 (write-region "foo" nil tmp-name1
)
1128 (should (file-exists-p tmp-name1
))
1129 (make-symbolic-link tmp-name1 tmp-name2
)
1130 (should (file-symlink-p tmp-name2
))
1131 (should-not (string-equal tmp-name2
(file-truename tmp-name2
)))
1133 (string-equal (file-truename tmp-name1
) (file-truename tmp-name2
)))
1134 (should (file-equal-p tmp-name1 tmp-name2
)))
1136 (delete-file tmp-name1
)
1137 (delete-file tmp-name2
)))
1139 ;; `file-truename' shall preserve trailing link of directories.
1140 (unless (file-symlink-p tramp-test-temporary-file-directory
)
1141 (let* ((dir1 (directory-file-name tramp-test-temporary-file-directory
))
1142 (dir2 (file-name-as-directory dir1
)))
1143 (should (string-equal (file-truename dir1
) (expand-file-name dir1
)))
1144 (should (string-equal (file-truename dir2
) (expand-file-name dir2
)))))))
1146 (ert-deftest tramp-test22-file-times
()
1147 "Check `set-file-times' and `file-newer-than-file-p'."
1148 (skip-unless (tramp--test-enabled))
1152 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory
)
1153 '(tramp-gvfs-file-name-handler tramp-smb-file-name-handler
))))
1155 (let ((tmp-name1 (tramp--test-make-temp-name))
1156 (tmp-name2 (tramp--test-make-temp-name))
1157 (tmp-name3 (tramp--test-make-temp-name)))
1160 (write-region "foo" nil tmp-name1
)
1161 (should (file-exists-p tmp-name1
))
1162 (should (consp (nth 5 (file-attributes tmp-name1
))))
1163 ;; '(0 0) means don't know, and will be replaced by
1164 ;; `current-time'. Therefore, we use '(0 1).
1165 ;; We skip the test, if the remote handler is not able to
1166 ;; set the correct time.
1167 (skip-unless (set-file-times tmp-name1
'(0 1)))
1168 ;; Dumb remote shells without perl(1) or stat(1) are not
1169 ;; able to return the date correctly. They say "don't know".
1170 (unless (equal (nth 5 (file-attributes tmp-name1
)) '(0 0))
1171 (should (equal (nth 5 (file-attributes tmp-name1
)) '(0 1)))
1172 (write-region "bla" nil tmp-name2
)
1173 (should (file-exists-p tmp-name2
))
1174 (should (file-newer-than-file-p tmp-name2 tmp-name1
))
1175 ;; `tmp-name3' does not exist.
1176 (should (file-newer-than-file-p tmp-name2 tmp-name3
))
1177 (should-not (file-newer-than-file-p tmp-name3 tmp-name1
))))
1179 (delete-file tmp-name1
)
1180 (delete-file tmp-name2
)))))
1182 (ert-deftest tramp-test23-visited-file-modtime
()
1183 "Check `set-visited-file-modtime' and `verify-visited-file-modtime'."
1184 (skip-unless (tramp--test-enabled))
1186 (let ((tmp-name (tramp--test-make-temp-name)))
1189 (write-region "foo" nil tmp-name
)
1190 (should (file-exists-p tmp-name
))
1192 (insert-file-contents tmp-name
)
1193 (should (verify-visited-file-modtime))
1194 (set-visited-file-modtime '(0 1))
1195 (should (verify-visited-file-modtime))
1196 (should (equal (visited-file-modtime) '(0 1 0 0)))))
1197 (ignore-errors (delete-file tmp-name
)))))
1199 (ert-deftest tramp-test24-file-name-completion
()
1200 "Check `file-name-completion' and `file-name-all-completions'."
1201 (skip-unless (tramp--test-enabled))
1203 (let ((tmp-name (tramp--test-make-temp-name)))
1206 (make-directory tmp-name
)
1207 (should (file-directory-p tmp-name
))
1208 (write-region "foo" nil
(expand-file-name "foo" tmp-name
))
1209 (write-region "bar" nil
(expand-file-name "bold" tmp-name
))
1210 (make-directory (expand-file-name "boz" tmp-name
))
1211 (should (equal (file-name-completion "fo" tmp-name
) "foo"))
1212 (should (equal (file-name-completion "b" tmp-name
) "bo"))
1214 (equal (file-name-completion "b" tmp-name
'file-directory-p
) "boz/"))
1215 (should (equal (file-name-all-completions "fo" tmp-name
) '("foo")))
1217 (equal (sort (file-name-all-completions "b" tmp-name
) 'string-lessp
)
1219 (ignore-errors (delete-directory tmp-name
'recursive
)))))
1221 (ert-deftest tramp-test25-load
()
1223 (skip-unless (tramp--test-enabled))
1225 (let ((tmp-name (tramp--test-make-temp-name)))
1228 (load tmp-name
'noerror
'nomessage
)
1229 (should-not (featurep 'tramp-test-load
))
1230 (write-region "(provide 'tramp-test-load)" nil tmp-name
)
1231 ;; `load' in lread.c does not pass `must-suffix'. Why?
1232 ;(should-error (load tmp-name nil 'nomessage 'nosuffix 'must-suffix))
1233 (load tmp-name nil
'nomessage
'nosuffix
)
1234 (should (featurep 'tramp-test-load
)))
1236 (and (featurep 'tramp-test-load
) (unload-feature 'tramp-test-load
))
1237 (delete-file tmp-name
)))))
1239 (ert-deftest tramp-test26-process-file
()
1240 "Check `process-file'."
1241 (skip-unless (tramp--test-enabled))
1245 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory
)
1246 '(tramp-gvfs-file-name-handler tramp-smb-file-name-handler
))))
1248 (let ((tmp-name (tramp--test-make-temp-name))
1249 (default-directory tramp-test-temporary-file-directory
)
1250 kill-buffer-query-functions
)
1253 ;; We cannot use "/bin/true" and "/bin/false"; those paths
1254 ;; do not exist on hydra.
1255 (should (zerop (process-file "true")))
1256 (should-not (zerop (process-file "false")))
1257 (should-not (zerop (process-file "binary-does-not-exist")))
1259 (write-region "foo" nil tmp-name
)
1260 (should (file-exists-p tmp-name
))
1263 (process-file "ls" nil t nil
(file-name-nondirectory tmp-name
))))
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
))
1270 (format "%s\n" (file-name-nondirectory tmp-name
))
1272 (ignore-errors (delete-file tmp-name
)))))
1274 (ert-deftest tramp-test27-start-file-process
()
1275 "Check `start-file-process'."
1276 (skip-unless (tramp--test-enabled))
1280 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory
)
1281 '(tramp-adb-file-name-handler
1282 tramp-gvfs-file-name-handler
1283 tramp-smb-file-name-handler
))))
1285 (let ((default-directory tramp-test-temporary-file-directory
)
1286 (tmp-name (tramp--test-make-temp-name))
1287 kill-buffer-query-functions proc
)
1290 (setq proc
(start-file-process "test1" (current-buffer) "cat"))
1291 (should (processp proc
))
1292 (should (equal (process-status proc
) 'run
))
1293 (process-send-string proc
"foo")
1294 (process-send-eof proc
)
1295 (accept-process-output proc
1)
1296 (should (string-equal (buffer-string) "foo")))
1297 (ignore-errors (delete-process proc
)))
1301 (write-region "foo" nil tmp-name
)
1302 (should (file-exists-p tmp-name
))
1305 "test2" (current-buffer)
1306 "cat" (file-name-nondirectory tmp-name
)))
1307 (should (processp proc
))
1308 (accept-process-output proc
1)
1309 (should (string-equal (buffer-string) "foo")))
1311 (delete-process proc
)
1312 (delete-file tmp-name
)))
1316 (setq proc
(start-file-process "test3" nil
"cat"))
1317 (should (processp proc
))
1318 (should (equal (process-status proc
) 'run
))
1320 proc
(lambda (_p s
) (should (string-equal s
"foo"))))
1321 (process-send-string proc
"foo")
1322 (process-send-eof proc
)
1323 (accept-process-output proc
1))
1324 (ignore-errors (delete-process proc
)))))
1326 (ert-deftest tramp-test28-shell-command
()
1327 "Check `shell-command'."
1328 (skip-unless (tramp--test-enabled))
1332 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory
)
1333 '(tramp-adb-file-name-handler
1334 tramp-gvfs-file-name-handler
1335 tramp-smb-file-name-handler
))))
1337 (let ((tmp-name (tramp--test-make-temp-name))
1338 (default-directory tramp-test-temporary-file-directory
)
1339 kill-buffer-query-functions
)
1342 (write-region "foo" nil tmp-name
)
1343 (should (file-exists-p tmp-name
))
1345 (format "ls %s" (file-name-nondirectory tmp-name
)) (current-buffer))
1346 ;; `ls' could produce colorized output.
1347 (goto-char (point-min))
1348 (while (re-search-forward tramp-color-escape-sequence-regexp nil t
)
1349 (replace-match "" nil nil
))
1352 (format "%s\n" (file-name-nondirectory tmp-name
)) (buffer-string))))
1353 (ignore-errors (delete-file tmp-name
)))
1357 (write-region "foo" nil tmp-name
)
1358 (should (file-exists-p tmp-name
))
1359 (async-shell-command
1360 (format "ls %s" (file-name-nondirectory tmp-name
)) (current-buffer))
1361 (accept-process-output (get-buffer-process (current-buffer)) 1)
1362 (with-timeout (10 (ert-fail "`async-shell-command' timed out"))
1365 (memq (process-status (get-buffer-process (current-buffer)))
1367 (accept-process-output (get-buffer-process (current-buffer)) 1)))
1368 ;; `ls' could produce colorized output.
1369 (goto-char (point-min))
1370 (while (re-search-forward tramp-color-escape-sequence-regexp nil t
)
1371 (replace-match "" nil nil
))
1374 (format "%s\n" (file-name-nondirectory tmp-name
)) (buffer-string))))
1375 (ignore-errors (delete-file tmp-name
)))
1379 (write-region "foo" nil tmp-name
)
1380 (should (file-exists-p tmp-name
))
1381 (async-shell-command "read line; ls $line" (current-buffer))
1382 (process-send-string
1383 (get-buffer-process (current-buffer))
1384 (format "%s\n" (file-name-nondirectory tmp-name
)))
1385 (accept-process-output (get-buffer-process (current-buffer)) 1)
1386 (with-timeout (10 (ert-fail "`async-shell-command' timed out"))
1389 (memq (process-status (get-buffer-process (current-buffer)))
1391 (accept-process-output (get-buffer-process (current-buffer)) 1)))
1394 (format "%s\n" (file-name-nondirectory tmp-name
)) (buffer-string))))
1395 (ignore-errors (delete-file tmp-name
)))))
1397 (ert-deftest tramp-test29-vc-registered
()
1398 "Check `vc-registered'."
1399 (skip-unless (tramp--test-enabled))
1402 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory
)
1403 'tramp-sh-file-name-handler
))
1405 (let* ((default-directory tramp-test-temporary-file-directory
)
1406 (tmp-name1 (tramp--test-make-temp-name))
1407 (tmp-name2 (expand-file-name "foo" tmp-name1
))
1408 (vc-handled-backends
1409 (with-parsed-tramp-file-name tramp-test-temporary-file-directory nil
1411 ((tramp-find-executable v vc-bzr-program
(tramp-get-remote-path v
))
1413 ((tramp-find-executable v vc-git-program
(tramp-get-remote-path v
))
1415 ((tramp-find-executable v vc-hg-program
(tramp-get-remote-path v
))
1418 (skip-unless vc-handled-backends
)
1419 (message "%s" vc-handled-backends
)
1423 (make-directory tmp-name1
)
1424 (write-region "foo" nil tmp-name2
)
1425 (should (file-directory-p tmp-name1
))
1426 (should (file-exists-p tmp-name2
))
1427 (should-not (vc-registered tmp-name1
))
1428 (should-not (vc-registered tmp-name2
))
1430 (let ((default-directory tmp-name1
))
1431 ;; Create empty repository, and register the file.
1432 (vc-create-repo (car vc-handled-backends
))
1433 ;; The structure of VC-FILESET is not documented. Let's
1434 ;; hope it won't change.
1436 nil
(list (car vc-handled-backends
)
1437 (list (file-name-nondirectory tmp-name2
)))))
1438 (should (vc-registered tmp-name2
)))
1440 (ignore-errors (delete-directory tmp-name1
'recursive
)))))
1442 (defun tramp--test-check-files (&rest files
)
1443 "Runs a simple but comprehensive test over every file in FILES."
1444 (let ((tmp-name1 (tramp--test-make-temp-name))
1445 (tmp-name2 (tramp--test-make-temp-name 'local
)))
1448 (make-directory tmp-name1
)
1449 (make-directory tmp-name2
)
1451 (let ((file1 (expand-file-name elt tmp-name1
))
1452 (file2 (expand-file-name elt tmp-name2
)))
1453 (write-region elt nil file1
)
1454 (should (file-exists-p file1
))
1455 ;; Check file contents.
1457 (insert-file-contents file1
)
1458 (should (string-equal (buffer-string) elt
)))
1459 ;; Copy file both directions.
1460 (copy-file file1 tmp-name2
)
1461 (should (file-exists-p file2
))
1463 (should-not (file-exists-p file1
))
1464 (copy-file file2 tmp-name1
)
1465 (should (file-exists-p file1
))))
1466 ;; Check file names.
1467 (should (equal (directory-files
1468 tmp-name1 nil directory-files-no-dot-files-regexp
)
1469 (sort (copy-sequence files
) 'string-lessp
)))
1470 (should (equal (directory-files
1471 tmp-name2 nil directory-files-no-dot-files-regexp
)
1472 (sort files
'string-lessp
))))
1473 (ignore-errors (delete-directory tmp-name1
'recursive
))
1474 (ignore-errors (delete-directory tmp-name2
'recursive
)))))
1476 ;; This test is inspired by Bug#17238.
1477 (ert-deftest tramp-test30-special-characters
()
1478 "Check special characters in file names."
1479 (skip-unless (tramp--test-enabled))
1481 ;; Newlines, slashes and backslashes in file names are not supported.
1482 ;; So we don't test.
1483 (tramp--test-check-files
1498 (ert-deftest tramp-test31-utf8
()
1499 "Check UTF8 encoding in file names and file contents."
1500 (skip-unless (tramp--test-enabled))
1502 (let ((coding-system-for-read 'utf-8
)
1503 (coding-system-for-write 'utf-8
)
1504 (file-name-coding-system 'utf-8
))
1505 (tramp--test-check-files
1506 "أصبح بوسعك الآن تنزيل نسخة كاملة من موسوعة ويكيبيديا العربية لتصفحها بلا اتصال بالإنترنت"
1508 "Автостопом по гала́ктике")))
1510 ;; This test is inspired by Bug#16928.
1511 (ert-deftest tramp-test32-asynchronous-requests
()
1512 "Check parallel asynchronous requests.
1513 Such requests could arrive from timers, process filters and
1514 process sentinels. They shall not disturb each other."
1515 ;; Mark as failed until bug has been fixed.
1516 :expected-result
:failed
1517 (skip-unless (tramp--test-enabled))
1520 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory
)
1521 'tramp-sh-file-name-handler
))
1523 ;; Keep instrumentation verbosity 0 until Tramp bug is fixed. This
1524 ;; has the side effect, that this test fails instead to abort. Good
1526 (tramp--instrument-test-case 0
1527 (let* ((tmp-name (tramp--test-make-temp-name))
1528 (default-directory tmp-name
)
1529 (remote-file-name-inhibit-cache t
)
1530 timer buffers kill-buffer-query-functions
)
1534 (make-directory tmp-name
)
1536 ;; Setup a timer in order to raise an ordinary command again
1537 ;; and again. `vc-registered' is well suited, because there
1546 (buffer-name (nth (random (length buffers
)) buffers
)))))))
1548 ;; Create temporary buffers. The number of buffers
1549 ;; corresponds to the number of processes; it could be
1550 ;; increased in order to make pressure on Tramp.
1552 (add-to-list 'buffers
(generate-new-buffer "*temp*")))
1554 ;; Open asynchronous processes. Set process sentinel.
1555 (dolist (buf buffers
)
1556 (async-shell-command "read line; touch $line; echo $line" buf
)
1557 (set-process-sentinel
1558 (get-buffer-process buf
)
1559 (lambda (proc _state
)
1560 (delete-file (buffer-name (process-buffer proc
))))))
1562 ;; Send a string. Use a random order of the buffers. Mix
1563 ;; with regular operation.
1564 (let ((buffers (copy-sequence buffers
))
1567 (setq buf
(nth (random (length buffers
)) buffers
))
1568 (process-send-string
1569 (get-buffer-process buf
) (format "'%s'\n" buf
))
1570 (file-attributes (buffer-name buf
))
1571 (setq buffers
(delq buf buffers
))))
1573 ;; Wait until the whole output has been read.
1574 (with-timeout ((* 10 (length buffers
))
1575 (ert-fail "`async-shell-command' timed out"))
1576 (let ((buffers (copy-sequence buffers
))
1579 (setq buf
(nth (random (length buffers
)) buffers
))
1581 (memq (process-status (get-buffer-process buf
))
1583 (accept-process-output (get-buffer-process buf
) 0.1)
1584 (setq buffers
(delq buf buffers
))))))
1587 (dolist (buf buffers
)
1588 (with-current-buffer buf
1590 (string-equal (format "'%s'\n" buf
) (buffer-string)))))
1592 (directory-files tmp-name nil directory-files-no-dot-files-regexp
)))
1595 (ignore-errors (cancel-timer timer
))
1596 (ignore-errors (delete-directory tmp-name
'recursive
))
1597 (dolist (buf buffers
)
1598 (ignore-errors (kill-buffer buf
)))))))
1600 (ert-deftest tramp-test33-recursive-load
()
1601 "Check that Tramp does not fail due to recursive load."
1602 (skip-unless (tramp--test-enabled))
1607 "(expand-file-name %S)"
1608 tramp-test-temporary-file-directory
)
1610 "(let ((default-directory %S)) (expand-file-name %S))"
1611 tramp-test-temporary-file-directory
1612 temporary-file-directory
)))
1616 (shell-command-to-string
1618 "%s -batch -Q -L %s --eval %s"
1619 (expand-file-name invocation-name invocation-directory
)
1620 (mapconcat 'shell-quote-argument load-path
" -L ")
1621 (shell-quote-argument code
)))))))
1623 (ert-deftest tramp-test34-unload
()
1624 "Check that Tramp and its subpackages unload completely.
1625 Since it unloads Tramp, it shall be the last test to run."
1626 ;; Mark as failed until all symbols are unbound.
1627 :expected-result
(if (featurep 'tramp
) :failed
:passed
)
1628 (when (featurep 'tramp
)
1629 (unload-feature 'tramp
'force
)
1630 ;; No Tramp feature must be left.
1631 (should-not (featurep 'tramp
))
1632 (should-not (all-completions "tramp" (delq 'tramp-tests features
)))
1633 ;; `file-name-handler-alist' must be clean.
1634 (should-not (all-completions "tramp" (mapcar 'cdr file-name-handler-alist
)))
1635 ;; There shouldn't be left a bound symbol. We do not regard our
1636 ;; test symbols, and the Tramp unload hooks.
1639 (and (or (boundp x
) (functionp x
))
1640 (string-match "^tramp" (symbol-name x
))
1641 (not (string-match "^tramp--?test" (symbol-name x
)))
1642 (not (string-match "unload-hook$" (symbol-name x
)))
1643 (ert-fail (format "`%s' still bound" x
)))))
1644 ; (progn (message "`%s' still bound" x)))))
1645 ;; There shouldn't be left a hook function containing a Tramp
1646 ;; function. We do not regard the Tramp unload hooks.
1650 (string-match "-hooks?$" (symbol-name x
))
1651 (not (string-match "unload-hook$" (symbol-name x
)))
1652 (consp (symbol-value x
))
1653 (ignore-errors (all-completions "tramp" (symbol-value x
)))
1654 (ert-fail (format "Hook `%s' still contains Tramp function" x
)))))))
1658 ;; * dired-compress-file
1661 ;; * file-ownership-preserved-p
1662 ;; * file-selinux-context
1663 ;; * find-backup-file-name
1664 ;; * make-auto-save-file-name
1666 ;; * set-file-selinux-context
1668 ;; * Fix `tramp-test27-start-file-process' on MS Windows (`process-send-eof'?).
1669 ;; * Fix `tramp-test28-shell-command' on MS Windows (nasty plink message).
1670 ;; * Fix `tramp-test31-utf8' for MS Windows and `nc'/`telnet' (when
1671 ;; target is a dumb busybox). Seems to be in `directory-files'.
1672 ;; * Fix Bug#16928. Set expected error of `tramp-test32-asynchronous-requests'.
1673 ;; * Fix `tramp-test34-unload' (Not all symbols are unbound). Set
1676 (defun tramp-test-all (&optional interactive
)
1677 "Run all tests for \\[tramp]."
1680 (if interactive
'ert-run-tests-interactively
'ert-run-tests-batch
) "^tramp"))
1682 (provide 'tramp-tests
)
1683 ;;; tramp-tests.el ends here