aboutsummaryrefslogtreecommitdiff
path: root/bin/run64
blob: 8091fe11f6d8bb8da3c1f081fe1b7000c30431e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!r6rs
;;; -*- mode: scheme -*-
;;; run64 --- SRFI-64 test runner
;;; Copyright © 2022 Arun Isaac <arunisaac@systemreboot.net>
;;;
;;; This file is part of run64.
;;;
;;; run64 is free software: you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation, either version 3 of the License, or
;;; (at your option) any later version.
;;;
;;; run64 is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;; General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with run64.  If not, see <https://www.gnu.org/licenses/>.

(import (rnrs programs)
        (srfi srfi-1)
        (srfi srfi-64))

(define (color code str)
  (string-append (string #\esc)
                 "["
                 (number->string code)
                 "m"
                 str
                 (string #\esc)
                 "[0m"))

(define (bold str)
  (color 1 str))

(define (red str)
  (color 31 str))

(define (green str)
  (color 32 str))

(define (yellow str)
  (color 33 str))

(define (magenta str)
  (color 35 str))

(define (make-runner)
  (let ((runner (test-runner-null)))
    (test-runner-on-group-begin! runner
      (lambda (runner suite-name count)
        (display suite-name)
        (display " ")))
    (test-runner-on-group-end! runner
      (lambda _
        (newline)))
    (test-runner-on-test-end! runner
      (lambda (runner)
        (let ((name (test-runner-test-name runner))
              (result (string-upcase
                       (symbol->string (test-result-kind runner))))
              (result-alist (test-result-alist runner)))
          (display (case (test-result-kind runner)
                     ((pass) (green "."))
                     ((fail) (red "F"))
                     ((xfail xpass) (yellow "X"))
                     ((skip) (yellow "S"))))
          (when (eq? (test-result-kind runner)
                     'fail)
            ;; Prepend test failure details to aux value.
            (test-runner-aux-value! runner
                                    (cons (cons (cons 'test-name (test-runner-test-name runner))
                                                (test-result-alist runner))
                                          (test-runner-aux-value runner)))))))
    ;; Initialize aux value to the empty list.
    (test-runner-aux-value! runner '())
    runner))

(define (headline text color)
  "Display headline TEXT in COLOR. COLOR is a function that wraps a
given string in an ANSI escape code."
  (display (color (string-append "==== " text)))
  (newline))

(define (main args)
  (let ((runner (make-runner)))
    (headline "test session starts" bold)
    (test-with-runner runner
      (for-each load (cdr args)))
    (newline)
    (unless (zero? (test-runner-fail-count runner))
      (headline "FAILURES" red)
      (for-each (lambda (failure)
                  (let ((name (assq-ref failure 'test-name))
                        (file (assq-ref failure 'source-file))
                        (line (assq-ref failure 'source-line)))
                    (when file
                      (display file)
                      (display ":")
                      (when line
                        (display line)
                        (display ":"))
                      (display " "))
                    (display name)
                    (newline)))
                (test-runner-aux-value runner))
      (newline))
    (headline
     (string-join
      (filter-map (lambda (count text color)
                    (if (zero? count)
                        #f
                        (color (string-append (number->string count)
                                              " " text))))
                  (list (test-runner-pass-count runner)
                        (test-runner-fail-count runner)
                        (test-runner-xpass-count runner)
                        (test-runner-xfail-count runner)
                        (test-runner-skip-count runner))
                  (list "passed" "failed"
                        "unexpected passes"
                        "expected failures"
                        "skipped")
                  (list green red yellow yellow yellow))
      ", ")
     (cond
      ((not (zero? (test-runner-fail-count runner)))
       red)
      ((or (not (zero? (test-runner-xpass-count runner)))
           (not (zero? (test-runner-xfail-count runner))))
       yellow)
      (else green)))
    (exit (zero? (test-runner-fail-count runner)))))

(main (command-line))