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