Fix: Check output file isn't already a directory, handle errors, fix and add tests

Fixes #1034.
Can you use ShellJS for this instead? Id prefer to stick to one way of doing things like this. 

Also, the file removal should take place in an `afterEach` block. As it is now, an failed assertion week leave the file in place, which could affect future test runs.