0.8.18.9:
[sbcl.git] / tests / pathnames.impure.lisp
1 ;;;; miscellaneous tests of pathname-related stuff
2
3 ;;;; This file is naturally impure because we mess with
4 ;;;; LOGICAL-PATHNAME-TRANSLATIONS.
5
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.
16
17 (load "assertoid.lisp")
18 (use-package "ASSERTOID")
19
20 (setf (logical-pathname-translations "demo0")
21       '(("**;*.*.*" "/tmp/")))
22
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))
29
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"))
51                      
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")
58
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)))
74
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?
80
81 (locally
82   ;; MAKE-PATHNAME is UNSAFELY-FLUSHABLE
83   (declare (optimize safety))
84   
85   (assert (not (ignore-errors
86                 (make-pathname :host "FOO" :directory "!bla" :name "bar"))))
87   
88   ;; error: name-component not valid
89   (assert (not (ignore-errors
90                 (make-pathname :host "FOO" :directory "bla" :name "!bar"))))
91   
92   ;; error: type-component not valid.
93   (assert (not (ignore-errors
94                 (make-pathname :host "FOO" :directory "bla" :name "bar"
95                                :type "&baz")))))
96
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)) ""))
103
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:")))
110
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)))
114
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"))))
120
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)))
127
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"))
130
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"))
159
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"))
166 \f
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.
182
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/**/*"))))
190
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.
228         ;;
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")
264
265         ;; FIXME: test version handling in LPNs
266         )
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))))
274 \f
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 '("**;*.*.*" "/**/*.*")))))
285 \f
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 "/")) "/"))
290
291 \f
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))
300 \f
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))))
317 \f
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))))
334 \f
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"))
338 \f
339 ;;; bug reported by Artem V. Andreev: :WILD not handled in unparsing
340 ;;; directory lists.
341 (assert (equal (namestring #p"/tmp/*/") "/tmp/*/"))
342 ;;;; success
343 (quit :unix-status 104)