Saturday, September 6, 2014

Xfvb-run for Profitable JS Testing


Now that I am getting my Patterns in Polymer testing house back in order with eee-polymer-tests, it is time to revisit my whole project testing script.

It already works through each code chapter in the book:
#!/bin/bash

for X in book/code-js/parent*
  # Run all tests for a chapter X here...
do
For each chapter's tests, it already upgrades Bower packages (mostly to get the latest Polymer) and performs a single run of Karma:
    # ...
    # Update bower
    echo -n "Updating bower..."
    bower update --quiet
    echo " done."

    # Run the Karma tests
    karma start --single-run

    # Handle failure
    if [[ $? -ne 0 ]]; then
        echo
        echo "Some tests failed in $X."
        exit 1
    fi
    # ...
That still works, even with my updates to the testing setup, but it pops up a browser window to execute the tests. Instead, I would like to run this all under Xvfb. I don't know if I have a standard way of doing this, but I seem to think that I should start Xvfb with a display port (e.g. :99) then run everything with that same display port:
#!/bin/bash

Xvfb :99 2>&1 >/dev/null &
DISPLAY=:99

for X in book/code-js/parent*
do
  # Run all tests for a chapter X here...
do
That works, but it produces a lot of noise. Even redirecting STDOUT/STDIN to /dev/null like I am doing, I get:
Updating bower...Initializing built-in extension Generic Event Extension
Initializing built-in extension SHAPE
Initializing built-in extension MIT-SHM
Initializing built-in extension XInputExtension
Initializing built-in extension XTEST
Initializing built-in extension BIG-REQUESTS
Initializing built-in extension SYNC
Initializing built-in extension XKEYBOARD
...
Initializing built-in extension XVideo-MotionCompensation
Initializing built-in extension SELinux
Initializing built-in extension GLX
[dix] Could not init font path element /usr/share/fonts/X11/cyrillic, removing from list!
[dix] Could not init font path element /usr/share/fonts/X11/100dpi/:unscaled, removing from list!
[dix] Could not init font path element /usr/share/fonts/X11/75dpi/:unscaled, removing from list!
[dix] Could not init font path element /usr/share/fonts/X11/100dpi, removing from list!
[dix] Could not init font path element /usr/share/fonts/X11/75dpi, removing from list!
Even after individual runs, I get a lot of junk like:
GC: 4 objects of 16 bytes = 64 total bytes 0 private allocs
CURSOR: 1 objects of 8 bytes = 8 total bytes 0 private allocs
TOTAL: 5 objects, 72 bytes, 0 allocs
1 CURSORs still allocated at reset
CURSOR: 1 objects of 8 bytes = 8 total bytes 0 private allocs
TOTAL: 1 objects, 8 bytes, 0 allocs
1 CURSOR_BITSs still allocated at reset
TOTAL: 0 objects, 0 bytes, 0 allocs
It is just noise obscuring the important testing stuff. I do not really understand how redirecting STDOUT/STDIN is failing, but I just want it gone.

Eventually, I find xvfb-run (on my Ubuntu system). I thought this might be a shell interpreter—the kind that goes after the sh-bang at the top of my shell script. But it turns out to be even simpler. I remove the DISPLAY setting as well as the Xvfb call. Instead, this simply wraps the call to Karma (which starts the browser):
    # Run the Karma tests
    xvfb-run karma start --single-run
With that, the output from my whole-book test script is just Karma output:
$ ./scripts/test.sh
START: book/code-js/parent_child
Updating bower... done.
INFO [karma]: Karma v0.12.23 server started at http://localhost:9876/
INFO [launcher]: Starting browser Chrome
INFO [Chrome 37.0.2062 (Linux)]: Connected on socket Pg5IMgnAOxMo5KIogEba with id 28554454
Chrome 37.0.2062 (Linux): Executed 3 of 3 SUCCESS (3.086 secs / 3.079 secs)
/home/chris/repos/polymer-book

START: book/code-js/parent_events
Updating bower... done.
INFO [karma]: Karma v0.12.23 server started at http://localhost:9876/
INFO [launcher]: Starting browser Chrome
INFO [Chrome 37.0.2062 (Linux)]: Connected on socket VI7JUF1u0gF4z62ngGTV with id 68348849
Chrome 37.0.2062 (Linux): Executed 2 of 2 SUCCESS (0.078 secs / 0.074 secs)
/home/chris/repos/polymer-book
...
Success!
I can even take this a step further and switch Karma to log warnings only:
    # Run the Karma tests
    xvfb-run karma start --single-run --log-level warn
Which results in test output like:
$ /scripts/test.sh
[TEST] book/code-js/parent_child (Bower updated).
Chrome 37.0.2062 (Linux): Executed 3 of 3 SUCCESS (3.08 secs / 3.075 secs)

[TEST] book/code-js/parent_events (Bower updated).
Chrome 37.0.2062 (Linux): Executed 2 of 2 SUCCESS (0.067 secs / 0.061 secs)

Success!
Success indeed.


Day #175

No comments:

Post a Comment