Пример #1
0
def fix_coverage(from_path, to_path):
    coverage_data = CoverageData()
    os.rename('.coverage', '.coverage.orig')
    coverage_data.read_file('.coverage.orig')
    merge_coverage(coverage_data, from_path, to_path)
    coverage_data.write_file('.coverage')
    os.remove('.coverage.orig')
Пример #2
0
    def test_read_write_arcs(self):
        covdata1 = CoverageData()
        covdata1.add_arcs(ARCS_3)
        covdata1.write_file("arcs.dat")

        covdata2 = CoverageData()
        covdata2.read_file("arcs.dat")
        self.assert_arcs3_data(covdata2)
Пример #3
0
    def test_read_write_arcs(self):
        covdata1 = CoverageData()
        covdata1.add_arcs(ARCS_3)
        covdata1.write_file("arcs.dat")

        covdata2 = CoverageData()
        covdata2.read_file("arcs.dat")
        self.assert_arcs3_data(covdata2)
Пример #4
0
    def test_read_write_lines(self):
        covdata1 = CoverageData()
        covdata1.add_lines(LINES_1)
        covdata1.write_file("lines.dat")

        covdata2 = CoverageData()
        covdata2.read_file("lines.dat")
        self.assert_lines1_data(covdata2)
Пример #5
0
    def test_read_write_lines(self):
        covdata1 = CoverageData()
        covdata1.add_lines(LINES_1)
        covdata1.write_file("lines.dat")

        covdata2 = CoverageData()
        covdata2.read_file("lines.dat")
        self.assert_lines1_data(covdata2)
Пример #6
0
    def test_read_write_lines_pickle(self):
        # Test the old pickle format.
        covdata1 = CoverageData()
        covdata1.set_lines(LINES_1)
        self.write_pickled_file(covdata1, "lines.pkl")

        pickle2json("lines.pkl", "lines.json")

        covdata2 = CoverageData()
        covdata2.read_file("lines.json")
        self.assert_lines1_data(covdata2)
Пример #7
0
    def test_read_write_lines(self):
        covdata1 = CoverageData()
        covdata1.set_lines(LINES_1)
        covdata1.write_file("lines.dat")

        covdata2 = CoverageData()
        covdata2.read_file("lines.dat")
        self.assert_line_counts(covdata2, SUMMARY_1)
        self.assert_measured_files(covdata2, MEASURED_FILES_1)
        self.assertCountEqual(covdata2.lines("a.py"), A_PY_LINES_1)
        self.assertEqual(covdata2.run_infos(), [])
Пример #8
0
    def test_read_write_lines_pickle(self):
        # Test the old pickle format.
        covdata1 = CoverageData()
        covdata1.add_lines(LINES_1)
        self.write_pickled_file(covdata1, "lines.pkl")

        pickle2json("lines.pkl", "lines.json")

        covdata2 = CoverageData()
        covdata2.read_file("lines.json")
        self.assert_lines1_data(covdata2)
Пример #9
0
    def test_read_write_arcs_pickle(self):
        # Test the old pickle format.
        covdata1 = CoverageData()
        covdata1.add_arcs(ARCS_3)
        self.write_pickled_file(covdata1, "arcs.pkl")

        pickle2json("arcs.pkl", "arcs.json")

        covdata2 = CoverageData()
        covdata2.read_file("arcs.json")
        self.assert_arcs3_data(covdata2)
Пример #10
0
    def test_read_write_arcs_pickle(self):
        # Test the old pickle format.
        covdata1 = CoverageData()
        covdata1.set_arcs(ARCS_3)
        self.write_pickled_file(covdata1, "arcs.pkl")

        pickle2json("arcs.pkl", "arcs.json")

        covdata2 = CoverageData()
        covdata2.read_file("arcs.json")
        self.assert_arcs3_data(covdata2)
Пример #11
0
def fix_paths(site_pkg_dir, cov_data_file):
    site_pkg_dir = os.path.abspath(site_pkg_dir)

    paths = PathAliases()
    paths.add(site_pkg_dir, '.')

    old_coverage_data = CoverageData()
    old_coverage_data.read_file(cov_data_file)

    new_coverage_data = CoverageData()
    new_coverage_data.update(old_coverage_data, paths)

    new_coverage_data.write_file(cov_data_file)
Пример #12
0
    def test_read_errors(self):
        covdata = CoverageData()

        msg = r"Couldn't read data from '{0}': \S+"
        self.make_file("xyzzy.dat", "xyzzy")
        with self.assertRaisesRegex(CoverageException,
                                    msg.format("xyzzy.dat")):
            covdata.read_file("xyzzy.dat")

        self.make_file("empty.dat", "")
        with self.assertRaisesRegex(CoverageException,
                                    msg.format("empty.dat")):
            covdata.read_file("empty.dat")

        with self.assertRaisesRegex(CoverageException,
                                    msg.format("nonexistent.dat")):
            covdata.read_file("nonexistent.dat")

        self.make_file("misleading.dat",
                       CoverageData._GO_AWAY + " this isn't JSON")
        with self.assertRaisesRegex(CoverageException,
                                    msg.format("misleading.dat")):
            covdata.read_file("misleading.dat")

        # After all that, no data should be in our CoverageData.
        self.assertFalse(covdata)
