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

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

Bug Description

Upstream bug:

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()
            with tempfile.TemporaryDirectory() as tmpdir:
                deleted_dir = os.path.join(tmpdir, "gone")

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

                env = os.environ.copy()
                env["PYTHONPATH"] = orig_cwd
                process =
                    [sys.executable, "../"],

                Traceback (most recent call last):
                  File "../", line 1, in <module>
                    raise ValueError()
        self.assertEqual(process.stdout.decode(), "")
        self.assertEqual(process.returncode, 1)

Run this script with `python3 -m pytest --cov=.` 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/", line 192, in addpackage
    File "<string>", line 1, in <module>
    File "/usr/lib/python3/dist-packages/", line 20, in <module>
      import cov_core # noqa: register multiprocessing handler
    File "/usr/lib/python3/dist-packages/", line 5, in <module>
      import coverage
    File "/usr/lib/python3/dist-packages/coverage/", line 15, in <module>
      from coverage.control import Coverage, process_startup
    File "/usr/lib/python3/dist-packages/coverage/", line 17, in <module>
      from coverage.annotate import AnnotateReporter
    File "/usr/lib/python3/dist-packages/coverage/", line 9, in <module>
      from coverage.files import flat_rootname
    File "/usr/lib/python3/dist-packages/coverage/", line 166, in <module>
    File "/usr/lib/python3/dist-packages/coverage/", line 28, in set_relative_directory
      RELATIVE_DIR = os.path.normcase(abs_file(os.curdir) + os.sep)
    File "/usr/lib/python3/dist-packages/coverage/", line 149, in abs_file
      path = os.path.realpath(path)
    File "/usr/lib/python3.10/", line 396, in realpath
      return abspath(path)
    File "/usr/lib/python3.10/", 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