Use TemporaryWorkingDirectory context manager in some tests
Ensure history tests release database before cleanup
Merge pull request #5872 from takluyver/tmpdir-cleanup-errors

Tmpdir cleanup errors