Use tempfile package instead of writing to /tmp

While I'm at it, a few changes to tests to eliminate the assumption that `/tmp` exists and is writable.  This should also clean up all the temporary files that were created for tests.