* automated/tramp-tests.el (top):
[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
c0b9bc72
MA
24;; Some of the tests require access to a remote host files. Since
25;; this could be problematic, a mock-up connection method "mock" is
26;; used. Emulating a remote connection, it simply calls "sh -i".
844465d6 27;; Tramp's file name handlers still run, so this test is sufficient
c0b9bc72
MA
28;; except for connection establishing.
29
30;; If you want to test a real Tramp connection, set
31;; $REMOTE_TEMPORARY_FILE_DIRECTORY to a suitable value in order to
32;; overwrite the default value. If you want to skip tests accessing a
33;; remote host, set this environment variable to "/dev/null" or
34;; whatever is appropriate on your system.
a213a541 35
1c49d6c2 36;; A whole test run can be performed calling the command `tramp-test-all'.
a213a541
MA
37
38;;; Code:
39
40(require 'ert)
41(require 'tramp)
581d24e7
MA
42(require 'vc)
43(require 'vc-bzr)
44(require 'vc-git)
45(require 'vc-hg)
46
47(declare-function tramp-find-executable "tramp-sh")
48(declare-function tramp-get-remote-path "tramp-sh")
162427fe 49(defvar tramp-copy-size-limit)
a213a541
MA
50
51;; There is no default value on w32 systems, which could work out of the box.
52(defconst tramp-test-temporary-file-directory
8ee0219f 53 (cond
1baa1e49 54 ((getenv "REMOTE_TEMPORARY_FILE_DIRECTORY"))
8ee0219f 55 ((eq system-type 'windows-nt) null-device)
c0b9bc72
MA
56 (t (add-to-list
57 'tramp-methods
58 '("mock"
59 (tramp-login-program "sh")
60 (tramp-login-args (("-i")))
61 (tramp-remote-shell "/bin/sh")
62 (tramp-remote-shell-args ("-c"))
63 (tramp-connection-timeout 10)))
64 (format "/mock::%s" temporary-file-directory)))
a213a541
MA
65 "Temporary directory for Tramp tests.")
66
dd7691b7
MA
67(setq password-cache-expiry nil
68 tramp-verbose 0
2a2e6726 69 tramp-copy-size-limit nil
a213a541 70 tramp-message-show-message nil)
8ee0219f 71
1c49d6c2
MA
72;; This shall happen on hydra only.
73(when (getenv "NIX_STORE")
74 (add-to-list 'tramp-remote-path 'tramp-own-remote-path))
a213a541
MA
75
76(defvar tramp--test-enabled-checked nil
77 "Cached result of `tramp--test-enabled'.
78If the function did run, the value is a cons cell, the `cdr'
79being the result.")
80
81(defun tramp--test-enabled ()
82 "Whether remote file access is enabled."
83 (unless (consp tramp--test-enabled-checked)
84 (setq
85 tramp--test-enabled-checked
86 (cons
87 t (ignore-errors
88 (and
89 (file-remote-p tramp-test-temporary-file-directory)
90 (file-directory-p tramp-test-temporary-file-directory)
91 (file-writable-p tramp-test-temporary-file-directory))))))
154ba796 92
5b5774e5 93 (when (cdr tramp--test-enabled-checked)
162427fe 94 ;; Cleanup connection.
154ba796
MA
95 (tramp-cleanup-connection
96 (tramp-dissect-file-name tramp-test-temporary-file-directory)
97 nil 'keep-password))
98
a213a541
MA
99 ;; Return result.
100 (cdr tramp--test-enabled-checked))
101
2a2e6726 102(defun tramp--test-make-temp-name (&optional local)
a213a541
MA
103 "Create a temporary file name for test."
104 (expand-file-name
2a2e6726
MA
105 (make-temp-name "tramp-test")
106 (if local temporary-file-directory tramp-test-temporary-file-directory)))
a213a541 107
d9386b0c
MA
108(defmacro tramp--instrument-test-case (verbose &rest body)
109 "Run BODY with `tramp-verbose' equal VERBOSE.
110Print the the content of the Tramp debug buffer, if BODY does not
111eval properly in `should', `should-not' or `should-error'."
154ba796
MA
112 (declare (indent 1) (debug (natnump body)))
113 `(let ((tramp-verbose ,verbose)
2a2e6726 114 (tramp-message-show-message t)
154ba796 115 (tramp-debug-on-error t))
d9386b0c 116 (condition-case err
2a2e6726
MA
117 ;; In general, we cannot use a timeout here: this would
118 ;; prevent traces when the test runs into an error.
119; (with-timeout (10 (ert-fail "`tramp--instrument-test-case' timed out"))
120 (progn
121 ,@body)
154ba796
MA
122 (ert-test-skipped
123 (signal (car err) (cdr err)))
2a2e6726 124 ((error quit)
d9386b0c 125 (with-parsed-tramp-file-name tramp-test-temporary-file-directory nil
154ba796
MA
126 (with-current-buffer (tramp-get-connection-buffer v)
127 (message "%s" (buffer-string)))
d9386b0c
MA
128 (with-current-buffer (tramp-get-debug-buffer v)
129 (message "%s" (buffer-string))))
154ba796 130 (message "%s" err)
d9386b0c 131 (signal (car err) (cdr err))))))
d9386b0c 132
a213a541
MA
133(ert-deftest tramp-test00-availability ()
134 "Test availability of Tramp functions."
135 :expected-result (if (tramp--test-enabled) :passed :failed)
c0b9bc72 136 (message "Remote directory: `%s'" tramp-test-temporary-file-directory)
a213a541
MA
137 (should (ignore-errors
138 (and
139 (file-remote-p tramp-test-temporary-file-directory)
140 (file-directory-p tramp-test-temporary-file-directory)
141 (file-writable-p tramp-test-temporary-file-directory)))))
142
143(ert-deftest tramp-test01-file-name-syntax ()
144 "Check remote file name syntax."
145 ;; Simple cases.
146 (should (tramp-tramp-file-p "/method::"))
147 (should (tramp-tramp-file-p "/host:"))
148 (should (tramp-tramp-file-p "/user@:"))
149 (should (tramp-tramp-file-p "/user@host:"))
150 (should (tramp-tramp-file-p "/method:host:"))
151 (should (tramp-tramp-file-p "/method:user@:"))
152 (should (tramp-tramp-file-p "/method:user@host:"))
153 (should (tramp-tramp-file-p "/method:user@email@host:"))
154
155 ;; Using a port.
156 (should (tramp-tramp-file-p "/host#1234:"))
157 (should (tramp-tramp-file-p "/user@host#1234:"))
158 (should (tramp-tramp-file-p "/method:host#1234:"))
159 (should (tramp-tramp-file-p "/method:user@host#1234:"))
160
161 ;; Using an IPv4 address.
162 (should (tramp-tramp-file-p "/1.2.3.4:"))
163 (should (tramp-tramp-file-p "/user@1.2.3.4:"))
164 (should (tramp-tramp-file-p "/method:1.2.3.4:"))
165 (should (tramp-tramp-file-p "/method:user@1.2.3.4:"))
166
167 ;; Using an IPv6 address.
168 (should (tramp-tramp-file-p "/[]:"))
169 (should (tramp-tramp-file-p "/[::1]:"))
170 (should (tramp-tramp-file-p "/user@[::1]:"))
171 (should (tramp-tramp-file-p "/method:[::1]:"))
172 (should (tramp-tramp-file-p "/method:user@[::1]:"))
173
174 ;; Local file name part.
175 (should (tramp-tramp-file-p "/host:/:"))
176 (should (tramp-tramp-file-p "/method:::"))
177 (should (tramp-tramp-file-p "/method::/path/to/file"))
178 (should (tramp-tramp-file-p "/method::file"))
179
180 ;; Multihop.
181 (should (tramp-tramp-file-p "/method1:|method2::"))
182 (should (tramp-tramp-file-p "/method1:host1|host2:"))
183 (should (tramp-tramp-file-p "/method1:host1|method2:host2:"))
184 (should (tramp-tramp-file-p "/method1:user1@host1|method2:user2@host2:"))
185 (should (tramp-tramp-file-p
186 "/method1:user1@host1|method2:user2@host2|method3:user3@host3:"))
187
188 ;; No strings.
189 (should-not (tramp-tramp-file-p nil))
190 (should-not (tramp-tramp-file-p 'symbol))
191 ;; "/:" suppresses file name handlers.
192 (should-not (tramp-tramp-file-p "/::"))
193 (should-not (tramp-tramp-file-p "/:@:"))
194 (should-not (tramp-tramp-file-p "/:[]:"))
195 ;; Multihops require a method.
196 (should-not (tramp-tramp-file-p "/host1|host2:"))
197 ;; Methods or hostnames shall be at least two characters on MS Windows.
198 (when (memq system-type '(cygwin windows-nt))
199 (should-not (tramp-tramp-file-p "/c:/path/to/file"))
200 (should-not (tramp-tramp-file-p "/c::/path/to/file"))))
201
202(ert-deftest tramp-test02-file-name-dissect ()
203 "Check remote file name components."
204 (let ((tramp-default-method "default-method")
205 (tramp-default-user "default-user")
206 (tramp-default-host "default-host"))
207 ;; Expand `tramp-default-user' and `tramp-default-host'.
208 (should (string-equal
209 (file-remote-p "/method::")
210 (format "/%s:%s@%s:" "method" "default-user" "default-host")))
211 (should (string-equal (file-remote-p "/method::" 'method) "method"))
212 (should (string-equal (file-remote-p "/method::" 'user) "default-user"))
213 (should (string-equal (file-remote-p "/method::" 'host) "default-host"))
214 (should (string-equal (file-remote-p "/method::" 'localname) ""))
215
216 ;; Expand `tramp-default-method' and `tramp-default-user'.
217 (should (string-equal
218 (file-remote-p "/host:")
219 (format "/%s:%s@%s:" "default-method" "default-user" "host")))
220 (should (string-equal (file-remote-p "/host:" 'method) "default-method"))
221 (should (string-equal (file-remote-p "/host:" 'user) "default-user"))
222 (should (string-equal (file-remote-p "/host:" 'host) "host"))
223 (should (string-equal (file-remote-p "/host:" 'localname) ""))
224
225 ;; Expand `tramp-default-method' and `tramp-default-host'.
226 (should (string-equal
227 (file-remote-p "/user@:")
228 (format "/%s:%s@%s:" "default-method""user" "default-host")))
229 (should (string-equal (file-remote-p "/user@:" 'method) "default-method"))
230 (should (string-equal (file-remote-p "/user@:" 'user) "user"))
231 (should (string-equal (file-remote-p "/user@:" 'host) "default-host"))
232 (should (string-equal (file-remote-p "/user@:" 'localname) ""))
233
234 ;; Expand `tramp-default-method'.
235 (should (string-equal
236 (file-remote-p "/user@host:")
237 (format "/%s:%s@%s:" "default-method" "user" "host")))
238 (should (string-equal
239 (file-remote-p "/user@host:" 'method) "default-method"))
240 (should (string-equal (file-remote-p "/user@host:" 'user) "user"))
241 (should (string-equal (file-remote-p "/user@host:" 'host) "host"))
242 (should (string-equal (file-remote-p "/user@host:" 'localname) ""))
243
244 ;; Expand `tramp-default-user'.
245 (should (string-equal
246 (file-remote-p "/method:host:")
247 (format "/%s:%s@%s:" "method" "default-user" "host")))
248 (should (string-equal (file-remote-p "/method:host:" 'method) "method"))
249 (should (string-equal (file-remote-p "/method:host:" 'user) "default-user"))
250 (should (string-equal (file-remote-p "/method:host:" 'host) "host"))
251 (should (string-equal (file-remote-p "/method:host:" 'localname) ""))
252
253 ;; Expand `tramp-default-host'.
254 (should (string-equal
255 (file-remote-p "/method:user@:")
256 (format "/%s:%s@%s:" "method" "user" "default-host")))
257 (should (string-equal (file-remote-p "/method:user@:" 'method) "method"))
258 (should (string-equal (file-remote-p "/method:user@:" 'user) "user"))
259 (should (string-equal (file-remote-p "/method:user@:" 'host)
260 "default-host"))
261 (should (string-equal (file-remote-p "/method:user@:" 'localname) ""))
262
263 ;; No expansion.
264 (should (string-equal
265 (file-remote-p "/method:user@host:")
266 (format "/%s:%s@%s:" "method" "user" "host")))
267 (should (string-equal
268 (file-remote-p "/method:user@host:" 'method) "method"))
269 (should (string-equal (file-remote-p "/method:user@host:" 'user) "user"))
270 (should (string-equal (file-remote-p "/method:user@host:" 'host) "host"))
271 (should (string-equal (file-remote-p "/method:user@host:" 'localname) ""))
272
273 ;; No expansion.
274 (should (string-equal
275 (file-remote-p "/method:user@email@host:")
276 (format "/%s:%s@%s:" "method" "user@email" "host")))
277 (should (string-equal
278 (file-remote-p "/method:user@email@host:" 'method) "method"))
279 (should (string-equal
280 (file-remote-p "/method:user@email@host:" 'user) "user@email"))
281 (should (string-equal
282 (file-remote-p "/method:user@email@host:" 'host) "host"))
283 (should (string-equal
284 (file-remote-p "/method:user@email@host:" 'localname) ""))
285
286 ;; Expand `tramp-default-method' and `tramp-default-user'.
287 (should (string-equal
288 (file-remote-p "/host#1234:")
289 (format "/%s:%s@%s:" "default-method" "default-user" "host#1234")))
290 (should (string-equal
291 (file-remote-p "/host#1234:" 'method) "default-method"))
292 (should (string-equal (file-remote-p "/host#1234:" 'user) "default-user"))
293 (should (string-equal (file-remote-p "/host#1234:" 'host) "host#1234"))
294 (should (string-equal (file-remote-p "/host#1234:" 'localname) ""))
295
296 ;; Expand `tramp-default-method'.
297 (should (string-equal
298 (file-remote-p "/user@host#1234:")
299 (format "/%s:%s@%s:" "default-method" "user" "host#1234")))
300 (should (string-equal
301 (file-remote-p "/user@host#1234:" 'method) "default-method"))
302 (should (string-equal (file-remote-p "/user@host#1234:" 'user) "user"))
303 (should (string-equal (file-remote-p "/user@host#1234:" 'host) "host#1234"))
304 (should (string-equal (file-remote-p "/user@host#1234:" 'localname) ""))
305
306 ;; Expand `tramp-default-user'.
307 (should (string-equal
308 (file-remote-p "/method:host#1234:")
309 (format "/%s:%s@%s:" "method" "default-user" "host#1234")))
310 (should (string-equal
311 (file-remote-p "/method:host#1234:" 'method) "method"))
312 (should (string-equal
313 (file-remote-p "/method:host#1234:" 'user) "default-user"))
314 (should (string-equal
315 (file-remote-p "/method:host#1234:" 'host) "host#1234"))
316 (should (string-equal (file-remote-p "/method:host#1234:" 'localname) ""))
317
318 ;; No expansion.
319 (should (string-equal
320 (file-remote-p "/method:user@host#1234:")
321 (format "/%s:%s@%s:" "method" "user" "host#1234")))
322 (should (string-equal
323 (file-remote-p "/method:user@host#1234:" 'method) "method"))
324 (should (string-equal
325 (file-remote-p "/method:user@host#1234:" 'user) "user"))
326 (should (string-equal
327 (file-remote-p "/method:user@host#1234:" 'host) "host#1234"))
328 (should (string-equal
329 (file-remote-p "/method:user@host#1234:" 'localname) ""))
330
331 ;; Expand `tramp-default-method' and `tramp-default-user'.
332 (should (string-equal
333 (file-remote-p "/1.2.3.4:")
334 (format "/%s:%s@%s:" "default-method" "default-user" "1.2.3.4")))
335 (should (string-equal (file-remote-p "/1.2.3.4:" 'method) "default-method"))
336 (should (string-equal (file-remote-p "/1.2.3.4:" 'user) "default-user"))
337 (should (string-equal (file-remote-p "/1.2.3.4:" 'host) "1.2.3.4"))
338 (should (string-equal (file-remote-p "/1.2.3.4:" 'localname) ""))
339
340 ;; Expand `tramp-default-method'.
341 (should (string-equal
342 (file-remote-p "/user@1.2.3.4:")
343 (format "/%s:%s@%s:" "default-method" "user" "1.2.3.4")))
344 (should (string-equal
345 (file-remote-p "/user@1.2.3.4:" 'method) "default-method"))
346 (should (string-equal (file-remote-p "/user@1.2.3.4:" 'user) "user"))
347 (should (string-equal (file-remote-p "/user@1.2.3.4:" 'host) "1.2.3.4"))
348 (should (string-equal (file-remote-p "/user@1.2.3.4:" 'localname) ""))
349
350 ;; Expand `tramp-default-user'.
351 (should (string-equal
352 (file-remote-p "/method:1.2.3.4:")
353 (format "/%s:%s@%s:" "method" "default-user" "1.2.3.4")))
354 (should (string-equal (file-remote-p "/method:1.2.3.4:" 'method) "method"))
355 (should (string-equal
356 (file-remote-p "/method:1.2.3.4:" 'user) "default-user"))
357 (should (string-equal (file-remote-p "/method:1.2.3.4:" 'host) "1.2.3.4"))
358 (should (string-equal (file-remote-p "/method:1.2.3.4:" 'localname) ""))
359
360 ;; No expansion.
361 (should (string-equal
362 (file-remote-p "/method:user@1.2.3.4:")
363 (format "/%s:%s@%s:" "method" "user" "1.2.3.4")))
364 (should (string-equal
365 (file-remote-p "/method:user@1.2.3.4:" 'method) "method"))
366 (should (string-equal (file-remote-p "/method:user@1.2.3.4:" 'user) "user"))
367 (should (string-equal
368 (file-remote-p "/method:user@1.2.3.4:" 'host) "1.2.3.4"))
369 (should (string-equal
370 (file-remote-p "/method:user@1.2.3.4:" 'localname) ""))
371
927fbd6b
MA
372 ;; Expand `tramp-default-method', `tramp-default-user' and
373 ;; `tramp-default-host'.
374 (should (string-equal
375 (file-remote-p "/[]:")
376 (format
377 "/%s:%s@%s:" "default-method" "default-user" "default-host")))
378 (should (string-equal (file-remote-p "/[]:" 'method) "default-method"))
379 (should (string-equal (file-remote-p "/[]:" 'user) "default-user"))
380 (should (string-equal (file-remote-p "/[]:" 'host) "default-host"))
381 (should (string-equal (file-remote-p "/[]:" 'localname) ""))
382
383 ;; Expand `tramp-default-method' and `tramp-default-user'.
384 (let ((tramp-default-host "::1"))
385 (should (string-equal
386 (file-remote-p "/[]:")
387 (format "/%s:%s@%s:" "default-method" "default-user" "[::1]")))
388 (should (string-equal (file-remote-p "/[]:" 'method) "default-method"))
389 (should (string-equal (file-remote-p "/[]:" 'user) "default-user"))
390 (should (string-equal (file-remote-p "/[]:" 'host) "::1"))
391 (should (string-equal (file-remote-p "/[]:" 'localname) "")))
a213a541
MA
392
393 ;; Expand `tramp-default-method' and `tramp-default-user'.
394 (should (string-equal
395 (file-remote-p "/[::1]:")
396 (format "/%s:%s@%s:" "default-method" "default-user" "[::1]")))
397 (should (string-equal (file-remote-p "/[::1]:" 'method) "default-method"))
398 (should (string-equal (file-remote-p "/[::1]:" 'user) "default-user"))
399 (should (string-equal (file-remote-p "/[::1]:" 'host) "::1"))
400 (should (string-equal (file-remote-p "/[::1]:" 'localname) ""))
401
402 ;; Expand `tramp-default-method'.
403 (should (string-equal
404 (file-remote-p "/user@[::1]:")
405 (format "/%s:%s@%s:" "default-method" "user" "[::1]")))
406 (should (string-equal
407 (file-remote-p "/user@[::1]:" 'method) "default-method"))
408 (should (string-equal (file-remote-p "/user@[::1]:" 'user) "user"))
409 (should (string-equal (file-remote-p "/user@[::1]:" 'host) "::1"))
410 (should (string-equal (file-remote-p "/user@[::1]:" 'localname) ""))
411
412 ;; Expand `tramp-default-user'.
413 (should (string-equal
414 (file-remote-p "/method:[::1]:")
415 (format "/%s:%s@%s:" "method" "default-user" "[::1]")))
416 (should (string-equal (file-remote-p "/method:[::1]:" 'method) "method"))
417 (should (string-equal
418 (file-remote-p "/method:[::1]:" 'user) "default-user"))
419 (should (string-equal (file-remote-p "/method:[::1]:" 'host) "::1"))
420 (should (string-equal (file-remote-p "/method:[::1]:" 'localname) ""))
421
422 ;; No expansion.
423 (should (string-equal
424 (file-remote-p "/method:user@[::1]:")
425 (format "/%s:%s@%s:" "method" "user" "[::1]")))
426 (should (string-equal
427 (file-remote-p "/method:user@[::1]:" 'method) "method"))
428 (should (string-equal (file-remote-p "/method:user@[::1]:" 'user) "user"))
429 (should (string-equal (file-remote-p "/method:user@[::1]:" 'host) "::1"))
430 (should (string-equal
431 (file-remote-p "/method:user@[::1]:" 'localname) ""))
432
433 ;; Local file name part.
434 (should (string-equal (file-remote-p "/host:/:" 'localname) "/:"))
435 (should (string-equal (file-remote-p "/method:::" 'localname) ":"))
436 (should (string-equal (file-remote-p "/method:: " 'localname) " "))
437 (should (string-equal (file-remote-p "/method::file" 'localname) "file"))
438 (should (string-equal
439 (file-remote-p "/method::/path/to/file" 'localname)
440 "/path/to/file"))
441
442 ;; Multihop.
443 (should
444 (string-equal
445 (file-remote-p "/method1:user1@host1|method2:user2@host2:/path/to/file")
446 (format "/%s:%s@%s:" "method2" "user2" "host2")))
447 (should
448 (string-equal
449 (file-remote-p
450 "/method1:user1@host1|method2:user2@host2:/path/to/file" 'method)
451 "method2"))
452 (should
453 (string-equal
454 (file-remote-p
455 "/method1:user1@host1|method2:user2@host2:/path/to/file" 'user)
456 "user2"))
457 (should
458 (string-equal
459 (file-remote-p
460 "/method1:user1@host1|method2:user2@host2:/path/to/file" 'host)
461 "host2"))
462 (should
463 (string-equal
464 (file-remote-p
465 "/method1:user1@host1|method2:user2@host2:/path/to/file" 'localname)
466 "/path/to/file"))
467
468 (should
469 (string-equal
470 (file-remote-p
471 "/method1:user1@host1|method2:user2@host2|method3:user3@host3:/path/to/file")
472 (format "/%s:%s@%s:" "method3" "user3" "host3")))
473 (should
474 (string-equal
475 (file-remote-p
476 "/method1:user1@host1|method2:user2@host2|method3:user3@host3:/path/to/file"
477 'method)
478 "method3"))
479 (should
480 (string-equal
481 (file-remote-p
482 "/method1:user1@host1|method2:user2@host2|method3:user3@host3:/path/to/file"
483 'user)
484 "user3"))
485 (should
486 (string-equal
487 (file-remote-p
488 "/method1:user1@host1|method2:user2@host2|method3:user3@host3:/path/to/file"
489 'host)
490 "host3"))
491 (should
492 (string-equal
493 (file-remote-p
494 "/method1:user1@host1|method2:user2@host2|method3:user3@host3:/path/to/file"
495 'localname)
496 "/path/to/file"))))
497
498(ert-deftest tramp-test03-file-name-defaults ()
499 "Check default values for some methods."
500 ;; Default values in tramp-adb.el.
501 (should (string-equal (file-remote-p "/adb::" 'host) ""))
502 ;; Default values in tramp-ftp.el.
503 (should (string-equal (file-remote-p "/ftp.host:" 'method) "ftp"))
504 (dolist (u '("ftp" "anonymous"))
505 (should (string-equal (file-remote-p (format "/%s@:" u) 'method) "ftp")))
506 ;; Default values in tramp-gvfs.el.
927fbd6b
MA
507 (when (and (load "tramp-gvfs" 'noerror 'nomessage)
508 (symbol-value 'tramp-gvfs-enabled))
509 (should (string-equal (file-remote-p "/synce::" 'user) nil)))
a213a541
MA
510 ;; Default values in tramp-gw.el.
511 (dolist (m '("tunnel" "socks"))
927fbd6b
MA
512 (should
513 (string-equal (file-remote-p (format "/%s::" m) 'user) (user-login-name))))
a213a541
MA
514 ;; Default values in tramp-sh.el.
515 (dolist (h `("127.0.0.1" "[::1]" "localhost" "localhost6" ,(system-name)))
516 (should (string-equal (file-remote-p (format "/root@%s:" h) 'method) "su")))
517 (dolist (m '("su" "sudo" "ksu"))
518 (should (string-equal (file-remote-p (format "/%s::" m) 'user) "root")))
519 (dolist (m '("rcp" "remcp" "rsh" "telnet" "krlogin" "fcp"))
927fbd6b
MA
520 (should
521 (string-equal (file-remote-p (format "/%s::" m) 'user) (user-login-name))))
a213a541
MA
522 ;; Default values in tramp-smb.el.
523 (should (string-equal (file-remote-p "/user%domain@host:" 'method) "smb"))
524 (should (string-equal (file-remote-p "/smb::" 'user) nil)))
525
526(ert-deftest tramp-test04-substitute-in-file-name ()
527 "Check `substitute-in-file-name'."
528 (should (string-equal (substitute-in-file-name "/method:host://foo") "/foo"))
927fbd6b
MA
529 (should
530 (string-equal
531 (substitute-in-file-name "/method:host:/path//foo") "/method:host:/foo"))
532 (should
533 (string-equal (substitute-in-file-name "/method:host:/path///foo") "/foo"))
534 (should
535 (string-equal
536 (substitute-in-file-name "/method:host:/path/~/foo") "/method:host:~/foo"))
537 (should
538 (string-equal (substitute-in-file-name "/method:host:/path//~/foo") "~/foo"))
a213a541
MA
539 (let (process-environment)
540 (should
927fbd6b
MA
541 (string-equal
542 (substitute-in-file-name "/method:host:/path/$FOO")
543 "/method:host:/path/$FOO"))
a213a541 544 (setenv "FOO" "bla")
927fbd6b
MA
545 (should
546 (string-equal
547 (substitute-in-file-name "/method:host:/path/$FOO")
548 "/method:host:/path/bla"))
549 (should
550 (string-equal
551 (substitute-in-file-name "/method:host:/path/$$FOO")
552 "/method:host:/path/$FOO"))))
a213a541
MA
553
554(ert-deftest tramp-test05-expand-file-name ()
555 "Check `expand-file-name'."
927fbd6b
MA
556 (should
557 (string-equal
558 (expand-file-name "/method:host:/path/./file") "/method:host:/path/file"))
559 (should
560 (string-equal
561 (expand-file-name "/method:host:/path/../file") "/method:host:/file")))
a213a541
MA
562
563(ert-deftest tramp-test06-directory-file-name ()
564 "Check `directory-file-name'.
565This checks also `file-name-as-directory', `file-name-directory'
566and `file-name-nondirectory'."
927fbd6b
MA
567 (should
568 (string-equal
569 (directory-file-name "/method:host:/path/to/file")
570 "/method:host:/path/to/file"))
571 (should
572 (string-equal
573 (directory-file-name "/method:host:/path/to/file/")
574 "/method:host:/path/to/file"))
575 (should
576 (string-equal
577 (file-name-as-directory "/method:host:/path/to/file")
578 "/method:host:/path/to/file/"))
579 (should
580 (string-equal
581 (file-name-as-directory "/method:host:/path/to/file/")
582 "/method:host:/path/to/file/"))
583 (should
584 (string-equal
585 (file-name-directory "/method:host:/path/to/file")
586 "/method:host:/path/to/"))
587 (should
588 (string-equal
589 (file-name-directory "/method:host:/path/to/file/")
590 "/method:host:/path/to/file/"))
591 (should
592 (string-equal (file-name-nondirectory "/method:host:/path/to/file") "file"))
593 (should
594 (string-equal (file-name-nondirectory "/method:host:/path/to/file/") ""))
595 (should-not
596 (file-remote-p
597 (unhandled-file-name-directory "/method:host:/path/to/file"))))
a213a541
MA
598
599(ert-deftest tramp-test07-file-exists-p ()
6865f4d5 600 "Check `file-exist-p', `write-region' and `delete-file'."
1c49d6c2 601 (skip-unless (tramp--test-enabled))
0010ca51 602
1c49d6c2
MA
603 (let ((tmp-name (tramp--test-make-temp-name)))
604 (should-not (file-exists-p tmp-name))
605 (write-region "foo" nil tmp-name)
606 (should (file-exists-p tmp-name))
607 (delete-file tmp-name)
608 (should-not (file-exists-p tmp-name))))
a213a541
MA
609
610(ert-deftest tramp-test08-file-local-copy ()
611 "Check `file-local-copy'."
612 (skip-unless (tramp--test-enabled))
0010ca51 613
a213a541
MA
614 (let ((tmp-name1 (tramp--test-make-temp-name))
615 tmp-name2)
616 (unwind-protect
617 (progn
618 (write-region "foo" nil tmp-name1)
619 (should (setq tmp-name2 (file-local-copy tmp-name1)))
620 (with-temp-buffer
621 (insert-file-contents tmp-name2)
622 (should (string-equal (buffer-string) "foo"))))
623 (ignore-errors
624 (delete-file tmp-name1)
625 (delete-file tmp-name2)))))
626
627(ert-deftest tramp-test09-insert-file-contents ()
628 "Check `insert-file-contents'."
629 (skip-unless (tramp--test-enabled))
0010ca51 630
a213a541
MA
631 (let ((tmp-name (tramp--test-make-temp-name)))
632 (unwind-protect
633 (progn
634 (write-region "foo" nil tmp-name)
635 (with-temp-buffer
636 (insert-file-contents tmp-name)
8ee0219f
MA
637 (should (string-equal (buffer-string) "foo"))
638 (insert-file-contents tmp-name)
639 (should (string-equal (buffer-string) "foofoo"))
640 ;; Insert partly.
641 (insert-file-contents tmp-name nil 1 3)
642 (should (string-equal (buffer-string) "oofoofoo"))
643 ;; Replace.
644 (insert-file-contents tmp-name nil nil nil 'replace)
a213a541
MA
645 (should (string-equal (buffer-string) "foo"))))
646 (ignore-errors (delete-file tmp-name)))))
647
648(ert-deftest tramp-test10-write-region ()
649 "Check `write-region'."
650 (skip-unless (tramp--test-enabled))
0010ca51 651
a213a541
MA
652 (let ((tmp-name (tramp--test-make-temp-name)))
653 (unwind-protect
654 (progn
655 (with-temp-buffer
656 (insert "foo")
657 (write-region nil nil tmp-name))
658 (with-temp-buffer
659 (insert-file-contents tmp-name)
8ee0219f
MA
660 (should (string-equal (buffer-string) "foo")))
661 ;; Append.
662 (with-temp-buffer
663 (insert "bla")
664 (write-region nil nil tmp-name 'append))
665 (with-temp-buffer
666 (insert-file-contents tmp-name)
667 (should (string-equal (buffer-string) "foobla")))
668 ;; Write string.
669 (write-region "foo" nil tmp-name)
670 (with-temp-buffer
671 (insert-file-contents tmp-name)
672 (should (string-equal (buffer-string) "foo")))
673 ;; Write partly.
674 (with-temp-buffer
675 (insert "123456789")
676 (write-region 3 5 tmp-name))
677 (with-temp-buffer
678 (insert-file-contents tmp-name)
2a2e6726 679 (should (string-equal (buffer-string) "34"))))
162427fe 680 (ignore-errors (delete-file tmp-name)))))
a213a541
MA
681
682(ert-deftest tramp-test11-copy-file ()
683 "Check `copy-file'."
684 (skip-unless (tramp--test-enabled))
0010ca51 685
a213a541 686 (let ((tmp-name1 (tramp--test-make-temp-name))
2a2e6726
MA
687 (tmp-name2 (tramp--test-make-temp-name))
688 (tmp-name3 (tramp--test-make-temp-name))
689 (tmp-name4 (tramp--test-make-temp-name 'local))
690 (tmp-name5 (tramp--test-make-temp-name 'local)))
691
692 ;; Copy on remote side.
a213a541
MA
693 (unwind-protect
694 (progn
695 (write-region "foo" nil tmp-name1)
696 (copy-file tmp-name1 tmp-name2)
697 (should (file-exists-p tmp-name2))
698 (with-temp-buffer
699 (insert-file-contents tmp-name2)
2a2e6726
MA
700 (should (string-equal (buffer-string) "foo")))
701 (should-error (copy-file tmp-name1 tmp-name2))
702 (copy-file tmp-name1 tmp-name2 'ok)
703 (make-directory tmp-name3)
704 (copy-file tmp-name1 tmp-name3)
705 (should
706 (file-exists-p
707 (expand-file-name (file-name-nondirectory tmp-name1) tmp-name3))))
708 (ignore-errors (delete-file tmp-name1))
709 (ignore-errors (delete-file tmp-name2))
710 (ignore-errors (delete-directory tmp-name3 'recursive)))
711
712 ;; Copy from remote side to local side.
713 (unwind-protect
714 (progn
715 (write-region "foo" nil tmp-name1)
716 (copy-file tmp-name1 tmp-name4)
717 (should (file-exists-p tmp-name4))
718 (with-temp-buffer
719 (insert-file-contents tmp-name4)
720 (should (string-equal (buffer-string) "foo")))
721 (should-error (copy-file tmp-name1 tmp-name4))
722 (copy-file tmp-name1 tmp-name4 'ok)
723 (make-directory tmp-name5)
724 (copy-file tmp-name1 tmp-name5)
725 (should
726 (file-exists-p
727 (expand-file-name (file-name-nondirectory tmp-name1) tmp-name5))))
728 (ignore-errors (delete-file tmp-name1))
729 (ignore-errors (delete-file tmp-name4))
730 (ignore-errors (delete-directory tmp-name5 'recursive)))
731
732 ;; Copy from local side to remote side.
733 (unwind-protect
734 (progn
735 (write-region "foo" nil tmp-name4 nil 'nomessage)
736 (copy-file tmp-name4 tmp-name1)
737 (should (file-exists-p tmp-name1))
738 (with-temp-buffer
739 (insert-file-contents tmp-name1)
740 (should (string-equal (buffer-string) "foo")))
741 (should-error (copy-file tmp-name4 tmp-name1))
742 (copy-file tmp-name4 tmp-name1 'ok)
743 (make-directory tmp-name3)
744 (copy-file tmp-name4 tmp-name3)
745 (should
746 (file-exists-p
747 (expand-file-name (file-name-nondirectory tmp-name4) tmp-name3))))
748 (ignore-errors (delete-file tmp-name1))
749 (ignore-errors (delete-file tmp-name4))
750 (ignore-errors (delete-directory tmp-name3 'recursive)))))
a213a541
MA
751
752(ert-deftest tramp-test12-rename-file ()
753 "Check `rename-file'."
754 (skip-unless (tramp--test-enabled))
0010ca51 755
a213a541 756 (let ((tmp-name1 (tramp--test-make-temp-name))
2a2e6726
MA
757 (tmp-name2 (tramp--test-make-temp-name))
758 (tmp-name3 (tramp--test-make-temp-name))
759 (tmp-name4 (tramp--test-make-temp-name 'local))
760 (tmp-name5 (tramp--test-make-temp-name 'local)))
761
762 ;; Rename on remote side.
a213a541
MA
763 (unwind-protect
764 (progn
765 (write-region "foo" nil tmp-name1)
766 (rename-file tmp-name1 tmp-name2)
767 (should-not (file-exists-p tmp-name1))
768 (should (file-exists-p tmp-name2))
769 (with-temp-buffer
770 (insert-file-contents tmp-name2)
2a2e6726
MA
771 (should (string-equal (buffer-string) "foo")))
772 (write-region "foo" nil tmp-name1)
773 (should-error (rename-file tmp-name1 tmp-name2))
774 (rename-file tmp-name1 tmp-name2 'ok)
775 (should-not (file-exists-p tmp-name1))
776 (write-region "foo" nil tmp-name1)
777 (make-directory tmp-name3)
778 (rename-file tmp-name1 tmp-name3)
779 (should-not (file-exists-p tmp-name1))
780 (should
781 (file-exists-p
782 (expand-file-name (file-name-nondirectory tmp-name1) tmp-name3))))
783 (ignore-errors (delete-file tmp-name1))
784 (ignore-errors (delete-file tmp-name2))
785 (ignore-errors (delete-directory tmp-name3 'recursive)))
786
787 ;; Rename from remote side to local side.
788 (unwind-protect
789 (progn
790 (write-region "foo" nil tmp-name1)
791 (rename-file tmp-name1 tmp-name4)
792 (should-not (file-exists-p tmp-name1))
793 (should (file-exists-p tmp-name4))
794 (with-temp-buffer
795 (insert-file-contents tmp-name4)
796 (should (string-equal (buffer-string) "foo")))
797 (write-region "foo" nil tmp-name1)
798 (should-error (rename-file tmp-name1 tmp-name4))
799 (rename-file tmp-name1 tmp-name4 'ok)
800 (should-not (file-exists-p tmp-name1))
801 (write-region "foo" nil tmp-name1)
802 (make-directory tmp-name5)
803 (rename-file tmp-name1 tmp-name5)
804 (should-not (file-exists-p tmp-name1))
805 (should
806 (file-exists-p
807 (expand-file-name (file-name-nondirectory tmp-name1) tmp-name5))))
808 (ignore-errors (delete-file tmp-name1))
809 (ignore-errors (delete-file tmp-name4))
810 (ignore-errors (delete-directory tmp-name5 'recursive)))
811
812 ;; Rename from local side to remote side.
813 (unwind-protect
814 (progn
815 (write-region "foo" nil tmp-name4 nil 'nomessage)
816 (rename-file tmp-name4 tmp-name1)
817 (should-not (file-exists-p tmp-name4))
818 (should (file-exists-p tmp-name1))
819 (with-temp-buffer
820 (insert-file-contents tmp-name1)
821 (should (string-equal (buffer-string) "foo")))
822 (write-region "foo" nil tmp-name4 nil 'nomessage)
823 (should-error (rename-file tmp-name4 tmp-name1))
824 (rename-file tmp-name4 tmp-name1 'ok)
825 (should-not (file-exists-p tmp-name4))
826 (write-region "foo" nil tmp-name4 nil 'nomessage)
827 (make-directory tmp-name3)
828 (rename-file tmp-name4 tmp-name3)
829 (should-not (file-exists-p tmp-name4))
830 (should
831 (file-exists-p
832 (expand-file-name (file-name-nondirectory tmp-name4) tmp-name3))))
833 (ignore-errors (delete-file tmp-name1))
834 (ignore-errors (delete-file tmp-name4))
835 (ignore-errors (delete-directory tmp-name3 'recursive)))))
a213a541
MA
836
837(ert-deftest tramp-test13-make-directory ()
838 "Check `make-directory'.
839This tests also `file-directory-p' and `file-accessible-directory-p'."
840 (skip-unless (tramp--test-enabled))
0010ca51 841
a213a541
MA
842 (let ((tmp-name (tramp--test-make-temp-name)))
843 (unwind-protect
844 (progn
845 (make-directory tmp-name)
846 (should (file-directory-p tmp-name))
847 (should (file-accessible-directory-p tmp-name)))
848 (ignore-errors (delete-directory tmp-name)))))
849
850(ert-deftest tramp-test14-delete-directory ()
851 "Check `delete-directory'."
852 (skip-unless (tramp--test-enabled))
0010ca51 853
a213a541
MA
854 (let ((tmp-name (tramp--test-make-temp-name)))
855 ;; Delete empty directory.
856 (make-directory tmp-name)
857 (should (file-directory-p tmp-name))
858 (delete-directory tmp-name)
859 (should-not (file-directory-p tmp-name))
860 ;; Delete non-empty directory.
861 (make-directory tmp-name)
862 (write-region "foo" nil (expand-file-name "bla" tmp-name))
154ba796 863 (should-error (delete-directory tmp-name) :type 'file-error)
a213a541
MA
864 (delete-directory tmp-name 'recursive)
865 (should-not (file-directory-p tmp-name))))
866
867(ert-deftest tramp-test15-copy-directory ()
868 "Check `copy-directory'."
869 (skip-unless (tramp--test-enabled))
0010ca51 870
a213a541
MA
871 (let* ((tmp-name1 (tramp--test-make-temp-name))
872 (tmp-name2 (tramp--test-make-temp-name))
873 (tmp-name3 (expand-file-name
874 (file-name-nondirectory tmp-name1) tmp-name2))
875 (tmp-name4 (expand-file-name "foo" tmp-name1))
876 (tmp-name5 (expand-file-name "foo" tmp-name2))
877 (tmp-name6 (expand-file-name "foo" tmp-name3)))
878 (unwind-protect
879 (progn
880 ;; Copy empty directory.
881 (make-directory tmp-name1)
882 (write-region "foo" nil tmp-name4)
883 (should (file-directory-p tmp-name1))
884 (should (file-exists-p tmp-name4))
5b5774e5 885 (copy-directory tmp-name1 tmp-name2)
a213a541
MA
886 (should (file-directory-p tmp-name2))
887 (should (file-exists-p tmp-name5))
888 ;; Target directory does exist already.
5b5774e5 889 (copy-directory tmp-name1 tmp-name2)
a213a541
MA
890 (should (file-directory-p tmp-name3))
891 (should (file-exists-p tmp-name6)))
3cd4192f
MA
892 (ignore-errors
893 (delete-directory tmp-name1 'recursive)
894 (delete-directory tmp-name2 'recursive)))))
a213a541
MA
895
896(ert-deftest tramp-test16-directory-files ()
897 "Check `directory-files'."
898 (skip-unless (tramp--test-enabled))
0010ca51 899
a213a541
MA
900 (let* ((tmp-name1 (tramp--test-make-temp-name))
901 (tmp-name2 (expand-file-name "bla" tmp-name1))
902 (tmp-name3 (expand-file-name "foo" tmp-name1)))
903 (unwind-protect
904 (progn
905 (make-directory tmp-name1)
906 (write-region "foo" nil tmp-name2)
907 (write-region "bla" nil tmp-name3)
908 (should (file-directory-p tmp-name1))
909 (should (file-exists-p tmp-name2))
910 (should (file-exists-p tmp-name3))
911 (should (equal (directory-files tmp-name1) '("." ".." "bla" "foo")))
912 (should (equal (directory-files tmp-name1 'full)
913 `(,(concat tmp-name1 "/.")
914 ,(concat tmp-name1 "/..")
915 ,tmp-name2 ,tmp-name3)))
916 (should (equal (directory-files
917 tmp-name1 nil directory-files-no-dot-files-regexp)
918 '("bla" "foo")))
919 (should (equal (directory-files
920 tmp-name1 'full directory-files-no-dot-files-regexp)
921 `(,tmp-name2 ,tmp-name3))))
3cd4192f 922 (ignore-errors (delete-directory tmp-name1 'recursive)))))
a213a541
MA
923
924(ert-deftest tramp-test17-insert-directory ()
925 "Check `insert-directory'."
926 (skip-unless (tramp--test-enabled))
0010ca51 927
a213a541
MA
928 (let* ((tmp-name1 (tramp--test-make-temp-name))
929 (tmp-name2 (expand-file-name "foo" tmp-name1)))
930 (unwind-protect
931 (progn
932 (make-directory tmp-name1)
933 (write-region "foo" nil tmp-name2)
934 (should (file-directory-p tmp-name1))
935 (should (file-exists-p tmp-name2))
936 (with-temp-buffer
937 (insert-directory tmp-name1 nil)
938 (goto-char (point-min))
939 (should (looking-at-p (regexp-quote tmp-name1))))
940 (with-temp-buffer
941 (insert-directory tmp-name1 "-al")
942 (goto-char (point-min))
943 (should (looking-at-p (format "^.+ %s$" (regexp-quote tmp-name1)))))
944 (with-temp-buffer
945 (insert-directory (file-name-as-directory tmp-name1) "-al")
946 (goto-char (point-min))
947 (should
948 (looking-at-p (format "^.+ %s/$" (regexp-quote tmp-name1)))))
949 (with-temp-buffer
950 (insert-directory
951 (file-name-as-directory tmp-name1) "-al" nil 'full-directory-p)
952 (goto-char (point-min))
953 (should
76c92fdd 954 (looking-at-p
d9386b0c 955 "\\(total.+[[:digit:]]+\n\\)?.+ \\.\n.+ \\.\\.\n.+ foo$"))))
3cd4192f 956 (ignore-errors (delete-directory tmp-name1 'recursive)))))
a213a541
MA
957
958(ert-deftest tramp-test18-file-attributes ()
959 "Check `file-attributes'.
960This tests also `file-readable-p' and `file-regular-p'."
961 (skip-unless (tramp--test-enabled))
0010ca51 962
a213a541
MA
963 (let ((tmp-name (tramp--test-make-temp-name))
964 attr)
965 (unwind-protect
966 (progn
967 (write-region "foo" nil tmp-name)
968 (should (file-exists-p tmp-name))
969 (setq attr (file-attributes tmp-name))
970 (should (consp attr))
971 (should (file-exists-p tmp-name))
972 (should (file-readable-p tmp-name))
973 (should (file-regular-p tmp-name))
974 ;; We do not test inodes and device numbers.
975 (should (null (car attr)))
976 (should (numberp (nth 1 attr))) ;; Link.
977 (should (numberp (nth 2 attr))) ;; Uid.
978 (should (numberp (nth 3 attr))) ;; Gid.
979 ;; Last access time.
980 (should (stringp (current-time-string (nth 4 attr))))
981 ;; Last modification time.
982 (should (stringp (current-time-string (nth 5 attr))))
983 ;; Last status change time.
984 (should (stringp (current-time-string (nth 6 attr))))
985 (should (numberp (nth 7 attr))) ;; Size.
986 (should (stringp (nth 8 attr))) ;; Modes.
987
988 (setq attr (file-attributes tmp-name 'string))
989 (should (stringp (nth 2 attr))) ;; Uid.
990 (should (stringp (nth 3 attr))) ;; Gid.
991 (delete-file tmp-name)
992
993 (make-directory tmp-name)
994 (should (file-exists-p tmp-name))
995 (should (file-readable-p tmp-name))
996 (should-not (file-regular-p tmp-name))
997 (setq attr (file-attributes tmp-name))
998 (should (eq (car attr) t)))
3cd4192f 999 (ignore-errors (delete-directory tmp-name)))))
a213a541
MA
1000
1001(ert-deftest tramp-test19-directory-files-and-attributes ()
1002 "Check `directory-files-and-attributes'."
1003 (skip-unless (tramp--test-enabled))
0010ca51 1004
154ba796 1005 ;; `directory-files-and-attributes' contains also values for "../".
fbaddd63 1006 ;; Ensure that this doesn't change during tests, for
154ba796
MA
1007 ;; example due to handling temporary files.
1008 (let* ((tmp-name1 (tramp--test-make-temp-name))
1009 (tmp-name2 (expand-file-name "bla" tmp-name1))
1010 attr)
a213a541
MA
1011 (unwind-protect
1012 (progn
154ba796
MA
1013 (make-directory tmp-name1)
1014 (should (file-directory-p tmp-name1))
1015 (make-directory tmp-name2)
1016 (should (file-directory-p tmp-name2))
1017 (write-region "foo" nil (expand-file-name "foo" tmp-name2))
1018 (write-region "bar" nil (expand-file-name "bar" tmp-name2))
1019 (write-region "boz" nil (expand-file-name "boz" tmp-name2))
1020 (setq attr (directory-files-and-attributes tmp-name2))
a213a541 1021 (should (consp attr))
f3a4812c
MA
1022 ;; Dumb remote shells without perl(1) or stat(1) are not
1023 ;; able to return the date correctly. They say "don't know".
a213a541 1024 (dolist (elt attr)
f3a4812c
MA
1025 (unless
1026 (equal
1027 (nth 5
1028 (file-attributes (expand-file-name (car elt) tmp-name2)))
1029 '(0 0))
1030 (should
1031 (equal (file-attributes (expand-file-name (car elt) tmp-name2))
1032 (cdr elt)))))
154ba796 1033 (setq attr (directory-files-and-attributes tmp-name2 'full))
a213a541 1034 (dolist (elt attr)
f3a4812c
MA
1035 (unless (equal (nth 5 (file-attributes (car elt))) '(0 0))
1036 (should
1037 (equal (file-attributes (car elt)) (cdr elt)))))
154ba796 1038 (setq attr (directory-files-and-attributes tmp-name2 nil "^b"))
a213a541 1039 (should (equal (mapcar 'car attr) '("bar" "boz"))))
154ba796 1040 (ignore-errors (delete-directory tmp-name1 'recursive)))))
a213a541
MA
1041
1042(ert-deftest tramp-test20-file-modes ()
1043 "Check `file-modes'.
1044This tests also `file-executable-p', `file-writable-p' and `set-file-modes'."
1045 (skip-unless (tramp--test-enabled))
76c92fdd
MA
1046 (skip-unless
1047 (not
1048 (memq
1049 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory)
154ba796
MA
1050 '(tramp-adb-file-name-handler
1051 tramp-gvfs-file-name-handler
1052 tramp-smb-file-name-handler))))
0010ca51 1053
8ee0219f 1054 (let ((tmp-name (tramp--test-make-temp-name)))
a213a541
MA
1055 (unwind-protect
1056 (progn
8ee0219f
MA
1057 (write-region "foo" nil tmp-name)
1058 (should (file-exists-p tmp-name))
1059 (set-file-modes tmp-name #o777)
1060 (should (= (file-modes tmp-name) #o777))
1061 (should (file-executable-p tmp-name))
1062 (should (file-writable-p tmp-name))
1063 (set-file-modes tmp-name #o444)
1064 (should (= (file-modes tmp-name) #o444))
1065 (should-not (file-executable-p tmp-name))
1066 ;; A file is always writable for user "root".
2a2e6726 1067 (unless (zerop (nth 2 (file-attributes tmp-name)))
8ee0219f 1068 (should-not (file-writable-p tmp-name))))
3cd4192f 1069 (ignore-errors (delete-file tmp-name)))))
a213a541
MA
1070
1071(ert-deftest tramp-test21-file-links ()
1072 "Check `file-symlink-p'.
1073This tests also `make-symbolic-link', `file-truename' and `add-name-to-file'."
1074 (skip-unless (tramp--test-enabled))
0010ca51 1075
a213a541
MA
1076 (let ((tmp-name1 (tramp--test-make-temp-name))
1077 (tmp-name2 (tramp--test-make-temp-name))
2a2e6726 1078 (tmp-name3 (tramp--test-make-temp-name 'local)))
a213a541
MA
1079 (unwind-protect
1080 (progn
1081 (write-region "foo" nil tmp-name1)
1082 (should (file-exists-p tmp-name1))
76c92fdd
MA
1083 ;; Method "smb" supports `make-symbolic-link' only if the
1084 ;; remote host has CIFS capabilities. tramp-adb.el and
1085 ;; tramp-gvfs.el do not support symbolic links at all.
1086 (condition-case err
1087 (make-symbolic-link tmp-name1 tmp-name2)
1088 (file-error
1089 (skip-unless
1090 (not (string-equal (error-message-string err)
1091 "make-symbolic-link not supported")))))
a213a541
MA
1092 (should (file-symlink-p tmp-name2))
1093 (should-error (make-symbolic-link tmp-name1 tmp-name2))
1094 (make-symbolic-link tmp-name1 tmp-name2 'ok-if-already-exists)
1095 (should (file-symlink-p tmp-name2))
1096 ;; `tmp-name3' is a local file name.
1097 (should-error (make-symbolic-link tmp-name1 tmp-name3)))
3cd4192f
MA
1098 (ignore-errors
1099 (delete-file tmp-name1)
1100 (delete-file tmp-name2)))
a213a541
MA
1101
1102 (unwind-protect
1103 (progn
1104 (write-region "foo" nil tmp-name1)
1105 (should (file-exists-p tmp-name1))
1106 (add-name-to-file tmp-name1 tmp-name2)
1107 (should-not (file-symlink-p tmp-name2))
1108 (should-error (add-name-to-file tmp-name1 tmp-name2))
1109 (add-name-to-file tmp-name1 tmp-name2 'ok-if-already-exists)
1110 (should-not (file-symlink-p tmp-name2))
1111 ;; `tmp-name3' is a local file name.
1112 (should-error (add-name-to-file tmp-name1 tmp-name3)))
3cd4192f
MA
1113 (ignore-errors
1114 (delete-file tmp-name1)
1115 (delete-file tmp-name2)))
a213a541
MA
1116
1117 (unwind-protect
1118 (progn
1119 (write-region "foo" nil tmp-name1)
1120 (should (file-exists-p tmp-name1))
1121 (make-symbolic-link tmp-name1 tmp-name2)
1122 (should (file-symlink-p tmp-name2))
927fbd6b
MA
1123 (should-not (string-equal tmp-name2 (file-truename tmp-name2)))
1124 (should
2a2e6726
MA
1125 (string-equal (file-truename tmp-name1) (file-truename tmp-name2)))
1126 (should (file-equal-p tmp-name1 tmp-name2)))
3cd4192f
MA
1127 (ignore-errors
1128 (delete-file tmp-name1)
154ba796
MA
1129 (delete-file tmp-name2)))
1130
1131 ;; `file-truename' shall preserve trailing link of directories.
2a2e6726
MA
1132 (unless (file-symlink-p tramp-test-temporary-file-directory)
1133 (let* ((dir1 (directory-file-name tramp-test-temporary-file-directory))
1134 (dir2 (file-name-as-directory dir1)))
1135 (should (string-equal (file-truename dir1) (expand-file-name dir1)))
1136 (should (string-equal (file-truename dir2) (expand-file-name dir2)))))))
a213a541
MA
1137
1138(ert-deftest tramp-test22-file-times ()
1139 "Check `set-file-times' and `file-newer-than-file-p'."
1140 (skip-unless (tramp--test-enabled))
76c92fdd
MA
1141 (skip-unless
1142 (not
1143 (memq
1144 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory)
1145 '(tramp-gvfs-file-name-handler tramp-smb-file-name-handler))))
0010ca51 1146
a213a541
MA
1147 (let ((tmp-name1 (tramp--test-make-temp-name))
1148 (tmp-name2 (tramp--test-make-temp-name))
1149 (tmp-name3 (tramp--test-make-temp-name)))
1150 (unwind-protect
1151 (progn
1152 (write-region "foo" nil tmp-name1)
1153 (should (file-exists-p tmp-name1))
1154 (should (consp (nth 5 (file-attributes tmp-name1))))
154ba796
MA
1155 ;; '(0 0) means don't know, and will be replaced by
1156 ;; `current-time'. Therefore, we use '(0 1).
1157 ;; We skip the test, if the remote handler is not able to
1158 ;; set the correct time.
1159 (skip-unless (set-file-times tmp-name1 '(0 1)))
f3a4812c
MA
1160 ;; Dumb remote shells without perl(1) or stat(1) are not
1161 ;; able to return the date correctly. They say "don't know".
1162 (unless (equal (nth 5 (file-attributes tmp-name1)) '(0 0))
1163 (should (equal (nth 5 (file-attributes tmp-name1)) '(0 1)))
1164 (write-region "bla" nil tmp-name2)
1165 (should (file-exists-p tmp-name2))
1166 (should (file-newer-than-file-p tmp-name2 tmp-name1))
1167 ;; `tmp-name3' does not exist.
1168 (should (file-newer-than-file-p tmp-name2 tmp-name3))
1169 (should-not (file-newer-than-file-p tmp-name3 tmp-name1))))
3cd4192f
MA
1170 (ignore-errors
1171 (delete-file tmp-name1)
1172 (delete-file tmp-name2)))))
a213a541
MA
1173
1174(ert-deftest tramp-test23-visited-file-modtime ()
1175 "Check `set-visited-file-modtime' and `verify-visited-file-modtime'."
1176 (skip-unless (tramp--test-enabled))
0010ca51 1177
a213a541
MA
1178 (let ((tmp-name (tramp--test-make-temp-name)))
1179 (unwind-protect
1180 (progn
1181 (write-region "foo" nil tmp-name)
1182 (should (file-exists-p tmp-name))
1183 (with-temp-buffer
1184 (insert-file-contents tmp-name)
1185 (should (verify-visited-file-modtime))
1186 (set-visited-file-modtime '(0 1))
1187 (should (verify-visited-file-modtime))
1188 (should (equal (visited-file-modtime) '(0 1 0 0)))))
3cd4192f 1189 (ignore-errors (delete-file tmp-name)))))
a213a541
MA
1190
1191(ert-deftest tramp-test24-file-name-completion ()
1192 "Check `file-name-completion' and `file-name-all-completions'."
1193 (skip-unless (tramp--test-enabled))
0010ca51 1194
a213a541
MA
1195 (let ((tmp-name (tramp--test-make-temp-name)))
1196 (unwind-protect
1197 (progn
1198 (make-directory tmp-name)
1199 (should (file-directory-p tmp-name))
1200 (write-region "foo" nil (expand-file-name "foo" tmp-name))
1201 (write-region "bar" nil (expand-file-name "bold" tmp-name))
1202 (make-directory (expand-file-name "boz" tmp-name))
1203 (should (equal (file-name-completion "fo" tmp-name) "foo"))
1204 (should (equal (file-name-completion "b" tmp-name) "bo"))
1205 (should
1206 (equal (file-name-completion "b" tmp-name 'file-directory-p) "boz/"))
1207 (should (equal (file-name-all-completions "fo" tmp-name) '("foo")))
1208 (should
1209 (equal (sort (file-name-all-completions "b" tmp-name) 'string-lessp)
1210 '("bold" "boz/"))))
3cd4192f 1211 (ignore-errors (delete-directory tmp-name 'recursive)))))
a213a541
MA
1212
1213(ert-deftest tramp-test25-load ()
1214 "Check `load'."
1215 (skip-unless (tramp--test-enabled))
0010ca51 1216
a213a541
MA
1217 (let ((tmp-name (tramp--test-make-temp-name)))
1218 (unwind-protect
1219 (progn
1220 (load tmp-name 'noerror 'nomessage)
1221 (should-not (featurep 'tramp-test-load))
1222 (write-region "(provide 'tramp-test-load)" nil tmp-name)
1223 ;; `load' in lread.c does not pass `must-suffix'. Why?
1224 ;(should-error (load tmp-name nil 'nomessage 'nosuffix 'must-suffix))
1225 (load tmp-name nil 'nomessage 'nosuffix)
1226 (should (featurep 'tramp-test-load)))
3cd4192f
MA
1227 (ignore-errors
1228 (and (featurep 'tramp-test-load) (unload-feature 'tramp-test-load))
1229 (delete-file tmp-name)))))
a213a541
MA
1230
1231(ert-deftest tramp-test26-process-file ()
1232 "Check `process-file'."
1233 (skip-unless (tramp--test-enabled))
76c92fdd
MA
1234 (skip-unless
1235 (not
1236 (memq
1237 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory)
1238 '(tramp-gvfs-file-name-handler tramp-smb-file-name-handler))))
0010ca51 1239
927fbd6b 1240 (let ((tmp-name (tramp--test-make-temp-name))
154ba796
MA
1241 (default-directory tramp-test-temporary-file-directory)
1242 kill-buffer-query-functions)
927fbd6b
MA
1243 (unwind-protect
1244 (progn
1245 ;; We cannot use "/bin/true" and "/bin/false"; those paths
1246 ;; do not exist on hydra.
1247 (should (zerop (process-file "true")))
1248 (should-not (zerop (process-file "false")))
1249 (should-not (zerop (process-file "binary-does-not-exist")))
1250 (with-temp-buffer
1251 (write-region "foo" nil tmp-name)
cad6dfb6
MA
1252 (should (file-exists-p tmp-name))
1253 (should
1254 (zerop
1255 (process-file "ls" nil t nil (file-name-nondirectory tmp-name))))
76c92fdd
MA
1256 ;; `ls' could produce colorized output.
1257 (goto-char (point-min))
1258 (while (re-search-forward tramp-color-escape-sequence-regexp nil t)
1259 (replace-match "" nil nil))
cad6dfb6
MA
1260 (should
1261 (string-equal
1262 (format "%s\n" (file-name-nondirectory tmp-name))
1263 (buffer-string)))))
3cd4192f 1264 (ignore-errors (delete-file tmp-name)))))
a213a541
MA
1265
1266(ert-deftest tramp-test27-start-file-process ()
1267 "Check `start-file-process'."
1268 (skip-unless (tramp--test-enabled))
76c92fdd
MA
1269 (skip-unless
1270 (not
1271 (memq
1272 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory)
154ba796
MA
1273 '(tramp-adb-file-name-handler
1274 tramp-gvfs-file-name-handler
1275 tramp-smb-file-name-handler))))
0010ca51 1276
a213a541
MA
1277 (let ((default-directory tramp-test-temporary-file-directory)
1278 (tmp-name (tramp--test-make-temp-name))
1279 kill-buffer-query-functions proc)
1280 (unwind-protect
1281 (with-temp-buffer
1282 (setq proc (start-file-process "test1" (current-buffer) "cat"))
1283 (should (processp proc))
1284 (should (equal (process-status proc) 'run))
1285 (process-send-string proc "foo")
1286 (process-send-eof proc)
1287 (accept-process-output proc 1)
1288 (should (string-equal (buffer-string) "foo")))
3cd4192f 1289 (ignore-errors (delete-process proc)))
a213a541
MA
1290
1291 (unwind-protect
1292 (with-temp-buffer
1293 (write-region "foo" nil tmp-name)
1294 (should (file-exists-p tmp-name))
1295 (setq proc
1296 (start-file-process
1297 "test2" (current-buffer)
1298 "cat" (file-name-nondirectory tmp-name)))
1299 (should (processp proc))
1300 (accept-process-output proc 1)
1301 (should (string-equal (buffer-string) "foo")))
3cd4192f
MA
1302 (ignore-errors
1303 (delete-process proc)
1304 (delete-file tmp-name)))
a213a541
MA
1305
1306 (unwind-protect
1307 (progn
1308 (setq proc (start-file-process "test3" nil "cat"))
1309 (should (processp proc))
1310 (should (equal (process-status proc) 'run))
1311 (set-process-filter
cad6dfb6 1312 proc (lambda (_p s) (should (string-equal s "foo"))))
a213a541
MA
1313 (process-send-string proc "foo")
1314 (process-send-eof proc)
1315 (accept-process-output proc 1))
3cd4192f 1316 (ignore-errors (delete-process proc)))))
a213a541
MA
1317
1318(ert-deftest tramp-test28-shell-command ()
1319 "Check `shell-command'."
1320 (skip-unless (tramp--test-enabled))
76c92fdd
MA
1321 (skip-unless
1322 (not
1323 (memq
1324 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory)
154ba796
MA
1325 '(tramp-adb-file-name-handler
1326 tramp-gvfs-file-name-handler
1327 tramp-smb-file-name-handler))))
0010ca51 1328
927fbd6b 1329 (let ((tmp-name (tramp--test-make-temp-name))
154ba796
MA
1330 (default-directory tramp-test-temporary-file-directory)
1331 kill-buffer-query-functions)
927fbd6b
MA
1332 (unwind-protect
1333 (with-temp-buffer
5a327e99 1334 (write-region "foo" nil tmp-name)
cad6dfb6
MA
1335 (should (file-exists-p tmp-name))
1336 (shell-command
1337 (format "ls %s" (file-name-nondirectory tmp-name)) (current-buffer))
76c92fdd
MA
1338 ;; `ls' could produce colorized output.
1339 (goto-char (point-min))
1340 (while (re-search-forward tramp-color-escape-sequence-regexp nil t)
1341 (replace-match "" nil nil))
cad6dfb6
MA
1342 (should
1343 (string-equal
1344 (format "%s\n" (file-name-nondirectory tmp-name)) (buffer-string))))
1345 (ignore-errors (delete-file tmp-name)))
1346
1347 (unwind-protect
1348 (with-temp-buffer
5a327e99 1349 (write-region "foo" nil tmp-name)
cad6dfb6
MA
1350 (should (file-exists-p tmp-name))
1351 (async-shell-command
1352 (format "ls %s" (file-name-nondirectory tmp-name)) (current-buffer))
76c92fdd 1353 (accept-process-output (get-buffer-process (current-buffer)) 1)
154ba796
MA
1354 (with-timeout (10 (ert-fail "`async-shell-command' timed out"))
1355 (while
1356 (ignore-errors
1357 (memq (process-status (get-buffer-process (current-buffer)))
1358 '(run open)))
1359 (accept-process-output (get-buffer-process (current-buffer)) 1)))
76c92fdd
MA
1360 ;; `ls' could produce colorized output.
1361 (goto-char (point-min))
1362 (while (re-search-forward tramp-color-escape-sequence-regexp nil t)
1363 (replace-match "" nil nil))
cad6dfb6
MA
1364 (should
1365 (string-equal
1366 (format "%s\n" (file-name-nondirectory tmp-name)) (buffer-string))))
1367 (ignore-errors (delete-file tmp-name)))
1368
1369 (unwind-protect
1370 (with-temp-buffer
1371 (write-region "foo" nil tmp-name)
1372 (should (file-exists-p tmp-name))
1373 (async-shell-command "read line; ls $line" (current-buffer))
1374 (process-send-string
1375 (get-buffer-process (current-buffer))
1376 (format "%s\n" (file-name-nondirectory tmp-name)))
76c92fdd 1377 (accept-process-output (get-buffer-process (current-buffer)) 1)
154ba796
MA
1378 (with-timeout (10 (ert-fail "`async-shell-command' timed out"))
1379 (while
1380 (ignore-errors
1381 (memq (process-status (get-buffer-process (current-buffer)))
1382 '(run open)))
1383 (accept-process-output (get-buffer-process (current-buffer)) 1)))
cad6dfb6
MA
1384 (should
1385 (string-equal
1386 (format "%s\n" (file-name-nondirectory tmp-name)) (buffer-string))))
3cd4192f
MA
1387 (ignore-errors (delete-file tmp-name)))))
1388
581d24e7
MA
1389(ert-deftest tramp-test29-vc-registered ()
1390 "Check `vc-registered'."
1391 (skip-unless (tramp--test-enabled))
1392 (skip-unless
1393 (eq
1394 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory)
1395 'tramp-sh-file-name-handler))
581d24e7
MA
1396
1397 (let* ((default-directory tramp-test-temporary-file-directory)
1398 (tmp-name1 (tramp--test-make-temp-name))
1399 (tmp-name2 (expand-file-name "foo" tmp-name1))
1400 (vc-handled-backends
1401 (with-parsed-tramp-file-name tramp-test-temporary-file-directory nil
1402 (cond
1403 ((tramp-find-executable v vc-bzr-program (tramp-get-remote-path v))
1404 '(Bzr))
1405 ((tramp-find-executable v vc-git-program (tramp-get-remote-path v))
1406 '(Git))
1407 ((tramp-find-executable v vc-hg-program (tramp-get-remote-path v))
1408 '(Hg))
1409 (t nil)))))
1410 (skip-unless vc-handled-backends)
1411 (message "%s" vc-handled-backends)
1412
1413 (unwind-protect
1414 (progn
1415 (make-directory tmp-name1)
1416 (write-region "foo" nil tmp-name2)
1417 (should (file-directory-p tmp-name1))
1418 (should (file-exists-p tmp-name2))
1419 (should-not (vc-registered tmp-name1))
1420 (should-not (vc-registered tmp-name2))
1421
1422 (let ((default-directory tmp-name1))
1423 ;; Create empty repository, and register the file.
1424 (vc-create-repo (car vc-handled-backends))
1425 ;; The structure of VC-FILESET is not documented. Let's
1426 ;; hope it won't change.
1427 (vc-register
1428 nil (list (car vc-handled-backends)
1429 (list (file-name-nondirectory tmp-name2)))))
1430 (should (vc-registered tmp-name2)))
1431
1432 (ignore-errors (delete-directory tmp-name1 'recursive)))))
1433
2a2e6726
MA
1434(defun tramp--test-check-files (&rest files)
1435 "Runs a simple but comprehensive test over every file in FILES."
5305137a
MA
1436 (let ((tmp-name1 (tramp--test-make-temp-name))
1437 (tmp-name2 (tramp--test-make-temp-name 'local)))
3cd4192f
MA
1438 (unwind-protect
1439 (progn
5305137a
MA
1440 (make-directory tmp-name1)
1441 (make-directory tmp-name2)
2a2e6726 1442 (dolist (elt files)
5305137a
MA
1443 (let ((file1 (expand-file-name elt tmp-name1))
1444 (file2 (expand-file-name elt tmp-name2)))
1445 (write-region elt nil file1)
1446 (should (file-exists-p file1))
3cd4192f
MA
1447 ;; Check file contents.
1448 (with-temp-buffer
5305137a
MA
1449 (insert-file-contents file1)
1450 (should (string-equal (buffer-string) elt)))
1451 ;; Copy file both directions.
1452 (copy-file file1 tmp-name2)
1453 (should (file-exists-p file2))
1454 (delete-file file1)
1455 (should-not (file-exists-p file1))
1456 (copy-file file2 tmp-name1)
1457 (should (file-exists-p file1))))
0010ca51 1458 ;; Check file names.
82407168 1459 (should (equal (directory-files
5305137a
MA
1460 tmp-name1 nil directory-files-no-dot-files-regexp)
1461 (sort (copy-sequence files) 'string-lessp)))
1462 (should (equal (directory-files
1463 tmp-name2 nil directory-files-no-dot-files-regexp)
f3a4812c 1464 (sort files 'string-lessp))))
5305137a 1465 (ignore-errors (delete-directory tmp-name1 'recursive))
d5ff4ded 1466 (ignore-errors (delete-directory tmp-name2 'recursive)))))
a213a541 1467
2a2e6726
MA
1468;; This test is inspired by Bug#17238.
1469(ert-deftest tramp-test30-special-characters ()
1470 "Check special characters in file names."
1471 (skip-unless (tramp--test-enabled))
1472
84b2095c
MA
1473 ;; Newlines, slashes and backslashes in file names are not supported.
1474 ;; So we don't test.
2a2e6726 1475 (tramp--test-check-files
84b2095c 1476 " foo\tbar baz\t"
2a2e6726
MA
1477 "$foo$bar$$baz$"
1478 "-foo-bar-baz-"
1479 "%foo%bar%baz%"
1480 "&foo&bar&baz&"
1481 "?foo?bar?baz?"
1482 "*foo*bar*baz*"
1483 "'foo\"bar'baz\""
2a2e6726
MA
1484 "#foo#bar#baz#"
1485 "!foo|bar!baz|"
1486 ":foo;bar:baz;"
1487 "<foo>bar<baz>"
1488 "(foo)bar(baz)"))
1489
1490(ert-deftest tramp-test31-utf8 ()
1491 "Check UTF8 encoding in file names and file contents."
1492 (skip-unless (tramp--test-enabled))
1493
1494 (let ((coding-system-for-read 'utf-8)
5305137a
MA
1495 (coding-system-for-write 'utf-8)
1496 (file-name-coding-system 'utf-8))
1497 (tramp--test-check-files
1498 "أصبح بوسعك الآن تنزيل نسخة كاملة من موسوعة ويكيبيديا العربية لتصفحها بلا اتصال بالإنترنت"
1499 "银河系漫游指南系列"
1500 "Автостопом по гала́ктике")))
2a2e6726 1501
162427fe 1502;; This test is inspired by Bug#16928.
2a2e6726 1503(ert-deftest tramp-test32-asynchronous-requests ()
162427fe
MA
1504 "Check parallel asynchronous requests.
1505Such requests could arrive from timers, process filters and
1506process sentinels. They shall not disturb each other."
1507 ;; Mark as failed until bug has been fixed.
1508 :expected-result :failed
1509 (skip-unless (tramp--test-enabled))
1510 (skip-unless
1511 (eq
1512 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory)
1513 'tramp-sh-file-name-handler))
1514
1515 ;; Keep instrumentation verbosity 0 until Tramp bug is fixed. This
1516 ;; has the side effect, that this test fails instead to abort. Good
1517 ;; for hydra.
1518 (tramp--instrument-test-case 0
1519 (let* ((tmp-name (tramp--test-make-temp-name))
1520 (default-directory tmp-name)
1521 (remote-file-name-inhibit-cache t)
1522 timer buffers kill-buffer-query-functions)
1523
1524 (unwind-protect
1525 (progn
1526 (make-directory tmp-name)
1527
1528 ;; Setup a timer in order to raise an ordinary command again
1529 ;; and again. `vc-registered' is well suited, because there
1530 ;; are many checks.
1531 (setq
1532 timer
1533 (run-at-time
1534 0 1
1535 (lambda ()
1536 (when buffers
1537 (vc-registered
1538 (buffer-name (nth (random (length buffers)) buffers)))))))
1539
1540 ;; Create temporary buffers. The number of buffers
1541 ;; corresponds to the number of processes; it could be
1542 ;; increased in order to make pressure on Tramp.
1543 (dotimes (i 5)
1544 (add-to-list 'buffers (generate-new-buffer "*temp*")))
1545
1546 ;; Open asynchronous processes. Set process sentinel.
1547 (dolist (buf buffers)
1548 (async-shell-command "read line; touch $line; echo $line" buf)
1549 (set-process-sentinel
1550 (get-buffer-process buf)
1551 (lambda (proc _state)
1552 (delete-file (buffer-name (process-buffer proc))))))
1553
1554 ;; Send a string. Use a random order of the buffers. Mix
1555 ;; with regular operation.
1556 (let ((buffers (copy-sequence buffers))
1557 buf)
1558 (while buffers
1559 (setq buf (nth (random (length buffers)) buffers))
1560 (process-send-string
1561 (get-buffer-process buf) (format "'%s'\n" buf))
1562 (file-attributes (buffer-name buf))
1563 (setq buffers (delq buf buffers))))
1564
1565 ;; Wait until the whole output has been read.
1566 (with-timeout ((* 10 (length buffers))
1567 (ert-fail "`async-shell-command' timed out"))
1568 (let ((buffers (copy-sequence buffers))
1569 buf)
1570 (while buffers
1571 (setq buf (nth (random (length buffers)) buffers))
1572 (if (ignore-errors
1573 (memq (process-status (get-buffer-process buf))
1574 '(run open)))
1575 (accept-process-output (get-buffer-process buf) 0.1)
1576 (setq buffers (delq buf buffers))))))
1577
1578 ;; Check.
1579 (dolist (buf buffers)
1580 (with-current-buffer buf
1581 (should
1582 (string-equal (format "'%s'\n" buf) (buffer-string)))))
1583 (should-not
1584 (directory-files tmp-name nil directory-files-no-dot-files-regexp)))
1585
1586 ;; Cleanup.
1587 (ignore-errors (cancel-timer timer))
1588 (ignore-errors (delete-directory tmp-name 'recursive))
1589 (dolist (buf buffers)
1590 (ignore-errors (kill-buffer buf)))))))
1591
2a2e6726
MA
1592(ert-deftest tramp-test33-recursive-load ()
1593 "Check that Tramp does not fail due to recursive load."
1594 (skip-unless (tramp--test-enabled))
1595
1596 (dolist (code
1597 (list
1598 (format
c0b9bc72 1599 "(expand-file-name %S)"
2a2e6726
MA
1600 tramp-test-temporary-file-directory)
1601 (format
1602 "(let ((default-directory %S)) (expand-file-name %S))"
1603 tramp-test-temporary-file-directory
1604 temporary-file-directory)))
1605 (should-not
1606 (string-match
1607 "Recursive load"
1608 (shell-command-to-string
1609 (format
1610 "%s -batch -Q -L %s --eval %s"
1611 (expand-file-name invocation-name invocation-directory)
1612 (mapconcat 'shell-quote-argument load-path " -L ")
1613 (shell-quote-argument code)))))))
1614
1615(ert-deftest tramp-test34-unload ()
1616 "Check that Tramp and its subpackages unload completely.
1617Since it unloads Tramp, it shall be the last test to run."
1618 ;; Mark as failed until all symbols are unbound.
1619 :expected-result (if (featurep 'tramp) :failed :passed)
1620 (when (featurep 'tramp)
1621 (unload-feature 'tramp 'force)
1622 ;; No Tramp feature must be left.
1623 (should-not (featurep 'tramp))
1624 (should-not (all-completions "tramp" (delq 'tramp-tests features)))
1625 ;; `file-name-handler-alist' must be clean.
1626 (should-not (all-completions "tramp" (mapcar 'cdr file-name-handler-alist)))
1627 ;; There shouldn't be left a bound symbol. We do not regard our
1628 ;; test symbols, and the Tramp unload hooks.
1629 (mapatoms
1630 (lambda (x)
1631 (and (or (boundp x) (functionp x))
1632 (string-match "^tramp" (symbol-name x))
1633 (not (string-match "^tramp--?test" (symbol-name x)))
1634 (not (string-match "unload-hook$" (symbol-name x)))
1635 (ert-fail (format "`%s' still bound" x)))))
1636; (progn (message "`%s' still bound" x)))))
1637 ;; There shouldn't be left a hook function containing a Tramp
1638 ;; function. We do not regard the Tramp unload hooks.
1639 (mapatoms
1640 (lambda (x)
1641 (and (boundp x)
1642 (string-match "-hooks?$" (symbol-name x))
1643 (not (string-match "unload-hook$" (symbol-name x)))
1644 (consp (symbol-value x))
1645 (ignore-errors (all-completions "tramp" (symbol-value x)))
1646 (ert-fail (format "Hook `%s' still contains Tramp function" x)))))))
1647
a213a541
MA
1648;; TODO:
1649
1650;; * dired-compress-file
1651;; * dired-uncache
1652;; * file-acl
1653;; * file-ownership-preserved-p
1654;; * file-selinux-context
1655;; * find-backup-file-name
1656;; * make-auto-save-file-name
1657;; * set-file-acl
1658;; * set-file-selinux-context
a213a541 1659
76c92fdd 1660;; * Fix `tramp-test27-start-file-process' on MS Windows (`process-send-eof'?).
162427fe 1661;; * Fix `tramp-test28-shell-command' on MS Windows (nasty plink message).
2a2e6726
MA
1662;; * Fix `tramp-test31-utf8' for MS Windows and `nc'/`telnet' (when
1663;; target is a dumb busybox). Seems to be in `directory-files'.
1664;; * Fix Bug#16928. Set expected error of `tramp-test32-asynchronous-requests'.
1665;; * Fix `tramp-test34-unload' (Not all symbols are unbound). Set
1666;; expected error.
1baa1e49 1667
a213a541
MA
1668(defun tramp-test-all (&optional interactive)
1669 "Run all tests for \\[tramp]."
1670 (interactive "p")
1671 (funcall
1672 (if interactive 'ert-run-tests-interactively 'ert-run-tests-batch) "^tramp"))
1673
1674(provide 'tramp-tests)
1675;;; tramp-tests.el ends here