Some extra helper functions

This commit is contained in:
2026-08-08 23:36:35 +02:00
parent 57707e5fc0
commit b563d359f0
8 changed files with 183 additions and 7 deletions
+41 -1
View File
@@ -2,7 +2,9 @@
(require rackunit
racket/file
(only-in racket-makefile rm-f rm-rf cleanup))
(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))
@@ -25,6 +27,44 @@
(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 ()