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