* automated/tramp-tests.el (tramp--instrument-test-case): New macro.
[bpt/emacs.git] / test / automated / tramp-tests.el
1 ;;; tramp-tests.el --- Tests of remote file access
2
3 ;; Copyright (C) 2013-2014 Free Software Foundation, Inc.
4
5 ;; Author: Michael Albinus <michael.albinus@gmx.de>
6
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.
11 ;;
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.
16 ;;
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/'.
19
20 ;;; Commentary:
21
22 ;; The tests require a recent ert.el from Emacs 24.4.
23
24 ;; Some of the tests require access to a remote host files. Set
25 ;; $REMOTE_TEMPORARY_FILE_DIRECTORY to a suitable value in order
26 ;; to overwrite the default value. If you want to skip tests
27 ;; accessing a remote host, set this environment variable to
28 ;; "/dev/null" or whatever is appropriate on your system.
29
30 ;; When running the tests in batch mode, it must NOT require an
31 ;; interactive password prompt unless the environment variable
32 ;; $REMOTE_ALLOW_PASSWORD is set.
33
34 ;; A whole test run can be performed calling the command `tramp-test-all'.
35
36 ;;; Code:
37
38 (require 'ert)
39 (require 'tramp)
40 (require 'vc)
41 (require 'vc-bzr)
42 (require 'vc-git)
43 (require 'vc-hg)
44
45 (declare-function tramp-find-executable "tramp-sh")
46 (declare-function tramp-get-remote-path "tramp-sh")
47
48 ;; There is no default value on w32 systems, which could work out of the box.
49 (defconst tramp-test-temporary-file-directory
50 (cond
51 ((getenv "REMOTE_TEMPORARY_FILE_DIRECTORY"))
52 ((eq system-type 'windows-nt) null-device)
53 (t (format "/ssh::%s" temporary-file-directory)))
54 "Temporary directory for Tramp tests.")
55
56 (setq password-cache-expiry nil
57 tramp-verbose 0
58 tramp-message-show-message nil)
59
60 ;; Disable interactive passwords in batch mode.
61 (when (and noninteractive (not (getenv "REMOTE_ALLOW_PASSWORD")))
62 (defalias 'tramp-read-passwd 'ignore))
63
64 ;; This shall happen on hydra only.
65 (when (getenv "NIX_STORE")
66 (add-to-list 'tramp-remote-path 'tramp-own-remote-path))
67
68 (defvar tramp--test-enabled-checked nil
69 "Cached result of `tramp--test-enabled'.
70 If the function did run, the value is a cons cell, the `cdr'
71 being the result.")
72
73 (defun tramp--test-enabled ()
74 "Whether remote file access is enabled."
75 (unless (consp tramp--test-enabled-checked)
76 (setq
77 tramp--test-enabled-checked
78 (cons
79 t (ignore-errors
80 (and
81 (file-remote-p tramp-test-temporary-file-directory)
82 (file-directory-p tramp-test-temporary-file-directory)
83 (file-writable-p tramp-test-temporary-file-directory))))))
84 ;; Return result.
85 (cdr tramp--test-enabled-checked))
86
87 (defun tramp--test-make-temp-name ()
88 "Create a temporary file name for test."
89 (expand-file-name
90 (make-temp-name "tramp-test") tramp-test-temporary-file-directory))
91
92 (defmacro tramp--instrument-test-case (verbose &rest body)
93 "Run BODY with `tramp-verbose' equal VERBOSE.
94 Print the the content of the Tramp debug buffer, if BODY does not
95 eval properly in `should', `should-not' or `should-error'."
96 `(let ((tramp-verbose ,verbose))
97 (condition-case err
98 (progn ,@body)
99 (ert-test-failed
100 (with-parsed-tramp-file-name tramp-test-temporary-file-directory nil
101 (with-current-buffer (tramp-get-debug-buffer v)
102 (message "%s" (buffer-string))))
103 (signal (car err) (cdr err))))))
104 (put 'tramp--instrument-test-case 'lisp-indent-function 1)
105
106 (ert-deftest tramp-test00-availability ()
107 "Test availability of Tramp functions."
108 :expected-result (if (tramp--test-enabled) :passed :failed)
109 (should (ignore-errors
110 (and
111 (file-remote-p tramp-test-temporary-file-directory)
112 (file-directory-p tramp-test-temporary-file-directory)
113 (file-writable-p tramp-test-temporary-file-directory)))))
114
115 (ert-deftest tramp-test01-file-name-syntax ()
116 "Check remote file name syntax."
117 ;; Simple cases.
118 (should (tramp-tramp-file-p "/method::"))
119 (should (tramp-tramp-file-p "/host:"))
120 (should (tramp-tramp-file-p "/user@:"))
121 (should (tramp-tramp-file-p "/user@host:"))
122 (should (tramp-tramp-file-p "/method:host:"))
123 (should (tramp-tramp-file-p "/method:user@:"))
124 (should (tramp-tramp-file-p "/method:user@host:"))
125 (should (tramp-tramp-file-p "/method:user@email@host:"))
126
127 ;; Using a port.
128 (should (tramp-tramp-file-p "/host#1234:"))
129 (should (tramp-tramp-file-p "/user@host#1234:"))
130 (should (tramp-tramp-file-p "/method:host#1234:"))
131 (should (tramp-tramp-file-p "/method:user@host#1234:"))
132
133 ;; Using an IPv4 address.
134 (should (tramp-tramp-file-p "/1.2.3.4:"))
135 (should (tramp-tramp-file-p "/user@1.2.3.4:"))
136 (should (tramp-tramp-file-p "/method:1.2.3.4:"))
137 (should (tramp-tramp-file-p "/method:user@1.2.3.4:"))
138
139 ;; Using an IPv6 address.
140 (should (tramp-tramp-file-p "/[]:"))
141 (should (tramp-tramp-file-p "/[::1]:"))
142 (should (tramp-tramp-file-p "/user@[::1]:"))
143 (should (tramp-tramp-file-p "/method:[::1]:"))
144 (should (tramp-tramp-file-p "/method:user@[::1]:"))
145
146 ;; Local file name part.
147 (should (tramp-tramp-file-p "/host:/:"))
148 (should (tramp-tramp-file-p "/method:::"))
149 (should (tramp-tramp-file-p "/method::/path/to/file"))
150 (should (tramp-tramp-file-p "/method::file"))
151
152 ;; Multihop.
153 (should (tramp-tramp-file-p "/method1:|method2::"))
154 (should (tramp-tramp-file-p "/method1:host1|host2:"))
155 (should (tramp-tramp-file-p "/method1:host1|method2:host2:"))
156 (should (tramp-tramp-file-p "/method1:user1@host1|method2:user2@host2:"))
157 (should (tramp-tramp-file-p
158 "/method1:user1@host1|method2:user2@host2|method3:user3@host3:"))
159
160 ;; No strings.
161 (should-not (tramp-tramp-file-p nil))
162 (should-not (tramp-tramp-file-p 'symbol))
163 ;; "/:" suppresses file name handlers.
164 (should-not (tramp-tramp-file-p "/::"))
165 (should-not (tramp-tramp-file-p "/:@:"))
166 (should-not (tramp-tramp-file-p "/:[]:"))
167 ;; Multihops require a method.
168 (should-not (tramp-tramp-file-p "/host1|host2:"))
169 ;; Methods or hostnames shall be at least two characters on MS Windows.
170 (when (memq system-type '(cygwin windows-nt))
171 (should-not (tramp-tramp-file-p "/c:/path/to/file"))
172 (should-not (tramp-tramp-file-p "/c::/path/to/file"))))
173
174 (ert-deftest tramp-test02-file-name-dissect ()
175 "Check remote file name components."
176 (let ((tramp-default-method "default-method")
177 (tramp-default-user "default-user")
178 (tramp-default-host "default-host"))
179 ;; Expand `tramp-default-user' and `tramp-default-host'.
180 (should (string-equal
181 (file-remote-p "/method::")
182 (format "/%s:%s@%s:" "method" "default-user" "default-host")))
183 (should (string-equal (file-remote-p "/method::" 'method) "method"))
184 (should (string-equal (file-remote-p "/method::" 'user) "default-user"))
185 (should (string-equal (file-remote-p "/method::" 'host) "default-host"))
186 (should (string-equal (file-remote-p "/method::" 'localname) ""))
187
188 ;; Expand `tramp-default-method' and `tramp-default-user'.
189 (should (string-equal
190 (file-remote-p "/host:")
191 (format "/%s:%s@%s:" "default-method" "default-user" "host")))
192 (should (string-equal (file-remote-p "/host:" 'method) "default-method"))
193 (should (string-equal (file-remote-p "/host:" 'user) "default-user"))
194 (should (string-equal (file-remote-p "/host:" 'host) "host"))
195 (should (string-equal (file-remote-p "/host:" 'localname) ""))
196
197 ;; Expand `tramp-default-method' and `tramp-default-host'.
198 (should (string-equal
199 (file-remote-p "/user@:")
200 (format "/%s:%s@%s:" "default-method""user" "default-host")))
201 (should (string-equal (file-remote-p "/user@:" 'method) "default-method"))
202 (should (string-equal (file-remote-p "/user@:" 'user) "user"))
203 (should (string-equal (file-remote-p "/user@:" 'host) "default-host"))
204 (should (string-equal (file-remote-p "/user@:" 'localname) ""))
205
206 ;; Expand `tramp-default-method'.
207 (should (string-equal
208 (file-remote-p "/user@host:")
209 (format "/%s:%s@%s:" "default-method" "user" "host")))
210 (should (string-equal
211 (file-remote-p "/user@host:" 'method) "default-method"))
212 (should (string-equal (file-remote-p "/user@host:" 'user) "user"))
213 (should (string-equal (file-remote-p "/user@host:" 'host) "host"))
214 (should (string-equal (file-remote-p "/user@host:" 'localname) ""))
215
216 ;; Expand `tramp-default-user'.
217 (should (string-equal
218 (file-remote-p "/method:host:")
219 (format "/%s:%s@%s:" "method" "default-user" "host")))
220 (should (string-equal (file-remote-p "/method:host:" 'method) "method"))
221 (should (string-equal (file-remote-p "/method:host:" 'user) "default-user"))
222 (should (string-equal (file-remote-p "/method:host:" 'host) "host"))
223 (should (string-equal (file-remote-p "/method:host:" 'localname) ""))
224
225 ;; Expand `tramp-default-host'.
226 (should (string-equal
227 (file-remote-p "/method:user@:")
228 (format "/%s:%s@%s:" "method" "user" "default-host")))
229 (should (string-equal (file-remote-p "/method:user@:" 'method) "method"))
230 (should (string-equal (file-remote-p "/method:user@:" 'user) "user"))
231 (should (string-equal (file-remote-p "/method:user@:" 'host)
232 "default-host"))
233 (should (string-equal (file-remote-p "/method:user@:" 'localname) ""))
234
235 ;; No expansion.
236 (should (string-equal
237 (file-remote-p "/method:user@host:")
238 (format "/%s:%s@%s:" "method" "user" "host")))
239 (should (string-equal
240 (file-remote-p "/method:user@host:" 'method) "method"))
241 (should (string-equal (file-remote-p "/method:user@host:" 'user) "user"))
242 (should (string-equal (file-remote-p "/method:user@host:" 'host) "host"))
243 (should (string-equal (file-remote-p "/method:user@host:" 'localname) ""))
244
245 ;; No expansion.
246 (should (string-equal
247 (file-remote-p "/method:user@email@host:")
248 (format "/%s:%s@%s:" "method" "user@email" "host")))
249 (should (string-equal
250 (file-remote-p "/method:user@email@host:" 'method) "method"))
251 (should (string-equal
252 (file-remote-p "/method:user@email@host:" 'user) "user@email"))
253 (should (string-equal
254 (file-remote-p "/method:user@email@host:" 'host) "host"))
255 (should (string-equal
256 (file-remote-p "/method:user@email@host:" 'localname) ""))
257
258 ;; Expand `tramp-default-method' and `tramp-default-user'.
259 (should (string-equal
260 (file-remote-p "/host#1234:")
261 (format "/%s:%s@%s:" "default-method" "default-user" "host#1234")))
262 (should (string-equal
263 (file-remote-p "/host#1234:" 'method) "default-method"))
264 (should (string-equal (file-remote-p "/host#1234:" 'user) "default-user"))
265 (should (string-equal (file-remote-p "/host#1234:" 'host) "host#1234"))
266 (should (string-equal (file-remote-p "/host#1234:" 'localname) ""))
267
268 ;; Expand `tramp-default-method'.
269 (should (string-equal
270 (file-remote-p "/user@host#1234:")
271 (format "/%s:%s@%s:" "default-method" "user" "host#1234")))
272 (should (string-equal
273 (file-remote-p "/user@host#1234:" 'method) "default-method"))
274 (should (string-equal (file-remote-p "/user@host#1234:" 'user) "user"))
275 (should (string-equal (file-remote-p "/user@host#1234:" 'host) "host#1234"))
276 (should (string-equal (file-remote-p "/user@host#1234:" 'localname) ""))
277
278 ;; Expand `tramp-default-user'.
279 (should (string-equal
280 (file-remote-p "/method:host#1234:")
281 (format "/%s:%s@%s:" "method" "default-user" "host#1234")))
282 (should (string-equal
283 (file-remote-p "/method:host#1234:" 'method) "method"))
284 (should (string-equal
285 (file-remote-p "/method:host#1234:" 'user) "default-user"))
286 (should (string-equal
287 (file-remote-p "/method:host#1234:" 'host) "host#1234"))
288 (should (string-equal (file-remote-p "/method:host#1234:" 'localname) ""))
289
290 ;; No expansion.
291 (should (string-equal
292 (file-remote-p "/method:user@host#1234:")
293 (format "/%s:%s@%s:" "method" "user" "host#1234")))
294 (should (string-equal
295 (file-remote-p "/method:user@host#1234:" 'method) "method"))
296 (should (string-equal
297 (file-remote-p "/method:user@host#1234:" 'user) "user"))
298 (should (string-equal
299 (file-remote-p "/method:user@host#1234:" 'host) "host#1234"))
300 (should (string-equal
301 (file-remote-p "/method:user@host#1234:" 'localname) ""))
302
303 ;; Expand `tramp-default-method' and `tramp-default-user'.
304 (should (string-equal
305 (file-remote-p "/1.2.3.4:")
306 (format "/%s:%s@%s:" "default-method" "default-user" "1.2.3.4")))
307 (should (string-equal (file-remote-p "/1.2.3.4:" 'method) "default-method"))
308 (should (string-equal (file-remote-p "/1.2.3.4:" 'user) "default-user"))
309 (should (string-equal (file-remote-p "/1.2.3.4:" 'host) "1.2.3.4"))
310 (should (string-equal (file-remote-p "/1.2.3.4:" 'localname) ""))
311
312 ;; Expand `tramp-default-method'.
313 (should (string-equal
314 (file-remote-p "/user@1.2.3.4:")
315 (format "/%s:%s@%s:" "default-method" "user" "1.2.3.4")))
316 (should (string-equal
317 (file-remote-p "/user@1.2.3.4:" 'method) "default-method"))
318 (should (string-equal (file-remote-p "/user@1.2.3.4:" 'user) "user"))
319 (should (string-equal (file-remote-p "/user@1.2.3.4:" 'host) "1.2.3.4"))
320 (should (string-equal (file-remote-p "/user@1.2.3.4:" 'localname) ""))
321
322 ;; Expand `tramp-default-user'.
323 (should (string-equal
324 (file-remote-p "/method:1.2.3.4:")
325 (format "/%s:%s@%s:" "method" "default-user" "1.2.3.4")))
326 (should (string-equal (file-remote-p "/method:1.2.3.4:" 'method) "method"))
327 (should (string-equal
328 (file-remote-p "/method:1.2.3.4:" 'user) "default-user"))
329 (should (string-equal (file-remote-p "/method:1.2.3.4:" 'host) "1.2.3.4"))
330 (should (string-equal (file-remote-p "/method:1.2.3.4:" 'localname) ""))
331
332 ;; No expansion.
333 (should (string-equal
334 (file-remote-p "/method:user@1.2.3.4:")
335 (format "/%s:%s@%s:" "method" "user" "1.2.3.4")))
336 (should (string-equal
337 (file-remote-p "/method:user@1.2.3.4:" 'method) "method"))
338 (should (string-equal (file-remote-p "/method:user@1.2.3.4:" 'user) "user"))
339 (should (string-equal
340 (file-remote-p "/method:user@1.2.3.4:" 'host) "1.2.3.4"))
341 (should (string-equal
342 (file-remote-p "/method:user@1.2.3.4:" 'localname) ""))
343
344 ;; Expand `tramp-default-method', `tramp-default-user' and
345 ;; `tramp-default-host'.
346 (should (string-equal
347 (file-remote-p "/[]:")
348 (format
349 "/%s:%s@%s:" "default-method" "default-user" "default-host")))
350 (should (string-equal (file-remote-p "/[]:" 'method) "default-method"))
351 (should (string-equal (file-remote-p "/[]:" 'user) "default-user"))
352 (should (string-equal (file-remote-p "/[]:" 'host) "default-host"))
353 (should (string-equal (file-remote-p "/[]:" 'localname) ""))
354
355 ;; Expand `tramp-default-method' and `tramp-default-user'.
356 (let ((tramp-default-host "::1"))
357 (should (string-equal
358 (file-remote-p "/[]:")
359 (format "/%s:%s@%s:" "default-method" "default-user" "[::1]")))
360 (should (string-equal (file-remote-p "/[]:" 'method) "default-method"))
361 (should (string-equal (file-remote-p "/[]:" 'user) "default-user"))
362 (should (string-equal (file-remote-p "/[]:" 'host) "::1"))
363 (should (string-equal (file-remote-p "/[]:" 'localname) "")))
364
365 ;; Expand `tramp-default-method' and `tramp-default-user'.
366 (should (string-equal
367 (file-remote-p "/[::1]:")
368 (format "/%s:%s@%s:" "default-method" "default-user" "[::1]")))
369 (should (string-equal (file-remote-p "/[::1]:" 'method) "default-method"))
370 (should (string-equal (file-remote-p "/[::1]:" 'user) "default-user"))
371 (should (string-equal (file-remote-p "/[::1]:" 'host) "::1"))
372 (should (string-equal (file-remote-p "/[::1]:" 'localname) ""))
373
374 ;; Expand `tramp-default-method'.
375 (should (string-equal
376 (file-remote-p "/user@[::1]:")
377 (format "/%s:%s@%s:" "default-method" "user" "[::1]")))
378 (should (string-equal
379 (file-remote-p "/user@[::1]:" 'method) "default-method"))
380 (should (string-equal (file-remote-p "/user@[::1]:" 'user) "user"))
381 (should (string-equal (file-remote-p "/user@[::1]:" 'host) "::1"))
382 (should (string-equal (file-remote-p "/user@[::1]:" 'localname) ""))
383
384 ;; Expand `tramp-default-user'.
385 (should (string-equal
386 (file-remote-p "/method:[::1]:")
387 (format "/%s:%s@%s:" "method" "default-user" "[::1]")))
388 (should (string-equal (file-remote-p "/method:[::1]:" 'method) "method"))
389 (should (string-equal
390 (file-remote-p "/method:[::1]:" 'user) "default-user"))
391 (should (string-equal (file-remote-p "/method:[::1]:" 'host) "::1"))
392 (should (string-equal (file-remote-p "/method:[::1]:" 'localname) ""))
393
394 ;; No expansion.
395 (should (string-equal
396 (file-remote-p "/method:user@[::1]:")
397 (format "/%s:%s@%s:" "method" "user" "[::1]")))
398 (should (string-equal
399 (file-remote-p "/method:user@[::1]:" 'method) "method"))
400 (should (string-equal (file-remote-p "/method:user@[::1]:" 'user) "user"))
401 (should (string-equal (file-remote-p "/method:user@[::1]:" 'host) "::1"))
402 (should (string-equal
403 (file-remote-p "/method:user@[::1]:" 'localname) ""))
404
405 ;; Local file name part.
406 (should (string-equal (file-remote-p "/host:/:" 'localname) "/:"))
407 (should (string-equal (file-remote-p "/method:::" 'localname) ":"))
408 (should (string-equal (file-remote-p "/method:: " 'localname) " "))
409 (should (string-equal (file-remote-p "/method::file" 'localname) "file"))
410 (should (string-equal
411 (file-remote-p "/method::/path/to/file" 'localname)
412 "/path/to/file"))
413
414 ;; Multihop.
415 (should
416 (string-equal
417 (file-remote-p "/method1:user1@host1|method2:user2@host2:/path/to/file")
418 (format "/%s:%s@%s:" "method2" "user2" "host2")))
419 (should
420 (string-equal
421 (file-remote-p
422 "/method1:user1@host1|method2:user2@host2:/path/to/file" 'method)
423 "method2"))
424 (should
425 (string-equal
426 (file-remote-p
427 "/method1:user1@host1|method2:user2@host2:/path/to/file" 'user)
428 "user2"))
429 (should
430 (string-equal
431 (file-remote-p
432 "/method1:user1@host1|method2:user2@host2:/path/to/file" 'host)
433 "host2"))
434 (should
435 (string-equal
436 (file-remote-p
437 "/method1:user1@host1|method2:user2@host2:/path/to/file" 'localname)
438 "/path/to/file"))
439
440 (should
441 (string-equal
442 (file-remote-p
443 "/method1:user1@host1|method2:user2@host2|method3:user3@host3:/path/to/file")
444 (format "/%s:%s@%s:" "method3" "user3" "host3")))
445 (should
446 (string-equal
447 (file-remote-p
448 "/method1:user1@host1|method2:user2@host2|method3:user3@host3:/path/to/file"
449 'method)
450 "method3"))
451 (should
452 (string-equal
453 (file-remote-p
454 "/method1:user1@host1|method2:user2@host2|method3:user3@host3:/path/to/file"
455 'user)
456 "user3"))
457 (should
458 (string-equal
459 (file-remote-p
460 "/method1:user1@host1|method2:user2@host2|method3:user3@host3:/path/to/file"
461 'host)
462 "host3"))
463 (should
464 (string-equal
465 (file-remote-p
466 "/method1:user1@host1|method2:user2@host2|method3:user3@host3:/path/to/file"
467 'localname)
468 "/path/to/file"))))
469
470 (ert-deftest tramp-test03-file-name-defaults ()
471 "Check default values for some methods."
472 ;; Default values in tramp-adb.el.
473 (should (string-equal (file-remote-p "/adb::" 'host) ""))
474 ;; Default values in tramp-ftp.el.
475 (should (string-equal (file-remote-p "/ftp.host:" 'method) "ftp"))
476 (dolist (u '("ftp" "anonymous"))
477 (should (string-equal (file-remote-p (format "/%s@:" u) 'method) "ftp")))
478 ;; Default values in tramp-gvfs.el.
479 (when (and (load "tramp-gvfs" 'noerror 'nomessage)
480 (symbol-value 'tramp-gvfs-enabled))
481 (should (string-equal (file-remote-p "/synce::" 'user) nil)))
482 ;; Default values in tramp-gw.el.
483 (dolist (m '("tunnel" "socks"))
484 (should
485 (string-equal (file-remote-p (format "/%s::" m) 'user) (user-login-name))))
486 ;; Default values in tramp-sh.el.
487 (dolist (h `("127.0.0.1" "[::1]" "localhost" "localhost6" ,(system-name)))
488 (should (string-equal (file-remote-p (format "/root@%s:" h) 'method) "su")))
489 (dolist (m '("su" "sudo" "ksu"))
490 (should (string-equal (file-remote-p (format "/%s::" m) 'user) "root")))
491 (dolist (m '("rcp" "remcp" "rsh" "telnet" "krlogin" "fcp"))
492 (should
493 (string-equal (file-remote-p (format "/%s::" m) 'user) (user-login-name))))
494 ;; Default values in tramp-smb.el.
495 (should (string-equal (file-remote-p "/user%domain@host:" 'method) "smb"))
496 (should (string-equal (file-remote-p "/smb::" 'user) nil)))
497
498 (ert-deftest tramp-test04-substitute-in-file-name ()
499 "Check `substitute-in-file-name'."
500 (should (string-equal (substitute-in-file-name "/method:host://foo") "/foo"))
501 (should
502 (string-equal
503 (substitute-in-file-name "/method:host:/path//foo") "/method:host:/foo"))
504 (should
505 (string-equal (substitute-in-file-name "/method:host:/path///foo") "/foo"))
506 (should
507 (string-equal
508 (substitute-in-file-name "/method:host:/path/~/foo") "/method:host:~/foo"))
509 (should
510 (string-equal (substitute-in-file-name "/method:host:/path//~/foo") "~/foo"))
511 (let (process-environment)
512 (should
513 (string-equal
514 (substitute-in-file-name "/method:host:/path/$FOO")
515 "/method:host:/path/$FOO"))
516 (setenv "FOO" "bla")
517 (should
518 (string-equal
519 (substitute-in-file-name "/method:host:/path/$FOO")
520 "/method:host:/path/bla"))
521 (should
522 (string-equal
523 (substitute-in-file-name "/method:host:/path/$$FOO")
524 "/method:host:/path/$FOO"))))
525
526 (ert-deftest tramp-test05-expand-file-name ()
527 "Check `expand-file-name'."
528 (should
529 (string-equal
530 (expand-file-name "/method:host:/path/./file") "/method:host:/path/file"))
531 (should
532 (string-equal
533 (expand-file-name "/method:host:/path/../file") "/method:host:/file")))
534
535 (ert-deftest tramp-test06-directory-file-name ()
536 "Check `directory-file-name'.
537 This checks also `file-name-as-directory', `file-name-directory'
538 and `file-name-nondirectory'."
539 (should
540 (string-equal
541 (directory-file-name "/method:host:/path/to/file")
542 "/method:host:/path/to/file"))
543 (should
544 (string-equal
545 (directory-file-name "/method:host:/path/to/file/")
546 "/method:host:/path/to/file"))
547 (should
548 (string-equal
549 (file-name-as-directory "/method:host:/path/to/file")
550 "/method:host:/path/to/file/"))
551 (should
552 (string-equal
553 (file-name-as-directory "/method:host:/path/to/file/")
554 "/method:host:/path/to/file/"))
555 (should
556 (string-equal
557 (file-name-directory "/method:host:/path/to/file")
558 "/method:host:/path/to/"))
559 (should
560 (string-equal
561 (file-name-directory "/method:host:/path/to/file/")
562 "/method:host:/path/to/file/"))
563 (should
564 (string-equal (file-name-nondirectory "/method:host:/path/to/file") "file"))
565 (should
566 (string-equal (file-name-nondirectory "/method:host:/path/to/file/") ""))
567 (should-not
568 (file-remote-p
569 (unhandled-file-name-directory "/method:host:/path/to/file"))))
570
571 (ert-deftest tramp-test07-file-exists-p ()
572 "Check `file-exist-p', `write-region' and `delete-file'."
573 (skip-unless (tramp--test-enabled))
574 (tramp-cleanup-connection
575 (tramp-dissect-file-name tramp-test-temporary-file-directory)
576 nil 'keep-password)
577
578 (let ((tmp-name (tramp--test-make-temp-name)))
579 (should-not (file-exists-p tmp-name))
580 (write-region "foo" nil tmp-name)
581 (should (file-exists-p tmp-name))
582 (delete-file tmp-name)
583 (should-not (file-exists-p tmp-name))))
584
585 (ert-deftest tramp-test08-file-local-copy ()
586 "Check `file-local-copy'."
587 (skip-unless (tramp--test-enabled))
588 (tramp-cleanup-connection
589 (tramp-dissect-file-name tramp-test-temporary-file-directory)
590 nil 'keep-password)
591
592 (let ((tmp-name1 (tramp--test-make-temp-name))
593 tmp-name2)
594 (unwind-protect
595 (progn
596 (write-region "foo" nil tmp-name1)
597 (should (setq tmp-name2 (file-local-copy tmp-name1)))
598 (with-temp-buffer
599 (insert-file-contents tmp-name2)
600 (should (string-equal (buffer-string) "foo"))))
601 (ignore-errors
602 (delete-file tmp-name1)
603 (delete-file tmp-name2)))))
604
605 (ert-deftest tramp-test09-insert-file-contents ()
606 "Check `insert-file-contents'."
607 (skip-unless (tramp--test-enabled))
608 (tramp-cleanup-connection
609 (tramp-dissect-file-name tramp-test-temporary-file-directory)
610 nil 'keep-password)
611
612 (let ((tmp-name (tramp--test-make-temp-name)))
613 (unwind-protect
614 (progn
615 (write-region "foo" nil tmp-name)
616 (with-temp-buffer
617 (insert-file-contents tmp-name)
618 (should (string-equal (buffer-string) "foo"))
619 (insert-file-contents tmp-name)
620 (should (string-equal (buffer-string) "foofoo"))
621 ;; Insert partly.
622 (insert-file-contents tmp-name nil 1 3)
623 (should (string-equal (buffer-string) "oofoofoo"))
624 ;; Replace.
625 (insert-file-contents tmp-name nil nil nil 'replace)
626 (should (string-equal (buffer-string) "foo"))))
627 (ignore-errors (delete-file tmp-name)))))
628
629 (ert-deftest tramp-test10-write-region ()
630 "Check `write-region'."
631 (skip-unless (tramp--test-enabled))
632 (tramp-cleanup-connection
633 (tramp-dissect-file-name tramp-test-temporary-file-directory)
634 nil 'keep-password)
635
636 (let ((tmp-name (tramp--test-make-temp-name)))
637 (unwind-protect
638 (progn
639 (with-temp-buffer
640 (insert "foo")
641 (write-region nil nil tmp-name))
642 (with-temp-buffer
643 (insert-file-contents tmp-name)
644 (should (string-equal (buffer-string) "foo")))
645 ;; Append.
646 (with-temp-buffer
647 (insert "bla")
648 (write-region nil nil tmp-name 'append))
649 (with-temp-buffer
650 (insert-file-contents tmp-name)
651 (should (string-equal (buffer-string) "foobla")))
652 ;; Write string.
653 (write-region "foo" nil tmp-name)
654 (with-temp-buffer
655 (insert-file-contents tmp-name)
656 (should (string-equal (buffer-string) "foo")))
657 ;; Write partly.
658 (with-temp-buffer
659 (insert "123456789")
660 (write-region 3 5 tmp-name))
661 (with-temp-buffer
662 (insert-file-contents tmp-name)
663 (should (string-equal (buffer-string) "34"))))
664 (ignore-errors (delete-file tmp-name)))))
665
666 (ert-deftest tramp-test11-copy-file ()
667 "Check `copy-file'."
668 (skip-unless (tramp--test-enabled))
669 (tramp-cleanup-connection
670 (tramp-dissect-file-name tramp-test-temporary-file-directory)
671 nil 'keep-password)
672
673 (let ((tmp-name1 (tramp--test-make-temp-name))
674 (tmp-name2 (tramp--test-make-temp-name)))
675 (unwind-protect
676 (progn
677 (write-region "foo" nil tmp-name1)
678 (copy-file tmp-name1 tmp-name2)
679 (should (file-exists-p tmp-name2))
680 (with-temp-buffer
681 (insert-file-contents tmp-name2)
682 (should (string-equal (buffer-string) "foo"))))
683 (ignore-errors
684 (delete-file tmp-name1)
685 (delete-file tmp-name2)))))
686
687 (ert-deftest tramp-test12-rename-file ()
688 "Check `rename-file'."
689 (skip-unless (tramp--test-enabled))
690 (tramp-cleanup-connection
691 (tramp-dissect-file-name tramp-test-temporary-file-directory)
692 nil 'keep-password)
693
694 (let ((tmp-name1 (tramp--test-make-temp-name))
695 (tmp-name2 (tramp--test-make-temp-name)))
696 (unwind-protect
697 (progn
698 (write-region "foo" nil tmp-name1)
699 (rename-file tmp-name1 tmp-name2)
700 (should-not (file-exists-p tmp-name1))
701 (should (file-exists-p tmp-name2))
702 (with-temp-buffer
703 (insert-file-contents tmp-name2)
704 (should (string-equal (buffer-string) "foo"))))
705 (ignore-errors (delete-file tmp-name2)))))
706
707 (ert-deftest tramp-test13-make-directory ()
708 "Check `make-directory'.
709 This tests also `file-directory-p' and `file-accessible-directory-p'."
710 (skip-unless (tramp--test-enabled))
711 (tramp-cleanup-connection
712 (tramp-dissect-file-name tramp-test-temporary-file-directory)
713 nil 'keep-password)
714
715 (let ((tmp-name (tramp--test-make-temp-name)))
716 (unwind-protect
717 (progn
718 (make-directory tmp-name)
719 (should (file-directory-p tmp-name))
720 (should (file-accessible-directory-p tmp-name)))
721 (ignore-errors (delete-directory tmp-name)))))
722
723 (ert-deftest tramp-test14-delete-directory ()
724 "Check `delete-directory'."
725 (skip-unless (tramp--test-enabled))
726 (tramp-cleanup-connection
727 (tramp-dissect-file-name tramp-test-temporary-file-directory)
728 nil 'keep-password)
729
730 (let ((tmp-name (tramp--test-make-temp-name)))
731 ;; Delete empty directory.
732 (make-directory tmp-name)
733 (should (file-directory-p tmp-name))
734 (delete-directory tmp-name)
735 (should-not (file-directory-p tmp-name))
736 ;; Delete non-empty directory.
737 (make-directory tmp-name)
738 (write-region "foo" nil (expand-file-name "bla" tmp-name))
739 (should-error (delete-directory tmp-name))
740 (delete-directory tmp-name 'recursive)
741 (should-not (file-directory-p tmp-name))))
742
743 (ert-deftest tramp-test15-copy-directory ()
744 "Check `copy-directory'."
745 (skip-unless (tramp--test-enabled))
746 (tramp-cleanup-connection
747 (tramp-dissect-file-name tramp-test-temporary-file-directory)
748 nil 'keep-password)
749
750 (let* ((tmp-name1 (tramp--test-make-temp-name))
751 (tmp-name2 (tramp--test-make-temp-name))
752 (tmp-name3 (expand-file-name
753 (file-name-nondirectory tmp-name1) tmp-name2))
754 (tmp-name4 (expand-file-name "foo" tmp-name1))
755 (tmp-name5 (expand-file-name "foo" tmp-name2))
756 (tmp-name6 (expand-file-name "foo" tmp-name3)))
757 (unwind-protect
758 (progn
759 ;; Copy empty directory.
760 (make-directory tmp-name1)
761 (write-region "foo" nil tmp-name4)
762 (should (file-directory-p tmp-name1))
763 (should (file-exists-p tmp-name4))
764 (copy-directory tmp-name1 tmp-name2)
765 (should (file-directory-p tmp-name2))
766 (should (file-exists-p tmp-name5))
767 ;; Target directory does exist already.
768 (copy-directory tmp-name1 tmp-name2)
769 (should (file-directory-p tmp-name3))
770 (should (file-exists-p tmp-name6)))
771 (ignore-errors
772 (delete-directory tmp-name1 'recursive)
773 (delete-directory tmp-name2 'recursive)))))
774
775 (ert-deftest tramp-test16-directory-files ()
776 "Check `directory-files'."
777 (skip-unless (tramp--test-enabled))
778 (tramp-cleanup-connection
779 (tramp-dissect-file-name tramp-test-temporary-file-directory)
780 nil 'keep-password)
781
782 (let* ((tmp-name1 (tramp--test-make-temp-name))
783 (tmp-name2 (expand-file-name "bla" tmp-name1))
784 (tmp-name3 (expand-file-name "foo" tmp-name1)))
785 (unwind-protect
786 (progn
787 (make-directory tmp-name1)
788 (write-region "foo" nil tmp-name2)
789 (write-region "bla" nil tmp-name3)
790 (should (file-directory-p tmp-name1))
791 (should (file-exists-p tmp-name2))
792 (should (file-exists-p tmp-name3))
793 (should (equal (directory-files tmp-name1) '("." ".." "bla" "foo")))
794 (should (equal (directory-files tmp-name1 'full)
795 `(,(concat tmp-name1 "/.")
796 ,(concat tmp-name1 "/..")
797 ,tmp-name2 ,tmp-name3)))
798 (should (equal (directory-files
799 tmp-name1 nil directory-files-no-dot-files-regexp)
800 '("bla" "foo")))
801 (should (equal (directory-files
802 tmp-name1 'full directory-files-no-dot-files-regexp)
803 `(,tmp-name2 ,tmp-name3))))
804 (ignore-errors (delete-directory tmp-name1 'recursive)))))
805
806 (ert-deftest tramp-test17-insert-directory ()
807 "Check `insert-directory'."
808 (skip-unless (tramp--test-enabled))
809 (tramp-cleanup-connection
810 (tramp-dissect-file-name tramp-test-temporary-file-directory)
811 nil 'keep-password)
812
813 (let* ((tmp-name1 (tramp--test-make-temp-name))
814 (tmp-name2 (expand-file-name "foo" tmp-name1)))
815 (unwind-protect
816 (progn
817 (make-directory tmp-name1)
818 (write-region "foo" nil tmp-name2)
819 (should (file-directory-p tmp-name1))
820 (should (file-exists-p tmp-name2))
821 (with-temp-buffer
822 (insert-directory tmp-name1 nil)
823 (goto-char (point-min))
824 (should (looking-at-p (regexp-quote tmp-name1))))
825 (with-temp-buffer
826 (insert-directory tmp-name1 "-al")
827 (goto-char (point-min))
828 (should (looking-at-p (format "^.+ %s$" (regexp-quote tmp-name1)))))
829 (with-temp-buffer
830 (insert-directory (file-name-as-directory tmp-name1) "-al")
831 (goto-char (point-min))
832 (should
833 (looking-at-p (format "^.+ %s/$" (regexp-quote tmp-name1)))))
834 (with-temp-buffer
835 (insert-directory
836 (file-name-as-directory tmp-name1) "-al" nil 'full-directory-p)
837 (goto-char (point-min))
838 (should
839 (looking-at-p
840 "\\(total.+[[:digit:]]+\n\\)?.+ \\.\n.+ \\.\\.\n.+ foo$"))))
841 (ignore-errors (delete-directory tmp-name1 'recursive)))))
842
843 (ert-deftest tramp-test18-file-attributes ()
844 "Check `file-attributes'.
845 This tests also `file-readable-p' and `file-regular-p'."
846 (skip-unless (tramp--test-enabled))
847 (tramp-cleanup-connection
848 (tramp-dissect-file-name tramp-test-temporary-file-directory)
849 nil 'keep-password)
850
851 (let ((tmp-name (tramp--test-make-temp-name))
852 attr)
853 (unwind-protect
854 (progn
855 (write-region "foo" nil tmp-name)
856 (should (file-exists-p tmp-name))
857 (setq attr (file-attributes tmp-name))
858 (should (consp attr))
859 (should (file-exists-p tmp-name))
860 (should (file-readable-p tmp-name))
861 (should (file-regular-p tmp-name))
862 ;; We do not test inodes and device numbers.
863 (should (null (car attr)))
864 (should (numberp (nth 1 attr))) ;; Link.
865 (should (numberp (nth 2 attr))) ;; Uid.
866 (should (numberp (nth 3 attr))) ;; Gid.
867 ;; Last access time.
868 (should (stringp (current-time-string (nth 4 attr))))
869 ;; Last modification time.
870 (should (stringp (current-time-string (nth 5 attr))))
871 ;; Last status change time.
872 (should (stringp (current-time-string (nth 6 attr))))
873 (should (numberp (nth 7 attr))) ;; Size.
874 (should (stringp (nth 8 attr))) ;; Modes.
875
876 (setq attr (file-attributes tmp-name 'string))
877 (should (stringp (nth 2 attr))) ;; Uid.
878 (should (stringp (nth 3 attr))) ;; Gid.
879 (delete-file tmp-name)
880
881 (make-directory tmp-name)
882 (should (file-exists-p tmp-name))
883 (should (file-readable-p tmp-name))
884 (should-not (file-regular-p tmp-name))
885 (setq attr (file-attributes tmp-name))
886 (should (eq (car attr) t)))
887 (ignore-errors (delete-directory tmp-name)))))
888
889 (ert-deftest tramp-test19-directory-files-and-attributes ()
890 "Check `directory-files-and-attributes'."
891 (skip-unless (tramp--test-enabled))
892 (tramp-cleanup-connection
893 (tramp-dissect-file-name tramp-test-temporary-file-directory)
894 nil 'keep-password)
895
896 (let ((tmp-name (tramp--test-make-temp-name))
897 attr)
898 (unwind-protect
899 (progn
900 (make-directory tmp-name)
901 (should (file-directory-p tmp-name))
902 (write-region "foo" nil (expand-file-name "foo" tmp-name))
903 (write-region "bar" nil (expand-file-name "bar" tmp-name))
904 (write-region "boz" nil (expand-file-name "boz" tmp-name))
905 (setq attr (directory-files-and-attributes tmp-name))
906 (should (consp attr))
907 (dolist (elt attr)
908 (tramp--instrument-test-case 10
909 (should
910 (equal (file-attributes (expand-file-name (car elt) tmp-name))
911 (cdr elt)))))
912 (setq attr (directory-files-and-attributes tmp-name 'full))
913 (dolist (elt attr)
914 (tramp--instrument-test-case 10
915 (should
916 (equal (file-attributes (car elt)) (cdr elt)))))
917 (setq attr (directory-files-and-attributes tmp-name nil "^b"))
918 (should (equal (mapcar 'car attr) '("bar" "boz"))))
919 (ignore-errors (delete-directory tmp-name 'recursive)))))
920
921 (ert-deftest tramp-test20-file-modes ()
922 "Check `file-modes'.
923 This tests also `file-executable-p', `file-writable-p' and `set-file-modes'."
924 (skip-unless (tramp--test-enabled))
925 (skip-unless
926 (not
927 (memq
928 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory)
929 '(tramp-gvfs-file-name-handler tramp-smb-file-name-handler))))
930 (tramp-cleanup-connection
931 (tramp-dissect-file-name tramp-test-temporary-file-directory)
932 nil 'keep-password)
933
934 (let ((tmp-name (tramp--test-make-temp-name)))
935 (unwind-protect
936 (progn
937 (write-region "foo" nil tmp-name)
938 (should (file-exists-p tmp-name))
939 (set-file-modes tmp-name #o777)
940 (should (= (file-modes tmp-name) #o777))
941 (should (file-executable-p tmp-name))
942 (should (file-writable-p tmp-name))
943 (set-file-modes tmp-name #o444)
944 (should (= (file-modes tmp-name) #o444))
945 (should-not (file-executable-p tmp-name))
946 ;; A file is always writable for user "root".
947 (when (and (stringp (file-remote-p tmp-name 'user))
948 (not (string-equal (file-remote-p tmp-name 'user) "root")))
949 (should-not (file-writable-p tmp-name))))
950 (ignore-errors (delete-file tmp-name)))))
951
952 (ert-deftest tramp-test21-file-links ()
953 "Check `file-symlink-p'.
954 This tests also `make-symbolic-link', `file-truename' and `add-name-to-file'."
955 (skip-unless (tramp--test-enabled))
956 (tramp-cleanup-connection
957 (tramp-dissect-file-name tramp-test-temporary-file-directory)
958 nil 'keep-password)
959
960 (let ((tmp-name1 (tramp--test-make-temp-name))
961 (tmp-name2 (tramp--test-make-temp-name))
962 (tmp-name3 (make-temp-name "tramp-")))
963 (unwind-protect
964 (progn
965 (write-region "foo" nil tmp-name1)
966 (should (file-exists-p tmp-name1))
967 ;; Method "smb" supports `make-symbolic-link' only if the
968 ;; remote host has CIFS capabilities. tramp-adb.el and
969 ;; tramp-gvfs.el do not support symbolic links at all.
970 (condition-case err
971 (make-symbolic-link tmp-name1 tmp-name2)
972 (file-error
973 (skip-unless
974 (not (string-equal (error-message-string err)
975 "make-symbolic-link not supported")))))
976 (should (file-symlink-p tmp-name2))
977 (should-error (make-symbolic-link tmp-name1 tmp-name2))
978 (make-symbolic-link tmp-name1 tmp-name2 'ok-if-already-exists)
979 (should (file-symlink-p tmp-name2))
980 ;; `tmp-name3' is a local file name.
981 (should-error (make-symbolic-link tmp-name1 tmp-name3)))
982 (ignore-errors
983 (delete-file tmp-name1)
984 (delete-file tmp-name2)))
985
986 (unwind-protect
987 (progn
988 (write-region "foo" nil tmp-name1)
989 (should (file-exists-p tmp-name1))
990 (add-name-to-file tmp-name1 tmp-name2)
991 (should-not (file-symlink-p tmp-name2))
992 (should-error (add-name-to-file tmp-name1 tmp-name2))
993 (add-name-to-file tmp-name1 tmp-name2 'ok-if-already-exists)
994 (should-not (file-symlink-p tmp-name2))
995 ;; `tmp-name3' is a local file name.
996 (should-error (add-name-to-file tmp-name1 tmp-name3)))
997 (ignore-errors
998 (delete-file tmp-name1)
999 (delete-file tmp-name2)))
1000
1001 (unwind-protect
1002 (progn
1003 (write-region "foo" nil tmp-name1)
1004 (should (file-exists-p tmp-name1))
1005 (make-symbolic-link tmp-name1 tmp-name2)
1006 (should (file-symlink-p tmp-name2))
1007 (should-not (string-equal tmp-name2 (file-truename tmp-name2)))
1008 (should
1009 (string-equal (file-truename tmp-name1) (file-truename tmp-name2))))
1010 (ignore-errors
1011 (delete-file tmp-name1)
1012 (delete-file tmp-name2)))))
1013
1014 (ert-deftest tramp-test22-file-times ()
1015 "Check `set-file-times' and `file-newer-than-file-p'."
1016 (skip-unless (tramp--test-enabled))
1017 (skip-unless
1018 (not
1019 (memq
1020 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory)
1021 '(tramp-gvfs-file-name-handler tramp-smb-file-name-handler))))
1022 (tramp-cleanup-connection
1023 (tramp-dissect-file-name tramp-test-temporary-file-directory)
1024 nil 'keep-password)
1025
1026 (let ((tmp-name1 (tramp--test-make-temp-name))
1027 (tmp-name2 (tramp--test-make-temp-name))
1028 (tmp-name3 (tramp--test-make-temp-name)))
1029 (unwind-protect
1030 (progn
1031 (write-region "foo" nil tmp-name1)
1032 (should (file-exists-p tmp-name1))
1033 (should (consp (nth 5 (file-attributes tmp-name1))))
1034 ;; '(0 0) means don't know, and will be replaced by `current-time'.
1035 (set-file-times tmp-name1 '(0 1))
1036 ;; Dumb busyboxes are not able to return the date correctly.
1037 ;; They say "don't know.
1038 (skip-unless (not (equal (nth 5 (file-attributes tmp-name1)) '(0 0))))
1039 (should (equal (nth 5 (file-attributes tmp-name1)) '(0 1)))
1040 (write-region "bla" nil tmp-name2)
1041 (should (file-exists-p tmp-name2))
1042 (should (file-newer-than-file-p tmp-name2 tmp-name1))
1043 ;; `tmp-name3' does not exist.
1044 (should (file-newer-than-file-p tmp-name2 tmp-name3))
1045 (should-not (file-newer-than-file-p tmp-name3 tmp-name1)))
1046 (ignore-errors
1047 (delete-file tmp-name1)
1048 (delete-file tmp-name2)))))
1049
1050 (ert-deftest tramp-test23-visited-file-modtime ()
1051 "Check `set-visited-file-modtime' and `verify-visited-file-modtime'."
1052 (skip-unless (tramp--test-enabled))
1053 (tramp-cleanup-connection
1054 (tramp-dissect-file-name tramp-test-temporary-file-directory)
1055 nil 'keep-password)
1056
1057 (let ((tmp-name (tramp--test-make-temp-name)))
1058 (unwind-protect
1059 (progn
1060 (write-region "foo" nil tmp-name)
1061 (should (file-exists-p tmp-name))
1062 (with-temp-buffer
1063 (insert-file-contents tmp-name)
1064 (should (verify-visited-file-modtime))
1065 (set-visited-file-modtime '(0 1))
1066 (should (verify-visited-file-modtime))
1067 (should (equal (visited-file-modtime) '(0 1 0 0)))))
1068 (ignore-errors (delete-file tmp-name)))))
1069
1070 (ert-deftest tramp-test24-file-name-completion ()
1071 "Check `file-name-completion' and `file-name-all-completions'."
1072 (skip-unless (tramp--test-enabled))
1073 (tramp-cleanup-connection
1074 (tramp-dissect-file-name tramp-test-temporary-file-directory)
1075 nil 'keep-password)
1076
1077 (let ((tmp-name (tramp--test-make-temp-name)))
1078 (unwind-protect
1079 (progn
1080 (make-directory tmp-name)
1081 (should (file-directory-p tmp-name))
1082 (write-region "foo" nil (expand-file-name "foo" tmp-name))
1083 (write-region "bar" nil (expand-file-name "bold" tmp-name))
1084 (make-directory (expand-file-name "boz" tmp-name))
1085 (should (equal (file-name-completion "fo" tmp-name) "foo"))
1086 (should (equal (file-name-completion "b" tmp-name) "bo"))
1087 (should
1088 (equal (file-name-completion "b" tmp-name 'file-directory-p) "boz/"))
1089 (should (equal (file-name-all-completions "fo" tmp-name) '("foo")))
1090 (should
1091 (equal (sort (file-name-all-completions "b" tmp-name) 'string-lessp)
1092 '("bold" "boz/"))))
1093 (ignore-errors (delete-directory tmp-name 'recursive)))))
1094
1095 (ert-deftest tramp-test25-load ()
1096 "Check `load'."
1097 (skip-unless (tramp--test-enabled))
1098 (tramp-cleanup-connection
1099 (tramp-dissect-file-name tramp-test-temporary-file-directory)
1100 nil 'keep-password)
1101
1102 (let ((tmp-name (tramp--test-make-temp-name)))
1103 (unwind-protect
1104 (progn
1105 (load tmp-name 'noerror 'nomessage)
1106 (should-not (featurep 'tramp-test-load))
1107 (write-region "(provide 'tramp-test-load)" nil tmp-name)
1108 ;; `load' in lread.c does not pass `must-suffix'. Why?
1109 ;(should-error (load tmp-name nil 'nomessage 'nosuffix 'must-suffix))
1110 (load tmp-name nil 'nomessage 'nosuffix)
1111 (should (featurep 'tramp-test-load)))
1112 (ignore-errors
1113 (and (featurep 'tramp-test-load) (unload-feature 'tramp-test-load))
1114 (delete-file tmp-name)))))
1115
1116 (ert-deftest tramp-test26-process-file ()
1117 "Check `process-file'."
1118 (skip-unless (tramp--test-enabled))
1119 (skip-unless
1120 (not
1121 (memq
1122 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory)
1123 '(tramp-gvfs-file-name-handler tramp-smb-file-name-handler))))
1124 (tramp-cleanup-connection
1125 (tramp-dissect-file-name tramp-test-temporary-file-directory)
1126 nil 'keep-password)
1127
1128 (let ((tmp-name (tramp--test-make-temp-name))
1129 (default-directory tramp-test-temporary-file-directory))
1130 (unwind-protect
1131 (progn
1132 ;; We cannot use "/bin/true" and "/bin/false"; those paths
1133 ;; do not exist on hydra.
1134 (should (zerop (process-file "true")))
1135 (should-not (zerop (process-file "false")))
1136 (should-not (zerop (process-file "binary-does-not-exist")))
1137 (with-temp-buffer
1138 (write-region "foo" nil tmp-name)
1139 (should (file-exists-p tmp-name))
1140 (should
1141 (zerop
1142 (process-file "ls" nil t nil (file-name-nondirectory tmp-name))))
1143 ;; `ls' could produce colorized output.
1144 (goto-char (point-min))
1145 (while (re-search-forward tramp-color-escape-sequence-regexp nil t)
1146 (replace-match "" nil nil))
1147 (should
1148 (string-equal
1149 (format "%s\n" (file-name-nondirectory tmp-name))
1150 (buffer-string)))))
1151 (ignore-errors (delete-file tmp-name)))))
1152
1153 (ert-deftest tramp-test27-start-file-process ()
1154 "Check `start-file-process'."
1155 (skip-unless (tramp--test-enabled))
1156 (skip-unless
1157 (not
1158 (memq
1159 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory)
1160 '(tramp-gvfs-file-name-handler tramp-smb-file-name-handler))))
1161 (tramp-cleanup-connection
1162 (tramp-dissect-file-name tramp-test-temporary-file-directory)
1163 nil 'keep-password)
1164
1165 (let ((default-directory tramp-test-temporary-file-directory)
1166 (tmp-name (tramp--test-make-temp-name))
1167 kill-buffer-query-functions proc)
1168 (unwind-protect
1169 (with-temp-buffer
1170 (setq proc (start-file-process "test1" (current-buffer) "cat"))
1171 (should (processp proc))
1172 (should (equal (process-status proc) 'run))
1173 (process-send-string proc "foo")
1174 (process-send-eof proc)
1175 (accept-process-output proc 1)
1176 (should (string-equal (buffer-string) "foo")))
1177 (ignore-errors (delete-process proc)))
1178
1179 (unwind-protect
1180 (with-temp-buffer
1181 (write-region "foo" nil tmp-name)
1182 (should (file-exists-p tmp-name))
1183 (setq proc
1184 (start-file-process
1185 "test2" (current-buffer)
1186 "cat" (file-name-nondirectory tmp-name)))
1187 (should (processp proc))
1188 (accept-process-output proc 1)
1189 (should (string-equal (buffer-string) "foo")))
1190 (ignore-errors
1191 (delete-process proc)
1192 (delete-file tmp-name)))
1193
1194 (unwind-protect
1195 (progn
1196 (setq proc (start-file-process "test3" nil "cat"))
1197 (should (processp proc))
1198 (should (equal (process-status proc) 'run))
1199 (set-process-filter
1200 proc (lambda (_p s) (should (string-equal s "foo"))))
1201 (process-send-string proc "foo")
1202 (process-send-eof proc)
1203 (accept-process-output proc 1))
1204 (ignore-errors (delete-process proc)))))
1205
1206 (ert-deftest tramp-test28-shell-command ()
1207 "Check `shell-command'."
1208 (skip-unless (tramp--test-enabled))
1209 (skip-unless
1210 (not
1211 (memq
1212 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory)
1213 '(tramp-gvfs-file-name-handler tramp-smb-file-name-handler))))
1214 (tramp-cleanup-connection
1215 (tramp-dissect-file-name tramp-test-temporary-file-directory)
1216 nil 'keep-password)
1217
1218 (let ((tmp-name (tramp--test-make-temp-name))
1219 (default-directory tramp-test-temporary-file-directory))
1220 (unwind-protect
1221 (with-temp-buffer
1222 (write-region "foo" nil tmp-name)
1223 (should (file-exists-p tmp-name))
1224 (shell-command
1225 (format "ls %s" (file-name-nondirectory tmp-name)) (current-buffer))
1226 ;; `ls' could produce colorized output.
1227 (goto-char (point-min))
1228 (while (re-search-forward tramp-color-escape-sequence-regexp nil t)
1229 (replace-match "" nil nil))
1230 (should
1231 (string-equal
1232 (format "%s\n" (file-name-nondirectory tmp-name)) (buffer-string))))
1233 (ignore-errors (delete-file tmp-name)))
1234
1235 (unwind-protect
1236 (with-temp-buffer
1237 (write-region "foo" nil tmp-name)
1238 (should (file-exists-p tmp-name))
1239 (async-shell-command
1240 (format "ls %s" (file-name-nondirectory tmp-name)) (current-buffer))
1241 (accept-process-output (get-buffer-process (current-buffer)) 1)
1242 (while (ignore-errors
1243 (memq (process-status (get-buffer-process (current-buffer)))
1244 '(run open)))
1245 (accept-process-output (get-buffer-process (current-buffer)) 1))
1246 ;; `ls' could produce colorized output.
1247 (goto-char (point-min))
1248 (while (re-search-forward tramp-color-escape-sequence-regexp nil t)
1249 (replace-match "" nil nil))
1250 (should
1251 (string-equal
1252 (format "%s\n" (file-name-nondirectory tmp-name)) (buffer-string))))
1253 (ignore-errors (delete-file tmp-name)))
1254
1255 (unwind-protect
1256 (with-temp-buffer
1257 (write-region "foo" nil tmp-name)
1258 (should (file-exists-p tmp-name))
1259 (async-shell-command "read line; ls $line" (current-buffer))
1260 (process-send-string
1261 (get-buffer-process (current-buffer))
1262 (format "%s\n" (file-name-nondirectory tmp-name)))
1263 (accept-process-output (get-buffer-process (current-buffer)) 1)
1264 (while (ignore-errors
1265 (memq (process-status (get-buffer-process (current-buffer)))
1266 '(run open)))
1267 (accept-process-output (get-buffer-process (current-buffer)) 1))
1268 (should
1269 (string-equal
1270 (format "%s\n" (file-name-nondirectory tmp-name)) (buffer-string))))
1271 (ignore-errors (delete-file tmp-name)))))
1272
1273 (ert-deftest tramp-test29-vc-registered ()
1274 "Check `vc-registered'."
1275 (skip-unless (tramp--test-enabled))
1276 (skip-unless
1277 (eq
1278 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory)
1279 'tramp-sh-file-name-handler))
1280 (tramp-cleanup-connection
1281 (tramp-dissect-file-name tramp-test-temporary-file-directory)
1282 nil 'keep-password)
1283
1284 (let* ((default-directory tramp-test-temporary-file-directory)
1285 (tmp-name1 (tramp--test-make-temp-name))
1286 (tmp-name2 (expand-file-name "foo" tmp-name1))
1287 (vc-handled-backends
1288 (with-parsed-tramp-file-name tramp-test-temporary-file-directory nil
1289 (cond
1290 ((tramp-find-executable v vc-bzr-program (tramp-get-remote-path v))
1291 '(Bzr))
1292 ((tramp-find-executable v vc-git-program (tramp-get-remote-path v))
1293 '(Git))
1294 ((tramp-find-executable v vc-hg-program (tramp-get-remote-path v))
1295 '(Hg))
1296 (t nil)))))
1297 (skip-unless vc-handled-backends)
1298 (message "%s" vc-handled-backends)
1299
1300 (unwind-protect
1301 (progn
1302 (make-directory tmp-name1)
1303 (write-region "foo" nil tmp-name2)
1304 (should (file-directory-p tmp-name1))
1305 (should (file-exists-p tmp-name2))
1306 (should-not (vc-registered tmp-name1))
1307 (should-not (vc-registered tmp-name2))
1308
1309 (let ((default-directory tmp-name1))
1310 ;; Create empty repository, and register the file.
1311 (vc-create-repo (car vc-handled-backends))
1312 ;; The structure of VC-FILESET is not documented. Let's
1313 ;; hope it won't change.
1314 (vc-register
1315 nil (list (car vc-handled-backends)
1316 (list (file-name-nondirectory tmp-name2)))))
1317 (should (vc-registered tmp-name2)))
1318
1319 (ignore-errors (delete-directory tmp-name1 'recursive)))))
1320
1321 (ert-deftest tramp-test30-utf8 ()
1322 "Check UTF8 encoding in file names and file contents."
1323 (skip-unless (tramp--test-enabled))
1324 (tramp-cleanup-connection
1325 (tramp-dissect-file-name tramp-test-temporary-file-directory)
1326 nil 'keep-password)
1327
1328 (let ((tmp-name (tramp--test-make-temp-name))
1329 (coding-system-for-read 'utf-8)
1330 (coding-system-for-write 'utf-8)
1331 (arabic "أصبح بوسعك الآن تنزيل نسخة كاملة من موسوعة ويكيبيديا العربية لتصفحها بلا اتصال بالإنترنت")
1332 (chinese "银河系漫游指南系列")
1333 (russian "Автостопом по гала́ктике"))
1334 (unwind-protect
1335 (progn
1336 (make-directory tmp-name)
1337 (dolist (lang `(,arabic ,chinese ,russian))
1338 (let ((file (expand-file-name lang tmp-name)))
1339 (write-region lang nil file)
1340 (should (file-exists-p file))
1341 ;; Check file contents.
1342 (with-temp-buffer
1343 (insert-file-contents file)
1344 (should (string-equal (buffer-string) lang)))))
1345 ;; Check file names.
1346 (should (equal (directory-files
1347 tmp-name nil directory-files-no-dot-files-regexp)
1348 (sort `(,arabic ,chinese ,russian) 'string-lessp))))
1349 (ignore-errors (delete-directory tmp-name 'recursive)))))
1350
1351 ;; TODO:
1352
1353 ;; * dired-compress-file
1354 ;; * dired-uncache
1355 ;; * file-acl
1356 ;; * file-ownership-preserved-p
1357 ;; * file-selinux-context
1358 ;; * find-backup-file-name
1359 ;; * make-auto-save-file-name
1360 ;; * set-file-acl
1361 ;; * set-file-selinux-context
1362
1363 ;; * Fix `tramp-test27-start-file-process' on MS Windows (`process-send-eof'?).
1364 ;; * Fix `tramp-test28-shell-command' on MS Windows (`process-send-eof'?).
1365 ;; * Fix `tramp-test30-utf8' on MS Windows. Seems to be in `directory-files'.
1366
1367 (defun tramp-test-all (&optional interactive)
1368 "Run all tests for \\[tramp]."
1369 (interactive "p")
1370 (funcall
1371 (if interactive 'ert-run-tests-interactively 'ert-run-tests-batch) "^tramp"))
1372
1373 (provide 'tramp-tests)
1374 ;;; tramp-tests.el ends here