tests: Improve temporary directory handling.