1
# This file contains settings for pytest that are specific to astropy-helpers.
2
# Since we run many of the tests in sub-processes, we need to collect coverage
3
# data inside each subprocess and then combine it into a single .coverage file.
4
# To do this we set up a list which run_setup appends coverage objects to.
5
# This is not intended to be used by packages other than astropy-helpers.
6

7 0
import os
8 0
import glob
9

10 0
try:
11 0
    from coverage import CoverageData
12 0
except ImportError:
13 0
    HAS_COVERAGE = False
14
else:
15 0
    HAS_COVERAGE = True
16

17 0
if HAS_COVERAGE:
18 0
    SUBPROCESS_COVERAGE = []
19

20

21 0
def pytest_configure(config):
22 0
    if HAS_COVERAGE:
23 0
        SUBPROCESS_COVERAGE.clear()
24

25

26 0
def pytest_unconfigure(config):
27

28 0
    if HAS_COVERAGE:
29

30
        # We create an empty coverage data object
31 0
        combined_cdata = CoverageData()
32

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

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

41 0
        for cdata in SUBPROCESS_COVERAGE:
42
            # For each CoverageData object, we go through all the files and
43
            # change the filename from one which might be a temporary path
44
            # to the local filename. We then only keep files that actually
45
            # exist.
46 0
            for filename in cdata.measured_files():
47 0
                try:
48 0
                    pos = filename.rindex('astropy_helpers')
49 0
                except ValueError:
50 0
                    continue
51 0
                short_filename = filename[pos:]
52 0
                if os.path.exists(short_filename):
53 0
                    lines[os.path.abspath(short_filename)].extend(cdata.lines(filename))
54

55 0
        combined_cdata.add_lines(lines)
56

57 0
        combined_cdata.write_file('.coverage.subprocess')

Read our documentation on viewing source code .

Loading