Files
racket-makefile/tests/helpers.rkt
T
2026-08-08 23:36:35 +02:00

72 lines
2.6 KiB
Racket

#lang racket
(require rackunit
racket/file
(only-in racket-makefile
rm-f rm-rf cleanup
list-dir/files list-files list-dirs))
(define tmp (make-temporary-file "racket-makefile-test~a" 'directory))
(dynamic-wind
void
(lambda ()
(define file (build-path tmp "x.tmp"))
(call-with-output-file file (lambda (out) (display "x" out)))
(check-true (file-exists? file))
(rm-f file)
(check-false (file-exists? file))
(define nested (build-path tmp "a" "b"))
(make-directory* nested)
(define html (build-path nested "x.html"))
(define txt (build-path nested "x.txt"))
(call-with-output-file html (lambda (out) (display "html" out)))
(call-with-output-file txt (lambda (out) (display "txt" out)))
(cleanup tmp '("**.html"))
(check-false (file-exists? html))
(check-true (file-exists? txt))
;; Directory/regexp helpers.
(define top-bak (build-path tmp "TOP.BAK"))
(call-with-output-file top-bak (lambda (out) (display "bak" out)))
(define compiled (build-path tmp "a" "compiled"))
(make-directory* compiled)
(define nested-bak (build-path tmp "a" "nested.bak"))
(call-with-output-file nested-bak (lambda (out) (display "bak" out)))
;; Non-recursive results are complete paths and include both files and dirs.
(check-equal? (list-files tmp #px"(?i:[.]bak$)")
(list top-bak))
(check-equal? (list-dirs tmp #px"a$")
(list (build-path tmp "a")))
;; Recursive variants select the requested kind directly.
(define recursive-baks
(list-files tmp #px"(?i:[.]bak$)" #:recursive #t))
(check-not-false (member top-bak recursive-baks))
(check-not-false (member nested-bak recursive-baks))
(check-equal? (list-dirs tmp #px"compiled$" #:recursive #t)
(list compiled))
;; list-dir/files returns both matching files and directories.
(define all-a
(list-dir/files tmp #px"(?i:(?:a|[.]bak))$" #:recursive #t))
(check-not-false (member (build-path tmp "a") all-a))
(check-not-false (member top-bak all-a))
(check-not-false (member nested-bak all-a))
;; Intended makefile idiom: feed the path lists directly into variadic
;; rm-rf and rm-f.
(apply rm-rf (list-dirs tmp #px"compiled$" #:recursive #t))
(check-false (directory-exists? compiled))
(apply rm-f (list-files tmp #px"(?i:[.]bak$)" #:recursive #t))
(check-false (file-exists? top-bak))
(check-false (file-exists? nested-bak))
(rm-rf (build-path tmp "a"))
(check-false (directory-exists? (build-path tmp "a"))))
(lambda ()
(delete-directory/files tmp #:must-exist? #f)))