Don't use "ansify" for self-hosted build.
[sbcl.git] / tests / pathnames.impure.lisp
blob0c0f223907d5ad3dfbb7d5cb2935556075fe7efb
1 ;;;; miscellaneous tests of pathname-related stuff
3 ;;;; This file is naturally impure because we mess with
4 ;;;; LOGICAL-PATHNAME-TRANSLATIONS.
6 ;;;; This software is part of the SBCL system. See the README file for
7 ;;;; more information.
8 ;;;;
9 ;;;; While most of SBCL is derived from the CMU CL system, the test
10 ;;;; files (like this one) were written from scratch after the fork
11 ;;;; from CMU CL.
12 ;;;;
13 ;;;; This software is in the public domain and is provided with
14 ;;;; absolutely no warranty. See the COPYING and CREDITS files for
15 ;;;; more information.
17 (load "assertoid.lisp")
18 (use-package "ASSERTOID")
20 (load "test-util.lisp")
21 (use-package "TEST-UTIL")
23 (setf (logical-pathname-translations "demo0")
24 '(("**;*.*.*" "/tmp/")))
26 ;;; In case of a parse error we want to get a condition of type TYPE-ERROR,
27 ;;; because ANSI says so. (This used to be PARSE-ERROR.)
28 (with-test (:name (logical-pathname :signals type-error))
29 (mapc (lambda (namestring)
30 (assert-error (logical-pathname namestring) type-error))
31 '("demo0::bla;file.lisp"
32 "FOO.txt"
33 "SYS:%")))
35 ;;; some things SBCL-0.6.9 used not to parse correctly:
36 ;;;
37 ;;; SBCL used to throw an error saying there's no translation.
38 (with-test (:name (:logical-pathname 1))
39 (assert (equal (namestring (translate-logical-pathname "demo0:file.lisp"))
40 "/tmp/file.lisp")))
42 ;;; We do not match a null directory to every wild path:
43 (with-test (:name (:logical-pathname 2))
44 (assert (not (pathname-match-p "demo0:file.lisp"
45 (logical-pathname "demo0:tmp;**;*.*.*")))))
47 ;;; Remove "**" from our resulting pathname when the source-dir is NIL:
48 (with-test (:name (:logical-pathname 3))
49 (setf (logical-pathname-translations "demo1")
50 '(("**;*.*.*" "/tmp/**/*.*") (";**;*.*.*" "/tmp/rel/**/*.*")))
51 (assert (not (equal (namestring (translate-logical-pathname "demo1:foo.lisp"))
52 "/tmp/**/foo.lisp"))))
54 ;;; That should be correct:
55 (with-test (:name (:logical-pathname 4))
56 (assert (equal (namestring (translate-logical-pathname "demo1:foo.lisp"))
57 "/tmp/foo.lisp")))
59 ;;; Check for absolute/relative path confusion:
60 (with-test (:name (:logical-pathname 5))
61 (assert (not (equal (namestring (translate-logical-pathname "demo1:;foo.lisp"))
62 "tmp/rel/foo.lisp")))
63 (assert (equal (namestring (translate-logical-pathname "demo1:;foo.lisp"))
64 "/tmp/rel/foo.lisp")))
66 ;;; Under SBCL: new function #'UNPARSE-ENOUGH-NAMESTRING, to
67 ;;; handle the following case exactly (otherwise we get an error:
68 ;;; "#'IDENTITY CALLED WITH 2 ARGS."
69 (with-test (:name (:logical-pathname 6))
70 (setf (logical-pathname-translations "demo2")
71 '(("test;**;*.*" "/tmp/demo2/test")))
72 (enough-namestring "demo2:test;foo.lisp"))
74 ;;; When a pathname comes from a logical host, it should be in upper
75 ;;; case. (This doesn't seem to be specifically required in the ANSI
76 ;;; spec, but it's left up to the implementors, and the arguments made
77 ;;; in the cleanup issue PATHNAME-LOGICAL:ADD seem to be a pretty
78 ;;; compelling reason for the implementors to choose case
79 ;;; insensitivity and a canonical case.)
80 (with-test (:name (:logical-pathname 7))
81 (setf (logical-pathname-translations "FOO")
82 '(("**;*.*.*" "/full/path/to/foo/**/*.*")))
83 (let* ((pn1 (make-pathname :host "FOO" :directory "etc" :name "INETD"
84 :type "conf"))
85 (pn2 (make-pathname :host "foo" :directory "ETC" :name "inetd"
86 :type "CONF"))
87 (pn3 (read-from-string (prin1-to-string pn1))))
88 (assert (equal pn1 pn2))
89 (assert (equal pn1 pn3))))
91 ;;; In addition to the upper-case constraint above, if the logical-pathname
92 ;;; contains a string component in e.g. the directory, name and type slot,
93 ;;; these should be valid "WORDS", according to CLHS 19.3.1.
94 ;;; FIXME: currently SBCL throws NAMESTRING-PARSE-ERROR: should this be
95 ;;; a TYPE-ERROR?
96 (with-test (:name (:logical-pathname 8))
97 (locally
98 ;; MAKE-PATHNAME is UNSAFELY-FLUSHABLE
99 (declare (optimize safety))
101 (assert (not (ignore-errors
102 (make-pathname :host "FOO" :directory "!bla" :name "bar"))))
104 ;; error: name-component not valid
105 (assert (not (ignore-errors
106 (make-pathname :host "FOO" :directory "bla" :name "!bar"))))
108 ;; error: type-component not valid.
109 (assert (not (ignore-errors
110 (make-pathname :host "FOO" :directory "bla" :name "bar"
111 :type "&baz"))))))
113 ;;; We may need to parse the host as a LOGICAL-NAMESTRING HOST. The
114 ;;; HOST in PARSE-NAMESTRING can be either a string or :UNSPECIFIC
115 ;;; without actually requiring the system to signal an error (apart
116 ;;; from host mismatches).
117 (with-test (:name (:logical-pathname 9))
118 (assert (equal (namestring (parse-namestring "" "FOO")) "FOO:"))
119 (assert (equal (namestring (parse-namestring "" :unspecific)) "")))
121 ;;; The third would work if the call were (and it should continue to
122 ;;; work ...)
123 (with-test (:name (:logical-pathname 10))
124 (parse-namestring ""
125 (pathname-host
126 (translate-logical-pathname
127 "FOO:"))))
129 ;;; ANSI says PARSE-NAMESTRING returns TYPE-ERROR on host mismatch.
130 (with-test (:name (:logical-pathname 11))
131 (let ((cond (grab-condition (parse-namestring "foo:jeamland" "demo2"))))
132 (assert (typep cond 'type-error))))
134 ;;; turning one logical pathname into another:
135 (with-test (:name (:logical-pathname 12))
136 (setf (logical-pathname-translations "foo")
137 '(("todemo;*.*.*" "demo0:*.*.*")))
138 (assert (equal (namestring (translate-logical-pathname "foo:todemo;x.y"))
139 (namestring (translate-logical-pathname "demo0:x.y")))))
141 ;;; ANSI, in its wisdom, specifies that it's an error (specifically a
142 ;;; TYPE-ERROR) to query the system about the translations of a string
143 ;;; which doesn't have any translations. It's not clear why we don't
144 ;;; just return NIL in that case, but they make the rules..
145 (with-test (:name (:logical-pathname 13))
146 (let ((cond (grab-condition (logical-pathname-translations "unregistered-host"))))
147 (assert (typep cond 'type-error)))
149 (assert (not (string-equal (host-namestring (parse-namestring "OTHER-HOST:ILLEGAL/LPN")) "OTHER-HOST")))
150 (assert (string-equal (pathname-name (parse-namestring "OTHER-HOST:ILLEGAL/LPN")) "LPN")))
152 ;;; FIXME: A comment on this section up to sbcl-0.6.11.30 or so said
153 ;;; examples from CLHS: Section 19.4, LOGICAL-PATHNAME-TRANSLATIONS
154 ;;; (sometimes converted to the Un*x way of things)
155 ;;; but when I looked it up I didn't see the connection. Presumably
156 ;;; there's some code in this section which should be attributed
157 ;;; to something in the ANSI spec, but I don't know what code it is
158 ;;; or what section of the specification has the related code.
159 (with-test (:name (:logical-pathname 14))
160 (setf (logical-pathname-translations "test0")
161 '(("**;*.*.*" "/library/foo/**/")))
162 (assert (equal (namestring (translate-logical-pathname
163 "test0:foo;bar;baz;mum.quux"))
164 "/library/foo/foo/bar/baz/mum.quux"))
165 (setf (logical-pathname-translations "prog")
166 '(("RELEASED;*.*.*" "MY-UNIX:/sys/bin/my-prog/")
167 ("RELEASED;*;*.*.*" "MY-UNIX:/sys/bin/my-prog/*/")
168 ("EXPERIMENTAL;*.*.*" "MY-UNIX:/usr/Joe/development/prog/")
169 ("EXPERIMENTAL;*;*.*.*" "MY-UNIX:/usr/Joe/development/prog/*/")))
170 (setf (logical-pathname-translations "prog")
171 '(("CODE;*.*.*" "/lib/prog/")))
172 (assert (equal (namestring (translate-logical-pathname
173 "prog:code;documentation.lisp"))
174 "/lib/prog/documentation.lisp"))
175 (setf (logical-pathname-translations "prog")
176 '(("CODE;DOCUMENTATION.*.*" "/lib/prog/docum.*")
177 ("CODE;*.*.*" "/lib/prog/")))
178 (assert (equal (namestring (translate-logical-pathname
179 "prog:code;documentation.lisp"))
180 "/lib/prog/docum.lisp")))
182 ;;; ANSI section 19.3.1.1.5 specifies that translation to a filesystem
183 ;;; which doesn't have versions should ignore the version slot. CMU CL
184 ;;; didn't ignore this as it should, but we do.
185 (with-test (:name (:logical-pathname 15))
186 (assert (equal (namestring (translate-logical-pathname
187 "test0:foo;bar;baz;mum.quux.3"))
188 "/library/foo/foo/bar/baz/mum.quux")))
191 ;;;; MERGE-PATHNAME tests
192 ;;;;
193 ;;;; There are some things we don't bother testing, just because they're
194 ;;;; not meaningful on the underlying filesystem anyway.
195 ;;;;
196 ;;;; Mostly that means that we don't do devices, we don't do versions
197 ;;;; except minimally in LPNs (they get lost in the translation to
198 ;;;; physical hosts, so it's not much of an issue), and we don't do
199 ;;;; hosts except for LPN hosts
200 ;;;;
201 ;;;; Although these tests could conceivably be useful in principle for
202 ;;;; other implementations, they depend quite heavily on the rules for
203 ;;;; namestring parsing, which are implementation-specific. So, success
204 ;;;; or failure in these tests doesn't tell you anything about
205 ;;;; ANSI-compliance unless your PARSE-NAMESTRING works like ours.
207 ;;; Needs to be done at compile time, so that the #p"" read-macro
208 ;;; correctly parses things as logical pathnames. This is not a
209 ;;; problem as was, as this is an impure file and so gets loaded in,
210 ;;; but just for future proofing...
211 (eval-when (:compile-toplevel :load-toplevel :execute)
212 (setf (logical-pathname-translations "scratch")
213 '(("**;*.*.*" "/usr/local/doc/**/*"))))
215 (with-test (:name (merge-pathnames 1))
216 (loop for (expected-result . params) in
217 `( ;; trivial merge
218 (#P"/usr/local/doc/foo" #p"foo" #p"/usr/local/doc/")
219 ;; If pathname does not specify a host, device, directory,
220 ;; name, or type, each such component is copied from
221 ;; default-pathname.
222 ;; 1) no name, no type
223 (#p"/supplied-dir/name.type" #p"/supplied-dir/" #p"/dir/name.type")
224 ;; 2) no directory, no type
225 (#p"/dir/supplied-name.type" #p"supplied-name" #p"/dir/name.type")
226 ;; 3) no name, no dir (must use make-pathname as ".foo" is parsed
227 ;; as a name)
228 (#p"/dir/name.supplied-type"
229 ,(make-pathname :type "supplied-type")
230 #p"/dir/name.type")
231 ;; If (pathname-directory pathname) is a list whose car is
232 ;; :relative, and (pathname-directory default-pathname) is a
233 ;; list, then the merged directory is [...]
234 (#p"/aaa/bbb/ccc/ddd/qqq/www" #p"qqq/www" #p"/aaa/bbb/ccc/ddd/eee")
235 ;; except that if the resulting list contains a string or
236 ;; :wild immediately followed by :back, both of them are
237 ;; removed.
238 (#P"/aaa/bbb/ccc/blah/eee"
239 ;; "../" in a namestring is parsed as :up not :back, so make-pathname
240 ,(make-pathname :directory '(:relative :back "blah"))
241 #p"/aaa/bbb/ccc/ddd/eee")
242 ;; If (pathname-directory default-pathname) is not a list or
243 ;; (pathname-directory pathname) is not a list whose car is
244 ;; :relative, the merged directory is (or (pathname-directory
245 ;; pathname) (pathname-directory default-pathname))
246 (#P"/absolute/path/name.type"
247 #p"/absolute/path/name"
248 #p"/dir/default-name.type")
249 ;; === logical pathnames ===
250 ;; recognizes a logical pathname namestring when
251 ;; default-pathname is a logical pathname
252 ;; FIXME: 0.6.12.23 fails this one.
254 ;; And, as it happens, it's right to fail it. Because
255 ;; #p"name1" is read in with the ambient *d-p-d* value, which
256 ;; has a physical (Unix) host; therefore, the host of the
257 ;; default-pathname argument to merge-pathnames is
258 ;; irrelevant. The result is (correctly) different if
259 ;; '#p"name1"' is replaced by "name1", below, though it's
260 ;; still not what one might expect... -- CSR, 2002-05-09
261 #+nil (#P"scratch:foo;name1" #p"name1" #p"scratch:foo;")
262 ;; or when the namestring begins with the name of a defined
263 ;; logical host followed by a colon [I assume that refers to pathname
264 ;; rather than default-pathname]
265 (#p"SCRATCH:FOO;NAME2" #p"scratch:;name2" #p"scratch:foo;")
266 ;; conduct the previous set of tests again, with a lpn first argument
267 (#P"SCRATCH:USR;LOCAL;DOC;FOO" #p"scratch:;foo" #p"/usr/local/doc/")
268 (#p"SCRATCH:SUPPLIED-DIR;NAME.TYPE"
269 #p"scratch:supplied-dir;"
270 #p"/dir/name.type")
271 (#p"SCRATCH:DIR;SUPPLIED-NAME.TYPE"
272 #p"scratch:;supplied-name"
273 #p"/dir/name.type")
274 (#p"SCRATCH:DIR;NAME.SUPPLIED-TYPE"
275 ,(make-pathname :host "scratch" :type "supplied-type")
276 #p"/dir/name.type")
277 (#p"SCRATCH:AAA;BBB;CCC;DDD;FOO;BAR"
278 ,(make-pathname :host "scratch"
279 :directory '(:relative "foo")
280 :name "bar")
281 #p"/aaa/bbb/ccc/ddd/eee")
282 (#p"SCRATCH:AAA;BBB;CCC;FOO;BAR"
283 ,(make-pathname :host "scratch"
284 :directory '(:relative :back "foo")
285 :name "bar")
286 #p"/aaa/bbb/ccc/ddd/eee")
287 (#p"SCRATCH:ABSOLUTE;PATH;NAME.TYPE"
288 #p"scratch:absolute;path;name" #p"/dir/default-name.type")
290 ;; FIXME: test version handling in LPNs
292 do (let ((result (apply #'merge-pathnames params)))
293 (macrolet ((frob (op)
294 `(assert (equal (,op result) (,op expected-result)))))
295 (frob pathname-host)
296 (frob pathname-directory)
297 (frob pathname-name)
298 (frob pathname-type)))))
300 ;;; host-namestring testing
301 (with-test (:name :host-namestring)
302 (assert (string=
303 (namestring (parse-namestring "/foo" (host-namestring #p"/bar")))
304 "/foo"))
305 (assert (string=
306 (namestring (parse-namestring "FOO" (host-namestring #p"SCRATCH:BAR")))
307 "SCRATCH:FOO"))
308 (assert-error
309 (setf (logical-pathname-translations "")
310 (list '("**;*.*.*" "/**/*.*")))))
312 ;;; Bug 200: translate-logical-pathname is according to the spec supposed
313 ;;; not to give errors if asked to translate a namestring for a valid
314 ;;; physical pathname. Failed in 0.7.7.28 and before
315 (with-test (:name (:logical-pathname 16))
316 (assert (string= (namestring (translate-logical-pathname "/")) "/")))
319 ;;; Not strictly pathname logic testing, but until sbcl-0.7.6.19 we
320 ;;; had difficulty with non-FILE-STREAM stream arguments to pathname
321 ;;; functions (they would cause memory protection errors). Make sure
322 ;;; that those errors are gone:
323 (with-test (:name (string-stream :not-a pathname))
324 (flet ((test (form)
325 (multiple-value-bind (fun failurep warnings)
326 (checked-compile
327 `(lambda () ,form) :allow-warnings 'sb-int:type-warning)
328 (declare (ignore failurep))
329 (assert (= 1 (length warnings)))
330 (assert-error (funcall fun) type-error))))
331 (test '(pathname (make-string-input-stream "FOO")))
332 (test '(merge-pathnames (make-string-output-stream)))))
334 ;;; ensure print-read consistency (or print-not-readable-error) on
335 ;;; pathnames:
336 (with-test (:name :print/read-consistency)
337 (dolist (p (list (make-pathname :name "foo" :type "txt" :version :newest)
338 (make-pathname :name "foo" :type "txt" :version 1)
339 (make-pathname :name "foo" :type ".txt")
340 (make-pathname :name "foo." :type "txt")
341 (make-pathname :name "\\" :type "txt")
342 (make-pathname :name "^" :type "txt")
343 (make-pathname :name "foo*" :type "txt")
344 (make-pathname :name "foo[" :type "txt")
345 (parse-namestring "SCRATCH:FOO.TXT.1")
346 (parse-namestring "SCRATCH:FOO.TXT.NEWEST")
347 (parse-namestring "SCRATCH:FOO.TXT")))
348 (handler-case
349 (let* ((*print-readably* t)
350 (new (read-from-string (format nil "~S" p))))
351 (unless (equal new p)
352 (let ((*print-readably* nil))
353 (error "oops: host:~S device:~S dir:~S version:~S~% ->~%~
354 host:~S device:~S dir:~S version:~S"
355 (pathname-host p) (pathname-device p)
356 (pathname-directory p) (pathname-version p)
357 (pathname-host new) (pathname-device new)
358 (pathname-directory new) (pathname-version new)))))
359 (print-not-readable ()
360 nil))))
362 ;;; BUG 330: "PARSE-NAMESTRING should accept namestrings as the
363 ;;; default argument" ...and streams as well
364 (with-test (:name (parse-namestring stream))
365 (assert (equal (parse-namestring "foo" nil "/")
366 (parse-namestring "foo" nil #P"/")))
367 (let ((test "parse-namestring-test.tmp"))
368 (unwind-protect
369 (with-open-file (f test :direction :output)
370 ;; FIXME: This test is a bit flaky, since we only check that
371 ;; no error is signalled. The dilemma here is "what is the
372 ;; correct result when defaults is a _file_, not a
373 ;; directory". Currently (0.8.10.73) we get #P"foo" here (as
374 ;; opposed to eg. #P"/path/to/current/foo"), which is
375 ;; possibly mildly surprising but probably conformant.
376 (assert (parse-namestring "foo" nil f)))
377 (when (probe-file test)
378 (delete-file test)))))
380 ;;; ENOUGH-NAMESTRING should probably not fail when the namestring in
381 ;;; question has a :RELATIVE pathname.
382 (with-test (:name enough-namestring)
383 (assert (equal (enough-namestring #p"foo" #p"./") "foo")))
385 ;;; bug reported by Artem V. Andreev: :WILD not handled in unparsing
386 ;;; directory lists.
387 (with-test (:name :unparse-wild)
388 (assert (equal (namestring #p"/tmp/*/") "/tmp/*/")))
390 ;;; Printing of pathnames; see CLHS 22.1.3.1. This section was started
391 ;;; to confirm that pathnames are printed as their namestrings under
392 ;;; :escape nil :readably nil.
393 (with-test (:name :print-as-namestrings)
394 (loop for (pathname expected . vars) in
395 `((#p"/foo" "#P\"/foo\"")
396 (#p"/foo" "#P\"/foo\"" :readably nil)
397 (#p"/foo" "#P\"/foo\"" :escape nil)
398 (#p"/foo" "/foo" :readably nil :escape nil))
399 for actual = (with-standard-io-syntax
400 (apply #'write-to-string pathname vars))
401 do (assert (string= expected actual)
403 "~S should be ~S, was ~S"
404 (list* 'write-to-string pathname vars)
405 expected
406 actual)))
408 ;;; we got (truename "/") wrong for about 6 months. Check that it's
409 ;;; still right.
410 (with-test (:name :root-truename)
411 (let ((pathname (truename "/")))
412 (assert (equalp pathname (merge-pathnames #p"/")))
413 (assert (equal (pathname-directory pathname) '(:absolute)))))
415 ;;; we failed to unparse logical pathnames with :NAME :WILD :TYPE NIL.
416 ;;; (Reported by Pascal Bourguignon.
417 (with-test (:name :unparse-logical-wild)
418 (let ((pathname (make-pathname :host "SYS" :directory '(:absolute :wild-inferiors)
419 :name :wild :type nil)))
420 (assert (string= (namestring pathname) "SYS:**;*"))
421 (assert (string= (write-to-string pathname :readably t) "#P\"SYS:**;*\""))))
423 ;;; reported by James Y Knight on sbcl-devel 2006-05-17
424 (with-test (:name :merge-back)
425 (let ((p1 (make-pathname :directory '(:relative "bar")))
426 (p2 (make-pathname :directory '(:relative :back "foo"))))
427 (assert (equal (merge-pathnames p1 p2)
428 (make-pathname :directory '(:relative :back "foo" "bar"))))))
430 ;;; construct native namestrings even if the directory is empty (means
431 ;;; that same as if (:relative))
432 (with-test (:name (sb-ext:native-namestring 1))
433 (assert (equal (sb-ext:native-namestring (make-pathname :directory '(:relative)
434 :name "foo"
435 :type "txt"))
436 (sb-ext:native-namestring (let ((p (make-pathname :directory nil
437 :name "foo"
438 :type "txt")))
439 (assert (not (pathname-directory p)))
440 p)))))
442 ;;; reported by Richard Kreuter: PATHNAME and MERGE-PATHNAMES used to
443 ;;; be unsafely-flushable. Since they are known to return non-nil values
444 ;;; only, the test-node of the IF is flushed, and since the function
445 ;;; is unsafely-flushable, out it goes, and bad pathname designators
446 ;;; breeze through.
448 ;;; These tests rely on using a stream that appears as a file-stream
449 ;;; but isn't a valid pathname-designator.
450 (with-test (:name :dont-flush-pathnames)
451 (assert (eq :false
452 (if (ignore-errors (pathname sb-sys::*tty*)) :true :false)))
453 (assert (eq :false
454 (if (ignore-errors (merge-pathnames sb-sys::*tty*)) :true :false))))
456 ;;; This used to return "quux/bar.lisp"
457 (with-test (:name :dpd-output-file)
458 (assert (equal #p"quux/bar.fasl"
459 (let ((*default-pathname-defaults* #p"quux/"))
460 (compile-file-pathname "foo.lisp" :output-file "bar"))))
461 (assert (equal #p"quux/bar.fasl"
462 (let ((*default-pathname-defaults* #p"quux/"))
463 (compile-file-pathname "bar.lisp")))))
465 (with-test (:name :wild-enough)
466 (enough-namestring #p".a*"))
469 (with-test (:name :translated-wild-version)
470 (assert (eq 99
471 (pathname-version
472 (translate-pathname
473 (make-pathname :name "foo" :type "bar" :version 99)
474 (make-pathname :name :wild :type :wild :version :wild)
475 (make-pathname :name :wild :type :wild :version :wild)))))
477 (assert (eq 99
478 (pathname-version
479 (translate-pathname
480 (make-pathname :name "foo" :type "bar" :version 99)
481 (make-pathname :name :wild :type :wild :version :wild)
482 (make-pathname :name :wild :type :wild :version nil))))))
484 ;;; enough-namestring relative to root
485 (with-test (:name :enough-relative-to-root)
486 (assert (equal "foo" (enough-namestring "/foo" "/"))))
488 ;;; Check the handling of NIL, :UNSPECIFIC, the empty string, and
489 ;;; non-NIL strings in NATIVE-NAMESTRING implementations. Revised by
490 ;;; RMK 2007-11-28, attempting to preserve the apparent intended
491 ;;; denotation of SBCL's then-current pathname implementation.
492 (with-test (:name (sb-ext:native-namestring 2))
493 (assert (equal
494 (loop with components = (list nil :unspecific "" "a")
495 for name in components
496 appending (loop for type in components
497 as pathname = (make-pathname
498 #+win32 :device #+win32 "C"
499 :directory '(:absolute "tmp")
500 :name name :type type)
501 collect (ignore-errors
502 (sb-ext:native-namestring pathname))))
503 #-win32
504 #|type NIL :UNSPECIFIC "" "a" |#
505 #|name |#
506 #|NIL |# '("/tmp/" "/tmp/" NIL NIL
507 #|:UNSPECIFIC|# "/tmp/" "/tmp/" NIL NIL
508 #|"" |# "/tmp/" "/tmp/" "/tmp/." "/tmp/.a"
509 #|"a" |# "/tmp/a" "/tmp/a" "/tmp/a." "/tmp/a.a")
511 #+win32
512 #|type NIL :UNSPECIFIC "" "a" |#
513 #|name |#
514 #|NIL |# '("C:\\tmp\\" "C:\\tmp\\" NIL NIL
515 #|:UNSPECIFIC|# "C:\\tmp\\" "C:\\tmp\\" NIL NIL
516 #|"" |# "C:\\tmp\\" "C:\\tmp\\" "C:\\tmp\\." "C:\\tmp\\.a"
517 #|"a" |# "C:\\tmp\\a" "C:\\tmp\\a" "C:\\tmp\\a." "C:\\tmp\\a.a"))))
519 (with-test (:name (delete-file logical-pathname))
520 (setf (logical-pathname-translations "SB-TEST")
521 (list (list "**;*.*.*" (make-pathname :name :wild
522 :type :wild
523 :defaults (truename ".")))))
524 (let ((test (pathname "SB-TEST:delete-logical-pathname.tmp")))
525 (assert (typep test 'logical-pathname))
526 (with-open-file (f test :direction :output)
527 (write-line "delete me!" f))
528 (assert (probe-file test))
529 (assert (delete-file test))
530 (assert (not (probe-file test)))))
532 ;;; Reported by Willem Broekema: Reading #p"\\\\" caused an error due
533 ;;; to insufficient sanity in input testing in EXTRACT-DEVICE (in
534 ;;; src;code;win32-pathname).
535 (with-test (:name :bug-489698 :skipped-on '(not :win32))
536 (assert (equal (make-pathname :directory '(:absolute))
537 (read-from-string "#p\"\\\\\\\\\""))))
539 (with-test (:name :load-logical-pathname-translations)
540 (let* ((cwd (truename "."))
541 (foo (merge-pathnames "llpnt-foo.translations" cwd))
542 (bar (merge-pathnames "llpnt-bar.translations" cwd))
543 (translations (logical-pathname-translations "SYS")))
544 (unwind-protect
545 (progn
546 (with-open-file (f foo :direction :output)
547 (prin1 (list (list "*.TEXT" (make-pathname
548 :directory '(:absolute "my" "foo")
549 :name :wild :type "txt")))
551 (with-open-file (f bar :direction :output)
552 (prin1 (list (list "*.CL" (make-pathname
553 :directory '(:absolute "my" "bar")
554 :name :wild :type "lisp"))) f))
555 (setf (logical-pathname-translations "SYS")
556 (list* (list "SITE;LLPNT-FOO.TRANSLATIONS.NEWEST" foo)
557 (list "SITE;LLPNT-BAR.TRANSLATIONS.NEWEST" bar)
558 translations))
559 (assert (load-logical-pathname-translations "LLPNT-FOO"))
560 (assert (load-logical-pathname-translations "LLPNT-BAR"))
561 (assert
562 (and
563 (equal "/my/bar/quux.lisp"
564 (namestring (translate-logical-pathname "LLPNT-BAR:QUUX.CL")))
565 (equal "/my/foo/quux.txt"
566 (namestring (translate-logical-pathname "LLPNT-FOO:QUUX.TEXT"))))))
567 (ignore-errors (delete-file foo))
568 (ignore-errors (delete-file bar))
569 (setf (logical-pathname-translations "SYS") translations))))
571 (with-test (:name :tilde-expansion)
572 (assert (equal '(:absolute :home "foo") (pathname-directory "~/foo/bar.txt")))
573 (assert (equal '(:absolute (:home "jdoe") "quux") (pathname-directory "~jdoe/quux/")))
574 (assert (equal "~/foo/x" (namestring (make-pathname :directory '(:absolute :home "foo")
575 :name "x"))))
576 (assert (equal (native-namestring (merge-pathnames "a/b.c" (user-homedir-pathname)))
577 (native-namestring #p"~/a/b.c")))
578 ;; Not a directory.
579 (assert (equal (native-namestring #p"~foo") "~foo"))
580 ;; Not at the start of the first directory
581 (assert (equal (native-namestring #p"foo/~/bar")
582 #-win32 "foo/~/bar"
583 #+win32 "foo\\~\\bar"))
584 (equal (native-namestring (merge-pathnames "~/"))
585 (native-namestring (user-homedir-pathname))))
587 ;;; lp#673625
588 (with-test (:name :pathname-escape-first-directory-component
589 :fails-on :win32)
590 ;; ~ / :HOME
591 (assert (equal (pathname-directory #p"\\~/foo/") '(:relative "~" "foo")))
592 (assert (equal (native-namestring #p"\\~/foo/") "~/foo/"))
593 (assert (equal (namestring (make-pathname :directory '(:absolute "~zot")))
594 "\\~zot/"))
595 ;; * / :WILD
596 (assert (equal (pathname-directory #p"\\*/") '(:relative "*"))))
598 (with-test (:name (ensure-directories-exist :with-odd-d-p-d))
599 (let ((*default-pathname-defaults* #p"/tmp/foo"))
600 (ensure-directories-exist "/")))
602 (with-test (:name :long-file-name :skipped-on '(not :win32))
603 (let* ((x '("hint--if-you-are-having-trouble-deleting-this-test-directory"
604 "use-the-7zip-file-manager"))
605 (base (truename
606 (directory-namestring (or *load-pathname* *compile-file-pathname*))))
607 (shallow (make-pathname :directory `(:relative ,(car x))))
608 (shallow (merge-pathnames shallow base))
609 (deep (make-pathname
610 :directory `(:relative ,@(loop repeat 10 appending x))))
611 (deep (merge-pathnames deep base))
612 (native (sb-ext:native-namestring deep)))
613 (assert (> (length native) 260))
614 (assert (eql 3 (mismatch "\\\\?" native)))
615 (assert (not (probe-file shallow)))
616 (unwind-protect
617 (progn
618 (ensure-directories-exist deep)
619 (assert (probe-file deep)))
620 (sb-ext:delete-directory shallow :recursive t))
621 (assert (not (probe-file shallow)))))
623 #+unix
624 (with-test (:name sb-int:simplify-namestring)
625 (assert (string= (sb-int:simplify-namestring "./a/b/../c/")
626 "a/c/")))
628 (with-test (:name :back-and-truename)
629 (probe-file (make-pathname :directory '(:absolute "a" "b" :back))))
631 (with-test (:name (parse-namestring :displaced))
632 (let* ((string "abc")
633 (disp (make-array 0 :element-type (array-element-type string)
634 :displaced-to string
635 :displaced-index-offset 1)))
636 (multiple-value-bind (path pos)
637 (parse-namestring disp)
638 (assert (equal path #P""))
639 (assert (zerop pos)))))
641 (with-test (:name (sb-ext:parse-native-namestring :displaced))
642 (let* ((string "abc")
643 (disp (make-array 0 :element-type (array-element-type string)
644 :displaced-to string
645 :displaced-index-offset 1)))
646 (multiple-value-bind (path pos)
647 (sb-ext:parse-native-namestring disp)
648 (assert (equal path #P""))
649 (assert (zerop pos)))))
651 (with-test (:name (:parse-logical-pathname :displaced))
652 (let* ((string "XSYS:ABC.LISP")
653 (disp (make-array (1- (length string))
654 :element-type (array-element-type string)
655 :displaced-to string
656 :displaced-index-offset 1)))
657 (assert (equal (parse-namestring disp) #p"SYS:ABC.LISP"))))