Fix equality between #p"~" and (user-homedir-pathname) on Win32.
[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 (load "assertoid.lisp")
18 (use-package "ASSERTOID")
19
20 (load "test-util.lisp")
21 (use-package "TEST-UTIL")
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 TYPE-ERROR,
27 ;;; because ANSI says so. (This used to be PARSE-ERROR.)
28 (assert
29   (typep (grab-condition (logical-pathname "demo0::bla;file.lisp"))
30          'type-error))
31
32 ;;; some things SBCL-0.6.9 used not to parse correctly:
33 ;;;
34 ;;; SBCL used to throw an error saying there's no translation.
35 (with-test (:name (:logical-pathname 1))
36   (assert (equal (namestring (translate-logical-pathname "demo0:file.lisp"))
37                  "/tmp/file.lisp")))
38
39 ;;; We do not match a null directory to every wild path:
40 (with-test (:name (:logical-pathname 2))
41   (assert (not (pathname-match-p "demo0:file.lisp"
42                                  (logical-pathname "demo0:tmp;**;*.*.*")))))
43
44 ;;; Remove "**" from our resulting pathname when the source-dir is NIL:
45 (with-test (:name (:logical-pathname 3))
46   (setf (logical-pathname-translations "demo1")
47         '(("**;*.*.*" "/tmp/**/*.*") (";**;*.*.*" "/tmp/rel/**/*.*")))
48   (assert (not (equal (namestring (translate-logical-pathname "demo1:foo.lisp"))
49                     "/tmp/**/foo.lisp"))))
50
51 ;;; That should be correct:
52 (with-test (:name (:logical-pathname 4))
53   (assert (equal (namestring (translate-logical-pathname "demo1:foo.lisp"))
54                  "/tmp/foo.lisp")))
55
56 ;;; Check for absolute/relative path confusion:
57 (with-test (:name (:logical-pathname 5))
58   (assert (not (equal (namestring (translate-logical-pathname "demo1:;foo.lisp"))
59                       "tmp/rel/foo.lisp")))
60   (assert (equal (namestring (translate-logical-pathname "demo1:;foo.lisp"))
61                  "/tmp/rel/foo.lisp")))
62
63 ;;; Under SBCL: new function #'UNPARSE-ENOUGH-NAMESTRING, to
64 ;;; handle the following case exactly (otherwise we get an error:
65 ;;; "#'IDENTITY CALLED WITH 2 ARGS."
66 (with-test (:name (:logical-pathname 6))
67   (setf (logical-pathname-translations "demo2")
68         '(("test;**;*.*" "/tmp/demo2/test")))
69   (enough-namestring "demo2:test;foo.lisp"))
70
71 ;;; When a pathname comes from a logical host, it should be in upper
72 ;;; case. (This doesn't seem to be specifically required in the ANSI
73 ;;; spec, but it's left up to the implementors, and the arguments made
74 ;;; in the cleanup issue PATHNAME-LOGICAL:ADD seem to be a pretty
75 ;;; compelling reason for the implementors to choose case
76 ;;; insensitivity and a canonical case.)
77 (with-test (:name (:logical-pathname 7))
78   (setf (logical-pathname-translations "FOO")
79         '(("**;*.*.*" "/full/path/to/foo/**/*.*")))
80   (let* ((pn1 (make-pathname :host "FOO" :directory "etc" :name "INETD"
81                              :type "conf"))
82          (pn2 (make-pathname :host "foo" :directory "ETC" :name "inetd"
83                              :type "CONF"))
84          (pn3 (read-from-string (prin1-to-string pn1))))
85     (assert (equal pn1 pn2))
86     (assert (equal pn1 pn3))))
87
88 ;;; In addition to the upper-case constraint above, if the logical-pathname
89 ;;; contains a string component in e.g. the directory, name and type slot,
90 ;;; these should be valid "WORDS", according to CLHS 19.3.1.
91 ;;; FIXME: currently SBCL throws NAMESTRING-PARSE-ERROR: should this be
92 ;;; a TYPE-ERROR?
93 (with-test (:name (:logical-pathname 8))
94   (locally
95     ;; MAKE-PATHNAME is UNSAFELY-FLUSHABLE
96     (declare (optimize safety))
97
98     (assert (not (ignore-errors
99                   (make-pathname :host "FOO" :directory "!bla" :name "bar"))))
100
101     ;; error: name-component not valid
102     (assert (not (ignore-errors
103                   (make-pathname :host "FOO" :directory "bla" :name "!bar"))))
104
105     ;; error: type-component not valid.
106     (assert (not (ignore-errors
107                   (make-pathname :host "FOO" :directory "bla" :name "bar"
108                                  :type "&baz"))))))
109
110 ;;; We may need to parse the host as a LOGICAL-NAMESTRING HOST. The
111 ;;; HOST in PARSE-NAMESTRING can be either a string or :UNSPECIFIC
112 ;;; without actually requiring the system to signal an error (apart
113 ;;; from host mismatches).
114 (with-test (:name (:logical-pathname 9))
115   (assert (equal (namestring (parse-namestring "" "FOO")) "FOO:"))
116   (assert (equal (namestring (parse-namestring "" :unspecific)) "")))
117
118 ;;; The third would work if the call were (and it should continue to
119 ;;; work ...)
120 (with-test (:name (:logical-pathname 10))
121   (parse-namestring ""
122                     (pathname-host
123                      (translate-logical-pathname
124                       "FOO:"))))
125
126 ;;; ANSI says PARSE-NAMESTRING returns TYPE-ERROR on host mismatch.
127 (with-test (:name (:logical-pathname 11))
128   (let ((cond (grab-condition (parse-namestring "foo:jeamland" "demo2"))))
129     (assert (typep cond 'type-error))))
130
131 ;;; turning one logical pathname into another:
132 (with-test (:name (:logical-pathname 12))
133   (setf (logical-pathname-translations "foo")
134         '(("todemo;*.*.*" "demo0:*.*.*")))
135   (assert (equal (namestring (translate-logical-pathname "foo:todemo;x.y"))
136                  (namestring (translate-logical-pathname "demo0:x.y")))))
137
138 ;;; ANSI, in its wisdom, specifies that it's an error (specifically a
139 ;;; TYPE-ERROR) to query the system about the translations of a string
140 ;;; which doesn't have any translations. It's not clear why we don't
141 ;;; just return NIL in that case, but they make the rules..
142 (with-test (:name (:logical-pathname 13))
143   (let ((cond (grab-condition (logical-pathname-translations "unregistered-host"))))
144     (assert (typep cond 'type-error)))
145
146   (assert (not (string-equal (host-namestring (parse-namestring "OTHER-HOST:ILLEGAL/LPN")) "OTHER-HOST")))
147   (assert (string-equal (pathname-name (parse-namestring "OTHER-HOST:ILLEGAL/LPN")) "LPN")))
148
149 ;;; FIXME: A comment on this section up to sbcl-0.6.11.30 or so said
150 ;;;   examples from CLHS: Section 19.4, LOGICAL-PATHNAME-TRANSLATIONS
151 ;;;   (sometimes converted to the Un*x way of things)
152 ;;; but when I looked it up I didn't see the connection. Presumably
153 ;;; there's some code in this section which should be attributed
154 ;;; to something in the ANSI spec, but I don't know what code it is
155 ;;; or what section of the specification has the related code.
156 (with-test (:name (:logical-pathname 14))
157   (setf (logical-pathname-translations "test0")
158         '(("**;*.*.*"              "/library/foo/**/")))
159   (assert (equal (namestring (translate-logical-pathname
160                               "test0:foo;bar;baz;mum.quux"))
161                  "/library/foo/foo/bar/baz/mum.quux"))
162   (setf (logical-pathname-translations "prog")
163         '(("RELEASED;*.*.*"        "MY-UNIX:/sys/bin/my-prog/")
164           ("RELEASED;*;*.*.*"      "MY-UNIX:/sys/bin/my-prog/*/")
165           ("EXPERIMENTAL;*.*.*"    "MY-UNIX:/usr/Joe/development/prog/")
166           ("EXPERIMENTAL;*;*.*.*"  "MY-UNIX:/usr/Joe/development/prog/*/")))
167   (setf (logical-pathname-translations "prog")
168         '(("CODE;*.*.*"             "/lib/prog/")))
169   (assert (equal (namestring (translate-logical-pathname
170                               "prog:code;documentation.lisp"))
171                  "/lib/prog/documentation.lisp"))
172   (setf (logical-pathname-translations "prog")
173         '(("CODE;DOCUMENTATION.*.*" "/lib/prog/docum.*")
174           ("CODE;*.*.*"             "/lib/prog/")))
175   (assert (equal (namestring (translate-logical-pathname
176                               "prog:code;documentation.lisp"))
177                  "/lib/prog/docum.lisp")))
178
179 ;;; ANSI section 19.3.1.1.5 specifies that translation to a filesystem
180 ;;; which doesn't have versions should ignore the version slot. CMU CL
181 ;;; didn't ignore this as it should, but we do.
182 (with-test (:name (:logical-pathname 15))
183   (assert (equal (namestring (translate-logical-pathname
184                               "test0:foo;bar;baz;mum.quux.3"))
185                  "/library/foo/foo/bar/baz/mum.quux")))
186
187 \f
188 ;;;; MERGE-PATHNAME tests
189 ;;;;
190 ;;;; There are some things we don't bother testing, just because they're
191 ;;;; not meaningful on the underlying filesystem anyway.
192 ;;;;
193 ;;;; Mostly that means that we don't do devices, we don't do versions
194 ;;;; except minimally in LPNs (they get lost in the translation to
195 ;;;; physical hosts, so it's not much of an issue), and we don't do
196 ;;;; hosts except for LPN hosts
197 ;;;;
198 ;;;; Although these tests could conceivably be useful in principle for
199 ;;;; other implementations, they depend quite heavily on the rules for
200 ;;;; namestring parsing, which are implementation-specific. So, success
201 ;;;; or failure in these tests doesn't tell you anything about
202 ;;;; ANSI-compliance unless your PARSE-NAMESTRING works like ours.
203
204 ;;; Needs to be done at compile time, so that the #p"" read-macro
205 ;;; correctly parses things as logical pathnames. This is not a
206 ;;; problem as was, as this is an impure file and so gets loaded in,
207 ;;; but just for future proofing...
208 (eval-when (:compile-toplevel :load-toplevel :execute)
209   (setf (logical-pathname-translations "scratch")
210         '(("**;*.*.*" "/usr/local/doc/**/*"))))
211
212 (with-test (:name (:merge-pathname 1))
213   (loop for (expected-result . params) in
214         `( ;; trivial merge
215           (#P"/usr/local/doc/foo" #p"foo" #p"/usr/local/doc/")
216           ;; If pathname does not specify a host, device, directory,
217           ;; name, or type, each such component is copied from
218           ;; default-pathname.
219           ;; 1) no name, no type
220           (#p"/supplied-dir/name.type" #p"/supplied-dir/" #p"/dir/name.type")
221           ;; 2) no directory, no type
222           (#p"/dir/supplied-name.type" #p"supplied-name" #p"/dir/name.type")
223           ;; 3) no name, no dir (must use make-pathname as ".foo" is parsed
224           ;; as a name)
225           (#p"/dir/name.supplied-type"
226              ,(make-pathname :type "supplied-type")
227              #p"/dir/name.type")
228           ;; If (pathname-directory pathname) is a list whose car is
229           ;; :relative, and (pathname-directory default-pathname) is a
230           ;; list, then the merged directory is [...]
231           (#p"/aaa/bbb/ccc/ddd/qqq/www" #p"qqq/www" #p"/aaa/bbb/ccc/ddd/eee")
232           ;; except that if the resulting list contains a string or
233           ;; :wild immediately followed by :back, both of them are
234           ;; removed.
235           (#P"/aaa/bbb/ccc/blah/eee"
236              ;; "../" in a namestring is parsed as :up not :back, so make-pathname
237              ,(make-pathname :directory '(:relative :back "blah"))
238              #p"/aaa/bbb/ccc/ddd/eee")
239           ;; If (pathname-directory default-pathname) is not a list or
240           ;; (pathname-directory pathname) is not a list whose car is
241           ;; :relative, the merged directory is (or (pathname-directory
242           ;; pathname) (pathname-directory default-pathname))
243           (#P"/absolute/path/name.type"
244              #p"/absolute/path/name"
245              #p"/dir/default-name.type")
246           ;; === logical pathnames ===
247           ;; recognizes a logical pathname namestring when
248           ;; default-pathname is a logical pathname
249           ;; FIXME: 0.6.12.23 fails this one.
250           ;;
251           ;; And, as it happens, it's right to fail it. Because
252           ;; #p"name1" is read in with the ambient *d-p-d* value, which
253           ;; has a physical (Unix) host; therefore, the host of the
254           ;; default-pathname argument to merge-pathnames is
255           ;; irrelevant. The result is (correctly) different if
256           ;; '#p"name1"' is replaced by "name1", below, though it's
257           ;; still not what one might expect... -- CSR, 2002-05-09
258           #+nil (#P"scratch:foo;name1" #p"name1" #p"scratch:foo;")
259           ;; or when the namestring begins with the name of a defined
260           ;; logical host followed by a colon [I assume that refers to pathname
261           ;; rather than default-pathname]
262           (#p"SCRATCH:FOO;NAME2" #p"scratch:;name2" #p"scratch:foo;")
263           ;; conduct the previous set of tests again, with a lpn first argument
264           (#P"SCRATCH:USR;LOCAL;DOC;FOO" #p"scratch:;foo" #p"/usr/local/doc/")
265           (#p"SCRATCH:SUPPLIED-DIR;NAME.TYPE"
266              #p"scratch:supplied-dir;"
267              #p"/dir/name.type")
268           (#p"SCRATCH:DIR;SUPPLIED-NAME.TYPE"
269              #p"scratch:;supplied-name"
270              #p"/dir/name.type")
271           (#p"SCRATCH:DIR;NAME.SUPPLIED-TYPE"
272              ,(make-pathname :host "scratch" :type "supplied-type")
273              #p"/dir/name.type")
274           (#p"SCRATCH:AAA;BBB;CCC;DDD;FOO;BAR"
275              ,(make-pathname :host "scratch"
276                              :directory '(:relative "foo")
277                              :name "bar")
278              #p"/aaa/bbb/ccc/ddd/eee")
279           (#p"SCRATCH:AAA;BBB;CCC;FOO;BAR"
280              ,(make-pathname :host "scratch"
281                              :directory '(:relative :back "foo")
282                              :name "bar")
283              #p"/aaa/bbb/ccc/ddd/eee")
284           (#p"SCRATCH:ABSOLUTE;PATH;NAME.TYPE"
285              #p"scratch:absolute;path;name" #p"/dir/default-name.type")
286
287           ;; FIXME: test version handling in LPNs
288           )
289         do (let ((result (apply #'merge-pathnames params)))
290              (macrolet ((frob (op)
291                               `(assert (equal (,op result) (,op expected-result)))))
292                (frob pathname-host)
293                (frob pathname-directory)
294                (frob pathname-name)
295                (frob pathname-type)))))
296 \f
297 ;;; host-namestring testing
298 (with-test (:name :host-namestring)
299   (assert (string=
300            (namestring (parse-namestring "/foo" (host-namestring #p"/bar")))
301            "/foo"))
302   (assert (string=
303            (namestring (parse-namestring "FOO" (host-namestring #p"SCRATCH:BAR")))
304            "SCRATCH:FOO"))
305   (assert (raises-error?
306            (setf (logical-pathname-translations "")
307                  (list '("**;*.*.*" "/**/*.*"))))))
308 \f
309 ;;; Bug 200: translate-logical-pathname is according to the spec supposed
310 ;;; not to give errors if asked to translate a namestring for a valid
311 ;;; physical pathname.  Failed in 0.7.7.28 and before
312 (with-test (:name (:logical-pathname 16))
313   (assert (string= (namestring (translate-logical-pathname "/")) "/")))
314
315 \f
316 ;;; Not strictly pathname logic testing, but until sbcl-0.7.6.19 we
317 ;;; had difficulty with non-FILE-STREAM stream arguments to pathname
318 ;;; functions (they would cause memory protection errors).  Make sure
319 ;;; that those errors are gone:
320 (with-test (:name (:string-streams-as-pathnames 1))
321   (assert (raises-error? (pathname (make-string-input-stream "FOO"))
322                          type-error))
323   (assert (raises-error? (merge-pathnames (make-string-output-stream))
324                          type-error)))
325 \f
326 ;;; ensure print-read consistency (or print-not-readable-error) on
327 ;;; pathnames:
328 (with-test (:name :print/read-consistency)
329   (let ((pathnames (list
330                     (make-pathname :name "foo" :type "txt" :version :newest)
331                     (make-pathname :name "foo" :type "txt" :version 1)
332                     (make-pathname :name "foo" :type ".txt")
333                     (make-pathname :name "foo." :type "txt")
334                     (parse-namestring "SCRATCH:FOO.TXT.1")
335                     (parse-namestring "SCRATCH:FOO.TXT.NEWEST")
336                     (parse-namestring "SCRATCH:FOO.TXT"))))
337     (dolist (p pathnames)
338       (print p)
339       (handler-case
340           (let* ((*print-readably* t)
341                  (new (read-from-string (format nil "~S" p))))
342             (unless (equal new p)
343               (let ((*print-readably* nil))
344                 (error "oops: host:~S device:~S dir:~S version:~S~% ->~%~
345                              host:~S device:~S dir:~S version:~S"
346                        (pathname-host p) (pathname-device p)
347                        (pathname-directory p) (pathname-version p)
348                        (pathname-host new) (pathname-device new)
349                        (pathname-directory new) (pathname-version new)))))
350         (print-not-readable ()
351           nil)))))
352 \f
353 ;;; BUG 330: "PARSE-NAMESTRING should accept namestrings as the
354 ;;; default argument" ...and streams as well
355 (with-test (:name :parse-namestring/stream)
356   (assert (equal (parse-namestring "foo" nil "/")
357                  (parse-namestring "foo" nil #P"/")))
358   (let ((test "parse-namestring-test.tmp"))
359     (unwind-protect
360         (with-open-file (f test :direction :output)
361           ;; FIXME: This test is a bit flaky, since we only check that
362           ;; no error is signalled. The dilemma here is "what is the
363           ;; correct result when defaults is a _file_, not a
364           ;; directory". Currently (0.8.10.73) we get #P"foo" here (as
365           ;; opposed to eg. #P"/path/to/current/foo"), which is
366           ;; possibly mildly surprising but probably conformant.
367           (assert (parse-namestring "foo" nil f)))
368       (when (probe-file test)
369         (delete-file test)))))
370 \f
371 ;;; ENOUGH-NAMESTRING should probably not fail when the namestring in
372 ;;; question has a :RELATIVE pathname.
373 (with-test (:name :enough-namestring)
374   (assert (equal (enough-namestring #p"foo" #p"./") "foo")))
375 \f
376 ;;; bug reported by Artem V. Andreev: :WILD not handled in unparsing
377 ;;; directory lists.
378 (with-test (:name :unparse-wild)
379   (assert (equal (namestring #p"/tmp/*/") "/tmp/*/")))
380
381 ;;; Printing of pathnames; see CLHS 22.1.3.1. This section was started
382 ;;; to confirm that pathnames are printed as their namestrings under
383 ;;; :escape nil :readably nil.
384 (with-test (:name :print-as-namestrings)
385   (loop for (pathname expected . vars) in
386         `((#p"/foo" "#P\"/foo\"")
387           (#p"/foo" "#P\"/foo\"" :readably nil)
388           (#p"/foo" "#P\"/foo\"" :escape nil)
389           (#p"/foo" "/foo"       :readably nil :escape nil))
390         for actual = (with-standard-io-syntax
391                       (apply #'write-to-string pathname vars))
392         do (assert (string= expected actual)
393                    ()
394                    "~S should be ~S, was ~S"
395                    (list* 'write-to-string pathname vars)
396                    expected
397                    actual)))
398 \f
399 ;;; we got (truename "/") wrong for about 6 months.  Check that it's
400 ;;; still right.
401 (with-test (:name :root-truename)
402   (let ((pathname (truename "/")))
403     (assert (equalp pathname (merge-pathnames #p"/")))
404     (assert (equal (pathname-directory pathname) '(:absolute)))))
405 \f
406 ;;; we failed to unparse logical pathnames with :NAME :WILD :TYPE NIL.
407 ;;; (Reported by Pascal Bourguignon.
408 (with-test (:name :unparse-logical-wild)
409   (let ((pathname (make-pathname :host "SYS" :directory '(:absolute :wild-inferiors)
410                                  :name :wild :type nil)))
411     (assert (string= (namestring pathname) "SYS:**;*"))
412     (assert (string= (write-to-string pathname :readably t) "#P\"SYS:**;*\""))))
413 \f
414 ;;; reported by James Y Knight on sbcl-devel 2006-05-17
415 (with-test (:name :merge-back)
416   (let ((p1 (make-pathname :directory '(:relative "bar")))
417         (p2 (make-pathname :directory '(:relative :back "foo"))))
418     (assert (equal (merge-pathnames p1 p2)
419                    (make-pathname :directory '(:relative :back "foo" "bar"))))))
420
421 ;;; construct native namestrings even if the directory is empty (means
422 ;;; that same as if (:relative))
423 (with-test (:name :native-namestring)
424   (assert (equal (sb-ext:native-namestring (make-pathname :directory '(:relative)
425                                                           :name "foo"
426                                                           :type "txt"))
427                  (sb-ext:native-namestring (let ((p (make-pathname :directory nil
428                                                                    :name "foo"
429                                                                    :type "txt")))
430                                              (assert (not (pathname-directory p)))
431                                              p)))))
432
433 ;;; reported by Richard Kreuter: PATHNAME and MERGE-PATHNAMES used to
434 ;;; be unsafely-flushable. Since they are known to return non-nil values
435 ;;; only, the test-node of the IF is flushed, and since the function
436 ;;; is unsafely-flushable, out it goes, and bad pathname designators
437 ;;; breeze through.
438 ;;;
439 ;;; These tests rely on using a stream that appears as a file-stream
440 ;;; but isn't a valid pathname-designator.
441 (with-test (:name :dont-flush-pathnames)
442   (assert (eq :false
443               (if (ignore-errors (pathname sb-sys::*tty*)) :true :false)))
444   (assert (eq :false
445               (if (ignore-errors (merge-pathnames sb-sys::*tty*)) :true :false))))
446
447 ;;; This used to return "quux/bar.lisp"
448 (with-test (:name :dpd-output-file)
449   (assert (equal #p"quux/bar.fasl"
450                  (let ((*default-pathname-defaults* #p"quux/"))
451                    (compile-file-pathname "foo.lisp" :output-file "bar"))))
452   (assert (equal #p"quux/bar.fasl"
453                  (let ((*default-pathname-defaults* #p"quux/"))
454                    (compile-file-pathname "bar.lisp")))))
455 \f
456 (with-test (:name :wild-enough)
457   (enough-namestring #p".a*"))
458 \f
459
460 (with-test (:name :translated-wild-version)
461   (assert (eq 99
462             (pathname-version
463              (translate-pathname
464               (make-pathname :name "foo" :type "bar" :version 99)
465               (make-pathname :name :wild :type :wild :version :wild)
466               (make-pathname :name :wild :type :wild :version :wild)))))
467
468   (assert (eq 99
469               (pathname-version
470                (translate-pathname
471                 (make-pathname :name "foo" :type "bar" :version 99)
472                 (make-pathname :name :wild :type :wild :version :wild)
473                 (make-pathname :name :wild :type :wild :version nil))))))
474
475 ;;; enough-namestring relative to root
476 (with-test (:name :enough-relative-to-root)
477   (assert (equal "foo" (enough-namestring "/foo" "/"))))
478 \f
479 ;;; Check the handling of NIL, :UNSPECIFIC, the empty string, and
480 ;;; non-NIL strings in NATIVE-NAMESTRING implementations.  Revised by
481 ;;; RMK 2007-11-28, attempting to preserve the apparent intended
482 ;;; denotation of SBCL's then-current pathname implementation.
483 (with-test (:name (:native-namestring 2))
484   (assert (equal
485            (loop with components = (list nil :unspecific "" "a")
486                  for name in components
487                  appending (loop for type in components
488                                  as pathname = (make-pathname
489                                                 #+win32 :device #+win32 "C"
490                                                 :directory '(:absolute "tmp")
491                                                 :name name :type type)
492                                  collect (ignore-errors
493                                           (sb-ext:native-namestring pathname))))
494            #-win32
495               #|type  NIL       :UNSPECIFIC   ""        "a"         |#
496   #|name       |#
497   #|NIL        |#   '("/tmp/"   "/tmp/"       NIL       NIL
498   #|:UNSPECIFIC|#     "/tmp/"   "/tmp/"       NIL       NIL
499   #|""         |#     "/tmp/"   "/tmp/"       "/tmp/."  "/tmp/.a"
500   #|"a"        |#     "/tmp/a"  "/tmp/a"      "/tmp/a." "/tmp/a.a")
501
502            #+win32
503               #|type  NIL           :UNSPECIFIC   ""            "a"     |#
504   #|name       |#
505   #|NIL        |#   '("C:\\tmp\\"   "C:\\tmp\\"   NIL           NIL
506   #|:UNSPECIFIC|#     "C:\\tmp\\"   "C:\\tmp\\"   NIL           NIL
507   #|""         |#     "C:\\tmp\\"   "C:\\tmp\\"   "C:\\tmp\\."  "C:\\tmp\\.a"
508   #|"a"        |#     "C:\\tmp\\a"  "C:\\tmp\\a"  "C:\\tmp\\a." "C:\\tmp\\a.a"))))
509
510 (with-test (:name :delete-file-logical-pathname)
511   (setf (logical-pathname-translations "SB-TEST")
512         (list (list "**;*.*.*" (make-pathname :name :wild
513                                               :type :wild
514                                               :defaults (truename ".")))))
515   (let ((test (pathname "SB-TEST:delete-logical-pathname.tmp")))
516     (assert (typep test 'logical-pathname))
517     (with-open-file (f test :direction :output)
518       (write-line "delete me!" f))
519     (assert (probe-file test))
520     (assert (delete-file test))
521     (assert (not (probe-file test)))))
522
523 (with-test (:name :logical-pathname-type-error)
524   (assert (eq :type-error-ok
525               (handler-case (logical-pathname "FOO.txt")
526                 (type-error () :type-error-ok))))
527   (assert (eq :type-error-ok
528               (handler-case (logical-pathname "SYS:%")
529                 (type-error () :type-error-ok)))))
530
531 ;;; Reported by Willem Broekema: Reading #p"\\\\" caused an error due
532 ;;; to insufficient sanity in input testing in EXTRACT-DEVICE (in
533 ;;; src;code;win32-pathname).
534 (with-test (:name :bug-489698 :skipped-on '(not :win32))
535   (assert (equal (make-pathname :directory '(:absolute))
536                  (read-from-string "#p\"\\\\\\\\\""))))
537
538 (with-test (:name :load-logical-pathname-translations)
539   (let* ((cwd (truename "."))
540          (foo (merge-pathnames "llpnt-foo.translations" cwd))
541          (bar (merge-pathnames "llpnt-bar.translations" cwd))
542          (translations (logical-pathname-translations "SYS")))
543     (unwind-protect
544          (progn
545            (with-open-file (f foo :direction :output)
546              (prin1 (list (list "*.TEXT" (make-pathname
547                                           :directory '(:absolute "my" "foo")
548                                           :name :wild :type "txt")))
549                     f))
550            (with-open-file (f bar :direction :output)
551              (prin1 (list (list "*.CL" (make-pathname
552                                         :directory '(:absolute "my" "bar")
553                                         :name :wild :type "lisp"))) f))
554            (setf (logical-pathname-translations "SYS")
555                  (list* (list "SITE;LLPNT-FOO.TRANSLATIONS.NEWEST" foo)
556                         (list "SITE;LLPNT-BAR.TRANSLATIONS.NEWEST" bar)
557                         translations))
558            (assert (load-logical-pathname-translations "LLPNT-FOO"))
559            (assert (load-logical-pathname-translations "LLPNT-BAR"))
560            (assert
561             (and
562              (equal "/my/bar/quux.lisp"
563                     (namestring (translate-logical-pathname "LLPNT-BAR:QUUX.CL")))
564              (equal "/my/foo/quux.txt"
565                     (namestring (translate-logical-pathname "LLPNT-FOO:QUUX.TEXT"))))))
566       (ignore-errors (delete-file foo))
567       (ignore-errors (delete-file bar))
568       (setf (logical-pathname-translations "SYS") translations))))
569
570 (with-test (:name :tilde-expansion)
571   (assert (equal '(:absolute :home "foo") (pathname-directory "~/foo/bar.txt")))
572   (assert (equal '(:absolute (:home "jdoe") "quux") (pathname-directory "~jdoe/quux/")))
573   (assert (equal "~/foo/x" (namestring (make-pathname :directory '(:absolute :home "foo")
574                                                       :name "x"))))
575   (assert (equal (native-namestring (merge-pathnames "a/b.c" (user-homedir-pathname)))
576                  (native-namestring #p"~/a/b.c")))
577   ;; Not a directory.
578   (assert (equal (native-namestring #p"~foo") "~foo"))
579   ;; Not at the start of the first directory
580   (assert (equal (native-namestring #p"foo/~/bar")
581                  #-win32 "foo/~/bar"
582                  #+win32 "foo\\~\\bar")))
583
584 ;;; lp#673625
585 (with-test (:name :pathname-escape-first-directory-component
586                   :fails-on :win32)
587   ;; ~ / :HOME
588   (assert (equal (pathname-directory #p"\\~/foo/") '(:relative "~" "foo")))
589   (assert (equal (native-namestring #p"\\~/foo/") "~/foo/"))
590   (assert (equal (namestring (make-pathname :directory '(:absolute "~zot")))
591                  "\\~zot/"))
592   ;; * / :WILD
593   (assert (equal (pathname-directory #p"\\*/") '(:relative "*"))))
594
595 (with-test (:name :ensure-directories-exist-with-odd-d-p-d)
596   (let ((*default-pathname-defaults* #p"/tmp/foo"))
597     (ensure-directories-exist "/")))
598
599 (with-test (:name :long-file-name :skipped-on '(not :win32))
600   (let* ((x '("hint--if-you-are-having-trouble-deleting-this-test-directory"
601               "use-the-7zip-file-manager"))
602          (base (truename
603                 (directory-namestring (or *load-pathname* *compile-file-pathname*))))
604          (shallow (make-pathname :directory `(:relative ,(car x))))
605          (shallow (merge-pathnames shallow base))
606          (deep (make-pathname
607                 :directory `(:relative ,@(loop repeat 10 appending x))))
608          (deep (merge-pathnames deep base))
609          (native (sb-ext:native-namestring deep)))
610     (assert (> (length native) 260))
611     (assert (eql 3 (mismatch "\\\\?" native)))
612     (assert (not (probe-file shallow)))
613     (unwind-protect
614          (progn
615            (ensure-directories-exist deep)
616            (assert (probe-file deep)))
617       (sb-ext:delete-directory shallow :recursive t))
618     (assert (not (probe-file shallow)))))
619
620 ;;;; success