1.0.16.7: slightly faster LAST
[sbcl/tcr.git] / tests / pathnames.impure.lisp
bloba46c6e0d225cb6d72fc01ca3c0218378d8b48f38
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 (setf (logical-pathname-translations "demo0")
21 '(("**;*.*.*" "/tmp/")))
23 ;;; In case of a parse error we want to get a condition of type
24 ;;; CL:PARSE-ERROR (or more specifically, of type
25 ;;; SB-KERNEL:NAMESTRING-PARSE-ERROR).
26 (assert
27 (typep (grab-condition (logical-pathname "demo0::bla;file.lisp"))
28 'parse-error))
30 ;;; some things SBCL-0.6.9 used not to parse correctly:
31 ;;;
32 ;;; SBCL used to throw an error saying there's no translation.
33 (assert (equal (namestring (translate-logical-pathname "demo0:file.lisp"))
34 "/tmp/file.lisp"))
35 ;;; We do not match a null directory to every wild path:
36 (assert (not (pathname-match-p "demo0:file.lisp"
37 (logical-pathname "demo0:tmp;**;*.*.*"))))
38 ;;; Remove "**" from our resulting pathname when the source-dir is NIL:
39 (setf (logical-pathname-translations "demo1")
40 '(("**;*.*.*" "/tmp/**/*.*") (";**;*.*.*" "/tmp/rel/**/*.*")))
41 (assert (not (equal (namestring (translate-logical-pathname "demo1:foo.lisp"))
42 "/tmp/**/foo.lisp")))
43 ;;; That should be correct:
44 (assert (equal (namestring (translate-logical-pathname "demo1:foo.lisp"))
45 "/tmp/foo.lisp"))
46 ;;; Check for absolute/relative path confusion:
47 (assert (not (equal (namestring (translate-logical-pathname "demo1:;foo.lisp"))
48 "tmp/rel/foo.lisp")))
49 (assert (equal (namestring (translate-logical-pathname "demo1:;foo.lisp"))
50 "/tmp/rel/foo.lisp"))
52 ;;; Under SBCL: new function #'UNPARSE-ENOUGH-NAMESTRING, to
53 ;;; handle the following case exactly (otherwise we get an error:
54 ;;; "#'IDENTITY CALLED WITH 2 ARGS."
55 (setf (logical-pathname-translations "demo2")
56 '(("test;**;*.*" "/tmp/demo2/test")))
57 (enough-namestring "demo2:test;foo.lisp")
59 ;;; When a pathname comes from a logical host, it should be in upper
60 ;;; case. (This doesn't seem to be specifically required in the ANSI
61 ;;; spec, but it's left up to the implementors, and the arguments made
62 ;;; in the cleanup issue PATHNAME-LOGICAL:ADD seem to be a pretty
63 ;;; compelling reason for the implementors to choose case
64 ;;; insensitivity and a canonical case.)
65 (setf (logical-pathname-translations "FOO")
66 '(("**;*.*.*" "/full/path/to/foo/**/*.*")))
67 (let* ((pn1 (make-pathname :host "FOO" :directory "etc" :name "INETD"
68 :type "conf"))
69 (pn2 (make-pathname :host "foo" :directory "ETC" :name "inetd"
70 :type "CONF"))
71 (pn3 (read-from-string (prin1-to-string pn1))))
72 (assert (equal pn1 pn2))
73 (assert (equal pn1 pn3)))
75 ;;; In addition to the upper-case constraint above, if the logical-pathname
76 ;;; contains a string component in e.g. the directory, name and type slot,
77 ;;; these should be valid "WORDS", according to CLHS 19.3.1.
78 ;;; FIXME: currently SBCL throws NAMESTRING-PARSE-ERROR: should this be
79 ;;; a TYPE-ERROR?
81 (locally
82 ;; MAKE-PATHNAME is UNSAFELY-FLUSHABLE
83 (declare (optimize safety))
85 (assert (not (ignore-errors
86 (make-pathname :host "FOO" :directory "!bla" :name "bar"))))
88 ;; error: name-component not valid
89 (assert (not (ignore-errors
90 (make-pathname :host "FOO" :directory "bla" :name "!bar"))))
92 ;; error: type-component not valid.
93 (assert (not (ignore-errors
94 (make-pathname :host "FOO" :directory "bla" :name "bar"
95 :type "&baz")))))
97 ;;; We may need to parse the host as a LOGICAL-NAMESTRING HOST. The
98 ;;; HOST in PARSE-NAMESTRING can be either a string or :UNSPECIFIC
99 ;;; without actually requiring the system to signal an error (apart
100 ;;; from host mismatches).
101 (assert (equal (namestring (parse-namestring "" "FOO")) "FOO:"))
102 (assert (equal (namestring (parse-namestring "" :unspecific)) ""))
104 ;;; The third would work if the call were (and it should continue to
105 ;;; work ...)
106 (parse-namestring ""
107 (pathname-host
108 (translate-logical-pathname
109 "FOO:")))
111 ;;; ANSI says PARSE-NAMESTRING returns TYPE-ERROR on host mismatch.
112 (let ((cond (grab-condition (parse-namestring "foo:jeamland" "demo2"))))
113 (assert (typep cond 'type-error)))
115 ;;; turning one logical pathname into another:
116 (setf (logical-pathname-translations "foo")
117 '(("todemo;*.*.*" "demo0:*.*.*")))
118 (assert (equal (namestring (translate-logical-pathname "foo:todemo;x.y"))
119 (namestring (translate-logical-pathname "demo0:x.y"))))
121 ;;; ANSI, in its wisdom, specifies that it's an error (specifically a
122 ;;; TYPE-ERROR) to query the system about the translations of a string
123 ;;; which doesn't have any translations. It's not clear why we don't
124 ;;; just return NIL in that case, but they make the rules..
125 (let ((cond (grab-condition (logical-pathname-translations "unregistered-host"))))
126 (assert (typep cond 'type-error)))
128 (assert (not (string-equal (host-namestring (parse-namestring "OTHER-HOST:ILLEGAL/LPN")) "OTHER-HOST")))
129 (assert (string-equal (pathname-name (parse-namestring "OTHER-HOST:ILLEGAL/LPN")) "LPN"))
131 ;;; FIXME: A comment on this section up to sbcl-0.6.11.30 or so said
132 ;;; examples from CLHS: Section 19.4, LOGICAL-PATHNAME-TRANSLATIONS
133 ;;; (sometimes converted to the Un*x way of things)
134 ;;; but when I looked it up I didn't see the connection. Presumably
135 ;;; there's some code in this section which should be attributed
136 ;;; to something in the ANSI spec, but I don't know what code it is
137 ;;; or what section of the specification has the related code.
138 (setf (logical-pathname-translations "test0")
139 '(("**;*.*.*" "/library/foo/**/")))
140 (assert (equal (namestring (translate-logical-pathname
141 "test0:foo;bar;baz;mum.quux"))
142 "/library/foo/foo/bar/baz/mum.quux"))
143 (setf (logical-pathname-translations "prog")
144 '(("RELEASED;*.*.*" "MY-UNIX:/sys/bin/my-prog/")
145 ("RELEASED;*;*.*.*" "MY-UNIX:/sys/bin/my-prog/*/")
146 ("EXPERIMENTAL;*.*.*" "MY-UNIX:/usr/Joe/development/prog/")
147 ("EXPERIMENTAL;*;*.*.*" "MY-UNIX:/usr/Joe/development/prog/*/")))
148 (setf (logical-pathname-translations "prog")
149 '(("CODE;*.*.*" "/lib/prog/")))
150 (assert (equal (namestring (translate-logical-pathname
151 "prog:code;documentation.lisp"))
152 "/lib/prog/documentation.lisp"))
153 (setf (logical-pathname-translations "prog")
154 '(("CODE;DOCUMENTATION.*.*" "/lib/prog/docum.*")
155 ("CODE;*.*.*" "/lib/prog/")))
156 (assert (equal (namestring (translate-logical-pathname
157 "prog:code;documentation.lisp"))
158 "/lib/prog/docum.lisp"))
160 ;;; ANSI section 19.3.1.1.5 specifies that translation to a filesystem
161 ;;; which doesn't have versions should ignore the version slot. CMU CL
162 ;;; didn't ignore this as it should, but we do.
163 (assert (equal (namestring (translate-logical-pathname
164 "test0:foo;bar;baz;mum.quux.3"))
165 "/library/foo/foo/bar/baz/mum.quux"))
167 ;;;; MERGE-PATHNAME tests
168 ;;;;
169 ;;;; There are some things we don't bother testing, just because they're
170 ;;;; not meaningful on the underlying filesystem anyway.
171 ;;;;
172 ;;;; Mostly that means that we don't do devices, we don't do versions
173 ;;;; except minimally in LPNs (they get lost in the translation to
174 ;;;; physical hosts, so it's not much of an issue), and we don't do
175 ;;;; hosts except for LPN hosts
176 ;;;;
177 ;;;; Although these tests could conceivably be useful in principle for
178 ;;;; other implementations, they depend quite heavily on the rules for
179 ;;;; namestring parsing, which are implementation-specific. So, success
180 ;;;; or failure in these tests doesn't tell you anything about
181 ;;;; ANSI-compliance unless your PARSE-NAMESTRING works like ours.
183 ;;; Needs to be done at compile time, so that the #p"" read-macro
184 ;;; correctly parses things as logical pathnames. This is not a
185 ;;; problem as was, as this is an impure file and so gets loaded in,
186 ;;; but just for future proofing...
187 (eval-when (:compile-toplevel :load-toplevel :execute)
188 (setf (logical-pathname-translations "scratch")
189 '(("**;*.*.*" "/usr/local/doc/**/*"))))
191 (loop for (expected-result . params) in
192 `(;; trivial merge
193 (#P"/usr/local/doc/foo" #p"foo" #p"/usr/local/doc/")
194 ;; If pathname does not specify a host, device, directory,
195 ;; name, or type, each such component is copied from
196 ;; default-pathname.
197 ;; 1) no name, no type
198 (#p"/supplied-dir/name.type" #p"/supplied-dir/" #p"/dir/name.type")
199 ;; 2) no directory, no type
200 (#p"/dir/supplied-name.type" #p"supplied-name" #p"/dir/name.type")
201 ;; 3) no name, no dir (must use make-pathname as ".foo" is parsed
202 ;; as a name)
203 (#p"/dir/name.supplied-type"
204 ,(make-pathname :type "supplied-type")
205 #p"/dir/name.type")
206 ;; If (pathname-directory pathname) is a list whose car is
207 ;; :relative, and (pathname-directory default-pathname) is a
208 ;; list, then the merged directory is [...]
209 (#p"/aaa/bbb/ccc/ddd/qqq/www" #p"qqq/www" #p"/aaa/bbb/ccc/ddd/eee")
210 ;; except that if the resulting list contains a string or
211 ;; :wild immediately followed by :back, both of them are
212 ;; removed.
213 (#P"/aaa/bbb/ccc/blah/eee"
214 ;; "../" in a namestring is parsed as :up not :back, so make-pathname
215 ,(make-pathname :directory '(:relative :back "blah"))
216 #p"/aaa/bbb/ccc/ddd/eee")
217 ;; If (pathname-directory default-pathname) is not a list or
218 ;; (pathname-directory pathname) is not a list whose car is
219 ;; :relative, the merged directory is (or (pathname-directory
220 ;; pathname) (pathname-directory default-pathname))
221 (#P"/absolute/path/name.type"
222 #p"/absolute/path/name"
223 #p"/dir/default-name.type")
224 ;; === logical pathnames ===
225 ;; recognizes a logical pathname namestring when
226 ;; default-pathname is a logical pathname
227 ;; FIXME: 0.6.12.23 fails this one.
229 ;; And, as it happens, it's right to fail it. Because
230 ;; #p"name1" is read in with the ambient *d-p-d* value, which
231 ;; has a physical (Unix) host; therefore, the host of the
232 ;; default-pathname argument to merge-pathnames is
233 ;; irrelevant. The result is (correctly) different if
234 ;; '#p"name1"' is replaced by "name1", below, though it's
235 ;; still not what one might expect... -- CSR, 2002-05-09
236 #+nil (#P"scratch:foo;name1" #p"name1" #p"scratch:foo;")
237 ;; or when the namestring begins with the name of a defined
238 ;; logical host followed by a colon [I assume that refers to pathname
239 ;; rather than default-pathname]
240 (#p"SCRATCH:FOO;NAME2" #p"scratch:;name2" #p"scratch:foo;")
241 ;; conduct the previous set of tests again, with a lpn first argument
242 (#P"SCRATCH:USR;LOCAL;DOC;FOO" #p"scratch:;foo" #p"/usr/local/doc/")
243 (#p"SCRATCH:SUPPLIED-DIR;NAME.TYPE"
244 #p"scratch:supplied-dir;"
245 #p"/dir/name.type")
246 (#p"SCRATCH:DIR;SUPPLIED-NAME.TYPE"
247 #p"scratch:;supplied-name"
248 #p"/dir/name.type")
249 (#p"SCRATCH:DIR;NAME.SUPPLIED-TYPE"
250 ,(make-pathname :host "scratch" :type "supplied-type")
251 #p"/dir/name.type")
252 (#p"SCRATCH:AAA;BBB;CCC;DDD;FOO;BAR"
253 ,(make-pathname :host "scratch"
254 :directory '(:relative "foo")
255 :name "bar")
256 #p"/aaa/bbb/ccc/ddd/eee")
257 (#p"SCRATCH:AAA;BBB;CCC;FOO;BAR"
258 ,(make-pathname :host "scratch"
259 :directory '(:relative :back "foo")
260 :name "bar")
261 #p"/aaa/bbb/ccc/ddd/eee")
262 (#p"SCRATCH:ABSOLUTE;PATH;NAME.TYPE"
263 #p"scratch:absolute;path;name" #p"/dir/default-name.type")
265 ;; FIXME: test version handling in LPNs
267 do (let ((result (apply #'merge-pathnames params)))
268 (macrolet ((frob (op)
269 `(assert (equal (,op result) (,op expected-result)))))
270 (frob pathname-host)
271 (frob pathname-directory)
272 (frob pathname-name)
273 (frob pathname-type))))
275 ;;; host-namestring testing
276 (assert (string=
277 (namestring (parse-namestring "/foo" (host-namestring #p"/bar")))
278 "/foo"))
279 (assert (string=
280 (namestring (parse-namestring "FOO" (host-namestring #p"SCRATCH:BAR")))
281 "SCRATCH:FOO"))
282 (assert (raises-error?
283 (setf (logical-pathname-translations "")
284 (list '("**;*.*.*" "/**/*.*")))))
286 ;;; Bug 200: translate-logical-pathname is according to the spec supposed
287 ;;; not to give errors if asked to translate a namestring for a valid
288 ;;; physical pathname. Failed in 0.7.7.28 and before
289 (assert (string= (namestring (translate-logical-pathname "/")) "/"))
292 ;;; Not strictly pathname logic testing, but until sbcl-0.7.6.19 we
293 ;;; had difficulty with non-FILE-STREAM stream arguments to pathname
294 ;;; functions (they would cause memory protection errors). Make sure
295 ;;; that those errors are gone:
296 (assert (raises-error? (pathname (make-string-input-stream "FOO"))
297 type-error))
298 (assert (raises-error? (merge-pathnames (make-string-output-stream))
299 type-error))
301 ;;; ensure read/print consistency (or print-not-readable-error) on
302 ;;; pathnames:
303 (let ((pathnames (list
304 (make-pathname :name "foo" :type "txt" :version :newest)
305 (make-pathname :name "foo" :type "txt" :version 1)
306 (make-pathname :name "foo" :type ".txt")
307 (make-pathname :name "foo." :type "txt")
308 (parse-namestring "SCRATCH:FOO.TXT.1")
309 (parse-namestring "SCRATCH:FOO.TXT.NEWEST")
310 (parse-namestring "SCRATCH:FOO.TXT"))))
311 (dolist (p pathnames)
312 (print p)
313 (handler-case
314 (let ((*print-readably* t))
315 (assert (equal (read-from-string (format nil "~S" p)) p)))
316 (print-not-readable () nil))))
318 ;;; BUG 330: "PARSE-NAMESTRING should accept namestrings as the
319 ;;; default argument" ...and streams as well
320 (assert (equal (parse-namestring "foo" nil "/")
321 (parse-namestring "foo" nil #P"/")))
322 (let ((test "parse-namestring-test.tmp"))
323 (unwind-protect
324 (with-open-file (f test :direction :output)
325 ;; FIXME: This test is a bit flaky, since we only check that
326 ;; no error is signalled. The dilemma here is "what is the
327 ;; correct result when defaults is a _file_, not a
328 ;; directory". Currently (0.8.10.73) we get #P"foo" here (as
329 ;; opposed to eg. #P"/path/to/current/foo"), which is
330 ;; possibly mildly surprising but probably conformant.
331 (assert (parse-namestring "foo" nil f)))
332 (when (probe-file test)
333 (delete-file test))))
335 ;;; ENOUGH-NAMESTRING should probably not fail when the namestring in
336 ;;; question has a :RELATIVE pathname.
337 (assert (equal (enough-namestring #p"foo" #p"./") "foo"))
339 ;;; bug reported by Artem V. Andreev: :WILD not handled in unparsing
340 ;;; directory lists.
341 (assert (equal (namestring #p"/tmp/*/") "/tmp/*/"))
343 ;;; Printing of pathnames; see CLHS 22.1.3.1. This section was started
344 ;;; to confirm that pathnames are printed as their namestrings under
345 ;;; :escape nil :readably nil.
346 (loop for (pathname expected . vars) in
347 `((#p"/foo" "#P\"/foo\"")
348 (#p"/foo" "#P\"/foo\"" :readably nil)
349 (#p"/foo" "#P\"/foo\"" :escape nil)
350 (#p"/foo" "/foo" :readably nil :escape nil))
351 for actual = (with-standard-io-syntax
352 (apply #'write-to-string pathname vars))
353 do (assert (string= expected actual)
355 "~S should be ~S, was ~S"
356 (list* 'write-to-string pathname vars)
357 expected
358 actual))
360 ;;; we got (truename "/") wrong for about 6 months. Check that it's
361 ;;; still right.
362 (let ((pathname (truename "/")))
363 (assert (equalp pathname #p"/"))
364 (assert (equal (pathname-directory pathname) '(:absolute))))
366 ;;; we failed to unparse logical pathnames with :NAME :WILD :TYPE NIL.
367 ;;; (Reported by Pascal Bourguignon.
368 (let ((pathname (make-pathname :host "SYS" :directory '(:absolute :wild-inferiors)
369 :name :wild :type nil)))
370 (assert (string= (namestring pathname) "SYS:**;*"))
371 (assert (string= (write-to-string pathname :readably t) "#P\"SYS:**;*\"")))
373 ;;; reported by James Y Knight on sbcl-devel 2006-05-17
374 (let ((p1 (make-pathname :directory '(:relative "bar")))
375 (p2 (make-pathname :directory '(:relative :back "foo"))))
376 (assert (equal (merge-pathnames p1 p2)
377 (make-pathname :directory '(:relative :back "foo" "bar")))))
379 ;;; construct native namestrings even if the directory is empty (means
380 ;;; that same as if (:relative))
381 (assert (equal (sb-ext:native-namestring (make-pathname :directory '(:relative)
382 :name "foo"
383 :type "txt"))
384 (sb-ext:native-namestring (let ((p (make-pathname :directory nil
385 :name "foo"
386 :type "txt")))
387 (assert (not (pathname-directory p)))
388 p))))
390 ;;; reported by Richard Kreuter: PATHNAME and MERGE-PATHNAMES used to
391 ;;; be unsafely-flushable. Since they are known to return non-nil values
392 ;;; only, the test-node of the IF is flushed, and since the function
393 ;;; is unsafely-flushable, out it goes, and bad pathname designators
394 ;;; breeze through.
396 ;;; These tests rely on using a stream that appears as a file-stream
397 ;;; but isn't a valid pathname-designator.
398 (assert (eq :false
399 (if (ignore-errors (pathname sb-sys::*tty*)) :true :false)))
400 (assert (eq :false
401 (if (ignore-errors (merge-pathnames sb-sys::*tty*)) :true :false)))
403 ;;; This used to return "quux/bar.lisp"
404 (assert (equal #p"quux/bar.fasl"
405 (let ((*default-pathname-defaults* #p"quux/"))
406 (compile-file-pathname "foo.lisp" :output-file "bar"))))
407 (assert (equal #p"quux/bar.fasl"
408 (let ((*default-pathname-defaults* #p"quux/"))
409 (compile-file-pathname "bar.lisp"))))
411 (enough-namestring #p".a*")
414 (assert (eq 99
415 (pathname-version
416 (translate-pathname
417 (make-pathname :name "foo" :type "bar" :version 99)
418 (make-pathname :name :wild :type :wild :version :wild)
419 (make-pathname :name :wild :type :wild :version :wild)))))
421 (assert (eq 99
422 (pathname-version
423 (translate-pathname
424 (make-pathname :name "foo" :type "bar" :version 99)
425 (make-pathname :name :wild :type :wild :version :wild)
426 (make-pathname :name :wild :type :wild :version nil)))))
428 ;;; enough-namestring relative to root
429 (assert (equal "foo" (enough-namestring "/foo" "/")))
431 ;;; Check the handling of NIL, :UNSPECIFIC, the empty string, and
432 ;;; non-NIL strings in NATIVE-NAMESTRING implementations. Revised by
433 ;;; RMK 2007-11-28, attempting to preserve the apparent intended
434 ;;; denotation of SBCL's then-current pathname implementation.
435 (assert (equal
436 (loop with components = (list nil :unspecific "" "a")
437 for name in components
438 appending (loop for type in components
439 as pathname = (make-pathname
440 #+win32 "C"
441 :directory '(:absolute "tmp")
442 :name name :type type)
443 collect (ignore-errors
444 (sb-ext:native-namestring pathname))))
445 #-win32
446 #|type NIL :UNSPECIFIC "" "a" |#
447 #|name |#
448 #|NIL |# '("/tmp/" "/tmp/" NIL NIL
449 #|:UNSPECIFIC|# "/tmp/" "/tmp/" NIL NIL
450 #|"" |# "/tmp/" "/tmp/" "/tmp/." "/tmp/.a"
451 #|"a" |# "/tmp/a" "/tmp/a" "/tmp/a." "/tmp/a.a")
453 #+win32
454 #|type NIL :UNSPECIFIC "" "a" |#
455 #|name |#
456 #|NIL |# '("C:\\tmp\\" "C:\\tmp\\" NIL NIL
457 #|:UNSPECIFIC|# "C:\\tmp\\" "C:\\tmp\\" NIL NIL
458 #|"" |# "C:\\tmp\\" "C:\\tmp\\" "C:\\tmp\\." "C:\\tmp\\.a"
459 #|"a" |# "C:\\tmp\\a" "C:\\tmp\\a" "C:\\tmp\\a." "C:\\tmp\\a.a")))
460 ;;;; success