import os
import glob

try:
    from coverage import CoverageData
except ImportError:
    HAS_COVERAGE = False
else:
    HAS_COVERAGE = True

# Since we run many of the tests in sub-processes, we need to collect coverage
# data inside each subprocess and then combine it into a single .coverage file.
# To do this we set up a list which run_setup appends coverage objects to.

if HAS_COVERAGE:
    SUBPROCESS_COVERAGE = []


def pytest_configure(config):
    if HAS_COVERAGE:
        SUBPROCESS_COVERAGE.clear()


def pytest_unconfigure(config):

    if HAS_COVERAGE:

        # We create an empty coverage data object
        combined_cdata = CoverageData()

        # Add all files from astropy_helpers to make sure we compute the total
        # coverage, not just the coverage of the files that have non-zero
        # coverage.

        lines = {}
        for filename in glob.glob(os.path.join('astropy_helpers', '**', '*.py'), recursive=True):
            lines[os.path.abspath(filename)] = []

        for cdata in SUBPROCESS_COVERAGE:
            # For each CoverageData object, we go through all the files and
            # change the filename from one which might be a temporary path
            # to the local filename. We then only keep files that actually
            # exist.
            for filename in cdata.measured_files():
                try:
                    pos = filename.rindex('astropy_helpers')
                except ValueError:
                    continue
                short_filename = filename[pos:]
                if os.path.exists(short_filename):
                    lines[os.path.abspath(short_filename)].extend(cdata.lines(filename))

        combined_cdata.add_lines(lines)

        combined_cdata.write_file('.coverage.subprocess')

@@ -1,145 +1,82 @@
-import os%0Aimport glob%0A%0Atry:%0A    from coverage import CoverageData%0Aexcept ImportError:%0A    HAS_COVERAGE = False%0Aelse:%0A    HAS_COVERAGE = True%0A
+# This file contains settings for pytest that are specific to astropy-helpers.
 %0A# S
@@ -305,16 +305,232 @@
 ects to.
+%0A# This is not intended to be used by packages other than astropy-helpers.%0A%0Aimport os%0Aimport glob%0A%0Atry:%0A    from coverage import CoverageData%0Aexcept ImportError:%0A    HAS_COVERAGE = False%0Aelse:%0A    HAS_COVERAGE = True
 %0A%0Aif HAS

Add a comment inside conftest.py to explain that this file is there only for astropy-helpers [ci skip]