pytest-cov: Failed to setup subprocess coverage if os.getcwd() fails

Bug #1979640 reported by Benjamin Drung
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
python-pytest-cov (Ubuntu)
New
Undecided
Unassigned

Bug Description

Upstream bug: https://github.com/pytest-dev/pytest-cov/issues/541

Following sample test case causes pytest-cov to fail:

```
import os
import subprocess
import sys
import tempfile
import textwrap
import unittest

class Test(unittest.TestCase):
    maxDiff = None

    def test_deleted_working_directory(self):
        """Relative Python script from deleted working directory."""
        orig_cwd = os.getcwd()
        try:
            with tempfile.TemporaryDirectory() as tmpdir:
                deleted_dir = os.path.join(tmpdir, "gone")
                os.mkdir(deleted_dir)
                os.chdir(deleted_dir)
                os.rmdir(deleted_dir)

                with open(os.path.join(tmpdir, "script.py"), "w") as script:
                    script.write("raise ValueError()\n")

                env = os.environ.copy()
                env["PYTHONPATH"] = orig_cwd
                process = subprocess.run(
                    [sys.executable, "../script.py"],
                    env=env,
                    stdout=subprocess.PIPE,
                    stderr=subprocess.PIPE,
                )
        finally:
            os.chdir(orig_cwd)

        print(process.stderr.decode())
        self.assertEqual(
            process.stderr.decode(),
            textwrap.dedent(
                """\
                Traceback (most recent call last):
                  File "../script.py", line 1, in <module>
                    raise ValueError()
                ValueError
                """
            ),
        )
        self.assertEqual(process.stdout.decode(), "")
        self.assertEqual(process.returncode, 1)
```

Run this script with `python3 -m pytest --cov=. test.py` and stderr will contain following stack trace:

```
Error processing line 1 of /usr/lib/python3/dist-packages/init_cov_core.pth:

  Traceback (most recent call last):
    File "/usr/lib/python3.10/site.py", line 192, in addpackage
      exec(line)
    File "<string>", line 1, in <module>
    File "/usr/lib/python3/dist-packages/cov_core_init.py", line 20, in <module>
      import cov_core # noqa: register multiprocessing handler
    File "/usr/lib/python3/dist-packages/cov_core.py", line 5, in <module>
      import coverage
    File "/usr/lib/python3/dist-packages/coverage/__init__.py", line 15, in <module>
      from coverage.control import Coverage, process_startup
    File "/usr/lib/python3/dist-packages/coverage/control.py", line 17, in <module>
      from coverage.annotate import AnnotateReporter
    File "/usr/lib/python3/dist-packages/coverage/annotate.py", line 9, in <module>
      from coverage.files import flat_rootname
    File "/usr/lib/python3/dist-packages/coverage/files.py", line 166, in <module>
      set_relative_directory()
    File "/usr/lib/python3/dist-packages/coverage/files.py", line 28, in set_relative_directory
      RELATIVE_DIR = os.path.normcase(abs_file(os.curdir) + os.sep)
    File "/usr/lib/python3/dist-packages/coverage/files.py", line 149, in abs_file
      path = os.path.realpath(path)
    File "/usr/lib/python3.10/posixpath.py", line 396, in realpath
      return abspath(path)
    File "/usr/lib/python3.10/posixpath.py", line 383, in abspath
      cwd = os.getcwd()
  FileNotFoundError: [Errno 2] No such file or directory

Remainder of file ignored
pytest-cov: Failed to setup subprocess coverage.
```

Revision history for this message
Benjamin Drung (bdrung) wrote :
Benjamin Drung (bdrung)
description: updated
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Bug attachments

Remote bug watches

Bug watches keep track of this bug in other bug trackers.