The return of chui red/green indicators

Automated unit testing is not yet to a mature state here on the C++ side.  We’re kind of just getting started*.  Part of just getting started is a lack of tool support.  Funny – when Jon talked about the importance of maintaining a readiness to create tools that streamline the work in your specific project or environment, I wasn’t sure if he was on track or not.  How important is it, really?

Then I realized that I’m doing the same thing in my current environment. So I guess either I’m as crazy as Jon** or Jon’s onto something here.  :)

On to my issue.  Our C++ unit test support is currently command-line based, and it puts out a lot of text to the screen.  I found I was having to carefully cull through a few screens of data looking for what went right or wrong.  It was too much work for each test run.  I decided to colorize.  ANSI escape sequences to the rescue! (putting to use our research from before.)

The script

# chuiredgreenbar.py
#
# Purpose: Colorize the output of dmake runtest to highlight passing and failing tests.
# Author: Daniel Meyer
# Version: 0.1
# Date: 10/28/2009
#
# Usage:
#   dmake runtest 2>&1 | python c:\bin\chuiredgreenbar.py | cat
#
# TODO: Figure out a way to get ANSI escape sequence support without cat (On my XP machine, cmd.exe doesn't
# seem to natively support the ANSI escape sequences; cat works around this).
import sys, re

def green_bar(s):
    return chr(27) + "[32;40m" + s + chr(27) + "[0m"

def red_bar(s):
    return chr(27) + "[31;40m" + s + chr(27) + "[0m"

pass_pattern = re.compile("passed")
fail_pattern = re.compile("([0-9]+).+[0-9]+ (failed|aborted)")
for raw_line in sys.stdin:
    line = raw_line[:-1]
    result = fail_pattern.search(line)
    if result:
        if result.group(1) == 0:
            print green_bar(line)
        else:
            print red_bar(line)
    elif pass_pattern.search(line):
        print green_bar(line)
    else:
        print line

The command line

Filter the output of dmake through this script with the following command line (cover your eyes):

dmake runtest 2>&1 | python c:\bin\chuiredgreenbar.py | cat

Example output

Observations

  1. The red background is not part of the highlighting – that’s another tool thing I won’t get into here.
  2. I barely know Python
  3. The super hacky part is having to pipe the output to cat to get ANSI escape sequences interpreted (not to mention merging stderr in with stdout since some of dmake’s output goes one place, some the other and we need to process both)

This warty construction is “in production” on my PC – I use it ‘most every day.  If it weren’t for something like this, I would probably still be straining my eyes to see which test cases failed.  Perhaps eventually there will be a graphical UI for the Boost unit test output; but till then, this was an efficient way to fill a need.


*I could look at this negatively, but hey – I get to be part of bringing this discipline to my company, and  besides that, there’s openness to the idea.   Smells like opportunity to me!

**I would consider that designation an honor.  We need more of that kind of crazy in the industry!

Advertisements

, ,

  1. #1 by Jon on December 2, 2009 - 10:15 am

    Yeah! That’s what I’m talking about!

    This is the kind of thing I love. Quick little snips that will make you happier/more productive many times, every day.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s