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