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