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.
96 (tramp-cleanup-connection
97 (tramp-dissect-file-name tramp-test-temporary-file-directory
)
101 (cdr tramp--test-enabled-checked
))
103 (defun tramp--test-make-temp-name (&optional local
)
104 "Create a temporary file name for test."
106 (make-temp-name "tramp-test")
107 (if local temporary-file-directory tramp-test-temporary-file-directory
)))
109 (defmacro tramp--instrument-test-case
(verbose &rest body
)
110 "Run BODY with `tramp-verbose' equal VERBOSE.
111 Print the the content of the Tramp debug buffer, if BODY does not
112 eval properly in `should', `should-not' or `should-error'."
113 (declare (indent 1) (debug (natnump body
)))
114 `(let ((tramp-verbose ,verbose
)
115 (tramp-message-show-message t
)
116 (tramp-debug-on-error t
))
118 ;; In general, we cannot use a timeout here: this would
119 ;; prevent traces when the test runs into an error.
120 ; (with-timeout (10 (ert-fail "`tramp--instrument-test-case' timed out"))
124 (signal (car err
) (cdr err
)))
126 (with-parsed-tramp-file-name tramp-test-temporary-file-directory nil
127 (with-current-buffer (tramp-get-connection-buffer v
)
128 (message "%s" (buffer-string)))
129 (with-current-buffer (tramp-get-debug-buffer v
)
130 (message "%s" (buffer-string))))
132 (signal (car err
) (cdr err
))))))
134 (ert-deftest tramp-test00-availability
()
135 "Test availability of Tramp functions."
136 :expected-result
(if (tramp--test-enabled) :passed
:failed
)
137 (message "Remote directory: `%s'" tramp-test-temporary-file-directory
)
138 (should (ignore-errors
140 (file-remote-p tramp-test-temporary-file-directory
)
141 (file-directory-p tramp-test-temporary-file-directory
)
142 (file-writable-p tramp-test-temporary-file-directory
)))))
144 (ert-deftest tramp-test01-file-name-syntax
()
145 "Check remote file name syntax."
147 (should (tramp-tramp-file-p "/method::"))
148 (should (tramp-tramp-file-p "/host:"))
149 (should (tramp-tramp-file-p "/user@:"))
150 (should (tramp-tramp-file-p "/user@host:"))
151 (should (tramp-tramp-file-p "/method:host:"))
152 (should (tramp-tramp-file-p "/method:user@:"))
153 (should (tramp-tramp-file-p "/method:user@host:"))
154 (should (tramp-tramp-file-p "/method:user@email@host:"))
157 (should (tramp-tramp-file-p "/host#1234:"))
158 (should (tramp-tramp-file-p "/user@host#1234:"))
159 (should (tramp-tramp-file-p "/method:host#1234:"))
160 (should (tramp-tramp-file-p "/method:user@host#1234:"))
162 ;; Using an IPv4 address.
163 (should (tramp-tramp-file-p "/1.2.3.4:"))
164 (should (tramp-tramp-file-p "/user@1.2.3.4:"))
165 (should (tramp-tramp-file-p "/method:1.2.3.4:"))
166 (should (tramp-tramp-file-p "/method:user@1.2.3.4:"))
168 ;; Using an IPv6 address.
169 (should (tramp-tramp-file-p "/[]:"))
170 (should (tramp-tramp-file-p "/[::1]:"))
171 (should (tramp-tramp-file-p "/user@[::1]:"))
172 (should (tramp-tramp-file-p "/method:[::1]:"))
173 (should (tramp-tramp-file-p "/method:user@[::1]:"))
175 ;; Local file name part.
176 (should (tramp-tramp-file-p "/host:/:"))
177 (should (tramp-tramp-file-p "/method:::"))
178 (should (tramp-tramp-file-p "/method::/path/to/file"))
179 (should (tramp-tramp-file-p "/method::file"))
182 (should (tramp-tramp-file-p "/method1:|method2::"))
183 (should (tramp-tramp-file-p "/method1:host1|host2:"))
184 (should (tramp-tramp-file-p "/method1:host1|method2:host2:"))
185 (should (tramp-tramp-file-p "/method1:user1@host1|method2:user2@host2:"))
186 (should (tramp-tramp-file-p
187 "/method1:user1@host1|method2:user2@host2|method3:user3@host3:"))
190 (should-not (tramp-tramp-file-p nil
))
191 (should-not (tramp-tramp-file-p 'symbol
))
192 ;; "/:" suppresses file name handlers.
193 (should-not (tramp-tramp-file-p "/::"))
194 (should-not (tramp-tramp-file-p "/:@:"))
195 (should-not (tramp-tramp-file-p "/:[]:"))
196 ;; Multihops require a method.
197 (should-not (tramp-tramp-file-p "/host1|host2:"))
198 ;; Methods or hostnames shall be at least two characters on MS Windows.
199 (when (memq system-type
'(cygwin windows-nt
))
200 (should-not (tramp-tramp-file-p "/c:/path/to/file"))
201 (should-not (tramp-tramp-file-p "/c::/path/to/file"))))
203 (ert-deftest tramp-test02-file-name-dissect
()
204 "Check remote file name components."
205 (let ((tramp-default-method "default-method")
206 (tramp-default-user "default-user")
207 (tramp-default-host "default-host"))
208 ;; Expand `tramp-default-user' and `tramp-default-host'.
209 (should (string-equal
210 (file-remote-p "/method::")
211 (format "/%s:%s@%s:" "method" "default-user" "default-host")))
212 (should (string-equal (file-remote-p "/method::" 'method
) "method"))
213 (should (string-equal (file-remote-p "/method::" 'user
) "default-user"))
214 (should (string-equal (file-remote-p "/method::" 'host
) "default-host"))
215 (should (string-equal (file-remote-p "/method::" 'localname
) ""))
217 ;; Expand `tramp-default-method' and `tramp-default-user'.
218 (should (string-equal
219 (file-remote-p "/host:")
220 (format "/%s:%s@%s:" "default-method" "default-user" "host")))
221 (should (string-equal (file-remote-p "/host:" 'method
) "default-method"))
222 (should (string-equal (file-remote-p "/host:" 'user
) "default-user"))
223 (should (string-equal (file-remote-p "/host:" 'host
) "host"))
224 (should (string-equal (file-remote-p "/host:" 'localname
) ""))
226 ;; Expand `tramp-default-method' and `tramp-default-host'.
227 (should (string-equal
228 (file-remote-p "/user@:")
229 (format "/%s:%s@%s:" "default-method""user" "default-host")))
230 (should (string-equal (file-remote-p "/user@:" 'method
) "default-method"))
231 (should (string-equal (file-remote-p "/user@:" 'user
) "user"))
232 (should (string-equal (file-remote-p "/user@:" 'host
) "default-host"))
233 (should (string-equal (file-remote-p "/user@:" 'localname
) ""))
235 ;; Expand `tramp-default-method'.
236 (should (string-equal
237 (file-remote-p "/user@host:")
238 (format "/%s:%s@%s:" "default-method" "user" "host")))
239 (should (string-equal
240 (file-remote-p "/user@host:" 'method
) "default-method"))
241 (should (string-equal (file-remote-p "/user@host:" 'user
) "user"))
242 (should (string-equal (file-remote-p "/user@host:" 'host
) "host"))
243 (should (string-equal (file-remote-p "/user@host:" 'localname
) ""))
245 ;; Expand `tramp-default-user'.
246 (should (string-equal
247 (file-remote-p "/method:host:")
248 (format "/%s:%s@%s:" "method" "default-user" "host")))
249 (should (string-equal (file-remote-p "/method:host:" 'method
) "method"))
250 (should (string-equal (file-remote-p "/method:host:" 'user
) "default-user"))
251 (should (string-equal (file-remote-p "/method:host:" 'host
) "host"))
252 (should (string-equal (file-remote-p "/method:host:" 'localname
) ""))
254 ;; Expand `tramp-default-host'.
255 (should (string-equal
256 (file-remote-p "/method:user@:")
257 (format "/%s:%s@%s:" "method" "user" "default-host")))
258 (should (string-equal (file-remote-p "/method:user@:" 'method
) "method"))
259 (should (string-equal (file-remote-p "/method:user@:" 'user
) "user"))
260 (should (string-equal (file-remote-p "/method:user@:" 'host
)
262 (should (string-equal (file-remote-p "/method:user@:" 'localname
) ""))
265 (should (string-equal
266 (file-remote-p "/method:user@host:")
267 (format "/%s:%s@%s:" "method" "user" "host")))
268 (should (string-equal
269 (file-remote-p "/method:user@host:" 'method
) "method"))
270 (should (string-equal (file-remote-p "/method:user@host:" 'user
) "user"))
271 (should (string-equal (file-remote-p "/method:user@host:" 'host
) "host"))
272 (should (string-equal (file-remote-p "/method:user@host:" 'localname
) ""))
275 (should (string-equal
276 (file-remote-p "/method:user@email@host:")
277 (format "/%s:%s@%s:" "method" "user@email" "host")))
278 (should (string-equal
279 (file-remote-p "/method:user@email@host:" 'method
) "method"))
280 (should (string-equal
281 (file-remote-p "/method:user@email@host:" 'user
) "user@email"))
282 (should (string-equal
283 (file-remote-p "/method:user@email@host:" 'host
) "host"))
284 (should (string-equal
285 (file-remote-p "/method:user@email@host:" 'localname
) ""))
287 ;; Expand `tramp-default-method' and `tramp-default-user'.
288 (should (string-equal
289 (file-remote-p "/host#1234:")
290 (format "/%s:%s@%s:" "default-method" "default-user" "host#1234")))
291 (should (string-equal
292 (file-remote-p "/host#1234:" 'method
) "default-method"))
293 (should (string-equal (file-remote-p "/host#1234:" 'user
) "default-user"))
294 (should (string-equal (file-remote-p "/host#1234:" 'host
) "host#1234"))
295 (should (string-equal (file-remote-p "/host#1234:" 'localname
) ""))
297 ;; Expand `tramp-default-method'.
298 (should (string-equal
299 (file-remote-p "/user@host#1234:")
300 (format "/%s:%s@%s:" "default-method" "user" "host#1234")))
301 (should (string-equal
302 (file-remote-p "/user@host#1234:" 'method
) "default-method"))
303 (should (string-equal (file-remote-p "/user@host#1234:" 'user
) "user"))
304 (should (string-equal (file-remote-p "/user@host#1234:" 'host
) "host#1234"))
305 (should (string-equal (file-remote-p "/user@host#1234:" 'localname
) ""))
307 ;; Expand `tramp-default-user'.
308 (should (string-equal
309 (file-remote-p "/method:host#1234:")
310 (format "/%s:%s@%s:" "method" "default-user" "host#1234")))
311 (should (string-equal
312 (file-remote-p "/method:host#1234:" 'method
) "method"))
313 (should (string-equal
314 (file-remote-p "/method:host#1234:" 'user
) "default-user"))
315 (should (string-equal
316 (file-remote-p "/method:host#1234:" 'host
) "host#1234"))
317 (should (string-equal (file-remote-p "/method:host#1234:" 'localname
) ""))
320 (should (string-equal
321 (file-remote-p "/method:user@host#1234:")
322 (format "/%s:%s@%s:" "method" "user" "host#1234")))
323 (should (string-equal
324 (file-remote-p "/method:user@host#1234:" 'method
) "method"))
325 (should (string-equal
326 (file-remote-p "/method:user@host#1234:" 'user
) "user"))
327 (should (string-equal
328 (file-remote-p "/method:user@host#1234:" 'host
) "host#1234"))
329 (should (string-equal
330 (file-remote-p "/method:user@host#1234:" 'localname
) ""))
332 ;; Expand `tramp-default-method' and `tramp-default-user'.
333 (should (string-equal
334 (file-remote-p "/1.2.3.4:")
335 (format "/%s:%s@%s:" "default-method" "default-user" "1.2.3.4")))
336 (should (string-equal (file-remote-p "/1.2.3.4:" 'method
) "default-method"))
337 (should (string-equal (file-remote-p "/1.2.3.4:" 'user
) "default-user"))
338 (should (string-equal (file-remote-p "/1.2.3.4:" 'host
) "1.2.3.4"))
339 (should (string-equal (file-remote-p "/1.2.3.4:" 'localname
) ""))
341 ;; Expand `tramp-default-method'.
342 (should (string-equal
343 (file-remote-p "/user@1.2.3.4:")
344 (format "/%s:%s@%s:" "default-method" "user" "1.2.3.4")))
345 (should (string-equal
346 (file-remote-p "/user@1.2.3.4:" 'method
) "default-method"))
347 (should (string-equal (file-remote-p "/user@1.2.3.4:" 'user
) "user"))
348 (should (string-equal (file-remote-p "/user@1.2.3.4:" 'host
) "1.2.3.4"))
349 (should (string-equal (file-remote-p "/user@1.2.3.4:" 'localname
) ""))
351 ;; Expand `tramp-default-user'.
352 (should (string-equal
353 (file-remote-p "/method:1.2.3.4:")
354 (format "/%s:%s@%s:" "method" "default-user" "1.2.3.4")))
355 (should (string-equal (file-remote-p "/method:1.2.3.4:" 'method
) "method"))
356 (should (string-equal
357 (file-remote-p "/method:1.2.3.4:" 'user
) "default-user"))
358 (should (string-equal (file-remote-p "/method:1.2.3.4:" 'host
) "1.2.3.4"))
359 (should (string-equal (file-remote-p "/method:1.2.3.4:" 'localname
) ""))
362 (should (string-equal
363 (file-remote-p "/method:user@1.2.3.4:")
364 (format "/%s:%s@%s:" "method" "user" "1.2.3.4")))
365 (should (string-equal
366 (file-remote-p "/method:user@1.2.3.4:" 'method
) "method"))
367 (should (string-equal (file-remote-p "/method:user@1.2.3.4:" 'user
) "user"))
368 (should (string-equal
369 (file-remote-p "/method:user@1.2.3.4:" 'host
) "1.2.3.4"))
370 (should (string-equal
371 (file-remote-p "/method:user@1.2.3.4:" 'localname
) ""))
373 ;; Expand `tramp-default-method', `tramp-default-user' and
374 ;; `tramp-default-host'.
375 (should (string-equal
376 (file-remote-p "/[]:")
378 "/%s:%s@%s:" "default-method" "default-user" "default-host")))
379 (should (string-equal (file-remote-p "/[]:" 'method
) "default-method"))
380 (should (string-equal (file-remote-p "/[]:" 'user
) "default-user"))
381 (should (string-equal (file-remote-p "/[]:" 'host
) "default-host"))
382 (should (string-equal (file-remote-p "/[]:" 'localname
) ""))
384 ;; Expand `tramp-default-method' and `tramp-default-user'.
385 (let ((tramp-default-host "::1"))
386 (should (string-equal
387 (file-remote-p "/[]:")
388 (format "/%s:%s@%s:" "default-method" "default-user" "[::1]")))
389 (should (string-equal (file-remote-p "/[]:" 'method
) "default-method"))
390 (should (string-equal (file-remote-p "/[]:" 'user
) "default-user"))
391 (should (string-equal (file-remote-p "/[]:" 'host
) "::1"))
392 (should (string-equal (file-remote-p "/[]:" 'localname
) "")))
394 ;; Expand `tramp-default-method' and `tramp-default-user'.
395 (should (string-equal
396 (file-remote-p "/[::1]:")
397 (format "/%s:%s@%s:" "default-method" "default-user" "[::1]")))
398 (should (string-equal (file-remote-p "/[::1]:" 'method
) "default-method"))
399 (should (string-equal (file-remote-p "/[::1]:" 'user
) "default-user"))
400 (should (string-equal (file-remote-p "/[::1]:" 'host
) "::1"))
401 (should (string-equal (file-remote-p "/[::1]:" 'localname
) ""))
403 ;; Expand `tramp-default-method'.
404 (should (string-equal
405 (file-remote-p "/user@[::1]:")
406 (format "/%s:%s@%s:" "default-method" "user" "[::1]")))
407 (should (string-equal
408 (file-remote-p "/user@[::1]:" 'method
) "default-method"))
409 (should (string-equal (file-remote-p "/user@[::1]:" 'user
) "user"))
410 (should (string-equal (file-remote-p "/user@[::1]:" 'host
) "::1"))
411 (should (string-equal (file-remote-p "/user@[::1]:" 'localname
) ""))
413 ;; Expand `tramp-default-user'.
414 (should (string-equal
415 (file-remote-p "/method:[::1]:")
416 (format "/%s:%s@%s:" "method" "default-user" "[::1]")))
417 (should (string-equal (file-remote-p "/method:[::1]:" 'method
) "method"))
418 (should (string-equal
419 (file-remote-p "/method:[::1]:" 'user
) "default-user"))
420 (should (string-equal (file-remote-p "/method:[::1]:" 'host
) "::1"))
421 (should (string-equal (file-remote-p "/method:[::1]:" 'localname
) ""))
424 (should (string-equal
425 (file-remote-p "/method:user@[::1]:")
426 (format "/%s:%s@%s:" "method" "user" "[::1]")))
427 (should (string-equal
428 (file-remote-p "/method:user@[::1]:" 'method
) "method"))
429 (should (string-equal (file-remote-p "/method:user@[::1]:" 'user
) "user"))
430 (should (string-equal (file-remote-p "/method:user@[::1]:" 'host
) "::1"))
431 (should (string-equal
432 (file-remote-p "/method:user@[::1]:" 'localname
) ""))
434 ;; Local file name part.
435 (should (string-equal (file-remote-p "/host:/:" 'localname
) "/:"))
436 (should (string-equal (file-remote-p "/method:::" 'localname
) ":"))
437 (should (string-equal (file-remote-p "/method:: " 'localname
) " "))
438 (should (string-equal (file-remote-p "/method::file" 'localname
) "file"))
439 (should (string-equal
440 (file-remote-p "/method::/path/to/file" 'localname
)
446 (file-remote-p "/method1:user1@host1|method2:user2@host2:/path/to/file")
447 (format "/%s:%s@%s:" "method2" "user2" "host2")))
451 "/method1:user1@host1|method2:user2@host2:/path/to/file" 'method
)
456 "/method1:user1@host1|method2:user2@host2:/path/to/file" 'user
)
461 "/method1:user1@host1|method2:user2@host2:/path/to/file" 'host
)
466 "/method1:user1@host1|method2:user2@host2:/path/to/file" 'localname
)
472 "/method1:user1@host1|method2:user2@host2|method3:user3@host3:/path/to/file")
473 (format "/%s:%s@%s:" "method3" "user3" "host3")))
477 "/method1:user1@host1|method2:user2@host2|method3:user3@host3:/path/to/file"
483 "/method1:user1@host1|method2:user2@host2|method3:user3@host3:/path/to/file"
489 "/method1:user1@host1|method2:user2@host2|method3:user3@host3:/path/to/file"
495 "/method1:user1@host1|method2:user2@host2|method3:user3@host3:/path/to/file"
499 (ert-deftest tramp-test03-file-name-defaults
()
500 "Check default values for some methods."
501 ;; Default values in tramp-adb.el.
502 (should (string-equal (file-remote-p "/adb::" 'host
) ""))
503 ;; Default values in tramp-ftp.el.
504 (should (string-equal (file-remote-p "/ftp.host:" 'method
) "ftp"))
505 (dolist (u '("ftp" "anonymous"))
506 (should (string-equal (file-remote-p (format "/%s@:" u
) 'method
) "ftp")))
507 ;; Default values in tramp-gvfs.el.
508 (when (and (load "tramp-gvfs" 'noerror
'nomessage
)
509 (symbol-value 'tramp-gvfs-enabled
))
510 (should (string-equal (file-remote-p "/synce::" 'user
) nil
)))
511 ;; Default values in tramp-gw.el.
512 (dolist (m '("tunnel" "socks"))
514 (string-equal (file-remote-p (format "/%s::" m
) 'user
) (user-login-name))))
515 ;; Default values in tramp-sh.el.
516 (dolist (h `("127.0.0.1" "[::1]" "localhost" "localhost6" ,(system-name)))
517 (should (string-equal (file-remote-p (format "/root@%s:" h
) 'method
) "su")))
518 (dolist (m '("su" "sudo" "ksu"))
519 (should (string-equal (file-remote-p (format "/%s::" m
) 'user
) "root")))
520 (dolist (m '("rcp" "remcp" "rsh" "telnet" "krlogin" "fcp"))
522 (string-equal (file-remote-p (format "/%s::" m
) 'user
) (user-login-name))))
523 ;; Default values in tramp-smb.el.
524 (should (string-equal (file-remote-p "/user%domain@host:" 'method
) "smb"))
525 (should (string-equal (file-remote-p "/smb::" 'user
) nil
)))
527 (ert-deftest tramp-test04-substitute-in-file-name
()
528 "Check `substitute-in-file-name'."
529 (should (string-equal (substitute-in-file-name "/method:host://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"))
537 (substitute-in-file-name "/method:host:/path/~/foo") "/method:host:~/foo"))
539 (string-equal (substitute-in-file-name "/method:host:/path//~/foo") "~/foo"))
540 (let (process-environment)
543 (substitute-in-file-name "/method:host:/path/$FOO")
544 "/method:host:/path/$FOO"))
548 (substitute-in-file-name "/method:host:/path/$FOO")
549 "/method:host:/path/bla"))
552 (substitute-in-file-name "/method:host:/path/$$FOO")
553 "/method:host:/path/$FOO"))))
555 (ert-deftest tramp-test05-expand-file-name
()
556 "Check `expand-file-name'."
559 (expand-file-name "/method:host:/path/./file") "/method:host:/path/file"))
562 (expand-file-name "/method:host:/path/../file") "/method:host:/file")))
564 (ert-deftest tramp-test06-directory-file-name
()
565 "Check `directory-file-name'.
566 This checks also `file-name-as-directory', `file-name-directory'
567 and `file-name-nondirectory'."
570 (directory-file-name "/method:host:/path/to/file")
571 "/method:host:/path/to/file"))
574 (directory-file-name "/method:host:/path/to/file/")
575 "/method:host:/path/to/file"))
578 (file-name-as-directory "/method:host:/path/to/file")
579 "/method:host:/path/to/file/"))
582 (file-name-as-directory "/method:host:/path/to/file/")
583 "/method:host:/path/to/file/"))
586 (file-name-directory "/method:host:/path/to/file")
587 "/method:host:/path/to/"))
590 (file-name-directory "/method:host:/path/to/file/")
591 "/method:host:/path/to/file/"))
593 (string-equal (file-name-nondirectory "/method:host:/path/to/file") "file"))
595 (string-equal (file-name-nondirectory "/method:host:/path/to/file/") ""))
598 (unhandled-file-name-directory "/method:host:/path/to/file"))))
600 (ert-deftest tramp-test07-file-exists-p
()
601 "Check `file-exist-p', `write-region' and `delete-file'."
602 (skip-unless (tramp--test-enabled))
604 (let ((tmp-name (tramp--test-make-temp-name)))
605 (should-not (file-exists-p tmp-name
))
606 (write-region "foo" nil tmp-name
)
607 (should (file-exists-p tmp-name
))
608 (delete-file tmp-name
)
609 (should-not (file-exists-p tmp-name
))))
611 (ert-deftest tramp-test08-file-local-copy
()
612 "Check `file-local-copy'."
613 (skip-unless (tramp--test-enabled))
615 (let ((tmp-name1 (tramp--test-make-temp-name))
619 (write-region "foo" nil tmp-name1
)
620 (should (setq tmp-name2
(file-local-copy tmp-name1
)))
622 (insert-file-contents tmp-name2
)
623 (should (string-equal (buffer-string) "foo"))))
625 (delete-file tmp-name1
)
626 (delete-file tmp-name2
)))))
628 (ert-deftest tramp-test09-insert-file-contents
()
629 "Check `insert-file-contents'."
630 (skip-unless (tramp--test-enabled))
632 (let ((tmp-name (tramp--test-make-temp-name)))
635 (write-region "foo" nil tmp-name
)
637 (insert-file-contents tmp-name
)
638 (should (string-equal (buffer-string) "foo"))
639 (insert-file-contents tmp-name
)
640 (should (string-equal (buffer-string) "foofoo"))
642 (insert-file-contents tmp-name nil
1 3)
643 (should (string-equal (buffer-string) "oofoofoo"))
645 (insert-file-contents tmp-name nil nil nil
'replace
)
646 (should (string-equal (buffer-string) "foo"))))
647 (ignore-errors (delete-file tmp-name
)))))
649 (ert-deftest tramp-test10-write-region
()
650 "Check `write-region'."
651 (skip-unless (tramp--test-enabled))
653 (let ((tmp-name (tramp--test-make-temp-name)))
658 (write-region nil nil tmp-name
))
660 (insert-file-contents tmp-name
)
661 (should (string-equal (buffer-string) "foo")))
665 (write-region nil nil tmp-name
'append
))
667 (insert-file-contents tmp-name
)
668 (should (string-equal (buffer-string) "foobla")))
670 (write-region "foo" nil tmp-name
)
672 (insert-file-contents tmp-name
)
673 (should (string-equal (buffer-string) "foo")))
677 (write-region 3 5 tmp-name
))
679 (insert-file-contents tmp-name
)
680 (should (string-equal (buffer-string) "34"))))
681 (ignore-errors (delete-file tmp-name
)))))
683 (ert-deftest tramp-test11-copy-file
()
685 (skip-unless (tramp--test-enabled))
687 (let ((tmp-name1 (tramp--test-make-temp-name))
688 (tmp-name2 (tramp--test-make-temp-name))
689 (tmp-name3 (tramp--test-make-temp-name))
690 (tmp-name4 (tramp--test-make-temp-name 'local
))
691 (tmp-name5 (tramp--test-make-temp-name 'local
)))
693 ;; Copy on remote side.
696 (write-region "foo" nil tmp-name1
)
697 (copy-file tmp-name1 tmp-name2
)
698 (should (file-exists-p tmp-name2
))
700 (insert-file-contents tmp-name2
)
701 (should (string-equal (buffer-string) "foo")))
702 (should-error (copy-file tmp-name1 tmp-name2
))
703 (copy-file tmp-name1 tmp-name2
'ok
)
704 (make-directory tmp-name3
)
705 (copy-file tmp-name1 tmp-name3
)
708 (expand-file-name (file-name-nondirectory tmp-name1
) tmp-name3
))))
709 (ignore-errors (delete-file tmp-name1
))
710 (ignore-errors (delete-file tmp-name2
))
711 (ignore-errors (delete-directory tmp-name3
'recursive
)))
713 ;; Copy from remote side to local side.
716 (write-region "foo" nil tmp-name1
)
717 (copy-file tmp-name1 tmp-name4
)
718 (should (file-exists-p tmp-name4
))
720 (insert-file-contents tmp-name4
)
721 (should (string-equal (buffer-string) "foo")))
722 (should-error (copy-file tmp-name1 tmp-name4
))
723 (copy-file tmp-name1 tmp-name4
'ok
)
724 (make-directory tmp-name5
)
725 (copy-file tmp-name1 tmp-name5
)
728 (expand-file-name (file-name-nondirectory tmp-name1
) tmp-name5
))))
729 (ignore-errors (delete-file tmp-name1
))
730 (ignore-errors (delete-file tmp-name4
))
731 (ignore-errors (delete-directory tmp-name5
'recursive
)))
733 ;; Copy from local side to remote side.
736 (write-region "foo" nil tmp-name4 nil
'nomessage
)
737 (copy-file tmp-name4 tmp-name1
)
738 (should (file-exists-p tmp-name1
))
740 (insert-file-contents tmp-name1
)
741 (should (string-equal (buffer-string) "foo")))
742 (should-error (copy-file tmp-name4 tmp-name1
))
743 (copy-file tmp-name4 tmp-name1
'ok
)
744 (make-directory tmp-name3
)
745 (copy-file tmp-name4 tmp-name3
)
748 (expand-file-name (file-name-nondirectory tmp-name4
) tmp-name3
))))
749 (ignore-errors (delete-file tmp-name1
))
750 (ignore-errors (delete-file tmp-name4
))
751 (ignore-errors (delete-directory tmp-name3
'recursive
)))))
753 (ert-deftest tramp-test12-rename-file
()
754 "Check `rename-file'."
755 (skip-unless (tramp--test-enabled))
757 (let ((tmp-name1 (tramp--test-make-temp-name))
758 (tmp-name2 (tramp--test-make-temp-name))
759 (tmp-name3 (tramp--test-make-temp-name))
760 (tmp-name4 (tramp--test-make-temp-name 'local
))
761 (tmp-name5 (tramp--test-make-temp-name 'local
)))
763 ;; Rename on remote side.
766 (write-region "foo" nil tmp-name1
)
767 (rename-file tmp-name1 tmp-name2
)
768 (should-not (file-exists-p tmp-name1
))
769 (should (file-exists-p tmp-name2
))
771 (insert-file-contents tmp-name2
)
772 (should (string-equal (buffer-string) "foo")))
773 (write-region "foo" nil tmp-name1
)
774 (should-error (rename-file tmp-name1 tmp-name2
))
775 (rename-file tmp-name1 tmp-name2
'ok
)
776 (should-not (file-exists-p tmp-name1
))
777 (write-region "foo" nil tmp-name1
)
778 (make-directory tmp-name3
)
779 (rename-file tmp-name1 tmp-name3
)
780 (should-not (file-exists-p tmp-name1
))
783 (expand-file-name (file-name-nondirectory tmp-name1
) tmp-name3
))))
784 (ignore-errors (delete-file tmp-name1
))
785 (ignore-errors (delete-file tmp-name2
))
786 (ignore-errors (delete-directory tmp-name3
'recursive
)))
788 ;; Rename from remote side to local side.
791 (write-region "foo" nil tmp-name1
)
792 (rename-file tmp-name1 tmp-name4
)
793 (should-not (file-exists-p tmp-name1
))
794 (should (file-exists-p tmp-name4
))
796 (insert-file-contents tmp-name4
)
797 (should (string-equal (buffer-string) "foo")))
798 (write-region "foo" nil tmp-name1
)
799 (should-error (rename-file tmp-name1 tmp-name4
))
800 (rename-file tmp-name1 tmp-name4
'ok
)
801 (should-not (file-exists-p tmp-name1
))
802 (write-region "foo" nil tmp-name1
)
803 (make-directory tmp-name5
)
804 (rename-file tmp-name1 tmp-name5
)
805 (should-not (file-exists-p tmp-name1
))
808 (expand-file-name (file-name-nondirectory tmp-name1
) tmp-name5
))))
809 (ignore-errors (delete-file tmp-name1
))
810 (ignore-errors (delete-file tmp-name4
))
811 (ignore-errors (delete-directory tmp-name5
'recursive
)))
813 ;; Rename from local side to remote side.
816 (write-region "foo" nil tmp-name4 nil
'nomessage
)
817 (rename-file tmp-name4 tmp-name1
)
818 (should-not (file-exists-p tmp-name4
))
819 (should (file-exists-p tmp-name1
))
821 (insert-file-contents tmp-name1
)
822 (should (string-equal (buffer-string) "foo")))
823 (write-region "foo" nil tmp-name4 nil
'nomessage
)
824 (should-error (rename-file tmp-name4 tmp-name1
))
825 (rename-file tmp-name4 tmp-name1
'ok
)
826 (should-not (file-exists-p tmp-name4
))
827 (write-region "foo" nil tmp-name4 nil
'nomessage
)
828 (make-directory tmp-name3
)
829 (rename-file tmp-name4 tmp-name3
)
830 (should-not (file-exists-p tmp-name4
))
833 (expand-file-name (file-name-nondirectory tmp-name4
) tmp-name3
))))
834 (ignore-errors (delete-file tmp-name1
))
835 (ignore-errors (delete-file tmp-name4
))
836 (ignore-errors (delete-directory tmp-name3
'recursive
)))))
838 (ert-deftest tramp-test13-make-directory
()
839 "Check `make-directory'.
840 This tests also `file-directory-p' and `file-accessible-directory-p'."
841 (skip-unless (tramp--test-enabled))
843 (let ((tmp-name (tramp--test-make-temp-name)))
846 (make-directory tmp-name
)
847 (should (file-directory-p tmp-name
))
848 (should (file-accessible-directory-p tmp-name
)))
849 (ignore-errors (delete-directory tmp-name
)))))
851 (ert-deftest tramp-test14-delete-directory
()
852 "Check `delete-directory'."
853 (skip-unless (tramp--test-enabled))
855 (let ((tmp-name (tramp--test-make-temp-name)))
856 ;; Delete empty directory.
857 (make-directory tmp-name
)
858 (should (file-directory-p tmp-name
))
859 (delete-directory tmp-name
)
860 (should-not (file-directory-p tmp-name
))
861 ;; Delete non-empty directory.
862 (make-directory tmp-name
)
863 (write-region "foo" nil
(expand-file-name "bla" tmp-name
))
864 (should-error (delete-directory tmp-name
) :type
'file-error
)
865 (delete-directory tmp-name
'recursive
)
866 (should-not (file-directory-p tmp-name
))))
868 (ert-deftest tramp-test15-copy-directory
()
869 "Check `copy-directory'."
870 (skip-unless (tramp--test-enabled))
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 (let ((tmp-name1 (tramp--test-make-temp-name))
1078 (tmp-name2 (tramp--test-make-temp-name))
1079 (tmp-name3 (tramp--test-make-temp-name 'local
)))
1082 (write-region "foo" nil tmp-name1
)
1083 (should (file-exists-p tmp-name1
))
1084 ;; Method "smb" supports `make-symbolic-link' only if the
1085 ;; remote host has CIFS capabilities. tramp-adb.el and
1086 ;; tramp-gvfs.el do not support symbolic links at all.
1088 (make-symbolic-link tmp-name1 tmp-name2
)
1091 (not (string-equal (error-message-string err
)
1092 "make-symbolic-link not supported")))))
1093 (should (file-symlink-p tmp-name2
))
1094 (should-error (make-symbolic-link tmp-name1 tmp-name2
))
1095 (make-symbolic-link tmp-name1 tmp-name2
'ok-if-already-exists
)
1096 (should (file-symlink-p tmp-name2
))
1097 ;; `tmp-name3' is a local file name.
1098 (should-error (make-symbolic-link tmp-name1 tmp-name3
)))
1100 (delete-file tmp-name1
)
1101 (delete-file tmp-name2
)))
1105 (write-region "foo" nil tmp-name1
)
1106 (should (file-exists-p tmp-name1
))
1107 (add-name-to-file tmp-name1 tmp-name2
)
1108 (should-not (file-symlink-p tmp-name2
))
1109 (should-error (add-name-to-file tmp-name1 tmp-name2
))
1110 (add-name-to-file tmp-name1 tmp-name2
'ok-if-already-exists
)
1111 (should-not (file-symlink-p tmp-name2
))
1112 ;; `tmp-name3' is a local file name.
1113 (should-error (add-name-to-file tmp-name1 tmp-name3
)))
1115 (delete-file tmp-name1
)
1116 (delete-file tmp-name2
)))
1120 (write-region "foo" nil tmp-name1
)
1121 (should (file-exists-p tmp-name1
))
1122 (make-symbolic-link tmp-name1 tmp-name2
)
1123 (should (file-symlink-p tmp-name2
))
1124 (should-not (string-equal tmp-name2
(file-truename tmp-name2
)))
1126 (string-equal (file-truename tmp-name1
) (file-truename tmp-name2
)))
1127 (should (file-equal-p tmp-name1 tmp-name2
)))
1129 (delete-file tmp-name1
)
1130 (delete-file tmp-name2
)))
1132 ;; `file-truename' shall preserve trailing link of directories.
1133 (unless (file-symlink-p tramp-test-temporary-file-directory
)
1134 (let* ((dir1 (directory-file-name tramp-test-temporary-file-directory
))
1135 (dir2 (file-name-as-directory dir1
)))
1136 (should (string-equal (file-truename dir1
) (expand-file-name dir1
)))
1137 (should (string-equal (file-truename dir2
) (expand-file-name dir2
)))))))
1139 (ert-deftest tramp-test22-file-times
()
1140 "Check `set-file-times' and `file-newer-than-file-p'."
1141 (skip-unless (tramp--test-enabled))
1145 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory
)
1146 '(tramp-gvfs-file-name-handler tramp-smb-file-name-handler
))))
1148 (let ((tmp-name1 (tramp--test-make-temp-name))
1149 (tmp-name2 (tramp--test-make-temp-name))
1150 (tmp-name3 (tramp--test-make-temp-name)))
1153 (write-region "foo" nil tmp-name1
)
1154 (should (file-exists-p tmp-name1
))
1155 (should (consp (nth 5 (file-attributes tmp-name1
))))
1156 ;; '(0 0) means don't know, and will be replaced by
1157 ;; `current-time'. Therefore, we use '(0 1).
1158 ;; We skip the test, if the remote handler is not able to
1159 ;; set the correct time.
1160 (skip-unless (set-file-times tmp-name1
'(0 1)))
1161 ;; Dumb remote shells without perl(1) or stat(1) are not
1162 ;; able to return the date correctly. They say "don't know".
1163 (unless (equal (nth 5 (file-attributes tmp-name1
)) '(0 0))
1164 (should (equal (nth 5 (file-attributes tmp-name1
)) '(0 1)))
1165 (write-region "bla" nil tmp-name2
)
1166 (should (file-exists-p tmp-name2
))
1167 (should (file-newer-than-file-p tmp-name2 tmp-name1
))
1168 ;; `tmp-name3' does not exist.
1169 (should (file-newer-than-file-p tmp-name2 tmp-name3
))
1170 (should-not (file-newer-than-file-p tmp-name3 tmp-name1
))))
1172 (delete-file tmp-name1
)
1173 (delete-file tmp-name2
)))))
1175 (ert-deftest tramp-test23-visited-file-modtime
()
1176 "Check `set-visited-file-modtime' and `verify-visited-file-modtime'."
1177 (skip-unless (tramp--test-enabled))
1179 (let ((tmp-name (tramp--test-make-temp-name)))
1182 (write-region "foo" nil tmp-name
)
1183 (should (file-exists-p tmp-name
))
1185 (insert-file-contents tmp-name
)
1186 (should (verify-visited-file-modtime))
1187 (set-visited-file-modtime '(0 1))
1188 (should (verify-visited-file-modtime))
1189 (should (equal (visited-file-modtime) '(0 1 0 0)))))
1190 (ignore-errors (delete-file tmp-name
)))))
1192 (ert-deftest tramp-test24-file-name-completion
()
1193 "Check `file-name-completion' and `file-name-all-completions'."
1194 (skip-unless (tramp--test-enabled))
1196 (let ((tmp-name (tramp--test-make-temp-name)))
1199 (make-directory tmp-name
)
1200 (should (file-directory-p tmp-name
))
1201 (write-region "foo" nil
(expand-file-name "foo" tmp-name
))
1202 (write-region "bar" nil
(expand-file-name "bold" tmp-name
))
1203 (make-directory (expand-file-name "boz" tmp-name
))
1204 (should (equal (file-name-completion "fo" tmp-name
) "foo"))
1205 (should (equal (file-name-completion "b" tmp-name
) "bo"))
1207 (equal (file-name-completion "b" tmp-name
'file-directory-p
) "boz/"))
1208 (should (equal (file-name-all-completions "fo" tmp-name
) '("foo")))
1210 (equal (sort (file-name-all-completions "b" tmp-name
) 'string-lessp
)
1212 (ignore-errors (delete-directory tmp-name
'recursive
)))))
1214 (ert-deftest tramp-test25-load
()
1216 (skip-unless (tramp--test-enabled))
1218 (let ((tmp-name (tramp--test-make-temp-name)))
1221 (load tmp-name
'noerror
'nomessage
)
1222 (should-not (featurep 'tramp-test-load
))
1223 (write-region "(provide 'tramp-test-load)" nil tmp-name
)
1224 ;; `load' in lread.c does not pass `must-suffix'. Why?
1225 ;(should-error (load tmp-name nil 'nomessage 'nosuffix 'must-suffix))
1226 (load tmp-name nil
'nomessage
'nosuffix
)
1227 (should (featurep 'tramp-test-load
)))
1229 (and (featurep 'tramp-test-load
) (unload-feature 'tramp-test-load
))
1230 (delete-file tmp-name
)))))
1232 (ert-deftest tramp-test26-process-file
()
1233 "Check `process-file'."
1234 (skip-unless (tramp--test-enabled))
1238 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory
)
1239 '(tramp-gvfs-file-name-handler tramp-smb-file-name-handler
))))
1241 (let ((tmp-name (tramp--test-make-temp-name))
1242 (default-directory tramp-test-temporary-file-directory
)
1243 kill-buffer-query-functions
)
1246 ;; We cannot use "/bin/true" and "/bin/false"; those paths
1247 ;; do not exist on hydra.
1248 (should (zerop (process-file "true")))
1249 (should-not (zerop (process-file "false")))
1250 (should-not (zerop (process-file "binary-does-not-exist")))
1252 (write-region "foo" nil tmp-name
)
1253 (should (file-exists-p tmp-name
))
1256 (process-file "ls" nil t nil
(file-name-nondirectory tmp-name
))))
1257 ;; `ls' could produce colorized output.
1258 (goto-char (point-min))
1259 (while (re-search-forward tramp-color-escape-sequence-regexp nil t
)
1260 (replace-match "" nil nil
))
1263 (format "%s\n" (file-name-nondirectory tmp-name
))
1265 (ignore-errors (delete-file tmp-name
)))))
1267 (ert-deftest tramp-test27-start-file-process
()
1268 "Check `start-file-process'."
1269 (skip-unless (tramp--test-enabled))
1273 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory
)
1274 '(tramp-adb-file-name-handler
1275 tramp-gvfs-file-name-handler
1276 tramp-smb-file-name-handler
))))
1278 (let ((default-directory tramp-test-temporary-file-directory
)
1279 (tmp-name (tramp--test-make-temp-name))
1280 kill-buffer-query-functions proc
)
1283 (setq proc
(start-file-process "test1" (current-buffer) "cat"))
1284 (should (processp proc
))
1285 (should (equal (process-status proc
) 'run
))
1286 (process-send-string proc
"foo")
1287 (process-send-eof proc
)
1288 (accept-process-output proc
1)
1289 (should (string-equal (buffer-string) "foo")))
1290 (ignore-errors (delete-process proc
)))
1294 (write-region "foo" nil tmp-name
)
1295 (should (file-exists-p tmp-name
))
1298 "test2" (current-buffer)
1299 "cat" (file-name-nondirectory tmp-name
)))
1300 (should (processp proc
))
1301 (accept-process-output proc
1)
1302 (should (string-equal (buffer-string) "foo")))
1304 (delete-process proc
)
1305 (delete-file tmp-name
)))
1309 (setq proc
(start-file-process "test3" nil
"cat"))
1310 (should (processp proc
))
1311 (should (equal (process-status proc
) 'run
))
1313 proc
(lambda (_p s
) (should (string-equal s
"foo"))))
1314 (process-send-string proc
"foo")
1315 (process-send-eof proc
)
1316 (accept-process-output proc
1))
1317 (ignore-errors (delete-process proc
)))))
1319 (ert-deftest tramp-test28-shell-command
()
1320 "Check `shell-command'."
1321 (skip-unless (tramp--test-enabled))
1325 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory
)
1326 '(tramp-adb-file-name-handler
1327 tramp-gvfs-file-name-handler
1328 tramp-smb-file-name-handler
))))
1330 (let ((tmp-name (tramp--test-make-temp-name))
1331 (default-directory tramp-test-temporary-file-directory
)
1332 kill-buffer-query-functions
)
1335 (write-region "foo" nil tmp-name
)
1336 (should (file-exists-p tmp-name
))
1338 (format "ls %s" (file-name-nondirectory tmp-name
)) (current-buffer))
1339 ;; `ls' could produce colorized output.
1340 (goto-char (point-min))
1341 (while (re-search-forward tramp-color-escape-sequence-regexp nil t
)
1342 (replace-match "" nil nil
))
1345 (format "%s\n" (file-name-nondirectory tmp-name
)) (buffer-string))))
1346 (ignore-errors (delete-file tmp-name
)))
1350 (write-region "foo" nil tmp-name
)
1351 (should (file-exists-p tmp-name
))
1352 (async-shell-command
1353 (format "ls %s" (file-name-nondirectory tmp-name
)) (current-buffer))
1354 (accept-process-output (get-buffer-process (current-buffer)) 1)
1355 (with-timeout (10 (ert-fail "`async-shell-command' timed out"))
1358 (memq (process-status (get-buffer-process (current-buffer)))
1360 (accept-process-output (get-buffer-process (current-buffer)) 1)))
1361 ;; `ls' could produce colorized output.
1362 (goto-char (point-min))
1363 (while (re-search-forward tramp-color-escape-sequence-regexp nil t
)
1364 (replace-match "" nil nil
))
1367 (format "%s\n" (file-name-nondirectory tmp-name
)) (buffer-string))))
1368 (ignore-errors (delete-file tmp-name
)))
1372 (write-region "foo" nil tmp-name
)
1373 (should (file-exists-p tmp-name
))
1374 (async-shell-command "read line; ls $line" (current-buffer))
1375 (process-send-string
1376 (get-buffer-process (current-buffer))
1377 (format "%s\n" (file-name-nondirectory tmp-name
)))
1378 (accept-process-output (get-buffer-process (current-buffer)) 1)
1379 (with-timeout (10 (ert-fail "`async-shell-command' timed out"))
1382 (memq (process-status (get-buffer-process (current-buffer)))
1384 (accept-process-output (get-buffer-process (current-buffer)) 1)))
1387 (format "%s\n" (file-name-nondirectory tmp-name
)) (buffer-string))))
1388 (ignore-errors (delete-file tmp-name
)))))
1390 (ert-deftest tramp-test29-vc-registered
()
1391 "Check `vc-registered'."
1392 (skip-unless (tramp--test-enabled))
1395 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory
)
1396 'tramp-sh-file-name-handler
))
1398 (let* ((default-directory tramp-test-temporary-file-directory
)
1399 (tmp-name1 (tramp--test-make-temp-name))
1400 (tmp-name2 (expand-file-name "foo" tmp-name1
))
1401 (tramp-remote-process-environment tramp-remote-process-environment
)
1402 (vc-handled-backends
1403 (with-parsed-tramp-file-name tramp-test-temporary-file-directory nil
1405 ((tramp-find-executable v vc-bzr-program
(tramp-get-remote-path v
))
1406 (setq tramp-remote-process-environment
1407 (cons (format "BZR_HOME=%s"
1408 (file-remote-p tmp-name1
'localname
))
1409 tramp-remote-process-environment
))
1410 ;; We must force a reconnect, in order to activate $BZR_HOME.
1411 (tramp-cleanup-connection
1412 (tramp-dissect-file-name tramp-test-temporary-file-directory
)
1415 ((tramp-find-executable v vc-git-program
(tramp-get-remote-path v
))
1417 ((tramp-find-executable v vc-hg-program
(tramp-get-remote-path v
))
1420 (skip-unless vc-handled-backends
)
1421 (message "%s" vc-handled-backends
)
1425 (make-directory tmp-name1
)
1426 (write-region "foo" nil tmp-name2
)
1427 (should (file-directory-p tmp-name1
))
1428 (should (file-exists-p tmp-name2
))
1429 (should-not (vc-registered tmp-name1
))
1430 (should-not (vc-registered tmp-name2
))
1432 (let ((default-directory tmp-name1
))
1433 ;; Create empty repository, and register the file.
1434 (vc-create-repo (car vc-handled-backends
))
1435 ;; The structure of VC-FILESET is not documented. Let's
1436 ;; hope it won't change.
1438 nil
(list (car vc-handled-backends
)
1439 (list (file-name-nondirectory tmp-name2
)))))
1440 (should (vc-registered tmp-name2
)))
1442 (ignore-errors (delete-directory tmp-name1
'recursive
)))))
1444 (defun tramp--test-check-files (&rest files
)
1445 "Runs a simple but comprehensive test over every file in FILES."
1446 (let ((tmp-name1 (tramp--test-make-temp-name))
1447 (tmp-name2 (tramp--test-make-temp-name 'local
)))
1450 (make-directory tmp-name1
)
1451 (make-directory tmp-name2
)
1453 (let ((file1 (expand-file-name elt tmp-name1
))
1454 (file2 (expand-file-name elt tmp-name2
)))
1455 (write-region elt nil file1
)
1456 (should (file-exists-p file1
))
1457 ;; Check file contents.
1459 (insert-file-contents file1
)
1460 (should (string-equal (buffer-string) elt
)))
1461 ;; Copy file both directions.
1462 (copy-file file1 tmp-name2
)
1463 (should (file-exists-p file2
))
1465 (should-not (file-exists-p file1
))
1466 (copy-file file2 tmp-name1
)
1467 (should (file-exists-p file1
))))
1468 ;; Check file names.
1469 (should (equal (directory-files
1470 tmp-name1 nil directory-files-no-dot-files-regexp
)
1471 (sort (copy-sequence files
) 'string-lessp
)))
1472 (should (equal (directory-files
1473 tmp-name2 nil directory-files-no-dot-files-regexp
)
1474 (sort files
'string-lessp
))))
1475 (ignore-errors (delete-directory tmp-name1
'recursive
))
1476 (ignore-errors (delete-directory tmp-name2
'recursive
)))))
1478 ;; This test is inspired by Bug#17238.
1479 (ert-deftest tramp-test30-special-characters
()
1480 "Check special characters in file names."
1481 (skip-unless (tramp--test-enabled))
1483 ;; Newlines, slashes and backslashes in file names are not supported.
1484 ;; So we don't test.
1485 (tramp--test-check-files
1500 (ert-deftest tramp-test31-utf8
()
1501 "Check UTF8 encoding in file names and file contents."
1502 (skip-unless (tramp--test-enabled))
1504 (let ((coding-system-for-read 'utf-8
)
1505 (coding-system-for-write 'utf-8
)
1506 (file-name-coding-system 'utf-8
))
1507 (tramp--test-check-files
1508 "أصبح بوسعك الآن تنزيل نسخة كاملة من موسوعة ويكيبيديا العربية لتصفحها بلا اتصال بالإنترنت"
1510 "Автостопом по гала́ктике")))
1512 ;; This test is inspired by Bug#16928.
1513 (ert-deftest tramp-test32-asynchronous-requests
()
1514 "Check parallel asynchronous requests.
1515 Such requests could arrive from timers, process filters and
1516 process sentinels. They shall not disturb each other."
1517 ;; Mark as failed until bug has been fixed.
1518 :expected-result
:failed
1519 (skip-unless (tramp--test-enabled))
1522 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory
)
1523 'tramp-sh-file-name-handler
))
1525 ;; Keep instrumentation verbosity 0 until Tramp bug is fixed. This
1526 ;; has the side effect, that this test fails instead to abort. Good
1528 (tramp--instrument-test-case 0
1529 (let* ((tmp-name (tramp--test-make-temp-name))
1530 (default-directory tmp-name
)
1531 (remote-file-name-inhibit-cache t
)
1532 timer buffers kill-buffer-query-functions
)
1536 (make-directory tmp-name
)
1538 ;; Setup a timer in order to raise an ordinary command again
1539 ;; and again. `vc-registered' is well suited, because there
1548 (buffer-name (nth (random (length buffers
)) buffers
)))))))
1550 ;; Create temporary buffers. The number of buffers
1551 ;; corresponds to the number of processes; it could be
1552 ;; increased in order to make pressure on Tramp.
1554 (add-to-list 'buffers
(generate-new-buffer "*temp*")))
1556 ;; Open asynchronous processes. Set process sentinel.
1557 (dolist (buf buffers
)
1558 (async-shell-command "read line; touch $line; echo $line" buf
)
1559 (set-process-sentinel
1560 (get-buffer-process buf
)
1561 (lambda (proc _state
)
1562 (delete-file (buffer-name (process-buffer proc
))))))
1564 ;; Send a string. Use a random order of the buffers. Mix
1565 ;; with regular operation.
1566 (let ((buffers (copy-sequence buffers
))
1569 (setq buf
(nth (random (length buffers
)) buffers
))
1570 (process-send-string
1571 (get-buffer-process buf
) (format "'%s'\n" buf
))
1572 (file-attributes (buffer-name buf
))
1573 (setq buffers
(delq buf buffers
))))
1575 ;; Wait until the whole output has been read.
1576 (with-timeout ((* 10 (length buffers
))
1577 (ert-fail "`async-shell-command' timed out"))
1578 (let ((buffers (copy-sequence buffers
))
1581 (setq buf
(nth (random (length buffers
)) buffers
))
1583 (memq (process-status (get-buffer-process buf
))
1585 (accept-process-output (get-buffer-process buf
) 0.1)
1586 (setq buffers
(delq buf buffers
))))))
1589 (dolist (buf buffers
)
1590 (with-current-buffer buf
1592 (string-equal (format "'%s'\n" buf
) (buffer-string)))))
1594 (directory-files tmp-name nil directory-files-no-dot-files-regexp
)))
1597 (ignore-errors (cancel-timer timer
))
1598 (ignore-errors (delete-directory tmp-name
'recursive
))
1599 (dolist (buf buffers
)
1600 (ignore-errors (kill-buffer buf
)))))))
1602 (ert-deftest tramp-test33-recursive-load
()
1603 "Check that Tramp does not fail due to recursive load."
1604 (skip-unless (tramp--test-enabled))
1609 "(expand-file-name %S)"
1610 tramp-test-temporary-file-directory
)
1612 "(let ((default-directory %S)) (expand-file-name %S))"
1613 tramp-test-temporary-file-directory
1614 temporary-file-directory
)))
1618 (shell-command-to-string
1620 "%s -batch -Q -L %s --eval %s"
1621 (expand-file-name invocation-name invocation-directory
)
1622 (mapconcat 'shell-quote-argument load-path
" -L ")
1623 (shell-quote-argument code
)))))))
1625 (ert-deftest tramp-test34-unload
()
1626 "Check that Tramp and its subpackages unload completely.
1627 Since it unloads Tramp, it shall be the last test to run."
1628 ;; Mark as failed until all symbols are unbound.
1629 :expected-result
(if (featurep 'tramp
) :failed
:passed
)
1630 (when (featurep 'tramp
)
1631 (unload-feature 'tramp
'force
)
1632 ;; No Tramp feature must be left.
1633 (should-not (featurep 'tramp
))
1634 (should-not (all-completions "tramp" (delq 'tramp-tests features
)))
1635 ;; `file-name-handler-alist' must be clean.
1636 (should-not (all-completions "tramp" (mapcar 'cdr file-name-handler-alist
)))
1637 ;; There shouldn't be left a bound symbol. We do not regard our
1638 ;; test symbols, and the Tramp unload hooks.
1641 (and (or (boundp x
) (functionp x
))
1642 (string-match "^tramp" (symbol-name x
))
1643 (not (string-match "^tramp--?test" (symbol-name x
)))
1644 (not (string-match "unload-hook$" (symbol-name x
)))
1645 (ert-fail (format "`%s' still bound" x
)))))
1646 ; (progn (message "`%s' still bound" x)))))
1647 ;; There shouldn't be left a hook function containing a Tramp
1648 ;; function. We do not regard the Tramp unload hooks.
1652 (string-match "-hooks?$" (symbol-name x
))
1653 (not (string-match "unload-hook$" (symbol-name x
)))
1654 (consp (symbol-value x
))
1655 (ignore-errors (all-completions "tramp" (symbol-value x
)))
1656 (ert-fail (format "Hook `%s' still contains Tramp function" x
)))))))
1660 ;; * dired-compress-file
1663 ;; * file-ownership-preserved-p
1664 ;; * file-selinux-context
1665 ;; * find-backup-file-name
1666 ;; * make-auto-save-file-name
1668 ;; * set-file-selinux-context
1670 ;; * Fix `tramp-test27-start-file-process' on MS Windows (`process-send-eof'?).
1671 ;; * Fix `tramp-test28-shell-command' on MS Windows (nasty plink message).
1672 ;; * Fix `tramp-test31-utf8' for MS Windows and `nc'/`telnet' (when
1673 ;; target is a dumb busybox). Seems to be in `directory-files'.
1674 ;; * Fix Bug#16928. Set expected error of `tramp-test32-asynchronous-requests'.
1675 ;; * Fix `tramp-test34-unload' (Not all symbols are unbound). Set
1678 (defun tramp-test-all (&optional interactive
)
1679 "Run all tests for \\[tramp]."
1682 (if interactive
'ert-run-tests-interactively
'ert-run-tests-batch
) "^tramp"))
1684 (provide 'tramp-tests
)
1685 ;;; tramp-tests.el ends here