1 ;;; tramp-tests.el --- Tests of remote file access
3 ;; Copyright (C) 2013-2014 Free Software Foundation, Inc.
5 ;; Author: Michael Albinus <michael.albinus@gmx.de>
7 ;; This program is free software: you can redistribute it and/or
8 ;; modify it under the terms of the GNU General Public License as
9 ;; published by the Free Software Foundation, either version 3 of the
10 ;; License, or (at your option) any later version.
12 ;; This program is distributed in the hope that it will be useful, but
13 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 ;; General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with this program. If not, see `http://www.gnu.org/licenses/'.
22 ;; The tests require a recent ert.el from Emacs 24.4.
24 ;; Some of the tests require access to a remote host files. Since
25 ;; this could be problematic, a mock-up connection method "mock" is
26 ;; used. Emulating a remote connection, it simply calls "sh -i".
27 ;; Tramp's file name handlers still run, so this test is sufficient
28 ;; except for connection establishing.
30 ;; If you want to test a real Tramp connection, set
31 ;; $REMOTE_TEMPORARY_FILE_DIRECTORY to a suitable value in order to
32 ;; overwrite the default value. If you want to skip tests accessing a
33 ;; remote host, set this environment variable to "/dev/null" or
34 ;; whatever is appropriate on your system.
36 ;; A whole test run can be performed calling the command `tramp-test-all'.
47 (declare-function tramp-find-executable
"tramp-sh")
48 (declare-function tramp-get-remote-path
"tramp-sh")
49 (defvar tramp-copy-size-limit
)
50 (defvar tramp-remote-process-environment
)
52 ;; There is no default value on w32 systems, which could work out of the box.
53 (defconst tramp-test-temporary-file-directory
55 ((getenv "REMOTE_TEMPORARY_FILE_DIRECTORY"))
56 ((eq system-type
'windows-nt
) null-device
)
60 (tramp-login-program "sh")
61 (tramp-login-args (("-i")))
62 (tramp-remote-shell "/bin/sh")
63 (tramp-remote-shell-args ("-c"))
64 (tramp-connection-timeout 10)))
65 (format "/mock::%s" temporary-file-directory
)))
66 "Temporary directory for Tramp tests.")
68 (setq password-cache-expiry nil
70 tramp-copy-size-limit nil
71 tramp-message-show-message nil
)
73 ;; This shall happen on hydra only.
74 (when (getenv "NIX_STORE")
75 (add-to-list 'tramp-remote-path
'tramp-own-remote-path
))
77 (defvar tramp--test-enabled-checked nil
78 "Cached result of `tramp--test-enabled'.
79 If the function did run, the value is a cons cell, the `cdr'
82 (defun tramp--test-enabled ()
83 "Whether remote file access is enabled."
84 (unless (consp tramp--test-enabled-checked
)
86 tramp--test-enabled-checked
90 (file-remote-p tramp-test-temporary-file-directory
)
91 (file-directory-p tramp-test-temporary-file-directory
)
92 (file-writable-p tramp-test-temporary-file-directory
))))))
94 (when (cdr tramp--test-enabled-checked
)
95 ;; Cleanup connection.
97 (tramp-cleanup-connection
98 (tramp-dissect-file-name tramp-test-temporary-file-directory
)
102 (cdr tramp--test-enabled-checked
))
104 (defun tramp--test-make-temp-name (&optional local
)
105 "Create a temporary file name for test."
107 (make-temp-name "tramp-test")
108 (if local temporary-file-directory tramp-test-temporary-file-directory
)))
110 (defmacro tramp--instrument-test-case
(verbose &rest body
)
111 "Run BODY with `tramp-verbose' equal VERBOSE.
112 Print the the content of the Tramp debug buffer, if BODY does not
113 eval properly in `should', `should-not' or `should-error'. BODY
114 shall not contain a timeout."
115 (declare (indent 1) (debug (natnump body
)))
116 `(let ((tramp-verbose ,verbose
)
117 (tramp-message-show-message t
)
118 (tramp-debug-on-error t
))
121 (when (> tramp-verbose
3)
122 (with-parsed-tramp-file-name tramp-test-temporary-file-directory nil
123 (with-current-buffer (tramp-get-connection-buffer v
)
124 (message "%s" (buffer-string)))
126 (tramp-get-debug-buffer v
)
127 (message "%s" (buffer-string))))))))
129 (ert-deftest tramp-test00-availability
()
130 "Test availability of Tramp functions."
131 :expected-result
(if (tramp--test-enabled) :passed
:failed
)
132 (message "Remote directory: `%s'" tramp-test-temporary-file-directory
)
133 (should (ignore-errors
135 (file-remote-p tramp-test-temporary-file-directory
)
136 (file-directory-p tramp-test-temporary-file-directory
)
137 (file-writable-p tramp-test-temporary-file-directory
)))))
139 (ert-deftest tramp-test01-file-name-syntax
()
140 "Check remote file name syntax."
142 (should (tramp-tramp-file-p "/method::"))
143 (should (tramp-tramp-file-p "/host:"))
144 (should (tramp-tramp-file-p "/user@:"))
145 (should (tramp-tramp-file-p "/user@host:"))
146 (should (tramp-tramp-file-p "/method:host:"))
147 (should (tramp-tramp-file-p "/method:user@:"))
148 (should (tramp-tramp-file-p "/method:user@host:"))
149 (should (tramp-tramp-file-p "/method:user@email@host:"))
152 (should (tramp-tramp-file-p "/host#1234:"))
153 (should (tramp-tramp-file-p "/user@host#1234:"))
154 (should (tramp-tramp-file-p "/method:host#1234:"))
155 (should (tramp-tramp-file-p "/method:user@host#1234:"))
157 ;; Using an IPv4 address.
158 (should (tramp-tramp-file-p "/1.2.3.4:"))
159 (should (tramp-tramp-file-p "/user@1.2.3.4:"))
160 (should (tramp-tramp-file-p "/method:1.2.3.4:"))
161 (should (tramp-tramp-file-p "/method:user@1.2.3.4:"))
163 ;; Using an IPv6 address.
164 (should (tramp-tramp-file-p "/[]:"))
165 (should (tramp-tramp-file-p "/[::1]:"))
166 (should (tramp-tramp-file-p "/user@[::1]:"))
167 (should (tramp-tramp-file-p "/method:[::1]:"))
168 (should (tramp-tramp-file-p "/method:user@[::1]:"))
170 ;; Local file name part.
171 (should (tramp-tramp-file-p "/host:/:"))
172 (should (tramp-tramp-file-p "/method:::"))
173 (should (tramp-tramp-file-p "/method::/path/to/file"))
174 (should (tramp-tramp-file-p "/method::file"))
177 (should (tramp-tramp-file-p "/method1:|method2::"))
178 (should (tramp-tramp-file-p "/method1:host1|host2:"))
179 (should (tramp-tramp-file-p "/method1:host1|method2:host2:"))
180 (should (tramp-tramp-file-p "/method1:user1@host1|method2:user2@host2:"))
181 (should (tramp-tramp-file-p
182 "/method1:user1@host1|method2:user2@host2|method3:user3@host3:"))
185 (should-not (tramp-tramp-file-p nil
))
186 (should-not (tramp-tramp-file-p 'symbol
))
187 ;; "/:" suppresses file name handlers.
188 (should-not (tramp-tramp-file-p "/::"))
189 (should-not (tramp-tramp-file-p "/:@:"))
190 (should-not (tramp-tramp-file-p "/:[]:"))
191 ;; Multihops require a method.
192 (should-not (tramp-tramp-file-p "/host1|host2:"))
193 ;; Methods or hostnames shall be at least two characters on MS Windows.
194 (when (memq system-type
'(cygwin windows-nt
))
195 (should-not (tramp-tramp-file-p "/c:/path/to/file"))
196 (should-not (tramp-tramp-file-p "/c::/path/to/file"))))
198 (ert-deftest tramp-test02-file-name-dissect
()
199 "Check remote file name components."
200 (let ((tramp-default-method "default-method")
201 (tramp-default-user "default-user")
202 (tramp-default-host "default-host"))
203 ;; Expand `tramp-default-user' and `tramp-default-host'.
204 (should (string-equal
205 (file-remote-p "/method::")
206 (format "/%s:%s@%s:" "method" "default-user" "default-host")))
207 (should (string-equal (file-remote-p "/method::" 'method
) "method"))
208 (should (string-equal (file-remote-p "/method::" 'user
) "default-user"))
209 (should (string-equal (file-remote-p "/method::" 'host
) "default-host"))
210 (should (string-equal (file-remote-p "/method::" 'localname
) ""))
212 ;; Expand `tramp-default-method' and `tramp-default-user'.
213 (should (string-equal
214 (file-remote-p "/host:")
215 (format "/%s:%s@%s:" "default-method" "default-user" "host")))
216 (should (string-equal (file-remote-p "/host:" 'method
) "default-method"))
217 (should (string-equal (file-remote-p "/host:" 'user
) "default-user"))
218 (should (string-equal (file-remote-p "/host:" 'host
) "host"))
219 (should (string-equal (file-remote-p "/host:" 'localname
) ""))
221 ;; Expand `tramp-default-method' and `tramp-default-host'.
222 (should (string-equal
223 (file-remote-p "/user@:")
224 (format "/%s:%s@%s:" "default-method""user" "default-host")))
225 (should (string-equal (file-remote-p "/user@:" 'method
) "default-method"))
226 (should (string-equal (file-remote-p "/user@:" 'user
) "user"))
227 (should (string-equal (file-remote-p "/user@:" 'host
) "default-host"))
228 (should (string-equal (file-remote-p "/user@:" 'localname
) ""))
230 ;; Expand `tramp-default-method'.
231 (should (string-equal
232 (file-remote-p "/user@host:")
233 (format "/%s:%s@%s:" "default-method" "user" "host")))
234 (should (string-equal
235 (file-remote-p "/user@host:" 'method
) "default-method"))
236 (should (string-equal (file-remote-p "/user@host:" 'user
) "user"))
237 (should (string-equal (file-remote-p "/user@host:" 'host
) "host"))
238 (should (string-equal (file-remote-p "/user@host:" 'localname
) ""))
240 ;; Expand `tramp-default-user'.
241 (should (string-equal
242 (file-remote-p "/method:host:")
243 (format "/%s:%s@%s:" "method" "default-user" "host")))
244 (should (string-equal (file-remote-p "/method:host:" 'method
) "method"))
245 (should (string-equal (file-remote-p "/method:host:" 'user
) "default-user"))
246 (should (string-equal (file-remote-p "/method:host:" 'host
) "host"))
247 (should (string-equal (file-remote-p "/method:host:" 'localname
) ""))
249 ;; Expand `tramp-default-host'.
250 (should (string-equal
251 (file-remote-p "/method:user@:")
252 (format "/%s:%s@%s:" "method" "user" "default-host")))
253 (should (string-equal (file-remote-p "/method:user@:" 'method
) "method"))
254 (should (string-equal (file-remote-p "/method:user@:" 'user
) "user"))
255 (should (string-equal (file-remote-p "/method:user@:" 'host
)
257 (should (string-equal (file-remote-p "/method:user@:" 'localname
) ""))
260 (should (string-equal
261 (file-remote-p "/method:user@host:")
262 (format "/%s:%s@%s:" "method" "user" "host")))
263 (should (string-equal
264 (file-remote-p "/method:user@host:" 'method
) "method"))
265 (should (string-equal (file-remote-p "/method:user@host:" 'user
) "user"))
266 (should (string-equal (file-remote-p "/method:user@host:" 'host
) "host"))
267 (should (string-equal (file-remote-p "/method:user@host:" 'localname
) ""))
270 (should (string-equal
271 (file-remote-p "/method:user@email@host:")
272 (format "/%s:%s@%s:" "method" "user@email" "host")))
273 (should (string-equal
274 (file-remote-p "/method:user@email@host:" 'method
) "method"))
275 (should (string-equal
276 (file-remote-p "/method:user@email@host:" 'user
) "user@email"))
277 (should (string-equal
278 (file-remote-p "/method:user@email@host:" 'host
) "host"))
279 (should (string-equal
280 (file-remote-p "/method:user@email@host:" 'localname
) ""))
282 ;; Expand `tramp-default-method' and `tramp-default-user'.
283 (should (string-equal
284 (file-remote-p "/host#1234:")
285 (format "/%s:%s@%s:" "default-method" "default-user" "host#1234")))
286 (should (string-equal
287 (file-remote-p "/host#1234:" 'method
) "default-method"))
288 (should (string-equal (file-remote-p "/host#1234:" 'user
) "default-user"))
289 (should (string-equal (file-remote-p "/host#1234:" 'host
) "host#1234"))
290 (should (string-equal (file-remote-p "/host#1234:" 'localname
) ""))
292 ;; Expand `tramp-default-method'.
293 (should (string-equal
294 (file-remote-p "/user@host#1234:")
295 (format "/%s:%s@%s:" "default-method" "user" "host#1234")))
296 (should (string-equal
297 (file-remote-p "/user@host#1234:" 'method
) "default-method"))
298 (should (string-equal (file-remote-p "/user@host#1234:" 'user
) "user"))
299 (should (string-equal (file-remote-p "/user@host#1234:" 'host
) "host#1234"))
300 (should (string-equal (file-remote-p "/user@host#1234:" 'localname
) ""))
302 ;; Expand `tramp-default-user'.
303 (should (string-equal
304 (file-remote-p "/method:host#1234:")
305 (format "/%s:%s@%s:" "method" "default-user" "host#1234")))
306 (should (string-equal
307 (file-remote-p "/method:host#1234:" 'method
) "method"))
308 (should (string-equal
309 (file-remote-p "/method:host#1234:" 'user
) "default-user"))
310 (should (string-equal
311 (file-remote-p "/method:host#1234:" 'host
) "host#1234"))
312 (should (string-equal (file-remote-p "/method:host#1234:" 'localname
) ""))
315 (should (string-equal
316 (file-remote-p "/method:user@host#1234:")
317 (format "/%s:%s@%s:" "method" "user" "host#1234")))
318 (should (string-equal
319 (file-remote-p "/method:user@host#1234:" 'method
) "method"))
320 (should (string-equal
321 (file-remote-p "/method:user@host#1234:" 'user
) "user"))
322 (should (string-equal
323 (file-remote-p "/method:user@host#1234:" 'host
) "host#1234"))
324 (should (string-equal
325 (file-remote-p "/method:user@host#1234:" 'localname
) ""))
327 ;; Expand `tramp-default-method' and `tramp-default-user'.
328 (should (string-equal
329 (file-remote-p "/1.2.3.4:")
330 (format "/%s:%s@%s:" "default-method" "default-user" "1.2.3.4")))
331 (should (string-equal (file-remote-p "/1.2.3.4:" 'method
) "default-method"))
332 (should (string-equal (file-remote-p "/1.2.3.4:" 'user
) "default-user"))
333 (should (string-equal (file-remote-p "/1.2.3.4:" 'host
) "1.2.3.4"))
334 (should (string-equal (file-remote-p "/1.2.3.4:" 'localname
) ""))
336 ;; Expand `tramp-default-method'.
337 (should (string-equal
338 (file-remote-p "/user@1.2.3.4:")
339 (format "/%s:%s@%s:" "default-method" "user" "1.2.3.4")))
340 (should (string-equal
341 (file-remote-p "/user@1.2.3.4:" 'method
) "default-method"))
342 (should (string-equal (file-remote-p "/user@1.2.3.4:" 'user
) "user"))
343 (should (string-equal (file-remote-p "/user@1.2.3.4:" 'host
) "1.2.3.4"))
344 (should (string-equal (file-remote-p "/user@1.2.3.4:" 'localname
) ""))
346 ;; Expand `tramp-default-user'.
347 (should (string-equal
348 (file-remote-p "/method:1.2.3.4:")
349 (format "/%s:%s@%s:" "method" "default-user" "1.2.3.4")))
350 (should (string-equal (file-remote-p "/method:1.2.3.4:" 'method
) "method"))
351 (should (string-equal
352 (file-remote-p "/method:1.2.3.4:" 'user
) "default-user"))
353 (should (string-equal (file-remote-p "/method:1.2.3.4:" 'host
) "1.2.3.4"))
354 (should (string-equal (file-remote-p "/method:1.2.3.4:" 'localname
) ""))
357 (should (string-equal
358 (file-remote-p "/method:user@1.2.3.4:")
359 (format "/%s:%s@%s:" "method" "user" "1.2.3.4")))
360 (should (string-equal
361 (file-remote-p "/method:user@1.2.3.4:" 'method
) "method"))
362 (should (string-equal (file-remote-p "/method:user@1.2.3.4:" 'user
) "user"))
363 (should (string-equal
364 (file-remote-p "/method:user@1.2.3.4:" 'host
) "1.2.3.4"))
365 (should (string-equal
366 (file-remote-p "/method:user@1.2.3.4:" 'localname
) ""))
368 ;; Expand `tramp-default-method', `tramp-default-user' and
369 ;; `tramp-default-host'.
370 (should (string-equal
371 (file-remote-p "/[]:")
373 "/%s:%s@%s:" "default-method" "default-user" "default-host")))
374 (should (string-equal (file-remote-p "/[]:" 'method
) "default-method"))
375 (should (string-equal (file-remote-p "/[]:" 'user
) "default-user"))
376 (should (string-equal (file-remote-p "/[]:" 'host
) "default-host"))
377 (should (string-equal (file-remote-p "/[]:" 'localname
) ""))
379 ;; Expand `tramp-default-method' and `tramp-default-user'.
380 (let ((tramp-default-host "::1"))
381 (should (string-equal
382 (file-remote-p "/[]:")
383 (format "/%s:%s@%s:" "default-method" "default-user" "[::1]")))
384 (should (string-equal (file-remote-p "/[]:" 'method
) "default-method"))
385 (should (string-equal (file-remote-p "/[]:" 'user
) "default-user"))
386 (should (string-equal (file-remote-p "/[]:" 'host
) "::1"))
387 (should (string-equal (file-remote-p "/[]:" 'localname
) "")))
389 ;; Expand `tramp-default-method' and `tramp-default-user'.
390 (should (string-equal
391 (file-remote-p "/[::1]:")
392 (format "/%s:%s@%s:" "default-method" "default-user" "[::1]")))
393 (should (string-equal (file-remote-p "/[::1]:" 'method
) "default-method"))
394 (should (string-equal (file-remote-p "/[::1]:" 'user
) "default-user"))
395 (should (string-equal (file-remote-p "/[::1]:" 'host
) "::1"))
396 (should (string-equal (file-remote-p "/[::1]:" 'localname
) ""))
398 ;; Expand `tramp-default-method'.
399 (should (string-equal
400 (file-remote-p "/user@[::1]:")
401 (format "/%s:%s@%s:" "default-method" "user" "[::1]")))
402 (should (string-equal
403 (file-remote-p "/user@[::1]:" 'method
) "default-method"))
404 (should (string-equal (file-remote-p "/user@[::1]:" 'user
) "user"))
405 (should (string-equal (file-remote-p "/user@[::1]:" 'host
) "::1"))
406 (should (string-equal (file-remote-p "/user@[::1]:" 'localname
) ""))
408 ;; Expand `tramp-default-user'.
409 (should (string-equal
410 (file-remote-p "/method:[::1]:")
411 (format "/%s:%s@%s:" "method" "default-user" "[::1]")))
412 (should (string-equal (file-remote-p "/method:[::1]:" 'method
) "method"))
413 (should (string-equal
414 (file-remote-p "/method:[::1]:" 'user
) "default-user"))
415 (should (string-equal (file-remote-p "/method:[::1]:" 'host
) "::1"))
416 (should (string-equal (file-remote-p "/method:[::1]:" 'localname
) ""))
419 (should (string-equal
420 (file-remote-p "/method:user@[::1]:")
421 (format "/%s:%s@%s:" "method" "user" "[::1]")))
422 (should (string-equal
423 (file-remote-p "/method:user@[::1]:" 'method
) "method"))
424 (should (string-equal (file-remote-p "/method:user@[::1]:" 'user
) "user"))
425 (should (string-equal (file-remote-p "/method:user@[::1]:" 'host
) "::1"))
426 (should (string-equal
427 (file-remote-p "/method:user@[::1]:" 'localname
) ""))
429 ;; Local file name part.
430 (should (string-equal (file-remote-p "/host:/:" 'localname
) "/:"))
431 (should (string-equal (file-remote-p "/method:::" 'localname
) ":"))
432 (should (string-equal (file-remote-p "/method:: " 'localname
) " "))
433 (should (string-equal (file-remote-p "/method::file" 'localname
) "file"))
434 (should (string-equal
435 (file-remote-p "/method::/path/to/file" 'localname
)
441 (file-remote-p "/method1:user1@host1|method2:user2@host2:/path/to/file")
442 (format "/%s:%s@%s:" "method2" "user2" "host2")))
446 "/method1:user1@host1|method2:user2@host2:/path/to/file" 'method
)
451 "/method1:user1@host1|method2:user2@host2:/path/to/file" 'user
)
456 "/method1:user1@host1|method2:user2@host2:/path/to/file" 'host
)
461 "/method1:user1@host1|method2:user2@host2:/path/to/file" 'localname
)
467 "/method1:user1@host1|method2:user2@host2|method3:user3@host3:/path/to/file")
468 (format "/%s:%s@%s:" "method3" "user3" "host3")))
472 "/method1:user1@host1|method2:user2@host2|method3:user3@host3:/path/to/file"
478 "/method1:user1@host1|method2:user2@host2|method3:user3@host3:/path/to/file"
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"
494 (ert-deftest tramp-test03-file-name-defaults
()
495 "Check default values for some methods."
496 ;; Default values in tramp-adb.el.
497 (should (string-equal (file-remote-p "/adb::" 'host
) ""))
498 ;; Default values in tramp-ftp.el.
499 (should (string-equal (file-remote-p "/ftp.host:" 'method
) "ftp"))
500 (dolist (u '("ftp" "anonymous"))
501 (should (string-equal (file-remote-p (format "/%s@:" u
) 'method
) "ftp")))
502 ;; Default values in tramp-gvfs.el.
503 (when (and (load "tramp-gvfs" 'noerror
'nomessage
)
504 (symbol-value 'tramp-gvfs-enabled
))
505 (should (string-equal (file-remote-p "/synce::" 'user
) nil
)))
506 ;; Default values in tramp-gw.el.
507 (dolist (m '("tunnel" "socks"))
509 (string-equal (file-remote-p (format "/%s::" m
) 'user
) (user-login-name))))
510 ;; Default values in tramp-sh.el.
511 (dolist (h `("127.0.0.1" "[::1]" "localhost" "localhost6" ,(system-name)))
512 (should (string-equal (file-remote-p (format "/root@%s:" h
) 'method
) "su")))
513 (dolist (m '("su" "sudo" "ksu"))
514 (should (string-equal (file-remote-p (format "/%s::" m
) 'user
) "root")))
515 (dolist (m '("rcp" "remcp" "rsh" "telnet" "krlogin" "fcp"))
517 (string-equal (file-remote-p (format "/%s::" m
) 'user
) (user-login-name))))
518 ;; Default values in tramp-smb.el.
519 (should (string-equal (file-remote-p "/user%domain@host:" 'method
) "smb"))
520 (should (string-equal (file-remote-p "/smb::" 'user
) nil
)))
522 (ert-deftest tramp-test04-substitute-in-file-name
()
523 "Check `substitute-in-file-name'."
524 (should (string-equal (substitute-in-file-name "/method:host://foo") "/foo"))
527 (substitute-in-file-name "/method:host:/path//foo") "/method:host:/foo"))
529 (string-equal (substitute-in-file-name "/method:host:/path///foo") "/foo"))
532 (substitute-in-file-name "/method:host:/path/~/foo") "/method:host:~/foo"))
534 (string-equal (substitute-in-file-name "/method:host:/path//~/foo") "~/foo"))
535 (let (process-environment)
538 (substitute-in-file-name "/method:host:/path/$FOO")
539 "/method:host:/path/$FOO"))
543 (substitute-in-file-name "/method:host:/path/$FOO")
544 "/method:host:/path/bla"))
547 (substitute-in-file-name "/method:host:/path/$$FOO")
548 "/method:host:/path/$FOO"))))
550 (ert-deftest tramp-test05-expand-file-name
()
551 "Check `expand-file-name'."
554 (expand-file-name "/method:host:/path/./file") "/method:host:/path/file"))
557 (expand-file-name "/method:host:/path/../file") "/method:host:/file")))
559 (ert-deftest tramp-test06-directory-file-name
()
560 "Check `directory-file-name'.
561 This checks also `file-name-as-directory', `file-name-directory'
562 and `file-name-nondirectory'."
565 (directory-file-name "/method:host:/path/to/file")
566 "/method:host:/path/to/file"))
569 (directory-file-name "/method:host:/path/to/file/")
570 "/method:host:/path/to/file"))
573 (file-name-as-directory "/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-directory "/method:host:/path/to/file")
582 "/method:host:/path/to/"))
585 (file-name-directory "/method:host:/path/to/file/")
586 "/method:host:/path/to/file/"))
588 (string-equal (file-name-nondirectory "/method:host:/path/to/file") "file"))
590 (string-equal (file-name-nondirectory "/method:host:/path/to/file/") ""))
593 (unhandled-file-name-directory "/method:host:/path/to/file"))))
595 (ert-deftest tramp-test07-file-exists-p
()
596 "Check `file-exist-p', `write-region' and `delete-file'."
597 (skip-unless (tramp--test-enabled))
599 (let ((tmp-name (tramp--test-make-temp-name)))
600 (should-not (file-exists-p tmp-name
))
601 (write-region "foo" nil tmp-name
)
602 (should (file-exists-p tmp-name
))
603 (delete-file tmp-name
)
604 (should-not (file-exists-p tmp-name
))))
606 (ert-deftest tramp-test08-file-local-copy
()
607 "Check `file-local-copy'."
608 (skip-unless (tramp--test-enabled))
610 (let ((tmp-name1 (tramp--test-make-temp-name))
614 (write-region "foo" nil tmp-name1
)
615 (should (setq tmp-name2
(file-local-copy tmp-name1
)))
617 (insert-file-contents tmp-name2
)
618 (should (string-equal (buffer-string) "foo"))))
620 (delete-file tmp-name1
)
621 (delete-file tmp-name2
)))))
623 (ert-deftest tramp-test09-insert-file-contents
()
624 "Check `insert-file-contents'."
625 (skip-unless (tramp--test-enabled))
627 (let ((tmp-name (tramp--test-make-temp-name)))
630 (write-region "foo" nil tmp-name
)
632 (insert-file-contents tmp-name
)
633 (should (string-equal (buffer-string) "foo"))
634 (insert-file-contents tmp-name
)
635 (should (string-equal (buffer-string) "foofoo"))
637 (insert-file-contents tmp-name nil
1 3)
638 (should (string-equal (buffer-string) "oofoofoo"))
640 (insert-file-contents tmp-name nil nil nil
'replace
)
641 (should (string-equal (buffer-string) "foo"))))
642 (ignore-errors (delete-file tmp-name
)))))
644 (ert-deftest tramp-test10-write-region
()
645 "Check `write-region'."
646 (skip-unless (tramp--test-enabled))
648 (let ((tmp-name (tramp--test-make-temp-name)))
653 (write-region nil nil tmp-name
))
655 (insert-file-contents tmp-name
)
656 (should (string-equal (buffer-string) "foo")))
660 (write-region nil nil tmp-name
'append
))
662 (insert-file-contents tmp-name
)
663 (should (string-equal (buffer-string) "foobla")))
665 (write-region "foo" nil tmp-name
)
667 (insert-file-contents tmp-name
)
668 (should (string-equal (buffer-string) "foo")))
672 (write-region 3 5 tmp-name
))
674 (insert-file-contents tmp-name
)
675 (should (string-equal (buffer-string) "34"))))
676 (ignore-errors (delete-file tmp-name
)))))
678 (ert-deftest tramp-test11-copy-file
()
680 (skip-unless (tramp--test-enabled))
682 (let ((tmp-name1 (tramp--test-make-temp-name))
683 (tmp-name2 (tramp--test-make-temp-name))
684 (tmp-name3 (tramp--test-make-temp-name))
685 (tmp-name4 (tramp--test-make-temp-name 'local
))
686 (tmp-name5 (tramp--test-make-temp-name 'local
)))
688 ;; Copy on remote side.
691 (write-region "foo" nil tmp-name1
)
692 (copy-file tmp-name1 tmp-name2
)
693 (should (file-exists-p tmp-name2
))
695 (insert-file-contents tmp-name2
)
696 (should (string-equal (buffer-string) "foo")))
697 (should-error (copy-file tmp-name1 tmp-name2
))
698 (copy-file tmp-name1 tmp-name2
'ok
)
699 (make-directory tmp-name3
)
700 (copy-file tmp-name1 tmp-name3
)
703 (expand-file-name (file-name-nondirectory tmp-name1
) tmp-name3
))))
704 (ignore-errors (delete-file tmp-name1
))
705 (ignore-errors (delete-file tmp-name2
))
706 (ignore-errors (delete-directory tmp-name3
'recursive
)))
708 ;; Copy from remote side to local side.
711 (write-region "foo" nil tmp-name1
)
712 (copy-file tmp-name1 tmp-name4
)
713 (should (file-exists-p tmp-name4
))
715 (insert-file-contents tmp-name4
)
716 (should (string-equal (buffer-string) "foo")))
717 (should-error (copy-file tmp-name1 tmp-name4
))
718 (copy-file tmp-name1 tmp-name4
'ok
)
719 (make-directory tmp-name5
)
720 (copy-file tmp-name1 tmp-name5
)
723 (expand-file-name (file-name-nondirectory tmp-name1
) tmp-name5
))))
724 (ignore-errors (delete-file tmp-name1
))
725 (ignore-errors (delete-file tmp-name4
))
726 (ignore-errors (delete-directory tmp-name5
'recursive
)))
728 ;; Copy from local side to remote side.
731 (write-region "foo" nil tmp-name4 nil
'nomessage
)
732 (copy-file tmp-name4 tmp-name1
)
733 (should (file-exists-p tmp-name1
))
735 (insert-file-contents tmp-name1
)
736 (should (string-equal (buffer-string) "foo")))
737 (should-error (copy-file tmp-name4 tmp-name1
))
738 (copy-file tmp-name4 tmp-name1
'ok
)
739 (make-directory tmp-name3
)
740 (copy-file tmp-name4 tmp-name3
)
743 (expand-file-name (file-name-nondirectory tmp-name4
) tmp-name3
))))
744 (ignore-errors (delete-file tmp-name1
))
745 (ignore-errors (delete-file tmp-name4
))
746 (ignore-errors (delete-directory tmp-name3
'recursive
)))))
748 (ert-deftest tramp-test12-rename-file
()
749 "Check `rename-file'."
750 (skip-unless (tramp--test-enabled))
752 (let ((tmp-name1 (tramp--test-make-temp-name))
753 (tmp-name2 (tramp--test-make-temp-name))
754 (tmp-name3 (tramp--test-make-temp-name))
755 (tmp-name4 (tramp--test-make-temp-name 'local
))
756 (tmp-name5 (tramp--test-make-temp-name 'local
)))
758 ;; Rename on remote side.
761 (write-region "foo" nil tmp-name1
)
762 (rename-file tmp-name1 tmp-name2
)
763 (should-not (file-exists-p tmp-name1
))
764 (should (file-exists-p tmp-name2
))
766 (insert-file-contents tmp-name2
)
767 (should (string-equal (buffer-string) "foo")))
768 (write-region "foo" nil tmp-name1
)
769 (should-error (rename-file tmp-name1 tmp-name2
))
770 (rename-file tmp-name1 tmp-name2
'ok
)
771 (should-not (file-exists-p tmp-name1
))
772 (write-region "foo" nil tmp-name1
)
773 (make-directory tmp-name3
)
774 (rename-file tmp-name1 tmp-name3
)
775 (should-not (file-exists-p tmp-name1
))
778 (expand-file-name (file-name-nondirectory tmp-name1
) tmp-name3
))))
779 (ignore-errors (delete-file tmp-name1
))
780 (ignore-errors (delete-file tmp-name2
))
781 (ignore-errors (delete-directory tmp-name3
'recursive
)))
783 ;; Rename from remote side to local side.
786 (write-region "foo" nil tmp-name1
)
787 (rename-file tmp-name1 tmp-name4
)
788 (should-not (file-exists-p tmp-name1
))
789 (should (file-exists-p tmp-name4
))
791 (insert-file-contents tmp-name4
)
792 (should (string-equal (buffer-string) "foo")))
793 (write-region "foo" nil tmp-name1
)
794 (should-error (rename-file tmp-name1 tmp-name4
))
795 (rename-file tmp-name1 tmp-name4
'ok
)
796 (should-not (file-exists-p tmp-name1
))
797 (write-region "foo" nil tmp-name1
)
798 (make-directory tmp-name5
)
799 (rename-file tmp-name1 tmp-name5
)
800 (should-not (file-exists-p tmp-name1
))
803 (expand-file-name (file-name-nondirectory tmp-name1
) tmp-name5
))))
804 (ignore-errors (delete-file tmp-name1
))
805 (ignore-errors (delete-file tmp-name4
))
806 (ignore-errors (delete-directory tmp-name5
'recursive
)))
808 ;; Rename from local side to remote side.
811 (write-region "foo" nil tmp-name4 nil
'nomessage
)
812 (rename-file tmp-name4 tmp-name1
)
813 (should-not (file-exists-p tmp-name4
))
814 (should (file-exists-p tmp-name1
))
816 (insert-file-contents tmp-name1
)
817 (should (string-equal (buffer-string) "foo")))
818 (write-region "foo" nil tmp-name4 nil
'nomessage
)
819 (should-error (rename-file tmp-name4 tmp-name1
))
820 (rename-file tmp-name4 tmp-name1
'ok
)
821 (should-not (file-exists-p tmp-name4
))
822 (write-region "foo" nil tmp-name4 nil
'nomessage
)
823 (make-directory tmp-name3
)
824 (rename-file tmp-name4 tmp-name3
)
825 (should-not (file-exists-p tmp-name4
))
828 (expand-file-name (file-name-nondirectory tmp-name4
) tmp-name3
))))
829 (ignore-errors (delete-file tmp-name1
))
830 (ignore-errors (delete-file tmp-name4
))
831 (ignore-errors (delete-directory tmp-name3
'recursive
)))))
833 (ert-deftest tramp-test13-make-directory
()
834 "Check `make-directory'.
835 This tests also `file-directory-p' and `file-accessible-directory-p'."
836 (skip-unless (tramp--test-enabled))
838 (let ((tmp-name (tramp--test-make-temp-name)))
841 (make-directory tmp-name
)
842 (should (file-directory-p tmp-name
))
843 (should (file-accessible-directory-p tmp-name
)))
844 (ignore-errors (delete-directory tmp-name
)))))
846 (ert-deftest tramp-test14-delete-directory
()
847 "Check `delete-directory'."
848 (skip-unless (tramp--test-enabled))
850 (let ((tmp-name (tramp--test-make-temp-name)))
851 ;; Delete empty directory.
852 (make-directory tmp-name
)
853 (should (file-directory-p tmp-name
))
854 (delete-directory tmp-name
)
855 (should-not (file-directory-p tmp-name
))
856 ;; Delete non-empty directory.
857 (make-directory tmp-name
)
858 (write-region "foo" nil
(expand-file-name "bla" tmp-name
))
859 (should-error (delete-directory tmp-name
) :type
'file-error
)
860 (delete-directory tmp-name
'recursive
)
861 (should-not (file-directory-p tmp-name
))))
863 (ert-deftest tramp-test15-copy-directory
()
864 "Check `copy-directory'."
865 (skip-unless (tramp--test-enabled))
869 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory
)
870 'tramp-smb-file-name-handler
)))
872 (let* ((tmp-name1 (tramp--test-make-temp-name))
873 (tmp-name2 (tramp--test-make-temp-name))
874 (tmp-name3 (expand-file-name
875 (file-name-nondirectory tmp-name1
) tmp-name2
))
876 (tmp-name4 (expand-file-name "foo" tmp-name1
))
877 (tmp-name5 (expand-file-name "foo" tmp-name2
))
878 (tmp-name6 (expand-file-name "foo" tmp-name3
)))
881 ;; Copy empty directory.
882 (make-directory tmp-name1
)
883 (write-region "foo" nil tmp-name4
)
884 (should (file-directory-p tmp-name1
))
885 (should (file-exists-p tmp-name4
))
886 (copy-directory tmp-name1 tmp-name2
)
887 (should (file-directory-p tmp-name2
))
888 (should (file-exists-p tmp-name5
))
889 ;; Target directory does exist already.
890 (copy-directory tmp-name1 tmp-name2
)
891 (should (file-directory-p tmp-name3
))
892 (should (file-exists-p tmp-name6
)))
894 (delete-directory tmp-name1
'recursive
)
895 (delete-directory tmp-name2
'recursive
)))))
897 (ert-deftest tramp-test16-directory-files
()
898 "Check `directory-files'."
899 (skip-unless (tramp--test-enabled))
901 (let* ((tmp-name1 (tramp--test-make-temp-name))
902 (tmp-name2 (expand-file-name "bla" tmp-name1
))
903 (tmp-name3 (expand-file-name "foo" tmp-name1
)))
906 (make-directory tmp-name1
)
907 (write-region "foo" nil tmp-name2
)
908 (write-region "bla" nil tmp-name3
)
909 (should (file-directory-p tmp-name1
))
910 (should (file-exists-p tmp-name2
))
911 (should (file-exists-p tmp-name3
))
912 (should (equal (directory-files tmp-name1
) '("." ".." "bla" "foo")))
913 (should (equal (directory-files tmp-name1
'full
)
914 `(,(concat tmp-name1
"/.")
915 ,(concat tmp-name1
"/..")
916 ,tmp-name2
,tmp-name3
)))
917 (should (equal (directory-files
918 tmp-name1 nil directory-files-no-dot-files-regexp
)
920 (should (equal (directory-files
921 tmp-name1
'full directory-files-no-dot-files-regexp
)
922 `(,tmp-name2
,tmp-name3
))))
923 (ignore-errors (delete-directory tmp-name1
'recursive
)))))
925 (ert-deftest tramp-test17-insert-directory
()
926 "Check `insert-directory'."
927 (skip-unless (tramp--test-enabled))
929 (let* ((tmp-name1 (tramp--test-make-temp-name))
930 (tmp-name2 (expand-file-name "foo" tmp-name1
)))
933 (make-directory tmp-name1
)
934 (write-region "foo" nil tmp-name2
)
935 (should (file-directory-p tmp-name1
))
936 (should (file-exists-p tmp-name2
))
938 (insert-directory tmp-name1 nil
)
939 (goto-char (point-min))
940 (should (looking-at-p (regexp-quote tmp-name1
))))
942 (insert-directory tmp-name1
"-al")
943 (goto-char (point-min))
944 (should (looking-at-p (format "^.+ %s$" (regexp-quote tmp-name1
)))))
946 (insert-directory (file-name-as-directory tmp-name1
) "-al")
947 (goto-char (point-min))
949 (looking-at-p (format "^.+ %s/$" (regexp-quote tmp-name1
)))))
952 (file-name-as-directory tmp-name1
) "-al" nil
'full-directory-p
)
953 (goto-char (point-min))
956 "\\(total.+[[:digit:]]+\n\\)?.+ \\.\n.+ \\.\\.\n.+ foo$"))))
957 (ignore-errors (delete-directory tmp-name1
'recursive
)))))
959 (ert-deftest tramp-test18-file-attributes
()
960 "Check `file-attributes'.
961 This tests also `file-readable-p' and `file-regular-p'."
962 (skip-unless (tramp--test-enabled))
964 (let ((tmp-name (tramp--test-make-temp-name))
968 (write-region "foo" nil tmp-name
)
969 (should (file-exists-p tmp-name
))
970 (setq attr
(file-attributes tmp-name
))
971 (should (consp attr
))
972 (should (file-exists-p tmp-name
))
973 (should (file-readable-p tmp-name
))
974 (should (file-regular-p tmp-name
))
975 ;; We do not test inodes and device numbers.
976 (should (null (car attr
)))
977 (should (numberp (nth 1 attr
))) ;; Link.
978 (should (numberp (nth 2 attr
))) ;; Uid.
979 (should (numberp (nth 3 attr
))) ;; Gid.
981 (should (stringp (current-time-string (nth 4 attr
))))
982 ;; Last modification time.
983 (should (stringp (current-time-string (nth 5 attr
))))
984 ;; Last status change time.
985 (should (stringp (current-time-string (nth 6 attr
))))
986 (should (numberp (nth 7 attr
))) ;; Size.
987 (should (stringp (nth 8 attr
))) ;; Modes.
989 (setq attr
(file-attributes tmp-name
'string
))
990 (should (stringp (nth 2 attr
))) ;; Uid.
991 (should (stringp (nth 3 attr
))) ;; Gid.
992 (delete-file tmp-name
)
994 (make-directory tmp-name
)
995 (should (file-exists-p tmp-name
))
996 (should (file-readable-p tmp-name
))
997 (should-not (file-regular-p tmp-name
))
998 (setq attr
(file-attributes tmp-name
))
999 (should (eq (car attr
) t
)))
1000 (ignore-errors (delete-directory tmp-name
)))))
1002 (ert-deftest tramp-test19-directory-files-and-attributes
()
1003 "Check `directory-files-and-attributes'."
1004 (skip-unless (tramp--test-enabled))
1006 ;; `directory-files-and-attributes' contains also values for "../".
1007 ;; Ensure that this doesn't change during tests, for
1008 ;; example due to handling temporary files.
1009 (let* ((tmp-name1 (tramp--test-make-temp-name))
1010 (tmp-name2 (expand-file-name "bla" tmp-name1
))
1014 (make-directory tmp-name1
)
1015 (should (file-directory-p tmp-name1
))
1016 (make-directory tmp-name2
)
1017 (should (file-directory-p tmp-name2
))
1018 (write-region "foo" nil
(expand-file-name "foo" tmp-name2
))
1019 (write-region "bar" nil
(expand-file-name "bar" tmp-name2
))
1020 (write-region "boz" nil
(expand-file-name "boz" tmp-name2
))
1021 (setq attr
(directory-files-and-attributes tmp-name2
))
1022 (should (consp attr
))
1023 ;; Dumb remote shells without perl(1) or stat(1) are not
1024 ;; able to return the date correctly. They say "don't know".
1029 (file-attributes (expand-file-name (car elt
) tmp-name2
)))
1032 (equal (file-attributes (expand-file-name (car elt
) tmp-name2
))
1034 (setq attr
(directory-files-and-attributes tmp-name2
'full
))
1036 (unless (equal (nth 5 (file-attributes (car elt
))) '(0 0))
1038 (equal (file-attributes (car elt
)) (cdr elt
)))))
1039 (setq attr
(directory-files-and-attributes tmp-name2 nil
"^b"))
1040 (should (equal (mapcar 'car attr
) '("bar" "boz"))))
1041 (ignore-errors (delete-directory tmp-name1
'recursive
)))))
1043 (ert-deftest tramp-test20-file-modes
()
1044 "Check `file-modes'.
1045 This tests also `file-executable-p', `file-writable-p' and `set-file-modes'."
1046 (skip-unless (tramp--test-enabled))
1050 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory
)
1051 '(tramp-adb-file-name-handler
1052 tramp-gvfs-file-name-handler
1053 tramp-smb-file-name-handler
))))
1055 (let ((tmp-name (tramp--test-make-temp-name)))
1058 (write-region "foo" nil tmp-name
)
1059 (should (file-exists-p tmp-name
))
1060 (set-file-modes tmp-name
#o777
)
1061 (should (= (file-modes tmp-name
) #o777
))
1062 (should (file-executable-p tmp-name
))
1063 (should (file-writable-p tmp-name
))
1064 (set-file-modes tmp-name
#o444
)
1065 (should (= (file-modes tmp-name
) #o444
))
1066 (should-not (file-executable-p tmp-name
))
1067 ;; A file is always writable for user "root".
1068 (unless (zerop (nth 2 (file-attributes tmp-name
)))
1069 (should-not (file-writable-p tmp-name
))))
1070 (ignore-errors (delete-file tmp-name
)))))
1072 (ert-deftest tramp-test21-file-links
()
1073 "Check `file-symlink-p'.
1074 This tests also `make-symbolic-link', `file-truename' and `add-name-to-file'."
1075 (skip-unless (tramp--test-enabled))
1077 ;; We must use `file-truename' for the temporary directory, because
1078 ;; it could be located on a symlinked directory. This would let the
1080 (let* ((tramp-test-temporary-file-directory
1081 (file-truename tramp-test-temporary-file-directory
))
1082 (tmp-name1 (tramp--test-make-temp-name))
1083 (tmp-name2 (tramp--test-make-temp-name))
1084 (tmp-name3 (tramp--test-make-temp-name 'local
)))
1087 (write-region "foo" nil tmp-name1
)
1088 (should (file-exists-p tmp-name1
))
1089 ;; Method "smb" supports `make-symbolic-link' only if the
1090 ;; remote host has CIFS capabilities. tramp-adb.el and
1091 ;; tramp-gvfs.el do not support symbolic links at all.
1093 (make-symbolic-link tmp-name1 tmp-name2
)
1096 (not (string-equal (error-message-string err
)
1097 "make-symbolic-link not supported")))))
1098 (should (file-symlink-p tmp-name2
))
1099 (should-error (make-symbolic-link tmp-name1 tmp-name2
))
1100 (make-symbolic-link tmp-name1 tmp-name2
'ok-if-already-exists
)
1101 (should (file-symlink-p tmp-name2
))
1102 ;; `tmp-name3' is a local file name.
1103 (should-error (make-symbolic-link tmp-name1 tmp-name3
)))
1105 (delete-file tmp-name1
)
1106 (delete-file tmp-name2
)))
1110 (write-region "foo" nil tmp-name1
)
1111 (should (file-exists-p tmp-name1
))
1112 (add-name-to-file tmp-name1 tmp-name2
)
1113 (should-not (file-symlink-p tmp-name2
))
1114 (should-error (add-name-to-file tmp-name1 tmp-name2
))
1115 (add-name-to-file tmp-name1 tmp-name2
'ok-if-already-exists
)
1116 (should-not (file-symlink-p tmp-name2
))
1117 ;; `tmp-name3' is a local file name.
1118 (should-error (add-name-to-file tmp-name1 tmp-name3
)))
1120 (delete-file tmp-name1
)
1121 (delete-file tmp-name2
)))
1125 (write-region "foo" nil tmp-name1
)
1126 (should (file-exists-p tmp-name1
))
1127 (make-symbolic-link tmp-name1 tmp-name2
)
1128 (should (file-symlink-p tmp-name2
))
1129 (should-not (string-equal tmp-name2
(file-truename tmp-name2
)))
1131 (string-equal (file-truename tmp-name1
) (file-truename tmp-name2
)))
1132 (should (file-equal-p tmp-name1 tmp-name2
)))
1134 (delete-file tmp-name1
)
1135 (delete-file tmp-name2
)))
1137 ;; `file-truename' shall preserve trailing link of directories.
1138 (unless (file-symlink-p tramp-test-temporary-file-directory
)
1139 (let* ((dir1 (directory-file-name tramp-test-temporary-file-directory
))
1140 (dir2 (file-name-as-directory dir1
)))
1141 (should (string-equal (file-truename dir1
) (expand-file-name dir1
)))
1142 (should (string-equal (file-truename dir2
) (expand-file-name dir2
)))))))
1144 (ert-deftest tramp-test22-file-times
()
1145 "Check `set-file-times' and `file-newer-than-file-p'."
1146 (skip-unless (tramp--test-enabled))
1150 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory
)
1151 '(tramp-gvfs-file-name-handler tramp-smb-file-name-handler
))))
1153 (let ((tmp-name1 (tramp--test-make-temp-name))
1154 (tmp-name2 (tramp--test-make-temp-name))
1155 (tmp-name3 (tramp--test-make-temp-name)))
1158 (write-region "foo" nil tmp-name1
)
1159 (should (file-exists-p tmp-name1
))
1160 (should (consp (nth 5 (file-attributes tmp-name1
))))
1161 ;; '(0 0) means don't know, and will be replaced by
1162 ;; `current-time'. Therefore, we use '(0 1).
1163 ;; We skip the test, if the remote handler is not able to
1164 ;; set the correct time.
1165 (skip-unless (set-file-times tmp-name1
'(0 1)))
1166 ;; Dumb remote shells without perl(1) or stat(1) are not
1167 ;; able to return the date correctly. They say "don't know".
1168 (unless (equal (nth 5 (file-attributes tmp-name1
)) '(0 0))
1169 (should (equal (nth 5 (file-attributes tmp-name1
)) '(0 1)))
1170 (write-region "bla" nil tmp-name2
)
1171 (should (file-exists-p tmp-name2
))
1172 (should (file-newer-than-file-p tmp-name2 tmp-name1
))
1173 ;; `tmp-name3' does not exist.
1174 (should (file-newer-than-file-p tmp-name2 tmp-name3
))
1175 (should-not (file-newer-than-file-p tmp-name3 tmp-name1
))))
1177 (delete-file tmp-name1
)
1178 (delete-file tmp-name2
)))))
1180 (ert-deftest tramp-test23-visited-file-modtime
()
1181 "Check `set-visited-file-modtime' and `verify-visited-file-modtime'."
1182 (skip-unless (tramp--test-enabled))
1184 (let ((tmp-name (tramp--test-make-temp-name)))
1187 (write-region "foo" nil tmp-name
)
1188 (should (file-exists-p tmp-name
))
1190 (insert-file-contents tmp-name
)
1191 (should (verify-visited-file-modtime))
1192 (set-visited-file-modtime '(0 1))
1193 (should (verify-visited-file-modtime))
1194 (should (equal (visited-file-modtime) '(0 1 0 0)))))
1195 (ignore-errors (delete-file tmp-name
)))))
1197 (ert-deftest tramp-test24-file-name-completion
()
1198 "Check `file-name-completion' and `file-name-all-completions'."
1199 (skip-unless (tramp--test-enabled))
1201 (let ((tmp-name (tramp--test-make-temp-name)))
1204 (make-directory tmp-name
)
1205 (should (file-directory-p tmp-name
))
1206 (write-region "foo" nil
(expand-file-name "foo" tmp-name
))
1207 (write-region "bar" nil
(expand-file-name "bold" tmp-name
))
1208 (make-directory (expand-file-name "boz" tmp-name
))
1209 (should (equal (file-name-completion "fo" tmp-name
) "foo"))
1210 (should (equal (file-name-completion "b" tmp-name
) "bo"))
1212 (equal (file-name-completion "b" tmp-name
'file-directory-p
) "boz/"))
1213 (should (equal (file-name-all-completions "fo" tmp-name
) '("foo")))
1215 (equal (sort (file-name-all-completions "b" tmp-name
) 'string-lessp
)
1217 (ignore-errors (delete-directory tmp-name
'recursive
)))))
1219 (ert-deftest tramp-test25-load
()
1221 (skip-unless (tramp--test-enabled))
1223 (let ((tmp-name (tramp--test-make-temp-name)))
1226 (load tmp-name
'noerror
'nomessage
)
1227 (should-not (featurep 'tramp-test-load
))
1228 (write-region "(provide 'tramp-test-load)" nil tmp-name
)
1229 ;; `load' in lread.c does not pass `must-suffix'. Why?
1230 ;(should-error (load tmp-name nil 'nomessage 'nosuffix 'must-suffix))
1231 (load tmp-name nil
'nomessage
'nosuffix
)
1232 (should (featurep 'tramp-test-load
)))
1234 (and (featurep 'tramp-test-load
) (unload-feature 'tramp-test-load
))
1235 (delete-file tmp-name
)))))
1237 (ert-deftest tramp-test26-process-file
()
1238 "Check `process-file'."
1239 (skip-unless (tramp--test-enabled))
1243 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory
)
1244 '(tramp-gvfs-file-name-handler tramp-smb-file-name-handler
))))
1246 (let* ((tmp-name (tramp--test-make-temp-name))
1247 (fnnd (file-name-nondirectory tmp-name
))
1248 (default-directory tramp-test-temporary-file-directory
)
1249 kill-buffer-query-functions
)
1252 ;; We cannot use "/bin/true" and "/bin/false"; those paths
1253 ;; do not exist on hydra.
1254 (should (zerop (process-file "true")))
1255 (should-not (zerop (process-file "false")))
1256 (should-not (zerop (process-file "binary-does-not-exist")))
1258 (write-region "foo" nil tmp-name
)
1259 (should (file-exists-p tmp-name
))
1260 (should (zerop (process-file "ls" nil t nil fnnd
)))
1261 ;; `ls' could produce colorized output.
1262 (goto-char (point-min))
1263 (while (re-search-forward tramp-color-escape-sequence-regexp nil t
)
1264 (replace-match "" nil nil
))
1265 (should (string-equal (format "%s\n" fnnd
) (buffer-string)))
1266 (should-not (get-buffer-window (current-buffer) t
))
1268 ;; Second run. The output must be appended.
1269 (should (zerop (process-file "ls" nil t t fnnd
)))
1270 ;; `ls' could produce colorized output.
1271 (goto-char (point-min))
1272 (while (re-search-forward tramp-color-escape-sequence-regexp nil t
)
1273 (replace-match "" nil nil
))
1275 (string-equal (format "%s\n%s\n" fnnd fnnd
) (buffer-string)))
1276 ;; A non-nil DISPLAY must not raise the buffer.
1277 (should-not (get-buffer-window (current-buffer) t
))))
1279 (ignore-errors (delete-file tmp-name
)))))
1281 (ert-deftest tramp-test27-start-file-process
()
1282 "Check `start-file-process'."
1283 (skip-unless (tramp--test-enabled))
1287 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory
)
1288 '(tramp-adb-file-name-handler
1289 tramp-gvfs-file-name-handler
1290 tramp-smb-file-name-handler
))))
1292 (let ((default-directory tramp-test-temporary-file-directory
)
1293 (tmp-name (tramp--test-make-temp-name))
1294 kill-buffer-query-functions proc
)
1297 (setq proc
(start-file-process "test1" (current-buffer) "cat"))
1298 (should (processp proc
))
1299 (should (equal (process-status proc
) 'run
))
1300 (process-send-string proc
"foo")
1301 (process-send-eof proc
)
1303 (with-timeout (10 (ert-fail "`start-file-process' timed out"))
1304 (while (< (- (point-max) (point-min)) (length "foo"))
1305 (accept-process-output proc
1)))
1306 (should (string-equal (buffer-string) "foo")))
1307 (ignore-errors (delete-process proc
)))
1311 (write-region "foo" nil tmp-name
)
1312 (should (file-exists-p tmp-name
))
1315 "test2" (current-buffer)
1316 "cat" (file-name-nondirectory tmp-name
)))
1317 (should (processp proc
))
1319 (with-timeout (10 (ert-fail "`start-file-process' timed out"))
1320 (while (< (- (point-max) (point-min)) (length "foo"))
1321 (accept-process-output proc
1)))
1322 (should (string-equal (buffer-string) "foo")))
1324 (delete-process proc
)
1325 (delete-file tmp-name
)))
1329 (setq proc
(start-file-process "test3" (current-buffer) "cat"))
1330 (should (processp proc
))
1331 (should (equal (process-status proc
) 'run
))
1334 (lambda (p s
) (with-current-buffer (process-buffer p
) (insert s
))))
1335 (process-send-string proc
"foo")
1336 (process-send-eof proc
)
1338 (with-timeout (10 (ert-fail "`start-file-process' timed out"))
1339 (while (< (- (point-max) (point-min)) (length "foo"))
1340 (accept-process-output proc
1)))
1341 (should (string-equal (buffer-string) "foo")))
1342 (ignore-errors (delete-process proc
)))))
1344 (ert-deftest tramp-test28-shell-command
()
1345 "Check `shell-command'."
1346 (skip-unless (tramp--test-enabled))
1350 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory
)
1351 '(tramp-adb-file-name-handler
1352 tramp-gvfs-file-name-handler
1353 tramp-smb-file-name-handler
))))
1355 (let ((tmp-name (tramp--test-make-temp-name))
1356 (default-directory tramp-test-temporary-file-directory
)
1357 kill-buffer-query-functions
)
1360 (write-region "foo" nil tmp-name
)
1361 (should (file-exists-p tmp-name
))
1363 (format "ls %s" (file-name-nondirectory tmp-name
)) (current-buffer))
1364 ;; `ls' could produce colorized output.
1365 (goto-char (point-min))
1366 (while (re-search-forward tramp-color-escape-sequence-regexp nil t
)
1367 (replace-match "" nil nil
))
1370 (format "%s\n" (file-name-nondirectory tmp-name
)) (buffer-string))))
1371 (ignore-errors (delete-file tmp-name
)))
1375 (write-region "foo" nil tmp-name
)
1376 (should (file-exists-p tmp-name
))
1377 (async-shell-command
1378 (format "ls %s" (file-name-nondirectory tmp-name
)) (current-buffer))
1379 (set-process-sentinel (get-buffer-process (current-buffer)) nil
)
1381 (with-timeout (10 (ert-fail "`async-shell-command' timed out"))
1382 (while (< (- (point-max) (point-min))
1383 (1+ (length (file-name-nondirectory tmp-name
))))
1384 (accept-process-output (get-buffer-process (current-buffer)) 1)))
1385 ;; `ls' could produce colorized output.
1386 (goto-char (point-min))
1387 (while (re-search-forward tramp-color-escape-sequence-regexp nil t
)
1388 (replace-match "" nil nil
))
1389 ;; There might be a nasty "Process *Async Shell* finished" message.
1390 (goto-char (point-min))
1392 (narrow-to-region (point-min) (point))
1395 (format "%s\n" (file-name-nondirectory tmp-name
)) (buffer-string))))
1396 (ignore-errors (delete-file tmp-name
)))
1400 (write-region "foo" nil tmp-name
)
1401 (should (file-exists-p tmp-name
))
1402 (async-shell-command "read line; ls $line" (current-buffer))
1403 (set-process-sentinel (get-buffer-process (current-buffer)) nil
)
1404 (process-send-string
1405 (get-buffer-process (current-buffer))
1406 (format "%s\n" (file-name-nondirectory tmp-name
)))
1408 (with-timeout (10 (ert-fail "`async-shell-command' timed out"))
1409 (while (< (- (point-max) (point-min))
1410 (1+ (length (file-name-nondirectory tmp-name
))))
1411 (accept-process-output (get-buffer-process (current-buffer)) 1)))
1412 ;; `ls' could produce colorized output.
1413 (goto-char (point-min))
1414 (while (re-search-forward tramp-color-escape-sequence-regexp nil t
)
1415 (replace-match "" nil nil
))
1416 ;; There might be a nasty "Process *Async Shell* finished" message.
1417 (goto-char (point-min))
1419 (narrow-to-region (point-min) (point))
1422 (format "%s\n" (file-name-nondirectory tmp-name
)) (buffer-string))))
1423 (ignore-errors (delete-file tmp-name
)))))
1425 (ert-deftest tramp-test29-vc-registered
()
1426 "Check `vc-registered'."
1427 (skip-unless (tramp--test-enabled))
1430 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory
)
1431 'tramp-sh-file-name-handler
))
1433 (let* ((default-directory tramp-test-temporary-file-directory
)
1434 (tmp-name1 (tramp--test-make-temp-name))
1435 (tmp-name2 (expand-file-name "foo" tmp-name1
))
1436 (tramp-remote-process-environment tramp-remote-process-environment
)
1437 (vc-handled-backends
1438 (with-parsed-tramp-file-name tramp-test-temporary-file-directory nil
1440 ((tramp-find-executable v vc-bzr-program
(tramp-get-remote-path v
))
1441 (setq tramp-remote-process-environment
1442 (cons (format "BZR_HOME=%s"
1443 (file-remote-p tmp-name1
'localname
))
1444 tramp-remote-process-environment
))
1445 ;; We must force a reconnect, in order to activate $BZR_HOME.
1446 (tramp-cleanup-connection
1447 (tramp-dissect-file-name tramp-test-temporary-file-directory
)
1450 ((tramp-find-executable v vc-git-program
(tramp-get-remote-path v
))
1452 ((tramp-find-executable v vc-hg-program
(tramp-get-remote-path v
))
1455 (skip-unless vc-handled-backends
)
1456 (message "%s" vc-handled-backends
)
1460 (make-directory tmp-name1
)
1461 (write-region "foo" nil tmp-name2
)
1462 (should (file-directory-p tmp-name1
))
1463 (should (file-exists-p tmp-name2
))
1464 (should-not (vc-registered tmp-name1
))
1465 (should-not (vc-registered tmp-name2
))
1467 (let ((default-directory tmp-name1
))
1468 ;; Create empty repository, and register the file.
1469 (vc-create-repo (car vc-handled-backends
))
1470 ;; The structure of VC-FILESET is not documented. Let's
1471 ;; hope it won't change.
1473 nil
(list (car vc-handled-backends
)
1474 (list (file-name-nondirectory tmp-name2
)))))
1475 (should (vc-registered tmp-name2
)))
1477 (ignore-errors (delete-directory tmp-name1
'recursive
)))))
1479 (defun tramp--test-check-files (&rest files
)
1480 "Runs a simple but comprehensive test over every file in FILES."
1481 (let ((tmp-name1 (tramp--test-make-temp-name))
1482 (tmp-name2 (tramp--test-make-temp-name 'local
)))
1485 (make-directory tmp-name1
)
1486 (make-directory tmp-name2
)
1488 (let ((file1 (expand-file-name elt tmp-name1
))
1489 (file2 (expand-file-name elt tmp-name2
)))
1490 (write-region elt nil file1
)
1491 (should (file-exists-p file1
))
1492 ;; Check file contents.
1494 (insert-file-contents file1
)
1495 (should (string-equal (buffer-string) elt
)))
1496 ;; Copy file both directions.
1497 (copy-file file1 tmp-name2
)
1498 (should (file-exists-p file2
))
1500 (should-not (file-exists-p file1
))
1501 (copy-file file2 tmp-name1
)
1502 (should (file-exists-p file1
))))
1504 ;; Check file names.
1505 (should (equal (directory-files
1506 tmp-name1 nil directory-files-no-dot-files-regexp
)
1507 (sort (copy-sequence files
) 'string-lessp
)))
1508 (should (equal (directory-files
1509 tmp-name2 nil directory-files-no-dot-files-regexp
)
1510 (sort (copy-sequence files
) 'string-lessp
)))
1512 ;; `substitute-in-file-name' could return different values.
1513 ;; For `adb', there could be strange file permissions
1514 ;; preventing overwriting a file. We don't care in this
1518 (substitute-in-file-name (expand-file-name elt tmp-name1
)))
1520 (substitute-in-file-name (expand-file-name elt tmp-name2
))))
1521 (ignore-errors (write-region elt nil file1
))
1522 (should (file-exists-p file1
))
1523 (ignore-errors (write-region elt nil file2 nil
'nomessage
))
1524 (should (file-exists-p file2
))))
1526 (should (equal (directory-files
1527 tmp-name1 nil directory-files-no-dot-files-regexp
)
1529 tmp-name2 nil directory-files-no-dot-files-regexp
))))
1531 (ignore-errors (delete-directory tmp-name1
'recursive
))
1532 (ignore-errors (delete-directory tmp-name2
'recursive
)))))
1534 ;; This test is inspired by Bug#17238.
1535 (ert-deftest tramp-test30-special-characters
()
1536 "Check special characters in file names."
1537 (skip-unless (tramp--test-enabled))
1541 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory
)
1542 '(tramp-adb-file-name-handler
1543 tramp-gvfs-file-name-handler
1544 tramp-smb-file-name-handler
))))
1546 ;; Newlines, slashes and backslashes in file names are not supported.
1547 ;; So we don't test.
1548 (tramp--test-check-files
1565 (ert-deftest tramp-test31-utf8
()
1566 "Check UTF8 encoding in file names and file contents."
1567 (skip-unless (tramp--test-enabled))
1569 (let ((coding-system-for-read 'utf-8
)
1570 (coding-system-for-write 'utf-8
)
1571 (file-name-coding-system 'utf-8
))
1572 (tramp--test-check-files
1573 "أصبح بوسعك الآن تنزيل نسخة كاملة من موسوعة ويكيبيديا العربية لتصفحها بلا اتصال بالإنترنت"
1575 "Автостопом по гала́ктике")))
1577 ;; This test is inspired by Bug#16928.
1578 (ert-deftest tramp-test32-asynchronous-requests
()
1579 "Check parallel asynchronous requests.
1580 Such requests could arrive from timers, process filters and
1581 process sentinels. They shall not disturb each other."
1582 ;; Mark as failed until bug has been fixed.
1583 :expected-result
:failed
1584 (skip-unless (tramp--test-enabled))
1587 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory
)
1588 'tramp-sh-file-name-handler
))
1590 ;; Keep instrumentation verbosity 0 until Tramp bug is fixed. This
1591 ;; has the side effect, that this test fails instead to abort. Good
1593 (tramp--instrument-test-case 0
1594 (let* ((tmp-name (tramp--test-make-temp-name))
1595 (default-directory tmp-name
)
1596 (remote-file-name-inhibit-cache t
)
1597 timer buffers kill-buffer-query-functions
)
1601 (make-directory tmp-name
)
1603 ;; Setup a timer in order to raise an ordinary command again
1604 ;; and again. `vc-registered' is well suited, because there
1613 (buffer-name (nth (random (length buffers
)) buffers
)))))))
1615 ;; Create temporary buffers. The number of buffers
1616 ;; corresponds to the number of processes; it could be
1617 ;; increased in order to make pressure on Tramp.
1619 (add-to-list 'buffers
(generate-new-buffer "*temp*")))
1621 ;; Open asynchronous processes. Set process sentinel.
1622 (dolist (buf buffers
)
1623 (async-shell-command "read line; touch $line; echo $line" buf
)
1624 (set-process-sentinel
1625 (get-buffer-process buf
)
1626 (lambda (proc _state
)
1627 (delete-file (buffer-name (process-buffer proc
))))))
1629 ;; Send a string. Use a random order of the buffers. Mix
1630 ;; with regular operation.
1631 (let ((buffers (copy-sequence buffers
))
1634 (setq buf
(nth (random (length buffers
)) buffers
))
1635 (process-send-string
1636 (get-buffer-process buf
) (format "'%s'\n" buf
))
1637 (file-attributes (buffer-name buf
))
1638 (setq buffers
(delq buf buffers
))))
1640 ;; Wait until the whole output has been read.
1641 (with-timeout ((* 10 (length buffers
))
1642 (ert-fail "`async-shell-command' timed out"))
1643 (let ((buffers (copy-sequence buffers
))
1646 (setq buf
(nth (random (length buffers
)) buffers
))
1648 (memq (process-status (get-buffer-process buf
))
1650 (accept-process-output (get-buffer-process buf
) 0.1)
1651 (setq buffers
(delq buf buffers
))))))
1654 (dolist (buf buffers
)
1655 (with-current-buffer buf
1657 (string-equal (format "'%s'\n" buf
) (buffer-string)))))
1659 (directory-files tmp-name nil directory-files-no-dot-files-regexp
)))
1662 (ignore-errors (cancel-timer timer
))
1663 (ignore-errors (delete-directory tmp-name
'recursive
))
1664 (dolist (buf buffers
)
1665 (ignore-errors (kill-buffer buf
)))))))
1667 (ert-deftest tramp-test33-recursive-load
()
1668 "Check that Tramp does not fail due to recursive load."
1669 (skip-unless (tramp--test-enabled))
1674 "(expand-file-name %S)"
1675 tramp-test-temporary-file-directory
)
1677 "(let ((default-directory %S)) (expand-file-name %S))"
1678 tramp-test-temporary-file-directory
1679 temporary-file-directory
)))
1683 (shell-command-to-string
1685 "%s -batch -Q -L %s --eval %s"
1686 (expand-file-name invocation-name invocation-directory
)
1687 (mapconcat 'shell-quote-argument load-path
" -L ")
1688 (shell-quote-argument code
)))))))
1690 (ert-deftest tramp-test34-unload
()
1691 "Check that Tramp and its subpackages unload completely.
1692 Since it unloads Tramp, it shall be the last test to run."
1693 ;; Mark as failed until all symbols are unbound.
1694 :expected-result
(if (featurep 'tramp
) :failed
:passed
)
1695 (when (featurep 'tramp
)
1696 (unload-feature 'tramp
'force
)
1697 ;; No Tramp feature must be left.
1698 (should-not (featurep 'tramp
))
1699 (should-not (all-completions "tramp" (delq 'tramp-tests features
)))
1700 ;; `file-name-handler-alist' must be clean.
1701 (should-not (all-completions "tramp" (mapcar 'cdr file-name-handler-alist
)))
1702 ;; There shouldn't be left a bound symbol. We do not regard our
1703 ;; test symbols, and the Tramp unload hooks.
1706 (and (or (boundp x
) (functionp x
))
1707 (string-match "^tramp" (symbol-name x
))
1708 (not (string-match "^tramp--?test" (symbol-name x
)))
1709 (not (string-match "unload-hook$" (symbol-name x
)))
1710 (ert-fail (format "`%s' still bound" x
)))))
1711 ; (progn (message "`%s' still bound" x)))))
1712 ;; There shouldn't be left a hook function containing a Tramp
1713 ;; function. We do not regard the Tramp unload hooks.
1717 (string-match "-hooks?$" (symbol-name x
))
1718 (not (string-match "unload-hook$" (symbol-name x
)))
1719 (consp (symbol-value x
))
1720 (ignore-errors (all-completions "tramp" (symbol-value x
)))
1721 (ert-fail (format "Hook `%s' still contains Tramp function" x
)))))))
1725 ;; * dired-compress-file
1728 ;; * file-ownership-preserved-p
1729 ;; * file-selinux-context
1730 ;; * find-backup-file-name
1731 ;; * make-auto-save-file-name
1733 ;; * set-file-selinux-context
1735 ;; * Work on skipped tests. Make a comment, when it is impossible.
1736 ;; * Fix `tramp-test15-copy-directory' for `smb'. Using tar in a pipe
1737 ;; doesn't work well when an interactive password must be provided.
1738 ;; * Fix `tramp-test27-start-file-process' for `nc' and on MS
1739 ;; Windows (`process-send-eof'?).
1740 ;; * Fix `tramp-test28-shell-command' on MS Windows (nasty plink message).
1741 ;; * Fix `tramp-test30-special-characters' for `adb', `nc' and `smb'.
1742 ;; * Fix `tramp-test31-utf8' for MS Windows and `nc'/`telnet' (when
1743 ;; target is a dumb busybox). Seems to be in `directory-files'.
1744 ;; * Fix Bug#16928. Set expected error of `tramp-test32-asynchronous-requests'.
1745 ;; * Fix `tramp-test34-unload' (Not all symbols are unbound). Set
1748 (defun tramp-test-all (&optional interactive
)
1749 "Run all tests for \\[tramp]."
1752 (if interactive
'ert-run-tests-interactively
'ert-run-tests-batch
) "^tramp"))
1754 (provide 'tramp-tests
)
1755 ;;; tramp-tests.el ends here