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