fix typo bg, add unit test for --tmpdir, restore environment and reset tempfile every time main() is called