Aded extended commands and editor

This commit is contained in:
2026-08-20 23:48:49 +02:00
parent 72bf39a27b
commit 4324da1467
12 changed files with 1616 additions and 261 deletions
+67
View File
@@ -9,6 +9,7 @@
"../private/dispatcher.rkt"
"../private/help.rkt"
"../private/racket-tools.rkt"
"../private/editor.rkt"
"../private/env-support.rkt")
(define (capture-output procedure . args)
@@ -95,8 +96,36 @@
(check-equal? (coreutils-help-search-term 'directory-list)
"directory-list")
(check-equal? (coreutils-help-search-term 'raco) "rash-coreutils-raco")
(check-equal? (coreutils-help-search-term 'edit) "rash-coreutils-edit")
(check-true (path? (find-raco-executable)))
(check-true (procedure? coreutils-raco))
(check-true (procedure? (current-coreutils-editor)))
(define edit-call #f)
(parameterize
([current-coreutils-editor
(λ (filename #:wait? [wait? #f])
(set! edit-call (list filename wait?)))])
(dispatch-coreutils-command 'edit '--wait "one.txt"))
(check-equal? (path->string (first edit-call)) "one.txt")
(check-true (second edit-call))
(set! edit-call #f)
(parameterize
([current-coreutils-editor
(λ (filename #:wait? [wait? #f])
(set! edit-call (list filename wait?)))])
(dispatch-coreutils-command 'edit "one.txt"))
(check-equal? (path->string (first edit-call)) "one.txt")
(check-false (second edit-call))
(check-exn
exn:fail?
(λ ()
(parameterize
([current-coreutils-editor
(λ (filename #:wait? [wait? #f]) (void))])
(dispatch-coreutils-command 'edit "one.txt" "two.txt"))))
(call-with-output-file "lines.txt"
#:exists 'truncate
@@ -106,6 +135,27 @@
"one\ntwo\n")
(check-equal? (capture-output coreutils-tail "-n" "1" "lines.txt")
"three\n")
;; A long physical line must not force head or tail to materialize that
;; line in memory. The 64 KiB internal block size is crossed many times.
(define long-lines-path (build-path test-root "long-lines.txt"))
(define long-chunk (make-bytes (* 64 1024) 120))
(call-with-output-file long-lines-path
#:exists 'truncate
(λ (out)
(display "first\n" out)
(for ([i (in-range 64)])
(write-bytes long-chunk out))
(display "\nlast-one\nlast-two\n" out)))
(check-equal? (capture-output coreutils-head "-n" "1" long-lines-path)
"first\n")
(check-equal? (capture-output coreutils-tail "-n" "2" long-lines-path)
"last-one\nlast-two\n")
(call-with-input-file long-lines-path
(λ (in)
(parameterize ([current-input-port in])
(check-equal? (capture-output coreutils-tail "-n" "2")
"last-one\nlast-two\n"))))
(check-equal? (capture-output coreutils-wc "-l" "lines.txt")
"3\n")
(check-equal? (capture-output coreutils-basename "a/b/c.txt")
@@ -146,6 +196,23 @@
(check-true (regexp-match? #px"[0-9]{4}-[0-9]{2}-[0-9]{2}T"
(capture-output coreutils-date "--iso")))
;; time writes measurements to stderr without contaminating command output.
(define time-error (open-output-string))
(define timed-output
(parameterize ([current-error-port time-error])
(capture-output
dispatch-coreutils-command
'time
'echo
'timed)))
(check-equal? timed-output "timed\n")
(define time-output (get-output-string time-error))
(check-true
(regexp-match?
#px"^real\\t[0-9]+[.][0-9]{3}s\ncpu\\t[0-9]+[.][0-9]{3}s\ngc\\t[0-9]+[.][0-9]{3}s\n$"
time-output))
(check-exn
exn:fail?
(λ ()
+16 -2
View File
@@ -2,6 +2,7 @@
(require rackunit
racket/file
racket/path
racket/port
rash-coreutils)
@@ -11,13 +12,17 @@
(open-output-string))
(define errors
(open-output-string))
(define edited #f)
(dynamic-wind
void
(λ ()
(parameterize ([current-directory test-root]
[current-output-port output]
[current-error-port errors])
[current-error-port errors]
[current-coreutils-editor
(λ (filename #:wait? [wait? #f])
(set! edited (list filename wait?)))])
{
(call-with-output-file "info.rkt"
#:exists 'truncate
@@ -30,21 +35,30 @@
ls -l rash-coreutils-smoke
ls *.rkt
ls #px"^info[.]rkt$"
edit --wait info.rkt
(define x 42)
env RASH_COREUTILS_VALUE=(values x) printenv RASH_COREUTILS_VALUE
echo one two three | wc -w
time echo timed
date --iso
rm -r rash-coreutils-smoke
(check-false (directory-exists? "rash-coreutils-smoke"))
})
(check-equal? (get-output-string errors) "")
(define error-text (get-output-string errors))
(check-true
(regexp-match?
#px"^real\\t[0-9]+[.][0-9]{3}s\ncpu\\t[0-9]+[.][0-9]{3}s\ngc\\t[0-9]+[.][0-9]{3}s\n$"
error-text))
(define text (get-output-string output))
(check-true (regexp-match? #rx"hello from rash-coreutils" text))
(check-true (regexp-match? #rx"info[.]rkt" text))
(check-true (regexp-match? #rx"42" text))
(check-true (regexp-match? #rx"timed" text))
(check-equal? (path->string (first edited)) "info.rkt")
(check-true (second edited))
(check-true (regexp-match? #rx"\n3\n" text))
(check-true (regexp-match? #px"[0-9]{4}-[0-9]{2}-[0-9]{2}T" text)))
(λ ()