0.7.2.16:
[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 (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 (setf (logical-pathname-translations "scratch")
181       '(("**;*.*.*" "/usr/local/doc/**/*")))
182
183 (loop for (expected-result . params) in
184       `(;; trivial merge
185         (#P"/usr/local/doc/foo" #p"foo" #p"/usr/local/doc/")
186         ;; If pathname does not specify a host, device, directory,
187         ;; name, or type, each such component is copied from
188         ;; default-pathname.
189         ;; 1) no name, no type
190         (#p"/supplied-dir/name.type" #p"/supplied-dir/" #p"/dir/name.type")
191         ;; 2) no directory, no type
192         (#p"/dir/supplied-name.type" #p"supplied-name" #p"/dir/name.type")
193         ;; 3) no name, no dir (must use make-pathname as ".foo" is parsed
194         ;; as a name)
195         (#p"/dir/name.supplied-type"
196          ,(make-pathname :type "supplied-type")
197          #p"/dir/name.type")
198         ;; If (pathname-directory pathname) is a list whose car is
199         ;; :relative, and (pathname-directory default-pathname) is a
200         ;; list, then the merged directory is [...]
201         (#p"/aaa/bbb/ccc/ddd/qqq/www" #p"qqq/www" #p"/aaa/bbb/ccc/ddd/eee")
202         ;; except that if the resulting list contains a string or
203         ;; :wild immediately followed by :back, both of them are
204         ;; removed.
205         (#P"/aaa/bbb/ccc/blah/eee"
206          ;; "../" in a namestring is parsed as :up not :back, so make-pathname
207          ,(make-pathname :directory '(:relative :back "blah"))
208          #p"/aaa/bbb/ccc/ddd/eee")
209         ;; If (pathname-directory default-pathname) is not a list or
210         ;; (pathname-directory pathname) is not a list whose car is
211         ;; :relative, the merged directory is (or (pathname-directory
212         ;; pathname) (pathname-directory default-pathname))
213         (#P"/absolute/path/name.type"
214          #p"/absolute/path/name"
215          #p"/dir/default-name.type")
216         ;; === logical pathnames ===
217         ;; recognizes a logical pathname namestring when
218         ;; default-pathname is a logical pathname
219         ;; FIXME: 0.6.12.23 fails this one.
220         #+nil (#P"scratch:foo;name1" #p"name1" #p"scratch:foo;")
221         ;; or when the namestring begins with the name of a defined
222         ;; logical host followed by a colon [I assume that refers to pathname
223         ;; rather than default-pathname]
224         (#p"SCRATCH:FOO;NAME2" #p"scratch:;name2" #p"scratch:foo;")
225         ;; conduct the previous set of tests again, with a lpn first argument
226         (#P"SCRATCH:USR;LOCAL;DOC;FOO" #p"scratch:;foo" #p"/usr/local/doc/")
227         (#p"SCRATCH:SUPPLIED-DIR;NAME.TYPE"
228          #p"scratch:supplied-dir;"
229          #p"/dir/name.type")
230         (#p"SCRATCH:DIR;SUPPLIED-NAME.TYPE"
231          #p"scratch:;supplied-name"
232          #p"/dir/name.type")
233         (#p"SCRATCH:DIR;NAME.SUPPLIED-TYPE"
234          ,(make-pathname :host "scratch" :type "supplied-type")
235          #p"/dir/name.type")
236         (#p"SCRATCH:AAA;BBB;CCC;DDD;FOO;BAR"
237          ,(make-pathname :host "scratch"
238                          :directory '(:relative "foo")
239                          :name "bar")
240          #p"/aaa/bbb/ccc/ddd/eee")
241         (#p"SCRATCH:AAA;BBB;CCC;FOO;BAR"
242          ,(make-pathname :host "scratch"
243                          :directory '(:relative :back "foo")
244                          :name "bar")
245          #p"/aaa/bbb/ccc/ddd/eee")
246         (#p"SCRATCH:ABSOLUTE;PATH;NAME.TYPE"
247          #p"scratch:absolute;path;name" #p"/dir/default-name.type")
248
249         ;; FIXME: test version handling in LPNs
250         )
251       do (assert (string= (namestring (apply #'merge-pathnames params))
252                           (namestring expected-result))))
253 \f
254 ;;;; success
255 (quit :unix-status 104)