pytest: add file removal helpers for TestCaseInTempDir
In several places we end a test by deleting a number of files and directories, but we do it rather haphazardly with unintentionally differing error handling. For example, in some tests we currently have something like:
try:
shutil.rmtree(os.path.join(self.tempdir, "a"))
os.remove(os.path.join(self.tempdir, "b"))
shutil.rmtree(os.path.join(self.tempdir, "c"))
except Exception:
pass
where if, for example, the removal of "b" fails, the removal of "c" will not be attempted. That will result in the tearDown method raising an exception, and we're no better off. If the above code is replaced with
self.rm_files('b')
self.rm_dirs('a', 'c')
the failure to remove 'b' will cause a test error, unless the failure was due to a FileNotFoundError (a.k.a. an OSError with errno ENOENT), in which case we ignore it, as was probably the original intention.
If on the other hand, we have
self.rm_files('b', must_exist=True)
self.rm_dirs('a', 'c')
then the FileNotFoundError causes a failure (not an error).
We take a little bit of care to stay within self.tempdir, to protect
test authors who accidentally write something like self.rm_dirs('/')
.
BUG: https://bugzilla.samba.org/show_bug.cgi?id=15191
Checklist
-
Commits have Signed-off-by:
with name/author being identical to the commit author -
CI timeout is 3h or higher (see Settings/CICD/General pipelines/ Timeout)
Reviewer's checklist:
-
There is a test suite reasonably covering new functionality or modifications -
Function naming, parameters, return values, types, etc., are consistent and according to README.Coding.md
-
This feature/change has adequate documentation added -
No obvious mistakes in the code