file-system-tests.lisp 18.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
;;; file-system-tests.lisp
;;;
;;; Copyright (C) 2005 Peter Graves
;;;
;;; This program is free software; you can redistribute it and/or
;;; modify it under the terms of the GNU General Public License
;;; as published by the Free Software Foundation; either version 2
;;; of the License, or (at your option) any later version.
;;;
;;; This program is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with this program; if not, write to the Free Software
;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

#+sbcl
(require '#:sb-posix)

22
(in-package #:abcl.test.lisp)
23 24 25

(defparameter *this-file*
  (merge-pathnames (make-pathname :type "lisp")
26 27 28
                   (if (find :asdf2 *features*)
                       (merge-pathnames 
                        (make-pathname :name (pathname-name *load-truename*))
29
                        (asdf:system-relative-pathname :abcl "test/lisp/abcl/"))
30
                       *load-truename*)))
31 32

(defparameter *this-directory*
33
  (if (find :asdf2 *features*)
34
      (asdf:system-relative-pathname :abcl "test/lisp/abcl/")
35 36 37
      (make-pathname :host (pathname-host *load-truename*)
                     :device (pathname-device *load-truename*)
                     :directory (pathname-directory *load-truename*))))
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343

(defun pathnames-equal-p (pathname1 pathname2)
  #-(or allegro clisp cmu lispworks)
  (equal pathname1 pathname2)
  #+(or allegro clisp cmu)
  (and (pathnamep pathname1)
       (pathnamep pathname2)
       (equal (pathname-host pathname1) (pathname-host pathname2))
       (equal (pathname-device pathname1) (pathname-device pathname2))
       (equal (pathname-directory pathname1) (pathname-directory pathname2))
       (equal (pathname-name pathname1) (pathname-name pathname2))
       (equal (pathname-type pathname1) (pathname-type pathname2))
       (or (equal (pathname-version pathname1) (pathname-version pathname2))
           (and (member (pathname-version pathname1) '(:newest nil))
                (member (pathname-version pathname2) '(:newest nil))
                t)))
  #+lispworks
  (string= (namestring pathname1) (namestring pathname2)))

#+abcl
(defun run-shell-command (command &key directory (output *standard-output*))
  (ext:run-shell-command command :directory directory :output output))

#+allegro
(defun run-shell-command (command &key directory (output *standard-output*))
  (excl:run-shell-command command :directory directory :input nil :output output))

#+clisp
(defun run-shell-command (command &key directory (output *standard-output*))
  (declare (ignore output)) ;; FIXME
  (let (status old-directory)
    (when directory
      (setf old-directory (ext:cd))
      (ext:cd directory))
    (unwind-protect
        (setf status (ext:run-shell-command command))
      (when old-directory
        (ext:cd old-directory)))
    (cond ((numberp status)
           status)
          ((eq status t)
           0)
          (t
           -1))))

#+cmu
(defun run-shell-command (command &key directory (output *standard-output*))
  (when directory
    (setf command (concatenate 'string
                               "\\cd \""
                               (namestring (pathname directory))
                               "\" && "
                               command)))
  (ext:process-exit-code
   (ext:run-program
    "/bin/sh"
    (list  "-c" command)
    :input nil :output output)))

#+sbcl
(defun run-shell-command (command &key directory (output *standard-output*))
  (when directory
    (setf command (concatenate 'string
                               "\\cd \""
                               (namestring (pathname directory))
                               "\" && "
                               command)))
  (sb-ext:process-exit-code
   (sb-ext:run-program
    "/bin/sh"
    (list  "-c" command)
    :input nil :output output)))

#+lispworks
(defun run-shell-command (command &key directory (output *standard-output*))
  (when directory
    #+unix
    (setf command (concatenate 'string
                               "\\cd \""
                               (namestring (pathname directory))
                               "\" && "
                               command)))
  (system:call-system-showing-output
   command
   :shell-type "/bin/sh"
   :output-stream output))

(defun copy-file (from to)
  (let* ((from-namestring (namestring (pathname from)))
         (to-namestring (namestring (pathname to)))
         (command (concatenate 'string "cp " from-namestring " " to-namestring)))
    (zerop (run-shell-command command))))

(defun make-symbolic-link (from to)
  (let* ((from-namestring (namestring (pathname from)))
         (to-namestring (namestring (pathname to)))
         (command (concatenate 'string "ln -s " from-namestring " " to-namestring)))
    (zerop (run-shell-command command))))

(defun probe-directory (pathname)
  #+abcl (ext:probe-directory pathname)
  #+allegro (excl:probe-directory pathname)
  #+clisp (ignore-errors (ext:probe-directory pathname))
  #+cmu (probe-file pathname) ; FIXME
  #+sbcl (probe-file pathname) ; FIXME
  #+lispworks (probe-file pathname)
  )

(defun file-directory-p (pathname)
  #+abcl (ext:file-directory-p pathname)
  #+allegro (excl:file-directory-p pathname)
  #-(or abcl allegro)
  (let* ((namestring (namestring pathname))
         (len (length namestring))
         (last-char (and (> len 0) (char namestring (1- len)))))
    (eql last-char #+windows #\\ #-windows #\/)))

(defun make-directory (pathname)
  #+allegro
  (excl:make-directory pathname)
  #-allegro
  (and (ensure-directories-exist pathname) t))

(defun delete-directory (pathname)
  #+abcl (delete-file pathname)
  #+allegro (excl:delete-directory pathname)
  #+clisp (ext:delete-dir (namestring pathname))
  #+cmu (unix:unix-rmdir (namestring pathname))
  #+sbcl (zerop (sb-posix:rmdir (namestring pathname)))
  #+lispworks (lw:delete-directory pathname)
  )

;; This approach is race-prone, but it should be adequate for our limited
;; purposes here.
(defun make-temporary-filename (directory)
  (unless (probe-directory directory)
    (error "The directory ~S does not exist." directory))
  (dotimes (i 10)
    (let ((pathname (merge-pathnames (make-pathname :name (symbol-name (gensym))
                                                    :type nil)
                                     directory)))
      (unless (probe-file pathname)
        (return-from make-temporary-filename pathname))))
  (error "Unable to create a temporary filename in ~S" directory))

(defun touch (filespec)
  (with-open-file (stream filespec :direction :output :if-exists :error)))

(defun make-temporary-directory (parent-directory)
  (let* ((tmp (make-temporary-filename parent-directory))
         (directory-namestring (concatenate 'string (namestring tmp) "/"))
         (directory-pathname (pathname directory-namestring)))
    (make-directory directory-pathname)
    directory-pathname))

(defun delete-directory-and-files (pathspec &key (quiet t) (dry-run nil))
  (let* ((namestring (namestring pathspec))
         (len (length namestring))
         (last-char (and (> len 0) (char namestring (1- len)))))
    (unless (eql last-char #+windows #\\ #-windows #\/)
      (setf namestring (concatenate 'string namestring #+windows "\\" #-windows "/")))
    (let ((pathname (pathname namestring)))
      (unless (probe-directory pathname)
        (error "Directory does not exist: ~S" pathname))
      (unless quiet
        (format t "~&processing directory ~S~%" pathname))
      (let ((list (directory (make-pathname :name :wild
                                            :type #-clisp :wild #+clisp nil
                                            :defaults pathname))))
        (dolist (x list)
          (cond ((file-directory-p x)
                 (delete-directory-and-files x :quiet quiet))
                (t
                 (unless quiet
                   (format t "~&deleting file ~S~%" x))
                 (unless dry-run
                   (delete-file x)))))
        (unless quiet
          (format t "~&deleting directory ~S~%" pathname))
        (unless dry-run
          (delete-directory pathname))))))

#-(or allegro clisp lispworks windows)
(deftest run-shell-command.1
  (let* ((raw-string
          (with-output-to-string (s) (run-shell-command "pwd"
                                                        :directory *this-directory*
                                                        :output s)))
         (string
          (string-right-trim '(#\newline #\return) raw-string))
         (length (length string)))
    (when (> length 0)
      (unless (eql (char string (1- length)) #\/)
        (setf string (concatenate 'string string (string #\/)))))
    (string= string (directory-namestring *this-directory*)))
  t)

#-(or allegro clisp lispworks windows)
(deftest run-shell-command.2
  (let* ((directory
          (probe-file (merge-pathnames "../" *this-directory*)))
         (raw-string
          (with-output-to-string (s) (run-shell-command "pwd"
                                                        :directory directory
                                                        :output s)))
         (string
          (string-right-trim '(#\newline #\return) raw-string))
         (length (length string)))
    (when (> length 0)
      (unless (eql (char string (1- length)) #\/)
        (setf string (concatenate 'string string (string #\/)))))
    (string= string (directory-namestring directory)))
  t)

(deftest probe-file.1
  (pathnames-equal-p (probe-file *this-file*) *this-file*)
  t)

(deftest probe-file.2
  (let ((pathname #p"."))
    #-clisp
    (pathnames-equal-p (probe-file pathname) (truename pathname))
    #+clisp
    ;; "." names a directory, not a file.
    (signals-error (probe-file pathname) 'file-error))
  t)
#+(and clisp windows)
(pushnew 'probe-file.2 *expected-failures*)

(deftest probe-file.3
  (let ((pathname #p"./"))
    #-clisp
    (pathnames-equal-p (probe-file pathname) *this-directory*)
    #+clisp
    ;; "no file name given"
    (signals-error (probe-file pathname) 'file-error))
  t)

(deftest probe-file.4
  (let ((pathname #p".."))
    #-clisp
    (pathnames-equal-p (probe-file pathname) (truename pathname))
    #+clisp
    ;; ".." names a directory, not a file.
    (signals-error (probe-file pathname) 'file-error))
  t)
#+(and clisp windows)
(pushnew 'probe-file.4 *expected-failures*)

(deftest probe-file.5
  (or
   ;; It might not exist. That's OK.
   (null (probe-directory #p"/home/"))
   (pathnames-equal-p (probe-file #p"/home") (probe-file #p"/home/")))
  t)
#+(or allegro cmu clisp)
(pushnew 'probe-file.5 *expected-failures*)

(deftest truename.1
  (pathnames-equal-p (truename *this-file*) *this-file*)
  t)

(deftest truename.2
  (pathnames-equal-p (truename #p"./") *this-directory*)
  t)

(deftest directory.1
  (let ((list (directory *this-file*)))
    (and
     (= (length list) 1)
     (pathnames-equal-p (car list) *this-file*)))
  t)

;; Verify that DIRECTORY returns nil if the directory is empty.
(deftest directory.2
  (let ((directory-pathname (make-temporary-directory *this-directory*)))
    (unwind-protect
        (directory (make-pathname :name :wild :defaults directory-pathname))
      (delete-directory-and-files directory-pathname)))
  nil)

;; A directory with a one file named "foo".
(deftest directory.3
  (let ((directory-pathname (make-temporary-directory *this-directory*)))
    (unwind-protect
        (let ((file-pathname (make-pathname :name "foo" :defaults directory-pathname)))
          (touch file-pathname)
          (let ((directory (directory (make-pathname :name :wild
                                                     :defaults directory-pathname))))
            (and (listp directory)
                 (= (length directory) 1)
                 (pathnames-equal-p (car directory) file-pathname))))
      (delete-directory-and-files directory-pathname)))
  t)

;; Same as DIRECTORY.3, but use :type :wild for the wildcard.
(deftest directory.4
  (let ((directory-pathname (make-temporary-directory *this-directory*)))
    (unwind-protect
        (let ((file-pathname (make-pathname :name "foo" :defaults directory-pathname)))
          (touch file-pathname)
          (let ((directory (directory (make-pathname :name :wild
                                                     :type :wild
                                                     :defaults directory-pathname))))
            (and (listp directory)
                 (= (length directory) 1)
344 345
                 (pathnames-equal-p (truename (car directory))
                                    (truename file-pathname)))))
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
      (delete-directory-and-files directory-pathname)))
  t)
#+clisp
;; A pathname with type nil does not match a wildcard with type :WILD.
(pushnew 'directory.4 *expected-failures*)

#-windows
(deftest symlink.1
  (let* ((tmp1 (make-temporary-filename *this-directory*))
         (tmp2 (make-temporary-filename *this-directory*)))
    (unwind-protect
        (values
         (unwind-protect
             (and
              ;; Copy this file to tmp1.
              (copy-file *this-file* tmp1)
              (pathnames-equal-p (probe-file tmp1) tmp1)
              ;; Create tmp2 as a symlink to tmp1.
              (make-symbolic-link tmp1 tmp2)
              ;; Verify that the symlink exists and points to the copy.
              (pathnames-equal-p (probe-file tmp2) tmp1)
              (pathnames-equal-p (truename tmp2) tmp1))
           ;; Delete the symlink.
           (when (probe-file tmp2)
             (delete-file tmp2)))
         ;; Copy should still exist after symlink is deleted.
         (pathnames-equal-p (probe-file tmp1) tmp1))
      (when (probe-file tmp1)
        (delete-file tmp1))))
  t t)
#+allegro
;; Allegro's PROBE-FILE doesn't follow the symlink.
(pushnew 'symlink.1 *expected-failures*)

#-windows
(deftest symlink.2
  (let* ((copy (make-temporary-filename *this-directory*))
         (link (make-temporary-filename *this-directory*))
         directory)
    (unwind-protect
        (and
         ;; Copy this file to copy.
         (copy-file *this-file* copy)
         ;; Verify that copy exists.
         (pathnames-equal-p (probe-file copy) copy)
         ;; Create link as a symlink to copy.
         (make-symbolic-link copy link)
         ;; Verify that the symlink appears in the directory listing.
         (setf directory (directory link))
         (= (length directory) 1)
         ;; The directory listing should contain the truename of the symlink.
         (pathnames-equal-p (car directory) (truename link)))
      (progn
        ;; Clean up.
        (when (probe-file link)
          (delete-file link))
        (when (probe-file copy)
          (delete-file copy)))))
  t)
#+allegro
(pushnew 'symlink.2 *expected-failures*)

;; user-homedir-pathname &optional host => pathname

;; "USER-HOMEDIR-PATHNAME returns a pathname without any name, type, or version
;; component (those components are all nil) for the user's home directory on
;; HOST. If it is impossible to determine the user's home directory on HOST,
;; then nil is returned. USER-HOMEDIR-PATHNAME never returns nil if HOST is not
;; supplied."
(deftest user-homedir-pathname.1
  (let ((pathname (user-homedir-pathname)))
    (values (pathnamep pathname)
            (pathname-name pathname)
            (pathname-type pathname)
            (pathname-version pathname)))
  t nil nil nil)
#+allegro
;; Allegro's version component is :UNSPECIFIC.
(pushnew 'user-homedir-pathname.1 *expected-failures*)

426
(deftest file-system.directory-namestring.1
427 428 429 430 431 432 433 434
  (let ((pathname (user-homedir-pathname)))
    (equal (namestring pathname) (directory-namestring pathname)))
  #-windows
  t
  #+windows
  ;; The drive prefix ("C:\\") is not part of the directory namestring.
  nil)
#+clisp
435
(pushnew 'file-system.directory-namestring.1 *expected-failures*)
436

437
(deftest file.system.directory-namestring.2
438 439 440 441 442
  (let ((pathname (user-homedir-pathname)))
    (equal (directory-namestring pathname)
           (namestring (make-pathname :directory (pathname-directory pathname)))))
  t)
#+clisp
443
(pushnew 'file-system.directory-namestring.2 *expected-failures*)
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462

(deftest ensure-directories-exist.1
  (let* ((tmp (make-temporary-filename *this-directory*))
         (directory-namestring (concatenate 'string (namestring tmp) "/"))
         (file-namestring (concatenate 'string directory-namestring "foo.bar")))
    (multiple-value-bind (path created)
        (ensure-directories-exist file-namestring)
      (values
       ;; 1. "The primary value is the given pathspec..."
       #+(or allegro clisp)
       (eq path file-namestring)
       #-(or allegro clisp)
       (pathnames-equal-p (pathname path) (pathname file-namestring))
       ;; 2. Verify that the directory was created.
       created
       ;; 3. Verify that the directory exists.
       #+clisp
       ;; CLISP's PROBE-DIRECTORY just returns T.
       (ext:probe-directory directory-namestring)
463 464 465 466 467
       ;; ABCL fills in DEVICE as :UNSPECIFIC when resolving
       ;; filesystem paths on non-M$DOG
       #+abcl
       (not (null (truename directory-namestring)))
       #-(or clisp abcl)
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
       (pathnames-equal-p (probe-file directory-namestring)
                          (pathname directory-namestring))
       ;; 4. Delete the directory.
       (when (probe-directory directory-namestring)
         (delete-directory directory-namestring))
       ;; 5. Verify that the directory is no longer there.
       (probe-directory directory-namestring))
       ))
  t t t t nil)

;; What happens if you call ENSURE-DIRECTORIES-EXIST with a pathname that has
;; no name, type, or version component?

;; Case 1: the directory in question already exists.
(deftest ensure-directories-exist.2
  (let ((pathname
         (make-pathname :host (pathname-host *this-directory*)
                        :device (pathname-device *this-directory*)
                        :directory (pathname-directory *this-directory*)
                        :name nil :type nil :version nil)))
    (multiple-value-bind (path created)
        (ensure-directories-exist pathname)
      (values
       #+(or allegro clisp)
       (eq path pathname)
       #-(or allegro clisp)
       (pathnames-equal-p (pathname path) (pathname pathname))
       created)))
  t nil)

;; Case 2: the directory in question does not exist.
(deftest ensure-directories-exist.3
  (let* ((tmp (make-temporary-filename *this-directory*))
         (directory-namestring (concatenate 'string (namestring tmp) "/"))
         (pathname (pathname directory-namestring)))
    (multiple-value-bind (path created)
        (ensure-directories-exist pathname)
      (values
       #+(or allegro clisp)
       (eq path pathname)
       #-(or allegro clisp)
       (pathnames-equal-p (pathname path) (pathname pathname))
       created
       (not (null (probe-directory directory-namestring)))
       (when (probe-directory directory-namestring)
         (delete-directory directory-namestring))
       )))
  t t t t)