Пример #13
0
    def test_read_write_arcs(self):
        covdata1 = CoverageData()
        covdata1.set_arcs(ARCS_3)
        covdata1.write_file("arcs.dat")

        covdata2 = CoverageData()
        covdata2.read_file("arcs.dat")
        self.assert_line_counts(covdata2, SUMMARY_3)
        self.assert_measured_files(covdata2, MEASURED_FILES_3)
        self.assertCountEqual(covdata2.lines("x.py"), X_PY_LINES_3)
        self.assertCountEqual(covdata2.arcs("x.py"), X_PY_ARCS_3)
        self.assertCountEqual(covdata2.lines("y.py"), Y_PY_LINES_3)
        self.assertCountEqual(covdata2.arcs("y.py"), Y_PY_ARCS_3)
        self.assertEqual(covdata2.run_infos(), [])
Пример #14
0
    def raise_exc(exc, content, ex, ex2, outfile, destcov, source, dests, inter, cov):

        from coverage.data import CoverageData

        def shorten(t):
            if len(t) > 2000:
                return t[:2000] + "\n..."
            else:
                return t
        if len(content) > 2000:
            content = content[:2000] + '\n...'
        ex = "\n-\n".join(shorten(_) for _ in ex)
        ex2 = "\n-\n".join(shorten(_) for _ in ex2)
        rows = ["destcov='{0}'".format(destcov),
                "outfile='{0}'".format(outfile),
                "source='{0}'".format(source),
                "cov.source={0}".format(cov.source),
                "dests='{0}'".format(';'.join(dests)),
                "inter={0}".format(inter)]
        if cov is not None and cov.data is not None and cov.data._lines is not None:
            rows.append("----- LINES")
            end = min(5, len(cov.data._lines))
            for k, v in list(sorted(cov.data._lines.items()))[:end]:
                rows.append('   {0}:{1}'.format(k, v))
            rows.append("----- RUNS")
            end = min(5, len(cov.data._runs))
            for k in cov.data._runs[:end]:
                rows.append('   {0}'.format(k))
            rows.append("----- END")
        for d in dests:
            dd = CoverageData()
            dd.read_file(d + "~")
            rows.append("------- LINES - '{0}'".format(d))
            end = min(5, len(dd._lines))
            for k, v in list(sorted(dd._lines.items()))[:end]:
                rows.append('   {0}:{1}'.format(k, v))
            rows.append("------- RUNS - '{0}'".format(d))
            end = min(5, len(dd._runs))
            for k in dd._runs[:end]:
                rows.append('   {0}'.format(k))
            rows.append("------- END")

        mes = "{5}. In '{0}'.\n{1}\n{2}\n---AFTER---\n{3}\n---BEGIN---\n{4}"
        raise RuntimeError(mes.format(output_path, "\n".join(
            rows), content, ex, ex2, exc, cov)) from exc
Пример #15
0
    def test_run_omit_vs_report_omit(self):
        # https://bitbucket.org/ned/coveragepy/issues/622/report-omit-overwrites-run-omit
        # report:omit shouldn't clobber run:omit.
        self.make_mycode()
        self.make_file(".coveragerc", """\
            [run]
            omit = */covmodzip1.py

            [report]
            omit = */covmod1.py
            """)
        self.run_command("coverage run mycode.py")

        # Read the data written, to see that the right files have been omitted from running.
        covdata = CoverageData()
        covdata.read_file(".coverage")
        files = [os.path.basename(p) for p in covdata.measured_files()]
        self.assertIn("covmod1.py", files)
        self.assertNotIn("covmodzip1.py", files)
Пример #16
0
    def test_read_errors(self):
        covdata = CoverageData()

        msg = r"Couldn't read data from '{0}': \S+"
        self.make_file("xyzzy.dat", "xyzzy")
        with self.assertRaisesRegex(CoverageException, msg.format("xyzzy.dat")):
            covdata.read_file("xyzzy.dat")

        self.make_file("empty.dat", "")
        with self.assertRaisesRegex(CoverageException, msg.format("empty.dat")):
            covdata.read_file("empty.dat")

        with self.assertRaisesRegex(CoverageException, msg.format("nonexistent.dat")):
            covdata.read_file("nonexistent.dat")

        self.make_file("misleading.dat", CoverageData._GO_AWAY + " this isn't JSON")
        with self.assertRaisesRegex(CoverageException, msg.format("misleading.dat")):
            covdata.read_file("misleading.dat")

        # After all that, no data should be in our CoverageData.
        self.assertFalse(covdata)
