1 ;;;; miscellaneous tests of pathname-related stuff
3 ;;;; This file is naturally impure because we mess with
4 ;;;; LOGICAL-PATHNAME-TRANSLATIONS.
6 ;;;; This software is part of the SBCL system. See the README file for
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
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.
17 (in-package "CL-USER")
19 (load "assertoid.lisp")
21 (setf (logical-pathname-translations "demo0")
22 '(("**;*.*.*" "/tmp/")))
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).
28 (typep (grab-condition (translate-logical-pathname "demo0::bla;file.lisp"))
31 ;;; some things SBCL-0.6.9 used not to parse correctly:
33 ;;; SBCL used to throw an error saying there's no translation.
34 (assert (equal (namestring (translate-logical-pathname "demo0: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"))
44 ;;; That should be correct:
45 (assert (equal (namestring (translate-logical-pathname "demo1:foo.lisp"))
47 ;;; Check for absolute/relative path confusion:
48 (assert (not (equal (namestring (translate-logical-pathname "demo1:;foo.lisp"))
50 (assert (equal (namestring (translate-logical-pathname "demo1:;foo.lisp"))
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")
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"
70 (pn2 (make-pathname :host "foo" :directory "ETC" :name "inetd"
72 (pn3 (read-from-string (prin1-to-string pn1))))
73 (assert (equal pn1 pn2))
74 (assert (equal pn1 pn3)))
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
82 (assert (not (ignore-errors
83 (make-pathname :host "FOO" :directory "!bla" :name "bar"))))
85 ;; error: name-component not valid
86 (assert (not (ignore-errors
87 (make-pathname :host "FOO" :directory "bla" :name "!bar"))))
89 ;; error: type-component not valid.
90 (assert (not (ignore-errors
91 (make-pathname :host "FOO" :directory "bla" :name "bar"
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)) ""))
101 ;;; The third would work if the call were (and it should continue to
105 (translate-logical-pathname
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)))
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"))))
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)))
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"))
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"))
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"))
164 ;;;; MERGE-PATHNAME tests
166 ;;;; There are some things we don't bother testing, just because they're
167 ;;;; not meaningful on the underlying filesystem anyway.
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
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.
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/**/*"))))
188 (loop for (expected-result . params) in
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
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
200 (#p"/dir/name.supplied-type"
201 ,(make-pathname :type "supplied-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
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.
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;"
243 (#p"SCRATCH:DIR;SUPPLIED-NAME.TYPE"
244 #p"scratch:;supplied-name"
246 (#p"SCRATCH:DIR;NAME.SUPPLIED-TYPE"
247 ,(make-pathname :host "scratch" :type "supplied-type")
249 (#p"SCRATCH:AAA;BBB;CCC;DDD;FOO;BAR"
250 ,(make-pathname :host "scratch"
251 :directory '(:relative "foo")
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")
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")
262 ;; FIXME: test version handling in LPNs
264 do (assert (string= (namestring (apply #'merge-pathnames params))
265 (namestring expected-result))))
267 ;;; host-namestring testing
269 (namestring (parse-namestring "/foo" (host-namestring #p"/bar")))
272 (namestring (parse-namestring "FOO" (host-namestring #p"SCRATCH:BAR")))
274 (assert (raises-error?
275 (setf (logical-pathname-translations "")
276 (list '("**;*.*.*" "/**/*.*")))))
278 ;;; Not strictly pathname logic testing, but until sbcl-0.7.6.19 we
279 ;;; had difficulty with non-FILE-STREAM stream arguments to pathname
280 ;;; functions (they would cause memory protection errors). Make sure
281 ;;; that those errors are gone:
282 (assert (raises-error? (pathname (make-string-input-stream "FOO"))
284 (assert (raises-error? (merge-pathnames (make-string-output-stream))
288 (quit :unix-status 104)