Fix file-handle-leaks when re-writing documents or slideshows When replacing a directory, we should close the previous one Close some resources in tests