1 ;;; tramp-tests.el --- Tests of remote file access
3 ;; Copyright (C) 2013-2015 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))
957 ;; There might be a summary line.
958 "\\(total.+[[:digit:]]+\n\\)?"
959 ;; We don't know in which order "." and ".." appear.
960 "\\(.+ \\.?\\.\n\\)\\{2\\}"
962 (ignore-errors (delete-directory tmp-name1
'recursive
)))))
964 (ert-deftest tramp-test18-file-attributes
()
965 "Check `file-attributes'.
966 This tests also `file-readable-p' and `file-regular-p'."
967 (skip-unless (tramp--test-enabled))
969 (let ((tmp-name (tramp--test-make-temp-name))
973 (write-region "foo" nil tmp-name
)
974 (should (file-exists-p tmp-name
))
975 (setq attr
(file-attributes tmp-name
))
976 (should (consp attr
))
977 (should (file-exists-p tmp-name
))
978 (should (file-readable-p tmp-name
))
979 (should (file-regular-p tmp-name
))
980 ;; We do not test inodes and device numbers.
981 (should (null (car attr
)))
982 (should (numberp (nth 1 attr
))) ;; Link.
983 (should (numberp (nth 2 attr
))) ;; Uid.
984 (should (numberp (nth 3 attr
))) ;; Gid.
986 (should (stringp (current-time-string (nth 4 attr
))))
987 ;; Last modification time.
988 (should (stringp (current-time-string (nth 5 attr
))))
989 ;; Last status change time.
990 (should (stringp (current-time-string (nth 6 attr
))))
991 (should (numberp (nth 7 attr
))) ;; Size.
992 (should (stringp (nth 8 attr
))) ;; Modes.
994 (setq attr
(file-attributes tmp-name
'string
))
995 (should (stringp (nth 2 attr
))) ;; Uid.
996 (should (stringp (nth 3 attr
))) ;; Gid.
997 (delete-file tmp-name
)
999 (make-directory tmp-name
)
1000 (should (file-exists-p tmp-name
))
1001 (should (file-readable-p tmp-name
))
1002 (should-not (file-regular-p tmp-name
))
1003 (setq attr
(file-attributes tmp-name
))
1004 (should (eq (car attr
) t
)))
1005 (ignore-errors (delete-directory tmp-name
)))))
1007 (ert-deftest tramp-test19-directory-files-and-attributes
()
1008 "Check `directory-files-and-attributes'."
1009 (skip-unless (tramp--test-enabled))
1011 ;; `directory-files-and-attributes' contains also values for "../".
1012 ;; Ensure that this doesn't change during tests, for
1013 ;; example due to handling temporary files.
1014 (let* ((tmp-name1 (tramp--test-make-temp-name))
1015 (tmp-name2 (expand-file-name "bla" tmp-name1
))
1019 (make-directory tmp-name1
)
1020 (should (file-directory-p tmp-name1
))
1021 (make-directory tmp-name2
)
1022 (should (file-directory-p tmp-name2
))
1023 (write-region "foo" nil
(expand-file-name "foo" tmp-name2
))
1024 (write-region "bar" nil
(expand-file-name "bar" tmp-name2
))
1025 (write-region "boz" nil
(expand-file-name "boz" tmp-name2
))
1026 (setq attr
(directory-files-and-attributes tmp-name2
))
1027 (should (consp attr
))
1028 ;; Dumb remote shells without perl(1) or stat(1) are not
1029 ;; able to return the date correctly. They say "don't know".
1034 (file-attributes (expand-file-name (car elt
) tmp-name2
)))
1037 (equal (file-attributes (expand-file-name (car elt
) tmp-name2
))
1039 (setq attr
(directory-files-and-attributes tmp-name2
'full
))
1041 (unless (equal (nth 5 (file-attributes (car elt
))) '(0 0))
1043 (equal (file-attributes (car elt
)) (cdr elt
)))))
1044 (setq attr
(directory-files-and-attributes tmp-name2 nil
"^b"))
1045 (should (equal (mapcar 'car attr
) '("bar" "boz"))))
1046 (ignore-errors (delete-directory tmp-name1
'recursive
)))))
1048 (ert-deftest tramp-test20-file-modes
()
1049 "Check `file-modes'.
1050 This tests also `file-executable-p', `file-writable-p' and `set-file-modes'."
1051 (skip-unless (tramp--test-enabled))
1055 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory
)
1056 '(tramp-adb-file-name-handler
1057 tramp-gvfs-file-name-handler
1058 tramp-smb-file-name-handler
))))
1060 (let ((tmp-name (tramp--test-make-temp-name)))
1063 (write-region "foo" nil tmp-name
)
1064 (should (file-exists-p tmp-name
))
1065 (set-file-modes tmp-name
#o777
)
1066 (should (= (file-modes tmp-name
) #o777
))
1067 (should (file-executable-p tmp-name
))
1068 (should (file-writable-p tmp-name
))
1069 (set-file-modes tmp-name
#o444
)
1070 (should (= (file-modes tmp-name
) #o444
))
1071 (should-not (file-executable-p tmp-name
))
1072 ;; A file is always writable for user "root".
1073 (unless (zerop (nth 2 (file-attributes tmp-name
)))
1074 (should-not (file-writable-p tmp-name
))))
1075 (ignore-errors (delete-file tmp-name
)))))
1077 (ert-deftest tramp-test21-file-links
()
1078 "Check `file-symlink-p'.
1079 This tests also `make-symbolic-link', `file-truename' and `add-name-to-file'."
1080 (skip-unless (tramp--test-enabled))
1082 ;; We must use `file-truename' for the temporary directory, because
1083 ;; it could be located on a symlinked directory. This would let the
1085 (let* ((tramp-test-temporary-file-directory
1086 (file-truename tramp-test-temporary-file-directory
))
1087 (tmp-name1 (tramp--test-make-temp-name))
1088 (tmp-name2 (tramp--test-make-temp-name))
1089 (tmp-name3 (tramp--test-make-temp-name 'local
)))
1092 (write-region "foo" nil tmp-name1
)
1093 (should (file-exists-p tmp-name1
))
1094 ;; Method "smb" supports `make-symbolic-link' only if the
1095 ;; remote host has CIFS capabilities. tramp-adb.el and
1096 ;; tramp-gvfs.el do not support symbolic links at all.
1098 (make-symbolic-link tmp-name1 tmp-name2
)
1101 (not (string-equal (error-message-string err
)
1102 "make-symbolic-link not supported")))))
1103 (should (file-symlink-p tmp-name2
))
1104 (should-error (make-symbolic-link tmp-name1 tmp-name2
))
1105 (make-symbolic-link tmp-name1 tmp-name2
'ok-if-already-exists
)
1106 (should (file-symlink-p tmp-name2
))
1107 ;; `tmp-name3' is a local file name.
1108 (should-error (make-symbolic-link tmp-name1 tmp-name3
)))
1110 (delete-file tmp-name1
)
1111 (delete-file tmp-name2
)))
1115 (write-region "foo" nil tmp-name1
)
1116 (should (file-exists-p tmp-name1
))
1117 (add-name-to-file tmp-name1 tmp-name2
)
1118 (should-not (file-symlink-p tmp-name2
))
1119 (should-error (add-name-to-file tmp-name1 tmp-name2
))
1120 (add-name-to-file tmp-name1 tmp-name2
'ok-if-already-exists
)
1121 (should-not (file-symlink-p tmp-name2
))
1122 ;; `tmp-name3' is a local file name.
1123 (should-error (add-name-to-file tmp-name1 tmp-name3
)))
1125 (delete-file tmp-name1
)
1126 (delete-file tmp-name2
)))
1130 (write-region "foo" nil tmp-name1
)
1131 (should (file-exists-p tmp-name1
))
1132 (make-symbolic-link tmp-name1 tmp-name2
)
1133 (should (file-symlink-p tmp-name2
))
1134 (should-not (string-equal tmp-name2
(file-truename tmp-name2
)))
1136 (string-equal (file-truename tmp-name1
) (file-truename tmp-name2
)))
1137 (should (file-equal-p tmp-name1 tmp-name2
)))
1139 (delete-file tmp-name1
)
1140 (delete-file tmp-name2
)))
1142 ;; `file-truename' shall preserve trailing link of directories.
1143 (unless (file-symlink-p tramp-test-temporary-file-directory
)
1144 (let* ((dir1 (directory-file-name tramp-test-temporary-file-directory
))
1145 (dir2 (file-name-as-directory dir1
)))
1146 (should (string-equal (file-truename dir1
) (expand-file-name dir1
)))
1147 (should (string-equal (file-truename dir2
) (expand-file-name dir2
)))))))
1149 (ert-deftest tramp-test22-file-times
()
1150 "Check `set-file-times' and `file-newer-than-file-p'."
1151 (skip-unless (tramp--test-enabled))
1155 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory
)
1156 '(tramp-gvfs-file-name-handler tramp-smb-file-name-handler
))))
1158 (let ((tmp-name1 (tramp--test-make-temp-name))
1159 (tmp-name2 (tramp--test-make-temp-name))
1160 (tmp-name3 (tramp--test-make-temp-name)))
1163 (write-region "foo" nil tmp-name1
)
1164 (should (file-exists-p tmp-name1
))
1165 (should (consp (nth 5 (file-attributes tmp-name1
))))
1166 ;; '(0 0) means don't know, and will be replaced by
1167 ;; `current-time'. Therefore, we use '(0 1).
1168 ;; We skip the test, if the remote handler is not able to
1169 ;; set the correct time.
1170 (skip-unless (set-file-times tmp-name1
'(0 1)))
1171 ;; Dumb remote shells without perl(1) or stat(1) are not
1172 ;; able to return the date correctly. They say "don't know".
1173 (unless (equal (nth 5 (file-attributes tmp-name1
)) '(0 0))
1174 (should (equal (nth 5 (file-attributes tmp-name1
)) '(0 1)))
1175 (write-region "bla" nil tmp-name2
)
1176 (should (file-exists-p tmp-name2
))
1177 (should (file-newer-than-file-p tmp-name2 tmp-name1
))
1178 ;; `tmp-name3' does not exist.
1179 (should (file-newer-than-file-p tmp-name2 tmp-name3
))
1180 (should-not (file-newer-than-file-p tmp-name3 tmp-name1
))))
1182 (delete-file tmp-name1
)
1183 (delete-file tmp-name2
)))))
1185 (ert-deftest tramp-test23-visited-file-modtime
()
1186 "Check `set-visited-file-modtime' and `verify-visited-file-modtime'."
1187 (skip-unless (tramp--test-enabled))
1189 (let ((tmp-name (tramp--test-make-temp-name)))
1192 (write-region "foo" nil tmp-name
)
1193 (should (file-exists-p tmp-name
))
1195 (insert-file-contents tmp-name
)
1196 (should (verify-visited-file-modtime))
1197 (set-visited-file-modtime '(0 1))
1198 (should (verify-visited-file-modtime))
1199 (should (equal (visited-file-modtime) '(0 1 0 0)))))
1200 (ignore-errors (delete-file tmp-name
)))))
1202 (ert-deftest tramp-test24-file-name-completion
()
1203 "Check `file-name-completion' and `file-name-all-completions'."
1204 (skip-unless (tramp--test-enabled))
1206 (let ((tmp-name (tramp--test-make-temp-name)))
1209 (make-directory tmp-name
)
1210 (should (file-directory-p tmp-name
))
1211 (write-region "foo" nil
(expand-file-name "foo" tmp-name
))
1212 (write-region "bar" nil
(expand-file-name "bold" tmp-name
))
1213 (make-directory (expand-file-name "boz" tmp-name
))
1214 (should (equal (file-name-completion "fo" tmp-name
) "foo"))
1215 (should (equal (file-name-completion "b" tmp-name
) "bo"))
1217 (equal (file-name-completion "b" tmp-name
'file-directory-p
) "boz/"))
1218 (should (equal (file-name-all-completions "fo" tmp-name
) '("foo")))
1220 (equal (sort (file-name-all-completions "b" tmp-name
) 'string-lessp
)
1222 (ignore-errors (delete-directory tmp-name
'recursive
)))))
1224 (ert-deftest tramp-test25-load
()
1226 (skip-unless (tramp--test-enabled))
1228 (let ((tmp-name (tramp--test-make-temp-name)))
1231 (load tmp-name
'noerror
'nomessage
)
1232 (should-not (featurep 'tramp-test-load
))
1233 (write-region "(provide 'tramp-test-load)" nil tmp-name
)
1234 ;; `load' in lread.c does not pass `must-suffix'. Why?
1235 ;(should-error (load tmp-name nil 'nomessage 'nosuffix 'must-suffix))
1236 (load tmp-name nil
'nomessage
'nosuffix
)
1237 (should (featurep 'tramp-test-load
)))
1239 (and (featurep 'tramp-test-load
) (unload-feature 'tramp-test-load
))
1240 (delete-file tmp-name
)))))
1242 (ert-deftest tramp-test26-process-file
()
1243 "Check `process-file'."
1244 (skip-unless (tramp--test-enabled))
1248 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory
)
1249 '(tramp-gvfs-file-name-handler tramp-smb-file-name-handler
))))
1251 (let* ((tmp-name (tramp--test-make-temp-name))
1252 (fnnd (file-name-nondirectory tmp-name
))
1253 (default-directory tramp-test-temporary-file-directory
)
1254 kill-buffer-query-functions
)
1257 ;; We cannot use "/bin/true" and "/bin/false"; those paths
1258 ;; do not exist on hydra.
1259 (should (zerop (process-file "true")))
1260 (should-not (zerop (process-file "false")))
1261 (should-not (zerop (process-file "binary-does-not-exist")))
1263 (write-region "foo" nil tmp-name
)
1264 (should (file-exists-p tmp-name
))
1265 (should (zerop (process-file "ls" nil t nil fnnd
)))
1266 ;; `ls' could produce colorized output.
1267 (goto-char (point-min))
1268 (while (re-search-forward tramp-color-escape-sequence-regexp nil t
)
1269 (replace-match "" nil nil
))
1270 (should (string-equal (format "%s\n" fnnd
) (buffer-string)))
1271 (should-not (get-buffer-window (current-buffer) t
))
1273 ;; Second run. The output must be appended.
1274 (should (zerop (process-file "ls" nil t t fnnd
)))
1275 ;; `ls' could produce colorized output.
1276 (goto-char (point-min))
1277 (while (re-search-forward tramp-color-escape-sequence-regexp nil t
)
1278 (replace-match "" nil nil
))
1280 (string-equal (format "%s\n%s\n" fnnd fnnd
) (buffer-string)))
1281 ;; A non-nil DISPLAY must not raise the buffer.
1282 (should-not (get-buffer-window (current-buffer) t
))))
1284 (ignore-errors (delete-file tmp-name
)))))
1286 (ert-deftest tramp-test27-start-file-process
()
1287 "Check `start-file-process'."
1288 (skip-unless (tramp--test-enabled))
1292 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory
)
1293 '(tramp-adb-file-name-handler
1294 tramp-gvfs-file-name-handler
1295 tramp-smb-file-name-handler
))))
1297 (let ((default-directory tramp-test-temporary-file-directory
)
1298 (tmp-name (tramp--test-make-temp-name))
1299 kill-buffer-query-functions proc
)
1302 (setq proc
(start-file-process "test1" (current-buffer) "cat"))
1303 (should (processp proc
))
1304 (should (equal (process-status proc
) 'run
))
1305 (process-send-string proc
"foo")
1306 (process-send-eof proc
)
1308 (with-timeout (10 (ert-fail "`start-file-process' timed out"))
1309 (while (< (- (point-max) (point-min)) (length "foo"))
1310 (accept-process-output proc
1)))
1311 (should (string-equal (buffer-string) "foo")))
1312 (ignore-errors (delete-process proc
)))
1316 (write-region "foo" nil tmp-name
)
1317 (should (file-exists-p tmp-name
))
1320 "test2" (current-buffer)
1321 "cat" (file-name-nondirectory tmp-name
)))
1322 (should (processp proc
))
1324 (with-timeout (10 (ert-fail "`start-file-process' timed out"))
1325 (while (< (- (point-max) (point-min)) (length "foo"))
1326 (accept-process-output proc
1)))
1327 (should (string-equal (buffer-string) "foo")))
1329 (delete-process proc
)
1330 (delete-file tmp-name
)))
1334 (setq proc
(start-file-process "test3" (current-buffer) "cat"))
1335 (should (processp proc
))
1336 (should (equal (process-status proc
) 'run
))
1339 (lambda (p s
) (with-current-buffer (process-buffer p
) (insert s
))))
1340 (process-send-string proc
"foo")
1341 (process-send-eof proc
)
1343 (with-timeout (10 (ert-fail "`start-file-process' timed out"))
1344 (while (< (- (point-max) (point-min)) (length "foo"))
1345 (accept-process-output proc
1)))
1346 (should (string-equal (buffer-string) "foo")))
1347 (ignore-errors (delete-process proc
)))))
1349 (ert-deftest tramp-test28-shell-command
()
1350 "Check `shell-command'."
1351 (skip-unless (tramp--test-enabled))
1355 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory
)
1356 '(tramp-adb-file-name-handler
1357 tramp-gvfs-file-name-handler
1358 tramp-smb-file-name-handler
))))
1360 (let ((tmp-name (tramp--test-make-temp-name))
1361 (default-directory tramp-test-temporary-file-directory
)
1362 kill-buffer-query-functions
)
1365 (write-region "foo" nil tmp-name
)
1366 (should (file-exists-p tmp-name
))
1368 (format "ls %s" (file-name-nondirectory tmp-name
)) (current-buffer))
1369 ;; `ls' could produce colorized output.
1370 (goto-char (point-min))
1371 (while (re-search-forward tramp-color-escape-sequence-regexp nil t
)
1372 (replace-match "" nil nil
))
1375 (format "%s\n" (file-name-nondirectory tmp-name
)) (buffer-string))))
1376 (ignore-errors (delete-file tmp-name
)))
1380 (write-region "foo" nil tmp-name
)
1381 (should (file-exists-p tmp-name
))
1382 (async-shell-command
1383 (format "ls %s" (file-name-nondirectory tmp-name
)) (current-buffer))
1384 (set-process-sentinel (get-buffer-process (current-buffer)) nil
)
1386 (with-timeout (10 (ert-fail "`async-shell-command' timed out"))
1387 (while (< (- (point-max) (point-min))
1388 (1+ (length (file-name-nondirectory tmp-name
))))
1389 (accept-process-output (get-buffer-process (current-buffer)) 1)))
1390 ;; `ls' could produce colorized output.
1391 (goto-char (point-min))
1392 (while (re-search-forward tramp-color-escape-sequence-regexp nil t
)
1393 (replace-match "" nil nil
))
1394 ;; There might be a nasty "Process *Async Shell* finished" message.
1395 (goto-char (point-min))
1397 (narrow-to-region (point-min) (point))
1400 (format "%s\n" (file-name-nondirectory tmp-name
)) (buffer-string))))
1401 (ignore-errors (delete-file tmp-name
)))
1405 (write-region "foo" nil tmp-name
)
1406 (should (file-exists-p tmp-name
))
1407 (async-shell-command "read line; ls $line" (current-buffer))
1408 (set-process-sentinel (get-buffer-process (current-buffer)) nil
)
1409 (process-send-string
1410 (get-buffer-process (current-buffer))
1411 (format "%s\n" (file-name-nondirectory tmp-name
)))
1413 (with-timeout (10 (ert-fail "`async-shell-command' timed out"))
1414 (while (< (- (point-max) (point-min))
1415 (1+ (length (file-name-nondirectory tmp-name
))))
1416 (accept-process-output (get-buffer-process (current-buffer)) 1)))
1417 ;; `ls' could produce colorized output.
1418 (goto-char (point-min))
1419 (while (re-search-forward tramp-color-escape-sequence-regexp nil t
)
1420 (replace-match "" nil nil
))
1421 ;; There might be a nasty "Process *Async Shell* finished" message.
1422 (goto-char (point-min))
1424 (narrow-to-region (point-min) (point))
1427 (format "%s\n" (file-name-nondirectory tmp-name
)) (buffer-string))))
1428 (ignore-errors (delete-file tmp-name
)))))
1430 (ert-deftest tramp-test29-vc-registered
()
1431 "Check `vc-registered'."
1432 (skip-unless (tramp--test-enabled))
1435 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory
)
1436 'tramp-sh-file-name-handler
))
1438 (let* ((default-directory tramp-test-temporary-file-directory
)
1439 (tmp-name1 (tramp--test-make-temp-name))
1440 (tmp-name2 (expand-file-name "foo" tmp-name1
))
1441 (tramp-remote-process-environment tramp-remote-process-environment
)
1442 (vc-handled-backends
1443 (with-parsed-tramp-file-name tramp-test-temporary-file-directory nil
1445 ((tramp-find-executable v vc-bzr-program
(tramp-get-remote-path v
))
1446 (setq tramp-remote-process-environment
1447 (cons (format "BZR_HOME=%s"
1448 (file-remote-p tmp-name1
'localname
))
1449 tramp-remote-process-environment
))
1450 ;; We must force a reconnect, in order to activate $BZR_HOME.
1451 (tramp-cleanup-connection
1452 (tramp-dissect-file-name tramp-test-temporary-file-directory
)
1455 ((tramp-find-executable v vc-git-program
(tramp-get-remote-path v
))
1457 ((tramp-find-executable v vc-hg-program
(tramp-get-remote-path v
))
1460 (skip-unless vc-handled-backends
)
1461 (message "%s" vc-handled-backends
)
1465 (make-directory tmp-name1
)
1466 (write-region "foo" nil tmp-name2
)
1467 (should (file-directory-p tmp-name1
))
1468 (should (file-exists-p tmp-name2
))
1469 (should-not (vc-registered tmp-name1
))
1470 (should-not (vc-registered tmp-name2
))
1472 (let ((default-directory tmp-name1
))
1473 ;; Create empty repository, and register the file.
1474 (vc-create-repo (car vc-handled-backends
))
1475 ;; The structure of VC-FILESET is not documented. Let's
1476 ;; hope it won't change.
1478 nil
(list (car vc-handled-backends
)
1479 (list (file-name-nondirectory tmp-name2
)))))
1480 (should (vc-registered tmp-name2
)))
1482 (ignore-errors (delete-directory tmp-name1
'recursive
)))))
1484 (defun tramp--test-smb-or-windows-nt-p ()
1485 "Check, whether the locale or remote host runs MS Windows.
1486 This requires restrictions of file name syntax."
1487 (or (eq system-type
'windows-nt
)
1488 (eq (tramp-find-foreign-file-name-handler
1489 tramp-test-temporary-file-directory
)
1490 'tramp-smb-file-name-handler
)))
1492 (defun tramp--test-check-files (&rest files
)
1493 "Runs a simple but comprehensive test over every file in FILES."
1494 (let ((tmp-name1 (tramp--test-make-temp-name))
1495 (tmp-name2 (tramp--test-make-temp-name 'local
)))
1498 (make-directory tmp-name1
)
1499 (make-directory tmp-name2
)
1500 (dolist (elt (delq nil files
))
1501 (let ((file1 (expand-file-name elt tmp-name1
))
1502 (file2 (expand-file-name elt tmp-name2
)))
1503 (write-region elt nil file1
)
1504 (should (file-exists-p file1
))
1505 ;; Check file contents.
1507 (insert-file-contents file1
)
1508 (should (string-equal (buffer-string) elt
)))
1509 ;; Copy file both directions.
1510 (copy-file file1 tmp-name2
)
1511 (should (file-exists-p file2
))
1513 (should-not (file-exists-p file1
))
1514 (copy-file file2 tmp-name1
)
1515 (should (file-exists-p file1
))))
1517 ;; Check file names.
1518 (should (equal (directory-files
1519 tmp-name1 nil directory-files-no-dot-files-regexp
)
1520 (sort (copy-sequence files
) 'string-lessp
)))
1521 (should (equal (directory-files
1522 tmp-name2 nil directory-files-no-dot-files-regexp
)
1523 (sort (copy-sequence files
) 'string-lessp
)))
1525 ;; `substitute-in-file-name' could return different values.
1526 ;; For `adb', there could be strange file permissions
1527 ;; preventing overwriting a file. We don't care in this
1531 (substitute-in-file-name (expand-file-name elt tmp-name1
)))
1533 (substitute-in-file-name (expand-file-name elt tmp-name2
))))
1534 (ignore-errors (write-region elt nil file1
))
1535 (should (file-exists-p file1
))
1536 (ignore-errors (write-region elt nil file2 nil
'nomessage
))
1537 (should (file-exists-p file2
))))
1539 (should (equal (directory-files
1540 tmp-name1 nil directory-files-no-dot-files-regexp
)
1542 tmp-name2 nil directory-files-no-dot-files-regexp
))))
1544 (ignore-errors (delete-directory tmp-name1
'recursive
))
1545 (ignore-errors (delete-directory tmp-name2
'recursive
)))))
1547 ;; This test is inspired by Bug#17238.
1548 (ert-deftest tramp-test30-special-characters
()
1549 "Check special characters in file names."
1550 (skip-unless (tramp--test-enabled))
1554 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory
)
1555 '(tramp-adb-file-name-handler
1556 tramp-gvfs-file-name-handler
))))
1558 ;; Newlines, slashes and backslashes in file names are not supported.
1559 ;; So we don't test.
1560 (tramp--test-check-files
1561 (if (tramp--test-smb-or-windows-nt-p) "foo bar baz" " foo\tbar baz\t")
1566 (unless (tramp--test-smb-or-windows-nt-p) "?foo?bar?baz?")
1567 (unless (tramp--test-smb-or-windows-nt-p) "*foo*bar*baz*")
1568 (if (tramp--test-smb-or-windows-nt-p) "'foo'bar'baz'" "'foo\"bar'baz\"")
1570 (if (tramp--test-smb-or-windows-nt-p) "!foo!bar!baz!" "!foo|bar!baz|")
1571 (if (tramp--test-smb-or-windows-nt-p) ";foo;bar;baz;" ":foo;bar:baz;")
1572 (unless (tramp--test-smb-or-windows-nt-p) "<foo>bar<baz>")
1577 (ert-deftest tramp-test31-utf8
()
1578 "Check UTF8 encoding in file names and file contents."
1579 (skip-unless (tramp--test-enabled))
1581 (let ((coding-system-for-read 'utf-8
)
1582 (coding-system-for-write 'utf-8
)
1583 (file-name-coding-system 'utf-8
))
1584 (tramp--test-check-files
1585 "أصبح بوسعك الآن تنزيل نسخة كاملة من موسوعة ويكيبيديا العربية لتصفحها بلا اتصال بالإنترنت"
1587 "Автостопом по гала́ктике")))
1589 ;; This test is inspired by Bug#16928.
1590 (ert-deftest tramp-test32-asynchronous-requests
()
1591 "Check parallel asynchronous requests.
1592 Such requests could arrive from timers, process filters and
1593 process sentinels. They shall not disturb each other."
1594 ;; Mark as failed until bug has been fixed.
1595 :expected-result
:failed
1596 (skip-unless (tramp--test-enabled))
1599 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory
)
1600 'tramp-sh-file-name-handler
))
1602 ;; Keep instrumentation verbosity 0 until Tramp bug is fixed. This
1603 ;; has the side effect, that this test fails instead to abort. Good
1605 (tramp--instrument-test-case 0
1606 (let* ((tmp-name (tramp--test-make-temp-name))
1607 (default-directory tmp-name
)
1608 (remote-file-name-inhibit-cache t
)
1609 timer buffers kill-buffer-query-functions
)
1613 (make-directory tmp-name
)
1615 ;; Setup a timer in order to raise an ordinary command again
1616 ;; and again. `vc-registered' is well suited, because there
1625 (buffer-name (nth (random (length buffers
)) buffers
)))))))
1627 ;; Create temporary buffers. The number of buffers
1628 ;; corresponds to the number of processes; it could be
1629 ;; increased in order to make pressure on Tramp.
1631 (add-to-list 'buffers
(generate-new-buffer "*temp*")))
1633 ;; Open asynchronous processes. Set process sentinel.
1634 (dolist (buf buffers
)
1635 (async-shell-command "read line; touch $line; echo $line" buf
)
1636 (set-process-sentinel
1637 (get-buffer-process buf
)
1638 (lambda (proc _state
)
1639 (delete-file (buffer-name (process-buffer proc
))))))
1641 ;; Send a string. Use a random order of the buffers. Mix
1642 ;; with regular operation.
1643 (let ((buffers (copy-sequence buffers
))
1646 (setq buf
(nth (random (length buffers
)) buffers
))
1647 (process-send-string
1648 (get-buffer-process buf
) (format "'%s'\n" buf
))
1649 (file-attributes (buffer-name buf
))
1650 (setq buffers
(delq buf buffers
))))
1652 ;; Wait until the whole output has been read.
1653 (with-timeout ((* 10 (length buffers
))
1654 (ert-fail "`async-shell-command' timed out"))
1655 (let ((buffers (copy-sequence buffers
))
1658 (setq buf
(nth (random (length buffers
)) buffers
))
1660 (memq (process-status (get-buffer-process buf
))
1662 (accept-process-output (get-buffer-process buf
) 0.1)
1663 (setq buffers
(delq buf buffers
))))))
1666 (dolist (buf buffers
)
1667 (with-current-buffer buf
1669 (string-equal (format "'%s'\n" buf
) (buffer-string)))))
1671 (directory-files tmp-name nil directory-files-no-dot-files-regexp
)))
1674 (ignore-errors (cancel-timer timer
))
1675 (ignore-errors (delete-directory tmp-name
'recursive
))
1676 (dolist (buf buffers
)
1677 (ignore-errors (kill-buffer buf
)))))))
1679 (ert-deftest tramp-test33-recursive-load
()
1680 "Check that Tramp does not fail due to recursive load."
1681 (skip-unless (tramp--test-enabled))
1686 "(expand-file-name %S)"
1687 tramp-test-temporary-file-directory
)
1689 "(let ((default-directory %S)) (expand-file-name %S))"
1690 tramp-test-temporary-file-directory
1691 temporary-file-directory
)))
1695 (shell-command-to-string
1697 "%s -batch -Q -L %s --eval %s"
1698 (expand-file-name invocation-name invocation-directory
)
1699 (mapconcat 'shell-quote-argument load-path
" -L ")
1700 (shell-quote-argument code
)))))))
1702 (ert-deftest tramp-test34-unload
()
1703 "Check that Tramp and its subpackages unload completely.
1704 Since it unloads Tramp, it shall be the last test to run."
1705 ;; Mark as failed until all symbols are unbound.
1706 :expected-result
(if (featurep 'tramp
) :failed
:passed
)
1707 (when (featurep 'tramp
)
1708 (unload-feature 'tramp
'force
)
1709 ;; No Tramp feature must be left.
1710 (should-not (featurep 'tramp
))
1711 (should-not (all-completions "tramp" (delq 'tramp-tests features
)))
1712 ;; `file-name-handler-alist' must be clean.
1713 (should-not (all-completions "tramp" (mapcar 'cdr file-name-handler-alist
)))
1714 ;; There shouldn't be left a bound symbol. We do not regard our
1715 ;; test symbols, and the Tramp unload hooks.
1718 (and (or (boundp x
) (functionp x
))
1719 (string-match "^tramp" (symbol-name x
))
1720 (not (string-match "^tramp--?test" (symbol-name x
)))
1721 (not (string-match "unload-hook$" (symbol-name x
)))
1722 (ert-fail (format "`%s' still bound" x
)))))
1723 ; (progn (message "`%s' still bound" x)))))
1724 ;; There shouldn't be left a hook function containing a Tramp
1725 ;; function. We do not regard the Tramp unload hooks.
1729 (string-match "-hooks?$" (symbol-name x
))
1730 (not (string-match "unload-hook$" (symbol-name x
)))
1731 (consp (symbol-value x
))
1732 (ignore-errors (all-completions "tramp" (symbol-value x
)))
1733 (ert-fail (format "Hook `%s' still contains Tramp function" x
)))))))
1737 ;; * dired-compress-file
1740 ;; * file-ownership-preserved-p
1741 ;; * file-selinux-context
1742 ;; * find-backup-file-name
1743 ;; * make-auto-save-file-name
1745 ;; * set-file-selinux-context
1747 ;; * Work on skipped tests. Make a comment, when it is impossible.
1748 ;; * Fix `tramp-test15-copy-directory' for `smb'. Using tar in a pipe
1749 ;; doesn't work well when an interactive password must be provided.
1750 ;; * Fix `tramp-test27-start-file-process' for `nc' and on MS
1751 ;; Windows (`process-send-eof'?).
1752 ;; * Fix `tramp-test30-special-characters' for `adb' and `nc'.
1753 ;; * Fix `tramp-test31-utf8' for `nc'/`telnet' (when target is a dumb
1754 ;; busybox). Seems to be in `directory-files'.
1755 ;; * Fix Bug#16928. Set expected error of `tramp-test32-asynchronous-requests'.
1756 ;; * Fix `tramp-test34-unload' (Not all symbols are unbound). Set
1759 (defun tramp-test-all (&optional interactive
)
1760 "Run all tests for \\[tramp]."
1763 (if interactive
'ert-run-tests-interactively
'ert-run-tests-batch
) "^tramp"))
1765 (provide 'tramp-tests
)
1766 ;;; tramp-tests.el ends here