def get_existing_tests(coverage_file_path: str) -> Set[str]: """Read all the test function names from the coverage file. pytest-cov creates the coverage file and adds a section at the end of each testname which need to be stripped. """ coverage_data = CoverageData(coverage_file_path) coverage_data.read() return { strip_pytest_cov_testname(testname) for testname in coverage_data.measured_contexts() }
def get_tests_from_changes( commithash_to_compare: str, coverage_file_path: str ) -> Set[str]: """Returns the test set from Git changes. The given commithash is compared to the current working copy to extract Git diffs, if the provided commithash exists in the repo. Otherwise only changes in the git working directory are considered. """ repo = get_git_repo() if commit_exists(commithash_to_compare, repo): file_diffs = { file_path: get_file_diff_data_committed_and_workdir( repo, file_path, commithash_to_compare ) for file_path in get_changed_files_committed_and_workdir( repo, commithash_to_compare ) } else: file_diffs = { file_path: get_file_diff_data_workdir(repo, file_path) for file_path in get_changed_files_workdir(repo) } coverage_data = CoverageData(coverage_file_path) coverage_data.read() tests: Set[str] = set() for changed_file in file_diffs: contexts = coverage_data.contexts_by_lineno(changed_file) if not contexts: continue changed_lines_with_tests = intersect_with_surroundings( get_changed_lines(file_diffs[changed_file]), contexts.keys() ) tests.update( strip_pytest_cov_testname(testname) for line in changed_lines_with_tests for testname in contexts[line] ) return tests
def get_coverage_data_from_file(path: str = '.coverage') -> 'CoverageData': """Get the coverage data from a file Coverage is generally stored in a '.covarage' file in sqllite format, which can be loaded for post processing. Args: path (str): path to the coverage file Raises: AssertionError: raised when coverage data object is empty Returns: CoverageData: previously recorded test coverage """ from coverage import CoverageData cov_data = CoverageData(basename=path) cov_data.read() assert cov_data, 'CoverageData is loaded' return cov_data
import sys from coverage import CoverageData # import this so we get our monkey patching done which is needed for # os.path.relpath on python2.5 import xtraceback def transform_data(data, transform): result = dict() for path, value in data.items(): result[transform(path)] = value return result if __name__ == "__main__": transform = getattr(os.path, "%spath" % sys.argv[1]) paths = sys.argv[2:] assert paths for path in paths: data = CoverageData(path) data.read() for field in ("lines", "arcs"): field_data = getattr(data, field) assert field_data setattr(data, field, transform_data(field_data, transform)) data.write()
from coverage import CoverageData # import this so we get our monkey patching done which is needed for # os.path.relpath on python2.5 import xtraceback def transform_data(data, transform): result = dict() for path, value in data.items(): result[transform(path)] = value return result if __name__ == "__main__": transform = getattr(os.path, "%spath" % sys.argv[1]) paths = sys.argv[2:] assert paths for path in paths: data = CoverageData(path) data.read() for field in ("lines", "arcs"): field_data = getattr(data, field) assert field_data setattr(data, field, transform_data(field_data, transform)) data.write()