capybara/util.rkt

35 lines
914 B
Racket

#lang racket/base
(require racket/format racket/string racket/port)
(provide ~r/pad run-external)
(define (~r/pad num pad-to)
(~r num #:min-width pad-to #:pad-string "0"))
(define (run-external argv source)
(printf "running external program: ~a\n" argv)
(define-values [proc out in err]
(apply subprocess #f #f #f "/usr/bin/env" argv))
(define out-str #f)
(define err-str "")
(define out-reader (thread (λ () (set! out-str (port->string out)))))
(define err-reader (thread (λ () (set! err-str (port->string err)))))
(write-string source in)
(flush-output in)
(close-output-port in)
(subprocess-wait proc)
(thread-wait out-reader)
(thread-wait err-reader)
(define err-trimmed (string-trim err-str))
(unless (string=? "" err-trimmed)
(error "child process raised error!" err-trimmed))
(unless out-str
(error "child process didn't return anything!"))
out-str)