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 7import os 8import glob 9 10try: 11 from coverage import CoverageData 12except ImportError: 13 HAS_COVERAGE = False 14else: 15 HAS_COVERAGE = True 16 17if HAS_COVERAGE: 18 SUBPROCESS_COVERAGE = [] 19 20 21def pytest_configure(config): 22 if HAS_COVERAGE: 23 SUBPROCESS_COVERAGE.clear() 24 25 26def pytest_unconfigure(config): 27 28 if HAS_COVERAGE: 29 30 # We create an empty coverage data object 31 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 lines = {} 38 for filename in glob.glob(os.path.join('astropy_helpers', '**', '*.py'), recursive=True): 39 lines[os.path.abspath(filename)] = [] 40 41 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 for filename in cdata.measured_files(): 47 try: 48 pos = filename.rindex('astropy_helpers') 49 except ValueError: 50 continue 51 short_filename = filename[pos:] 52 if os.path.exists(short_filename): 53 lines[os.path.abspath(short_filename)].extend(cdata.lines(filename)) 54 55 combined_cdata.add_lines(lines) 56 57 combined_cdata.write_file('.coverage.subprocess') 58