Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix appending to .coverage if exec ends in non-existent directory #806

merged 1 commit into from Jul 6, 2019


Copy link

@hemberger hemberger commented May 30, 2019

The --append option causes coverage 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,

import tempfile
import os

with tempfile.TemporaryDirectory() as tmpdir:

Run coverage on this as follows:

python -m coverage run --append

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:

Traceback (most recent call last):
  File "/usr/lib/python3.7/", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/lib/python3.7/", line 85, in _run_code
    exec(code, run_globals)
  File "/home/x/repos/coveragepy/coverage/", line 8, in <module>
  File "/home/x/repos/coveragepy/coverage/", line 762, in main
    status = CoverageScript().command_line(argv)
  File "/home/x/repos/coveragepy/coverage/", line 506, in command_line
    return self.do_run(options, args)
  File "/home/x/repos/coveragepy/coverage/", line 649, in do_run
  File "/home/x/repos/coveragepy/coverage/", line 553, in save
    data = self.get_data()
  File "/home/x/repos/coveragepy/coverage/", line 607, in get_data
    if self._collector and self._collector.flush_data():
  File "/home/x/repos/coveragepy/coverage/", line 425, in flush_data
  File "/home/x/repos/coveragepy/coverage/", line 245, in add_lines
  File "/home/x/repos/coveragepy/coverage/", line 288, in _choose_lines_or_arcs
    with self._connect() as con:
  File "/home/x/repos/coveragepy/coverage/", line 616, in __enter__
  File "/home/x/repos/coveragepy/coverage/", line 601, in connect
    filename = os.path.relpath(self.filename)
  File "/usr/lib/python3.7/", line 475, in relpath
    start_list = [x for x in abspath(start).split(sep) if x]
  File "/usr/lib/python3.7/", line 383, in abspath
    cwd = os.getcwd()
FileNotFoundError: [Errno 2] No such file or directory

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!

If the file that is exec'd chdirs to a directory that doesn't exist
at the end of the execution, then we will fail to connect to the
SQLite database (due to a failing `os.getcwd` command).

We can easily fix this if we ensure we are in a directory that
exists after executing the foreign code. Returning to the original
directory seems to be a sensible choice.
Copy link

@codecov-io codecov-io commented May 30, 2019

Codecov Report

Merging #806 into master will decrease coverage by 0.04%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff            @@
##           master    #806      +/-   ##
- Coverage   90.65%   90.6%   -0.05%     
  Files          81      81              
  Lines       11178   11180       +2     
  Branches     1150    1150              
- Hits        10133   10130       -3     
- Misses        936     938       +2     
- Partials      109     112       +3
Impacted Files Coverage Δ
coverage/ 82.75% <100%> (+0.2%) ⬆️
tests/ 98.54% <0%> (-1.46%) ⬇️
coverage/ 89.27% <0%> (-0.29%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d469d94...f9f0e65. Read the comment docs.

nedbat added a commit that referenced this issue Jul 6, 2019
@nedbat nedbat merged commit a9fcee8 into nedbat:master Jul 6, 2019
3 of 4 checks passed
Copy link

@nedbat nedbat commented Jul 6, 2019

Thanks! I tweaked the implementation, and added tests, in 9ae35a5

Copy link

@nedbat nedbat commented Jul 16, 2019

This has been released in 5.0a6.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
None yet

Successfully merging this pull request may close these issues.

None yet

3 participants