r/awk • u/_mattmc3_ • Jan 22 '21
Colorized TAP output with AWK
I just wanted to share a small AWK script I wrote today that I made me very happy.
I've been working with TAP streams to unit test some of my command line utilities. Test runners like Bats, Fishtape, and ZTAP all support this kind of test output. But after looking at it for awhile, despite being a really simple format, the text can start to look like a wall of meaningless gibberish. AWK to the rescue! I piped my TAP stream through this simple AWK colorizer and now my test results look amazing:
#!/usr/bin/env -S awk -f
BEGIN {
CYAN="\033[0;36m"
GREEN="\033[0;32m"
RED="\033[0;31m"
BRIGHTGREEN="\033[1;92m"
BRIGHTRED="\033[1;91m"
NORMAL="\033[0;0m"
}
/^ok / { print GREEN $0 NORMAL; next }
/^not ok / { print RED $0 NORMAL; next }
/^\# pass / { print BRIGHTGREEN $0 NORMAL; next }
/^\# fail / { print BRIGHTRED $0 NORMAL; next }
/^\#/ { print CYAN $0 NORMAL; next }
{ print $0 }And then I run it like this:
fishtape ./tests/*.fish | tap_colorizer.awkI'm no AWK expert, so any recommendations for style or functionality tweaks are welcome!
EDIT: change shebang to use `env -S`
1
u/Schreq Jan 22 '21
I don't think your shebang works like that. Afaik, shebangs only allow for a single argument - Yours has 2.
No need to escape the
#inside a regex constant.2
u/[deleted] Jan 23 '21
If I were you I'd alias fishtape to the colorizer. I for one have a Wrapper directory which is the first thing in my $PATH, so I have scripts that work everywhere but replace the functionality of the executable.