d462c1be4c3e47e0a065645ff14127b8016eb5a2
[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
162 ;;; success
163 (quit :unix-status 104)