Bug #6884
closedtest_fileutils.rb might end up rm -rf the whole current directory
Description
Hi,
I was preparing r36709 and launching make test-all
and I interrupted it (Ctrl+C) as I forgot something.
The next thing I saw is that my repository was deleted, as well as almost half the files.
I think I don't need to tell you how that is fun. Hopefully I had a recent backup.
So, the culprit seems the fileutils tests coupled with #teardown being in an ensure clause.
You can see this behavior in https://gist.github.com/ab7d5ea2ae68eda6398b (if you press Ctrl+C within 2 seconds or not), and in
test/fileutils/test_fileutils.rb lines 97-111. If #setup is interrupted, then #teardown is still run, because it is called an ensure block (in lib/minitest/unit.rb line 1074).
What probably happened in my case is #setup did not yet Dir.chdir'd, and thus Dir.pwd in #teardown was the original current directory, and it happily rm -rf'd that at line 110.
The fix for fileutils tests is obvious, Dir.pwd should not be used there.
I'll fix them unless objection.
But I'm worried about #teardown always being run (due to the ensure clause).
Is that safe in the general case? It means the code must handle any part of #setup which was ran, which is increasingly complex.
Sorry for the story-telling style, but I think it might better represent the danger in this issue.