adding tests for exit codes

I rebased this branch (`fix/333`) against `master`, and added tests for #1297 in 7c4ba93.

If you're happy with dc8fcf6, I will go ahead and merge it all into `master`.