Beispiel #1
0
def _coverage_wrapper(paths):
    try:
        import coverage  # NoQA
    except ImportError:
        click.secho(
            'Error: "coverage" package is missing, cannot run tests '
            'with --cov')
        sys.exit(1)

    for path in edb.__path__:
        cov_rc = pathlib.Path(path).parent / '.coveragerc'
        if cov_rc.exists():
            break
    else:
        raise RuntimeError('cannot locate the .coveragerc file')

    with tempfile.TemporaryDirectory() as td:
        cov_config = devmode.CoverageConfig(
            paths=paths,
            config=str(cov_rc),
            datadir=td)
        cov_config.save_to_environ()

        main_cov = cov_config.new_coverage_object()
        main_cov.start()

        try:
            yield
        finally:
            main_cov.stop()
            main_cov.save()

            data = coverage.CoverageData()

            with os.scandir(td) as it:
                for entry in it:
                    new_data = coverage.CoverageData()
                    new_data.read_file(entry.path)
                    data.update(new_data)

            covfile = str(pathlib.Path(td) / '.coverage')
            data.write_file(covfile)
            report_cov = cov_config.new_custom_coverage_object(
                config_file=str(cov_rc),
                data_file=covfile,
            )
            report_cov.load()
            click.secho('Coverage:')
            report_cov.report()
            # store the coverage file in cwd, so it can be used to produce
            # additional reports with coverage cli
            shutil.copy(covfile, '.')
    def run(self, test):
        if self.coverage:
            import coverage

            for path in edb.__path__:
                cov_rc = pathlib.Path(path).parent / '.coveragerc'
                if cov_rc.exists():
                    break
            else:
                raise RuntimeError('cannot locate the .coveragerc file')

            with tempfile.TemporaryDirectory() as td:
                cov_config = devmode.CoverageConfig(
                    paths=self.coverage,
                    config=str(cov_rc),
                    datadir=td)
                cov_config.save_to_environ()

                main_cov = cov_config.new_coverage_object()
                main_cov.start()

                try:
                    return self._run(test)
                finally:
                    main_cov.stop()
                    main_cov.save()

                    data = coverage.CoverageData()

                    with os.scandir(td) as it:
                        for entry in it:
                            new_data = coverage.CoverageData()
                            new_data.read_file(entry.path)
                            data.update(new_data)

                    data.write_file('.coverage')

                    report_cov = cov_config.new_custom_coverage_object(
                        config_file=str(cov_rc))
                    report_cov.load()
                    report_cov.report()
        else:
            return self._run(test)