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