Fix appending to .coverage if exec ends in non-existent directory #806
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The
--append
option causescoverage run
to fail if the<pyfile>
that is executed does a chdir to a directory that is subsequently removed before the exec completes.For example, with the following file,
test.py
:Run coverage on this as follows:
This results in a
FileNotFoundError
because the SQLite database connector internally tries to find the relative path to the database file, which fails since the cwd does not exist. The traceback looks like:This patch avoids this problem by ensuring that we return to a known directory (the original directory we ran in) once the
exec
completes.One could argue that it is the exec'd script's responsibility for ensuring that it doesn't end in an invalid directory, but it seems reasonable for
coverage
to handle this case since it can do so easily (and because temporary directories are likely to be a common occurrence in a test suite).I believe this issue also warrants a unit test, but I couldn't make out the right way to do this after a perusal of the test suite. Please feel free to add a test or to guide me to a good example for how to implement it. Thank you!