File: temporary-files.test.lisp

package info (click to toggle)
cl-fad 0.7.4-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 172 kB
  • sloc: lisp: 965; makefile: 13
file content (49 lines) | stat: -rw-r--r-- 2,132 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
(in-package :cl-fad-test)

(deftest 'temporary-file 'with-output-to-temporary-file ()
  (let ((pathname (with-output-to-temporary-file (f)
                    (write-string "hello" f))))
    (test-assert (probe-file pathname))
    (test-equal (alexandria:read-file-into-string pathname) "hello")
    (delete-file pathname)))

(deftest 'temporary-file 'with-open-temporary-file-keep ()

  (let ((pathname (with-open-temporary-file (f :keep nil)
                    (pathname f))))
    (test-assert (null (probe-file pathname))))
  (let ((pathname (with-open-temporary-file (f :keep t)
                    (pathname f))))
    (test-assert (probe-file pathname))
    (delete-file pathname))

  (let* ((keep nil)
         (pathname (with-open-temporary-file (f :keep keep)
                     (pathname f))))
    (test-assert (null (probe-file pathname))))
  (let* ((keep t)
         (pathname (with-open-temporary-file (f :keep keep)
                     (pathname f))))
    (test-assert (probe-file pathname))
    (delete-file pathname)))

(deftest 'temporary-file 'template-tests ()
  ;; error is signalled when template does not contain a percent sign.
  (let ((*default-template* "foo"))
    (test-condition (with-open-temporary-file (f :keep nil))
                    'invalid-temporary-pathname-template))
  ;; file name template occurs in generated file name (for logical path name)
  (let* ((*default-template* "temporary-files:bla%.txt")
         (pathname (with-open-temporary-file (f :keep nil)
                     (pathname f))))
    (test-assert (cl-ppcre:scan "(?i)bla.*\\.txt$" (namestring pathname))))
  ;; file name template occurs in generated file name (for pysical path name)
  (let* ((*default-template* (concatenate 'string
                                          (namestring (translate-logical-pathname "temporary-files:"))
                                          "bla%.txt"))
         (pathname (with-open-temporary-file (f :keep nil)
                     (pathname f))))
    (test-assert (cl-ppcre:scan "(?i)bla.*\\.txt$" (namestring pathname)))))