Пример #17
0
 def cov_data(self) -> CoverageData:
     """Read the coverage file for lines and arcs data."""
     cov_data = CoverageData()
     cov_data.read_file(self.cov_file)
     return cov_data
Пример #18
0
 def collect_covered_lines(self):
     from coverage.data import CoverageData
     data = CoverageData()
     data.read_file(self.coverage_file)
     return data.lines
Пример #19
0
def fix_coverage(from_path, to_path):
    coverage_data = CoverageData()
    os.rename(".coverage", ".coverage.orig")
    coverage_data.read_file(".coverage.orig")
    merge_coverage(coverage_data, from_path, to_path)
    coverage_data.write_file(".coverage")
Пример #20
0
 def collect_covered_lines(self):
     from coverage.data import CoverageData
     data = CoverageData()
     data.read_file(self.coverage_file)
     return data.lines
Пример #21
0
def fix_coverage(from_path, to_path):
    coverage_data = CoverageData()
    os.rename('.coverage', '.coverage.orig')
    coverage_data.read_file('.coverage.orig')
    merge_coverage(coverage_data, from_path, to_path)
    coverage_data.write_file('.coverage')
Пример #22
0
class CoverageFilter(Filter):
    """Filter for covered lines to be applied to mutation targets in Genome."""

    def __init__(self, coverage_file: Union[str, Path] = Path(".coverage")) -> None:
        """Initialize the filter.

        Args:
            coverage_file: an optional coverage file, a default ".coverage" is used.
        """
        self._coverage_file = Path(coverage_file)
        self._coverage_data: Optional[CoverageData] = None

    @property
    def coverage_file(self) -> Path:
        """Property accessor for ``_coverage_file`` set at initialization.

        Returns:
            The coverage file path.
        """
        return self._coverage_file

    @coverage_file.setter
    def coverage_file(self, value: Union[str, Path]) -> None:
        """Setter for the coverage file, clears local cache of CoverageData.

        Args:
             value: The path to the coverage file

        Returns:
            None
        """
        self._coverage_file = Path(value)
        self._coverage_data = None

    @property
    def coverage_data(self) -> CoverageData:
        """Read the coverage file for lines and arcs data.

        This is cached locally and updated if the coverage_file is changed.

        Returns:
             A CoverageData object based on the ``coverage_file``.

        Raises:
            FileNotFoundError: if coverage_file does not exist.
        """
        if not self.coverage_file.exists():
            raise FileNotFoundError(
                f"{self.coverage_file.resolve()} does not exist. "
                "Set the coverage_file property to a valid file."
            )

        if self._coverage_data is None:
            self._coverage_data = CoverageData()
            self._coverage_data.read_file(self.coverage_file)
        return self._coverage_data

    def filter(  # type: ignore
        self, loc_idxs: Set[LocIndex], source_file: Union[str, Path], invert: bool = False
    ) -> Set[LocIndex]:
        """Filter based on coverage measured file.

        This adds the source_file argument to the filter abstract method because the coverage
        file holds multiple measured-files, and the ``LocIndex`` object does not have a source
        file attribute. The choice is that the coverage file can be set and read once for the
        class instance, and any valid measured file can be used in the filter.

        Args:
            loc_idxs: location index set of targets
            source_file: source file that is measured by the coverage file
            invert: flag for inverted filter using NOT

        Returns:
            Filtered set of location index set
        """
        measured_file = str(Path(source_file).resolve())
        covered_lines = self.coverage_data.lines(measured_file) or list()

        if invert:
            return {l for l in loc_idxs if l.lineno not in covered_lines}
        return {l for l in loc_idxs if l.lineno in covered_lines}
#!/usr/bin/env python
# By Danilo J. S. Bellini
"""
Script to update the file paths stored in a single coverage data file

Syntax: python fixpath.py DATA_FILE OLD_PATH NEW_PATH
"""
import sys, os
from coverage.data import CoverageData, PathAliases

coverage_file_name, old_path, new_path = sys.argv[1:]

pa = PathAliases()
pa.add(old_path, new_path)

old_cd = CoverageData()
old_cd.read_file(coverage_file_name)

new_cd = CoverageData()
try:
    new_cd.update(old_cd, pa)
except AttributeError: # Coverage 3.7.1 (CPython 3.2)
    namer = lambda f: os.path.abspath(os.path.expanduser(pa.map(f)))
    new_cd.lines = dict((namer(f), d) for f, d in old_cd.lines.items())
    new_cd.arcs = dict((namer(f), d) for f, d in old_cd.arcs.items())
new_cd.write_file(coverage_file_name)