~dricottone/my-utils

ref: 6e88ef836939f27f3c6d1e830639bda8a895ed58 my-utils/src/unittest-color.awk -rwxr-xr-x 1.4 KiB
6e88ef83Dominic Ricottone Refactor tests 3 years ago
                                                                                
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
#!/bin/awk -f

# unittest-color.awk
# ==================
# Color filter for Python's 'unittest'

BEGIN {
  red="\033[31;1m"
  green="\033[32;1m"
  yellow="\033[33m"
  cyan="\033[36m";
  reset="\033[0m";
  dim="\033[2m";

  comma_replacement=reset ","
  brace_replacement=reset ")"
  equal_replacement="=" yellow
}
{
  # color tracebacks
  if ($0 ~ /^(ERROR|FAIL):/) {
    $1=red $1 reset dim cyan
    $0=$0 reset
  }
  else if ($0 ~ /^\s*File ".+", line [1-9][0-9]*, in/) {
    $2=yellow $2 reset dim cyan
    $4=yellow $4 reset dim cyan
    $6=yellow $6 reset dim cyan
    $0="  " dim cyan $0 reset
  }

  # highlight numeric measures
  else if ($0 ~ /^Ran [1-9][0-9]* tests?/) {
    $2=yellow $2 reset
    $5=yellow $5 reset
  }

  # color results
  else if ( $0 ~ /^OK\s*$/ || $0 ~ /^OK \(/ ) {
    $1=green $1 reset
    gsub(/=/, equal_replacement)
    gsub(/,/, comma_replacement)
    gsub(/)/, brace_replacement)
    $0=$0 reset
  }
  else if ($0 ~ /^(ERROR|FAILED) \(/) {
    $1=red $1 reset
    $0=$0 reset
    gsub(/=/, equal_replacement)
    gsub(/,/, comma_replacement)
    gsub(/)/, brace_replacement)
    $0=$0 reset
  }
  else if ($0 ~ /\.\.\. (ok|skipped)/) {
    $2=dim cyan $2
    $0=$0 reset
  }
  else if ($0 ~ /\.\.\. (ERROR|FAIL)/) {
    $2=dim cyan $2
    $NF=reset red $NF
    $0=$0 reset
  }

  # color string diffs
  else if ($0 ~ /^\+ /) {
    $1=green $1 reset
  }
  else if ($0 ~ /^\- /) {
    $1=red $1 reset
  }

  print $0
}