0.7.9.6:
[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 (in-package "CL-USER")
18
19 (load "assertoid.lisp")
20
21 (setf (logical-pathname-translations "demo0")
22       '(("**;*.*.*" "/tmp/")))
23
24 ;;; In case of a parse error we want to get a condition of type
25 ;;; CL:PARSE-ERROR (or more specifically, of type
26 ;;; SB-KERNEL:NAMESTRING-PARSE-ERROR).
27 (assert
28   (typep (grab-condition (logical-pathname "demo0::bla;file.lisp"))
29          'parse-error))
30
31 ;;; some things SBCL-0.6.9 used not to parse correctly:
32 ;;;
33 ;;; SBCL used to throw an error saying there's no translation.
34 (assert (equal (namestring (translate-logical-pathname "demo0:file.lisp"))
35                "/tmp/file.lisp"))
36 ;;; We do not match a null directory to every wild path:
37 (assert (not (pathname-match-p "demo0:file.lisp"
38                                (logical-pathname "demo0:tmp;**;*.*.*"))))
39 ;;; Remove "**" from our resulting pathname when the source-dir is NIL:
40 (setf (logical-pathname-translations "demo1")
41       '(("**;*.*.*" "/tmp/**/*.*") (";**;*.*.*" "/tmp/rel/**/*.*")))
42 (assert (not (equal (namestring (translate-logical-pathname "demo1:foo.lisp"))
43                     "/tmp/**/foo.lisp")))
44 ;;; That should be correct:
45 (assert (equal (namestring (translate-logical-pathname "demo1:foo.lisp"))
46                "/tmp/foo.lisp"))
47 ;;; Check for absolute/relative path confusion:
48 (assert (not (equal (namestring (translate-logical-pathname "demo1:;foo.lisp"))
49                      "tmp/rel/foo.lisp")))
50 (assert (equal (namestring (translate-logical-pathname "demo1:;foo.lisp"))
51                "/tmp/rel/foo.lisp"))
52                      
53 ;;; Under SBCL: new function #'UNPARSE-ENOUGH-NAMESTRING, to
54 ;;; handle the following case exactly (otherwise we get an error:
55 ;;; "#'IDENTITY CALLED WITH 2 ARGS."
56 (setf (logical-pathname-translations "demo2")
57         '(("test;**;*.*" "/tmp/demo2/test")))
58 (enough-namestring "demo2:test;foo.lisp")
59
60 ;;; When a pathname comes from a logical host, it should be in upper
61 ;;; case. (This doesn't seem to be specifically required in the ANSI
62 ;;; spec, but it's left up to the implementors, and the arguments made
63 ;;; in the cleanup issue PATHNAME-LOGICAL:ADD seem to be a pretty
64 ;;; compelling reason for the implementors to choose case
65 ;;; insensitivity and a canonical case.)
66 (setf (logical-pathname-translations "FOO") 
67       '(("**;*.*.*" "/full/path/to/foo/**/*.*.*")))
68 (let* ((pn1 (make-pathname :host "FOO" :directory "etc" :name "INETD" 
69                            :type "conf"))
70        (pn2 (make-pathname :host "foo" :directory "ETC" :name "inetd" 
71                            :type "CONF"))
72        (pn3 (read-from-string (prin1-to-string pn1))))
73   (assert (equal pn1 pn2))
74   (assert (equal pn1 pn3)))
75
76 ;;; In addition to the upper-case constraint above, if the logical-pathname
77 ;;; contains a string component in e.g. the directory, name and type slot,
78 ;;; these should be valid "WORDS", according to CLHS 19.3.1.
79 ;;; FIXME: currently SBCL throws NAMESTRING-PARSE-ERROR: should this be
80 ;;; a TYPE-ERROR?
81
82 (locally
83   ;; MAKE-PATHNAME is UNSAFELY-FLUSHABLE
84   (declare (optimize safety))
85   
86   (assert (not (ignore-errors
87                 (make-pathname :host "FOO" :directory "!bla" :name "bar"))))
88   
89   ;; error: name-component not valid
90   (assert (not (ignore-errors
91                 (make-pathname :host "FOO" :directory "bla" :name "!bar"))))
92   
93   ;; error: type-component not valid.
94   (assert (not (ignore-errors
95                 (make-pathname :host "FOO" :directory "bla" :name "bar"
96                                :type "&baz")))))
97
98 ;;; We may need to parse the host as a LOGICAL-NAMESTRING HOST. The
99 ;;; HOST in PARSE-NAMESTRING can be either a string or :UNSPECIFIC
100 ;;; without actually requiring the system to signal an error (apart
101 ;;; from host mismatches).
102 (assert (equal (namestring (parse-namestring "" "FOO")) "FOO:"))
103 (assert (equal (namestring (parse-namestring "" :unspecific)) ""))
104
105 ;;; The third would work if the call were (and it should continue to
106 ;;; work ...)
107 (parse-namestring ""
108                   (pathname-host
109                    (translate-logical-pathname
110                     "FOO:")))
111
112 ;;; ANSI says PARSE-NAMESTRING returns TYPE-ERROR on host mismatch.
113 (let ((cond (grab-condition (parse-namestring "foo:jeamland" "demo2"))))
114   (assert (typep cond 'type-error)))
115
116 ;;; turning one logical pathname into another:
117 (setf (logical-pathname-translations "foo")
118        '(("todemo;*.*.*" "demo0:*.*.*")))
119 (assert (equal (namestring (translate-logical-pathname "foo:todemo;x.y"))
120                (namestring (translate-logical-pathname "demo0:x.y"))))
121
122 ;;; ANSI, in its wisdom, specifies that it's an error (specifically a
123 ;;; TYPE-ERROR) to query the system about the translations of a string
124 ;;; which doesn't have any translations. It's not clear why we don't
125 ;;; just return NIL in that case, but they make the rules..
126 (let ((cond (grab-condition (logical-pathname-translations "unregistered-host"))))
127   (assert (typep cond 'type-error)))
128
129 (assert (not (string-equal (host-namestring (parse-namestring "OTHER-HOST:ILLEGAL/LPN")) "OTHER-HOST")))
130 (assert (string-equal (pathname-name (parse-namestring "OTHER-HOST:ILLEGAL/LPN")) "LPN"))
131
132 ;;; FIXME: A comment on this section up to sbcl-0.6.11.30 or so said
133 ;;;   examples from CLHS: Section 19.4, LOGICAL-PATHNAME-TRANSLATIONS
134 ;;;   (sometimes converted to the Un*x way of things)
135 ;;; but when I looked it up I didn't see the connection. Presumably
136 ;;; there's some code in this section which should be attributed
137 ;;; to something in the ANSI spec, but I don't know what code it is
138 ;;; or what section of the specification has the related code.
139 (setf (logical-pathname-translations "test0")
140         '(("**;*.*.*"              "/library/foo/**/")))
141 (assert (equal (namestring (translate-logical-pathname
142                             "test0:foo;bar;baz;mum.quux"))
143                "/library/foo/foo/bar/baz/mum.quux"))
144 (setf (logical-pathname-translations "prog")
145         '(("RELEASED;*.*.*"        "MY-UNIX:/sys/bin/my-prog/")
146           ("RELEASED;*;*.*.*"      "MY-UNIX:/sys/bin/my-prog/*/")
147           ("EXPERIMENTAL;*.*.*"    "MY-UNIX:/usr/Joe/development/prog/")
148           ("EXPERIMENTAL;*;*.*.*"  "MY-UNIX:/usr/Joe/development/prog/*/")))
149 (setf (logical-pathname-translations "prog")
150         '(("CODE;*.*.*"             "/lib/prog/")))
151 (assert (equal (namestring (translate-logical-pathname
152                             "prog:code;documentation.lisp"))
153                "/lib/prog/documentation.lisp"))
154 (setf (logical-pathname-translations "prog")
155         '(("CODE;DOCUMENTATION.*.*" "/lib/prog/docum.*")
156           ("CODE;*.*.*"             "/lib/prog/")))
157 (assert (equal (namestring (translate-logical-pathname
158                             "prog:code;documentation.lisp"))
159                "/lib/prog/docum.lisp"))
160
161 ;;; ANSI section 19.3.1.1.5 specifies that translation to a filesystem
162 ;;; which doesn't have versions should ignore the version slot. CMU CL
163 ;;; didn't ignore this as it should, but we do.
164 (assert (equal (namestring (translate-logical-pathname
165                             "test0:foo;bar;baz;mum.quux.3"))
166                "/library/foo/foo/bar/baz/mum.quux"))
167 \f
168 ;;;; MERGE-PATHNAME tests
169 ;;;;
170 ;;;; There are some things we don't bother testing, just because they're
171 ;;;; not meaningful on the underlying filesystem anyway.
172 ;;;;
173 ;;;; Mostly that means that we don't do devices, we don't do versions
174 ;;;; except minimally in LPNs (they get lost in the translation to
175 ;;;; physical hosts, so it's not much of an issue), and we don't do
176 ;;;; hosts except for LPN hosts
177 ;;;;
178 ;;;; Although these tests could conceivably be useful in principle for
179 ;;;; other implementations, they depend quite heavily on the rules for
180 ;;;; namestring parsing, which are implementation-specific. So, success
181 ;;;; or failure in these tests doesn't tell you anything about
182 ;;;; ANSI-compliance unless your PARSE-NAMESTRING works like ours.
183
184 ;;; Needs to be done at compile time, so that the #p"" read-macro
185 ;;; correctly parses things as logical pathnames. This is not a
186 ;;; problem as was, as this is an impure file and so gets loaded in,
187 ;;; but just for future proofing...
188 (eval-when (:compile-toplevel :load-toplevel :execute)
189   (setf (logical-pathname-translations "scratch")
190         '(("**;*.*.*" "/usr/local/doc/**/*"))))
191
192 (loop for (expected-result . params) in
193       `(;; trivial merge
194         (#P"/usr/local/doc/foo" #p"foo" #p"/usr/local/doc/")
195         ;; If pathname does not specify a host, device, directory,
196         ;; name, or type, each such component is copied from
197         ;; default-pathname.
198         ;; 1) no name, no type
199         (#p"/supplied-dir/name.type" #p"/supplied-dir/" #p"/dir/name.type")
200         ;; 2) no directory, no type
201         (#p"/dir/supplied-name.type" #p"supplied-name" #p"/dir/name.type")
202         ;; 3) no name, no dir (must use make-pathname as ".foo" is parsed
203         ;; as a name)
204         (#p"/dir/name.supplied-type"
205          ,(make-pathname :type "supplied-type")
206          #p"/dir/name.type")
207         ;; If (pathname-directory pathname) is a list whose car is
208         ;; :relative, and (pathname-directory default-pathname) is a
209         ;; list, then the merged directory is [...]
210         (#p"/aaa/bbb/ccc/ddd/qqq/www" #p"qqq/www" #p"/aaa/bbb/ccc/ddd/eee")
211         ;; except that if the resulting list contains a string or
212         ;; :wild immediately followed by :back, both of them are
213         ;; removed.
214         (#P"/aaa/bbb/ccc/blah/eee"
215          ;; "../" in a namestring is parsed as :up not :back, so make-pathname
216          ,(make-pathname :directory '(:relative :back "blah"))
217          #p"/aaa/bbb/ccc/ddd/eee")
218         ;; If (pathname-directory default-pathname) is not a list or
219         ;; (pathname-directory pathname) is not a list whose car is
220         ;; :relative, the merged directory is (or (pathname-directory
221         ;; pathname) (pathname-directory default-pathname))
222         (#P"/absolute/path/name.type"
223          #p"/absolute/path/name"
224          #p"/dir/default-name.type")
225         ;; === logical pathnames ===
226         ;; recognizes a logical pathname namestring when
227         ;; default-pathname is a logical pathname
228         ;; FIXME: 0.6.12.23 fails this one.
229         ;;
230         ;; And, as it happens, it's right to fail it. Because
231         ;; #p"name1" is read in with the ambient *d-p-d* value, which
232         ;; has a physical (Unix) host; therefore, the host of the
233         ;; default-pathname argument to merge-pathnames is
234         ;; irrelevant. The result is (correctly) different if
235         ;; '#p"name1"' is replaced by "name1", below, though it's
236         ;; still not what one might expect... -- CSR, 2002-05-09
237         #+nil (#P"scratch:foo;name1" #p"name1" #p"scratch:foo;")
238         ;; or when the namestring begins with the name of a defined
239         ;; logical host followed by a colon [I assume that refers to pathname
240         ;; rather than default-pathname]
241         (#p"SCRATCH:FOO;NAME2" #p"scratch:;name2" #p"scratch:foo;")
242         ;; conduct the previous set of tests again, with a lpn first argument
243         (#P"SCRATCH:USR;LOCAL;DOC;FOO" #p"scratch:;foo" #p"/usr/local/doc/")
244         (#p"SCRATCH:SUPPLIED-DIR;NAME.TYPE"
245          #p"scratch:supplied-dir;"
246          #p"/dir/name.type")
247         (#p"SCRATCH:DIR;SUPPLIED-NAME.TYPE"
248          #p"scratch:;supplied-name"
249          #p"/dir/name.type")
250         (#p"SCRATCH:DIR;NAME.SUPPLIED-TYPE"
251          ,(make-pathname :host "scratch" :type "supplied-type")
252          #p"/dir/name.type")
253         (#p"SCRATCH:AAA;BBB;CCC;DDD;FOO;BAR"
254          ,(make-pathname :host "scratch"
255                          :directory '(:relative "foo")
256                          :name "bar")
257          #p"/aaa/bbb/ccc/ddd/eee")
258         (#p"SCRATCH:AAA;BBB;CCC;FOO;BAR"
259          ,(make-pathname :host "scratch"
260                          :directory '(:relative :back "foo")
261                          :name "bar")
262          #p"/aaa/bbb/ccc/ddd/eee")
263         (#p"SCRATCH:ABSOLUTE;PATH;NAME.TYPE"
264          #p"scratch:absolute;path;name" #p"/dir/default-name.type")
265
266         ;; FIXME: test version handling in LPNs
267         )
268       do (assert (string= (namestring (apply #'merge-pathnames params))
269                           (namestring expected-result))))
270 \f
271 ;;; host-namestring testing
272 (assert (string=
273          (namestring (parse-namestring "/foo" (host-namestring #p"/bar")))
274          "/foo"))
275 (assert (string=
276          (namestring (parse-namestring "FOO" (host-namestring #p"SCRATCH:BAR")))
277          "SCRATCH:FOO"))
278 (assert (raises-error?
279          (setf (logical-pathname-translations "")
280                (list '("**;*.*.*" "/**/*.*")))))
281 \f
282 ;;; Bug 200: translate-logical-pathname is according to the spec supposed
283 ;;; not to give errors if asked to translate a namestring for a valid
284 ;;; physical pathname.  Failed in 0.7.7.28 and before
285 (assert (string= (namestring (translate-logical-pathname "/")) "/"))
286
287 \f
288 ;;; Not strictly pathname logic testing, but until sbcl-0.7.6.19 we
289 ;;; had difficulty with non-FILE-STREAM stream arguments to pathname
290 ;;; functions (they would cause memory protection errors).  Make sure
291 ;;; that those errors are gone:
292 (assert (raises-error? (pathname (make-string-input-stream "FOO"))
293                        type-error))
294 (assert (raises-error? (merge-pathnames (make-string-output-stream))
295                        type-error))
296 \f
297 ;;;; success
298 (quit :unix-status 104)