Adding summary mode, quiet program mode and changing tests/eli-tester to cooperate so it adds useful information in summary mode

This commit is contained in:
Jay McCarthy 2013-05-02 14:49:31 -06:00
parent 413ca68435
commit ccf1119b68
3 changed files with 130 additions and 35 deletions

View File

@ -1,6 +1,10 @@
#lang racket/base #lang racket/base
(require racket/cmdline (require racket/cmdline
racket/match racket/match
racket/format
racket/list
racket/function
racket/port
racket/path racket/path
raco/command-name raco/command-name
rackunit/log rackunit/log
@ -9,6 +13,76 @@
(define submodules '()) (define submodules '())
(define run-anyways? #t) (define run-anyways? #t)
(define quiet? #f) (define quiet? #f)
(define quiet-program? #f)
(define table? #f)
(define (dynamic-require* p d)
(parameterize
([current-output-port
(if quiet-program?
(open-output-nowhere)
(current-output-port))]
[current-error-port
(if quiet-program?
(open-output-nowhere)
(current-error-port))])
(dynamic-require p d)))
(struct summary (failed total label body-res))
(define-syntax-rule (with-summary label . body)
(let ()
(match-define (cons before-failed before-total)
(test-log #:display? #f #:exit? #f))
(define res (begin . body))
(match-define (cons after-failed after-total)
(test-log #:display? #f #:exit? #f))
(summary (- after-failed before-failed)
(- after-total before-total)
label
res)))
(define (iprintf i fmt . more)
(for ([j (in-range i)])
(display #\space))
(apply printf fmt more))
(define (display-summary top)
(define files
(let flatten ([sum top])
(match sum
[(list sum ...)
(append-map flatten sum)]
[(summary failed total `(file ,p) body)
(list sum)]
[(summary failed total label body)
(flatten body)]
[(? void?)
empty])))
(define sfiles
(sort files
(λ (x y)
(cond
[(= (summary-failed x) (summary-failed y))
(> (summary-total x) (summary-total y))]
[else
(< (summary-failed x) (summary-failed y))]))))
(define (max-width f)
(string-length
(number->string
(apply max (map f sfiles)))))
(define failed-wid (max-width summary-failed))
(define total-wid (max-width summary-total))
(for ([f (in-list sfiles)])
(match-define (summary failed total `(file ,p) _) f)
(displayln (~a (~a #:min-width failed-wid
#:align 'right
(if (zero? failed)
""
failed))
" "
(~a #:min-width total-wid
#:align 'right
total)
" " p))))
(define (do-test e [check-suffix? #f]) (define (do-test e [check-suffix? #f])
(match e (match e
@ -17,35 +91,39 @@
[(? path? p) [(? path? p)
(cond (cond
[(directory-exists? p) [(directory-exists? p)
(for-each (with-summary
(λ (dp) `(directory ,p)
(do-test (build-path p dp) #t)) (map
(directory-list p))] (λ (dp)
(do-test (build-path p dp) #t))
(directory-list p)))]
[(and (file-exists? p) [(and (file-exists? p)
(or (not check-suffix?) (or (not check-suffix?)
(regexp-match #rx#"\\.rkt$" (path->bytes p)))) (regexp-match #rx#"\\.rkt$" (path->bytes p))))
(parameterize ([current-command-line-arguments '#()]) (with-summary
(define something-wasnt-declared? #f) `(file ,p)
(for ([submodule (in-list (if (null? submodules) (parameterize ([current-command-line-arguments '#()])
'(test) (define something-wasnt-declared? #f)
(reverse submodules)))]) (for ([submodule (in-list (if (null? submodules)
(define mod `(submod ,p ,submodule)) '(test)
(cond (reverse submodules)))])
[(module-declared? mod #t) (define mod `(submod ,p ,submodule))
(unless quiet? (cond
(printf "raco test: ~s\n" `(submod ,(if (absolute-path? p) [(module-declared? mod #t)
`(file ,(path->string p)) (unless quiet?
(path->string p)) (printf "raco test: ~s\n" `(submod ,(if (absolute-path? p)
,submodule))) `(file ,(path->string p))
(dynamic-require mod 0)] (path->string p))
[else ,submodule)))
(set! something-wasnt-declared? #t)])) (dynamic-require* mod 0)]
(when (and run-anyways? something-wasnt-declared?) [else
(unless quiet? (set! something-wasnt-declared? #t)]))
(printf "raco test: ~s\n" (if (absolute-path? p) (when (and run-anyways? something-wasnt-declared?)
`(file ,(path->string p)) (unless quiet?
(path->string p)))) (printf "raco test: ~s\n" (if (absolute-path? p)
(dynamic-require p 0)))] `(file ,(path->string p))
(path->string p))))
(dynamic-require* p 0))))]
[(not (file-exists? p)) [(not (file-exists? p))
(error 'test "given path ~e does not exist" p)])])) (error 'test "given path ~e does not exist" p)])]))
@ -106,12 +184,16 @@
[(list) [(list)
(error 'test "Collection ~e is not installed" e)] (error 'test "Collection ~e is not installed" e)]
[l [l
(for-each do-test l)])] (with-summary
`(collection ,e)
(map do-test l))])]
[packages? [packages?
(define pd (pkg-directory e)) (define pd (pkg-directory e))
(if pd (if pd
(do-test pd) (with-summary
(error 'test "Package ~e is not installed" e))] `(package ,e)
(do-test pd))
(error 'test "Package ~e is not installed" e))]
[else [else
(do-test e)])) (do-test e)]))
@ -131,8 +213,14 @@
(set! run-anyways? #f)] (set! run-anyways? #f)]
#:once-each #:once-each
[("--quiet" "-q") [("--quiet" "-q")
"Suppress `Running ...' message" "Suppress `raco test: ...' message"
(set! quiet? #t)] (set! quiet? #t)]
[("--table" "-t")
"Print a summary table"
(set! table? #t)]
[("--quiet-program" "-Q")
"Quiet the program"
(set! quiet-program? #t)]
#:once-any #:once-any
[("--collection" "-c") [("--collection" "-c")
"Interpret arguments as collections" "Interpret arguments as collections"
@ -141,5 +229,7 @@
"Interpret arguments as packages" "Interpret arguments as packages"
(set! packages? #t)] (set! packages? #t)]
#:args file-or-directory #:args file-or-directory
(begin (for-each do-test-wrap file-or-directory) (begin (define sum (map do-test-wrap file-or-directory))
(test-log #:display? #t #:exit? #t))) (when table?
(display-summary sum))
(void (test-log #:display? #t #:exit? #t))))

View File

@ -26,11 +26,14 @@
FAILED TOTAL)]))) FAILED TOTAL)])))
(when exit? (when exit?
(unless (zero? FAILED) (unless (zero? FAILED)
(exit 1)))) (exit 1)))
(cons FAILED TOTAL))
(provide (provide
(contract-out (contract-out
[test-log! [test-log!
(-> any/c void?)] (-> any/c void?)]
[test-log [test-log
(->* () (#:display? boolean? #:exit? boolean?) void?)])) (->* () (#:display? boolean? #:exit? boolean?)
(cons/c exact-nonnegative-integer?
exact-nonnegative-integer?))]))

View File

@ -1,6 +1,6 @@
#lang racket/base #lang racket/base
(require scheme/match scheme/list scheme/string (require scheme/match scheme/list scheme/string rackunit/log
(for-syntax scheme/base scheme/match)) (for-syntax scheme/base scheme/match))
(define-syntax (safe stx) (define-syntax (safe stx)
@ -162,6 +162,8 @@
(lambda () (lambda ()
(test-context #f) (test-context #f)
(let ([num (mcar c)] [exns (mcdr c)]) (let ([num (mcar c)] [exns (mcdr c)])
(for ([i (in-range num)]) (test-log! #t))
(for ([i (in-list exns)]) (test-log! #f))
(if (null? exns) (if (null? exns)
(case pass (case pass
[(loud) (printf "~a test~a passed\n" num (if (= num 1) "" "s"))] [(loud) (printf "~a test~a passed\n" num (if (= num 1) "" "s"))]