The bug was that tilde expansion was inconsistent, so one of the tests always failed. Replaced ~ with $HOME.