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