Replace 22 lousy C casts with just 3
[sbcl.git] / tests / filesys.pure.lisp
blob7533ae4153eb1194798fe69230418146b2b4fb6b
1 ;;;; This software is part of the SBCL system. See the README file for
2 ;;;; more information.
3 ;;;;
4 ;;;; While most of SBCL is derived from the CMU CL system, the test
5 ;;;; files (like this one) were written from scratch after the fork
6 ;;;; from CMU CL.
7 ;;;;
8 ;;;; This software is in the public domain and is provided with
9 ;;;; absolutely no warranty. See the COPYING and CREDITS files for
10 ;;;; more information.
12 (in-package "CL-USER")
14 ;;; In sbcl-0.6.9 FOO-NAMESTRING functions returned "" instead of NIL.
15 (with-test (:name (file-namestring directory-namestring :name))
16 (let ((pathname0 (make-pathname :host nil
17 :directory
18 (pathname-directory
19 *default-pathname-defaults*)
20 :name "getty"))
21 (pathname1 (make-pathname :host nil
22 :directory nil
23 :name nil)))
24 (assert (equal (file-namestring pathname0) "getty"))
25 (assert (equal (directory-namestring pathname0)
26 (directory-namestring *default-pathname-defaults*)))
27 (assert (equal (file-namestring pathname1) ""))
28 (assert (equal (directory-namestring pathname1) ""))))
30 ;;; In sbcl-0.6.9 DIRECTORY failed on paths with :WILD or
31 ;;; :WILD-INFERIORS in their directory components.
32 (with-test (:name (directory :wild-inferiors))
33 (let ((dir (directory "../**/*.*")))
34 ;; We know a little bit about the structure of this result;
35 ;; let's test to make sure that this test file is in it.
36 (assert (find-if (lambda (pathname)
37 (search "tests/filesys.pure.lisp"
38 (namestring pathname)))
39 dir))))
40 ;;; In sbcl-0.9.7 DIRECTORY failed on pathnames with character-set
41 ;;; components.
42 (with-test (:name (directory :character-set :pattern) )
43 (let ((dir (directory "[f]*.*")))
44 ;; We know a little bit about the structure of this result;
45 ;; let's test to make sure that this test file is in it.
46 (assert (find-if (lambda (pathname)
47 (search "filesys.pure.lisp"
48 (namestring pathname)))
49 dir))))
51 ;;; Set *default-pathname-defaults* to something other than the unix
52 ;;; cwd, to catch functions which access the filesystem without
53 ;;; merging properly. We should test more functions than just OPEN
54 ;;; here, of course
56 (with-test (:name (open *default-pathname-defaults*))
57 (let ((*default-pathname-defaults*
58 (make-pathname :directory
59 (butlast
60 (pathname-directory *default-pathname-defaults*))
61 :defaults *default-pathname-defaults*)))
62 ;; SBCL 0.7.1.2 failed to merge on OPEN
63 (with-open-file (i "tests/filesys.pure.lisp")
64 (assert i))))
66 ;;; OPEN, LOAD and friends should signal an error of type FILE-ERROR
67 ;;; if they are fed wild pathname designators; firstly, with wild
68 ;;; pathnames that don't correspond to any files:
69 (with-test (:name (open :wild file-error 1))
70 (assert (typep (nth-value 1 (ignore-errors (open "non-existent*.lisp")))
71 'file-error)))
72 (with-test (:name (load :wild file-error 1))
73 (assert (typep (nth-value 1 (ignore-errors (load "non-existent*.lisp")))
74 'file-error)))
75 ;;; then for pathnames that correspond to precisely one:
76 (with-test (:name (open :wild file-error 2))
77 (assert (typep (nth-value 1 (ignore-errors (open "filesys.pur*.lisp")))
78 'file-error)))
79 (with-test (:name (load :wild file-error 2))
80 (assert (typep (nth-value 1 (ignore-errors (load "filesys.pur*.lisp")))
81 'file-error)))
82 ;;; then for pathnames corresponding to many:
83 (with-test (:name (open :wild file-error 3))
84 (assert (typep (nth-value 1 (ignore-errors (open "*.lisp")))
85 'file-error)))
86 (with-test (:name (load :wild file-error 3))
87 (assert (typep (nth-value 1 (ignore-errors (load "*.lisp")))
88 'file-error)))
90 ;;; ANSI: FILE-LENGTH should signal an error of type TYPE-ERROR if
91 ;;; STREAM is not a stream associated with a file.
92 ;;;
93 ;;; (Peter Van Eynde's ansi-test suite caught this, and Eric Marsden
94 ;;; reported a fix for CMU CL, which was ported to sbcl-0.6.12.35.)
95 (with-test (:name (file-length *terminal-io* type-error))
96 (assert (typep (nth-value 1 (ignore-errors (file-length *terminal-io*)))
97 'type-error)))
99 ;;; A few cases Windows does have enough marbles to pass right now
100 (with-test (:name (sb-ext:native-namestring :win32)
101 :skipped-on '(not :win32))
102 (assert (equal "C:\\FOO" (native-namestring "C:\\FOO")))
103 (assert (equal "C:\\FOO" (native-namestring "C:/FOO")))
104 (assert (equal "C:\\FOO\\BAR" (native-namestring "C:\\FOO\\BAR")))
105 (assert (equal "C:\\FOO\\BAR" (native-namestring "C:\\FOO\\BAR\\" :as-file t))))
107 (with-test (:name (sb-ext:parse-native-namestring :as-directory :junk-allowed))
108 (assert
109 (equal
110 (parse-native-namestring "foo.lisp" nil *default-pathname-defaults*
111 :as-directory t)
112 (parse-native-namestring "foo.lisp" nil *default-pathname-defaults*
113 :as-directory t
114 :junk-allowed t))))
116 ;;; Test for NATIVE-PATHNAME / NATIVE-NAMESTRING stuff
118 ;;; given only safe characters in the namestring, NATIVE-PATHNAME will
119 ;;; never error, and NATIVE-NAMESTRING on the result will return the
120 ;;; original namestring.
121 (with-test (:name (sb-ext:native-namestring sb-ext:native-pathname :random))
122 (let ((safe-chars
123 (coerce
124 (cons #\Newline
125 (loop for x from 32 to 127 collect (code-char x)))
126 'simple-base-string))
127 (tricky-sequences #("/../" "../" "/.." "." "/." "./" "/./"
128 "[]" "*" "**" "/**" "**/" "/**/" "?"
129 "\\*" "\\[]" "\\?" "\\*\\*" "*\\*")))
130 (loop repeat 1000
131 for length = (random 32)
132 for native-namestring = (coerce
133 (loop repeat length
134 collect
135 (char safe-chars
136 (random (length safe-chars))))
137 'simple-base-string)
138 for pathname = (native-pathname native-namestring)
139 for nnn = (native-namestring pathname)
140 do #+win32
141 ;; We canonicalize to \ as the directory separator
142 ;; on windows -- though both \ and / are legal.
143 (setf native-namestring (substitute #\\ #\/ native-namestring))
144 (unless (string= nnn native-namestring)
145 (error "1: wanted ~S, got ~S" native-namestring nnn)))
146 (loop repeat 1000
147 for native-namestring = (with-output-to-string (s)
148 (write-string "mu" s)
149 (loop
150 (let ((r (random 1.0)))
151 (cond
152 ((< r 1/20) (return))
153 ((< r 1/2)
154 (write-char
155 (char safe-chars
156 (random (length safe-chars)))
158 (t (write-string
159 (aref tricky-sequences
160 (random
161 (length tricky-sequences)))
162 s))))))
163 for pathname = (native-pathname native-namestring)
164 for tricky-nnn = (native-namestring pathname)
165 do #+win32
166 ;; We canonicalize to \ as the directory separator
167 ;; on windows -- though both \ and / are legal.
168 (setf native-namestring (substitute #\\ #\/ native-namestring))
169 (unless (string= tricky-nnn native-namestring)
170 (error "2: wanted ~S, got ~S" native-namestring tricky-nnn)))))
172 ;;; USER-HOMEDIR-PATHNAME and the extension SBCL-HOMEDIR-PATHNAME both
173 ;;; used to call PARSE-NATIVE-NAMESTRING without supplying a HOST
174 ;;; argument, and so would lose when *DEFAULT-PATHNAME-DEFAULTS* was a
175 ;;; logical pathname.
176 (with-test (:name (user-homedir-pathname :robustness))
177 (let ((*default-pathname-defaults* (pathname "SYS:")))
178 (assert (not (typep (user-homedir-pathname)
179 'logical-pathname)))))
181 (with-test (:name (sb-int:sbcl-homedir-pathname :robustness))
182 (let ((*default-pathname-defaults* (pathname "SYS:")))
183 (assert (not (typep (sb-int:sbcl-homedir-pathname)
184 'logical-pathname)))))
186 (with-test (:name (file-author stringp))
187 #-win32
188 (assert (stringp (file-author (user-homedir-pathname))))
189 #+win32
190 (assert (not (file-author (user-homedir-pathname)))))
191 (with-test (:name (file-write-date integerp))
192 (assert (integerp (file-write-date (user-homedir-pathname)))))
194 ;;; Canonicalization of pathnames for DIRECTORY
195 (with-test (:name (directory :/.))
196 (assert (equal (directory #p".") (directory #p"./")))
197 (assert (equal (directory #p".") (directory #p""))))
198 (with-test (:name (directory :/..))
199 (assert (equal (directory #p"..") (directory #p"../"))))
200 (with-test (:name (directory :unspecific))
201 (assert (equal (directory #p".")
202 (directory (make-pathname
203 :name :unspecific
204 :type :unspecific)))))
206 ;;; This used to signal a TYPE-ERROR.
207 (with-test (:name (directory :..*))
208 (directory "somedir/..*"))
210 ;;; DIRECTORY used to treat */** as **.
211 (with-test (:name (directory :*/**))
212 (assert (equal (directory "*/**/*.*")
213 (mapcan (lambda (directory)
214 (directory (merge-pathnames "**/*.*" directory)))
215 (directory "*/")))))
217 ;;; Generated with
218 ;;; (loop for exist in '(nil t)
219 ;;; append
220 ;;; (loop for (if-exists if-does-not-exist) in '((nil :error)
221 ;;; (:error nil)
222 ;;; (nil nil)
223 ;;; (:error :error))
224 ;;; collect (list 'do-open exist if-exists if-does-not-exist)))
225 (with-test (:name (open :never-openning))
226 (flet ((do-open (existing if-exists if-does-not-exist
227 &optional (direction :output))
228 (open (if existing
229 #.(or *compile-file-truename* *load-truename*)
230 "a-really-non-existing-file")
231 :direction direction
232 :if-exists if-exists :if-does-not-exist if-does-not-exist)))
233 (assert-error
234 (do-open nil nil :error))
235 (assert (not
236 (do-open nil :error nil)))
237 (assert (not
238 (do-open t nil :error)))
239 (assert-error
240 (do-open t :error nil))
241 (assert (not
242 (do-open nil nil nil)))
243 (assert-error
244 (do-open nil :error :error))
245 (assert (not
246 (do-open t nil nil)))
247 (assert-error (do-open t :error :error))
249 (assert-error
250 (do-open nil nil :error :io))
251 (assert (not
252 (do-open nil :error nil :io)))
253 (assert (not
254 (do-open t nil :error :io)))
255 (assert-error
256 (do-open t :error nil :io))
257 (assert (not
258 (do-open nil nil nil :io)))
259 (assert-error
260 (do-open nil :error :error :io))
261 (assert (not
262 (do-open t nil nil :io)))
263 (assert-error (do-open t :error :error :io))))
265 (with-test (:name (open :new-version))
266 (multiple-value-bind (value error)
267 (ignore-errors (open #.(or *compile-file-truename* *load-truename*)
268 :direction :output
269 :if-exists :new-version))
270 (assert (not value))
271 (assert error)
272 (assert (equal (simple-condition-format-control error)
273 "OPEN :IF-EXISTS :NEW-VERSION is not supported ~
274 when a new version must be created."))))