7fd7e3ac3d23306a07215e7867dbd64b2c697da4
[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 ;;(eval-when (:compile-toplevel :load-toplevel :execute)
20   (defmacro grab-condition (&body body)
21     `(nth-value 1
22       (ignore-errors ,@body)))
23 ;;)
24
25 (setf (logical-pathname-translations "demo0")
26       '(("**;*.*.*" "/tmp/")))
27
28 ;;; In case of a parse error we want to get a condition of type
29 ;;; CL:PARSE-ERROR (or more specifically, of type
30 ;;; SB-KERNEL:NAMESTRING-PARSE-ERROR).
31 (assert
32   (typep (grab-condition (translate-logical-pathname "demo0::bla;file.lisp"))
33          'parse-error))
34
35 ;;; some things SBCL-0.6.9 used not to parse correctly:
36 ;;;
37 ;;; SBCL used to throw an error saying there's no translation.
38 (assert (equal (namestring (translate-logical-pathname "demo0:file.lisp"))
39                "/tmp/file.lisp"))
40 ;;; We do not match a null directory to every wild path:
41 (assert (not (pathname-match-p "demo0:file.lisp"
42                                (logical-pathname "demo0:tmp;**;*.*.*"))))
43 ;;; Remove "**" from our resulting pathname when the source-dir is NIL:
44 (setf (logical-pathname-translations "demo1")
45       '(("**;*.*.*" "/tmp/**/*.*") (";**;*.*.*" "/tmp/rel/**/*.*")))
46 (assert (not (equal (namestring (translate-logical-pathname "demo1:foo.lisp"))
47                     "/tmp/**/foo.lisp")))
48 ;;; That should be correct:
49 (assert (equal (namestring (translate-logical-pathname "demo1:foo.lisp"))
50                "/tmp/foo.lisp"))
51 ;;; Check for absolute/relative path confusion:
52 (assert (not (equal (namestring (translate-logical-pathname "demo1:;foo.lisp"))
53                      "tmp/rel/foo.lisp")))
54 (assert (equal (namestring (translate-logical-pathname "demo1:;foo.lisp"))
55                "/tmp/rel/foo.lisp"))
56                      
57 ;;; Under SBCL: new function #'UNPARSE-ENOUGH-NAMESTRING, to
58 ;;; handle the following case exactly (otherwise we get an error:
59 ;;; "#'IDENTITY CALLED WITH 2 ARGS."
60 (setf (logical-pathname-translations "demo2")
61         '(("test;**;*.*" "/tmp/demo2/test/")))
62 (enough-namestring "demo2:test;foo.lisp")
63
64 ;;; When a pathname comes from a logical host, it should be in upper
65 ;;; case. (This doesn't seem to be specifically required in the ANSI
66 ;;; spec, but it's left up to the implementors, and the arguments made
67 ;;; in the cleanup issue PATHNAME-LOGICAL:ADD seem to be a pretty
68 ;;; compelling reason for the implementors to choose case
69 ;;; insensitivity and a canonical case.)
70 (setf (logical-pathname-translations "FOO") 
71       '(("**;*.*.*" "/full/path/to/foo/**/*.*.*")))
72 (let* ((pn1 (make-pathname :host "FOO" :directory "etc" :name "INETD" 
73                            :type "conf"))
74        (pn2 (make-pathname :host "foo" :directory "ETC" :name "inetd" 
75                            :type "CONF"))
76        (pn3 (read-from-string (prin1-to-string pn1))))
77   (assert (equal pn1 pn2))
78   (assert (equal pn1 pn3)))
79
80 ;;; We may need to parse the host as a LOGICAL-NAMESTRING HOST. The
81 ;;; HOST in PARSE-NAMESTRING can be either a string or :UNSPECIFIC
82 ;;; without actually requiring the system to signal an error (apart
83 ;;; from host mismatches).
84 (assert (equal (namestring (parse-namestring "" "FOO")) "FOO:"))
85 (assert (equal (namestring (parse-namestring "" :unspecific)) ""))
86
87 ;;; The third would work if the call were (and it should continue to
88 ;;; work ...)
89 (parse-namestring ""
90                   (pathname-host
91                    (translate-logical-pathname
92                     "FOO:")))
93
94 ;;; ANSI says PARSE-NAMESTRING returns TYPE-ERROR on host mismatch.
95 (let ((cond (grab-condition (parse-namestring "foo:jeamland" "demo2"))))
96   (assert (typep cond 'type-error)))
97
98 ;;; ANSI, in its wisdom, specifies that it's an error (specifically a
99 ;;; TYPE-ERROR) to query the system about the translations of a string
100 ;;; which doesn't have any translations. It's not clear why we don't
101 ;;; just return NIL in that case, but they make the rules..
102 (let ((cond (grab-condition (logical-pathname-translations "unregistered-host"))))
103   (assert (typep cond 'type-error)))
104
105 ;;; examples from CLHS: Section 19.4, Logical Pathname Translations
106 ;;; (sometimes converted to the Un*x way of things)
107 (setf (logical-pathname-translations "test0")
108         '(("**;*.*.*"              "/library/foo/**/")))
109 (assert (equal (namestring (translate-logical-pathname
110                             "test0:foo;bar;baz;mum.quux.3"))
111                "/library/foo/foo/bar/baz/mum.quux.3"))
112 (setf (logical-pathname-translations "prog")
113         '(("RELEASED;*.*.*"        "MY-UNIX:/sys/bin/my-prog/")
114           ("RELEASED;*;*.*.*"      "MY-UNIX:/sys/bin/my-prog/*/")
115           ("EXPERIMENTAL;*.*.*"    "MY-UNIX:/usr/Joe/development/prog/")
116           ("EXPERIMENTAL;*;*.*.*"  "MY-UNIX:/usr/Joe/development/prog/*/")))
117 (setf (logical-pathname-translations "prog")
118         '(("CODE;*.*.*"             "/lib/prog/")))
119 (assert (equal (namestring (translate-logical-pathname
120                             "prog:code;documentation.lisp"))
121                "/lib/prog/documentation.lisp"))
122 (setf (logical-pathname-translations "prog")
123         '(("CODE;DOCUMENTATION.*.*" "/lib/prog/docum.*")
124           ("CODE;*.*.*"             "/lib/prog/")))
125 (assert (equal (namestring (translate-logical-pathname
126                             "prog:code;documentation.lisp"))
127                "/lib/prog/docum.lisp"))
128
129 ;;; success
130 (quit :unix-status 104)
131 (in-package :cl-user)