| 1 |
#!/usr/bin/python3 |
| 2 |
# -*- python -*- |
| 3 |
# (c) 2014 John Lenton |
| 4 |
# MIT licensed. |
| 5 |
# from https://github.com/chipaca/goctest |
| 6 |
|
| 7 |
import re |
| 8 |
import signal |
| 9 |
import subprocess |
| 10 |
import sys |
| 11 |
|
| 12 |
ok_rx = re.compile(rb'^(PASS:?|ok\s+)') |
| 13 |
fail_rx = re.compile(rb'^(FAIL:?|OOPS:?)') |
| 14 |
panic_rx = re.compile(rb'^(PANIC:?|panic:?|\.\.\. Panic:?)') |
| 15 |
log_rx = re.compile(rb'^\[LOG\]|^\?\s+') |
| 16 |
|
| 17 |
class bcolors: |
| 18 |
OK = b'\033[38;5;34m' |
| 19 |
FAIL = b'\033[38;5;196m' |
| 20 |
PANIC = b'\033[38;5;226m\033[48;5;88m' |
| 21 |
OTHER = b'\033[38;5;241m' |
| 22 |
WARNING = b'\033[38;5;226m' |
| 23 |
ENDC = b'\033[0m' |
| 24 |
|
| 25 |
signal.signal(signal.SIGINT, lambda *_: None) |
| 26 |
|
| 27 |
if sys.stdout.isatty(): |
| 28 |
with subprocess.Popen(["go", "test"] + sys.argv[1:], |
| 29 |
bufsize=0, |
| 30 |
stderr=subprocess.STDOUT, |
| 31 |
stdout=subprocess.PIPE) as proc: |
| 32 |
for line in proc.stdout: |
| 33 |
if panic_rx.search(line) is not None: |
| 34 |
line = panic_rx.sub(bcolors.PANIC + rb'\1' + bcolors.ENDC, line) |
| 35 |
elif fail_rx.search(line) is not None: |
| 36 |
line = fail_rx.sub(bcolors.FAIL + rb'\1' + bcolors.ENDC, line) |
| 37 |
elif ok_rx.search(line) is not None: |
| 38 |
line = ok_rx.sub(bcolors.OK + rb'\1' + bcolors.ENDC, line) |
| 39 |
elif log_rx.search(line) is not None: |
| 40 |
line = bcolors.OTHER + line + bcolors.ENDC |
| 41 |
|
| 42 |
sys.stdout.write(line.decode("utf-8")) |
| 43 |
sys.stdout.flush() |
| 44 |
sys.exit(proc.wait()) |
| 45 |
else: |
| 46 |
sys.exit(subprocess.call(["go", "test"] + sys.argv[1:])) |