Beispiel #1
0
    def test_combining_arc_contexts(self):
        red_data, blue_data = self.run_red_blue(branch=True)
        for datas in [[red_data, blue_data], [blue_data, red_data]]:
            combined = CoverageData(suffix="combined")
            for data in datas:
                combined.update(data)

            self.assertEqual(combined.measured_contexts(), {'red', 'blue'})

            full_names = {
                os.path.basename(f): f
                for f in combined.measured_files()
            }
            self.assertCountEqual(full_names, ['red.py', 'blue.py'])

            fred = full_names['red.py']
            fblue = full_names['blue.py']

            self.assertEqual(combined.lines(fred, contexts=['red']),
                             self.LINES)
            self.assertEqual(combined.lines(fred, contexts=['blue']), [])
            self.assertEqual(combined.lines(fblue, contexts=['red']), [])
            self.assertEqual(combined.lines(fblue, contexts=['blue']),
                             self.LINES)

            self.assertEqual(combined.arcs(fred, contexts=['red']), self.ARCS)
            self.assertEqual(combined.arcs(fred, contexts=['blue']), [])
            self.assertEqual(combined.arcs(fblue, contexts=['red']), [])
            self.assertEqual(combined.arcs(fblue, contexts=['blue']),
                             self.ARCS)
Beispiel #2
0
def test_fix_coverage(tmpdir):
    base_file = tmpdir.join('foo.py')
    base_file.ensure()
    sub_file = tmpdir.join('site-packages/foo.py')
    sub_file.ensure()
    unrelated_file = tmpdir.join('bar.py')
    unrelated_file.ensure()

    coverage_data = CoverageData()
    coverage_data.add_arcs({
        str(base_file): {
            (1, 2): None
        },
        str(sub_file): {
            (3, 4): None
        },
        str(unrelated_file): {
            (5, 6): None
        },
    })

    assert coverage_data.lines(base_file) == [1]
    assert coverage_data.lines(sub_file) == [3]
    assert coverage_data.lines(unrelated_file) == [5]

    merge_coverage(coverage_data, '/site-packages/', str(tmpdir))

    # The base file should contain all the lines and arcs
    assert coverage_data.lines(base_file) == [1, 3]
    assert coverage_data.arcs(base_file) == [(1, 2), (3, 4)]
    assert coverage_data.lines(unrelated_file) == [5]
    assert coverage_data.arcs(unrelated_file) == [(5, 6)]

    # And the sub file should no longer exist
    assert sub_file not in coverage_data.measured_files()
Beispiel #3
0
 def test_lines_with_contexts(self):
     covdata = CoverageData()
     covdata.set_context('test_a')
     covdata.add_lines(LINES_1)
     self.assertEqual(covdata.lines('a.py'), [1, 2])
     self.assertEqual(covdata.lines('a.py', contexts=['test*']), [1, 2])
     self.assertEqual(covdata.lines('a.py', contexts=['other*']), [])
def test_fix_coverage(tmpdir):
    base_file = tmpdir.join('foo.py')
    base_file.ensure()
    sub_file = tmpdir.join('site-packages/foo.py')
    sub_file.ensure()
    unrelated_file = tmpdir.join('bar.py')
    unrelated_file.ensure()

    coverage_data = CoverageData()
    coverage_data.add_arcs({
        str(base_file): {(1, 2): None},
        str(sub_file): {(3, 4): None},
        str(unrelated_file): {(5, 6): None},
    })

    assert coverage_data.lines(base_file) == [1]
    assert coverage_data.lines(sub_file) == [3]
    assert coverage_data.lines(unrelated_file) == [5]

    merge_coverage(coverage_data, '/site-packages/', str(tmpdir))

    # The base file should contain all the lines and arcs
    assert coverage_data.lines(base_file) == [1, 3]
    assert coverage_data.arcs(base_file) == [(1, 2), (3, 4)]
    assert coverage_data.lines(unrelated_file) == [5]
    assert coverage_data.arcs(unrelated_file) == [(5, 6)]

    # And the sub file should no longer exist
    assert sub_file not in coverage_data.measured_files()
Beispiel #5
0
 def test_no_arcs_vs_unmeasured_file(self):
     covdata = CoverageData()
     covdata.set_arcs(ARCS_3)
     covdata.touch_file('zzz.py')
     self.assertEqual(covdata.lines('zzz.py'), [])
     self.assertIsNone(covdata.lines('no_such_file.py'))
     self.assertEqual(covdata.arcs('zzz.py'), [])
     self.assertIsNone(covdata.arcs('no_such_file.py'))
Beispiel #6
0
 def test_set_query_contexts(self):
     covdata = CoverageData()
     covdata.set_context('test_a')
     covdata.add_lines(LINES_1)
     covdata.set_query_contexts(['test_*'])
     assert covdata.lines('a.py') == [1, 2]
     covdata.set_query_contexts(['other*'])
     assert covdata.lines('a.py') == []
Beispiel #7
0
 def test_lines_with_contexts(self):
     self.skip_unless_data_storage_is("sql")
     covdata = CoverageData()
     covdata.set_context('test_a')
     covdata.add_lines(LINES_1)
     self.assertEqual(covdata.lines('a.py'), [1, 2])
     self.assertEqual(covdata.lines('a.py', contexts=['test*']), [1, 2])
     self.assertEqual(covdata.lines('a.py', contexts=['other*']), [])
Beispiel #8
0
 def test_set_query_contexts(self):
     covdata = CoverageData()
     covdata.set_context('test_a')
     covdata.add_lines(LINES_1)
     covdata.set_query_contexts(['test_*'])
     self.assertEqual(covdata.lines('a.py'), [1, 2])
     covdata.set_query_contexts(['other*'])
     self.assertEqual(covdata.lines('a.py'), [])
Beispiel #9
0
 def test_no_arcs_vs_unmeasured_file(self):
     covdata = CoverageData()
     covdata.add_arcs(ARCS_3)
     covdata.touch_file('zzz.py')
     self.assertEqual(covdata.lines('zzz.py'), [])
     self.assertIsNone(covdata.lines('no_such_file.py'))
     self.assertEqual(covdata.arcs('zzz.py'), [])
     self.assertIsNone(covdata.arcs('no_such_file.py'))
Beispiel #10
0
 def test_no_arcs_vs_unmeasured_file(self):
     covdata = CoverageData()
     covdata.add_arcs(ARCS_3)
     covdata.touch_file('zzz.py')
     assert covdata.lines('zzz.py') == []
     assert covdata.lines('no_such_file.py') is None
     assert covdata.arcs('zzz.py') == []
     assert covdata.arcs('no_such_file.py') is None
Beispiel #11
0
 def test_adding_arcs(self):
     covdata = CoverageData()
     covdata.set_arcs(ARCS_3)
     self.assert_line_counts(covdata, SUMMARY_3)
     self.assert_measured_files(covdata, MEASURED_FILES_3)
     self.assertCountEqual(covdata.lines("x.py"), X_PY_LINES_3)
     self.assertCountEqual(covdata.arcs("x.py"), X_PY_ARCS_3)
     self.assertCountEqual(covdata.lines("y.py"), Y_PY_LINES_3)
     self.assertCountEqual(covdata.arcs("y.py"), Y_PY_ARCS_3)
     self.assertTrue(covdata.has_arcs())
Beispiel #12
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(), [])
Beispiel #13
0
 def test_no_duplicate_lines(self):
     covdata = CoverageData()
     covdata.set_context("context1")
     covdata.add_lines(LINES_1)
     covdata.set_context("context2")
     covdata.add_lines(LINES_1)
     self.assertEqual(covdata.lines('a.py'), A_PY_LINES_1)
Beispiel #14
0
 def test_no_duplicate_lines(self):
     covdata = CoverageData()
     covdata.set_context("context1")
     covdata.add_lines(LINES_1)
     covdata.set_context("context2")
     covdata.add_lines(LINES_1)
     self.assertEqual(covdata.lines('a.py'), A_PY_LINES_1)
Beispiel #15
0
 def test_adding_lines(self):
     covdata = CoverageData()
     covdata.set_lines(LINES_1)
     self.assert_line_counts(covdata, SUMMARY_1)
     self.assert_measured_files(covdata, MEASURED_FILES_1)
     self.assertCountEqual(covdata.lines("a.py"), A_PY_LINES_1)
     self.assertFalse(covdata.has_arcs())
Beispiel #16
0
    def test_combining_line_contexts(self):
        red_data, blue_data = self.run_red_blue()
        for datas in [[red_data, blue_data], [blue_data, red_data]]:
            combined = CoverageData(suffix="combined")
            for data in datas:
                combined.update(data)

            self.assertEqual(combined.measured_contexts(), {'red', 'blue'})

            full_names = {os.path.basename(f): f for f in combined.measured_files()}
            self.assertCountEqual(full_names, ['red.py', 'blue.py'])

            fred = full_names['red.py']
            fblue = full_names['blue.py']

            self.assertEqual(combined.lines(fred, context='red'), self.LINES)
            self.assertEqual(combined.lines(fred, context='blue'), [])
            self.assertEqual(combined.lines(fblue, context='red'), [])
            self.assertEqual(combined.lines(fblue, context='blue'), self.LINES)
    def get_code_coverage(self, cid, filemap=None):
        """Obtain code coverage data from a container.

        Containers can be programmed to collect code coverage from executed
        programs automatically. Our convention is to place coverage files in
        ``/coverage``.

        This method will fetch coverage files and parse them into data
        structures, which it will emit.

        If a ``filemap`` dict is passed, it will be used to map filenames
        inside the container to local filesystem paths. When present,
        files not inside the map will be ignored.
        """
        filemap = filemap or {}

        for member, fh in self.get_directory_contents(cid, '/coverage'):
            if not member.name.startswith('coverage.'):
                continue

            data = pickle.load(fh)

            c = CoverageData(basename=member.name,
                             collector=data.get('collector'))

            lines = {}
            for f, linenos in data.get('lines', {}).items():
                newname = filemap.get(f)
                if not newname:
                    # Ignore entries missing from map.
                    if filemap:
                        continue

                    newname = f

                lines[newname] = dict.fromkeys(linenos, None)

            arcs = {}
            for f, arcpairs in data.get('arcs', {}).items():
                newname = filemap.get(f)
                if not newname:
                    if filemap:
                        continue

                    newname = f

                arcs[newname] = dict.fromkeys(arcpairs, None)

            if not lines and not arcs:
                continue

            c.lines = lines
            c.arcs = arcs

            yield c
Beispiel #18
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(), [])
def test_fix_coverage(tmpdir):
    base_file = tmpdir.join('foo.py')
    base_file.ensure()
    sub_file = tmpdir.join('site-packages/foo.py')
    sub_file.ensure()
    unrelated_file = tmpdir.join('bar.py')
    unrelated_file.ensure()

    coverage_data = CoverageData(basename='.coverage.orig')
    coverage_data.add_arcs({
        str(base_file): {
            (1, 2): None
        },
        str(sub_file): {
            (3, 4): None
        },
        str(unrelated_file): {
            (5, 6): None
        },
    })

    assert coverage_data.lines(base_file) == [1, 2]
    assert coverage_data.lines(sub_file) == [3, 4]
    assert coverage_data.lines(unrelated_file) == [5, 6]

    new_coverage_data = merge_coverage(coverage_data, '/site-packages/',
                                       str(tmpdir))

    # The new file should contain all the lines and arcs
    assert new_coverage_data.lines(base_file) == [1, 2, 3, 4]
    assert new_coverage_data.arcs(base_file) == [(1, 2), (3, 4)]
    assert new_coverage_data.lines(unrelated_file) == [5, 6]
    assert new_coverage_data.arcs(unrelated_file) == [(5, 6)]

    # And it should not contain the original, un-merged names.
    assert sub_file not in new_coverage_data.measured_files()
#!/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)
Beispiel #21
0
 def test_no_lines_vs_unmeasured_file(self):
     covdata = CoverageData()
     covdata.add_lines(LINES_1)
     covdata.touch_file('zzz.py')
     assert covdata.lines('zzz.py') == []
     assert covdata.lines('no_such_file.py') is None
Beispiel #22
0
 def test_no_lines_vs_unmeasured_file(self):
     covdata = CoverageData()
     covdata.set_lines(LINES_1)
     covdata.touch_file('zzz.py')
     self.assertEqual(covdata.lines('zzz.py'), [])
     self.assertIsNone(covdata.lines('no_such_file.py'))
Beispiel #23
0
 def test_no_lines_vs_unmeasured_file(self):
     covdata = CoverageData()
     covdata.set_lines(LINES_1)
     covdata.touch_file('zzz.py')
     self.assertEqual(covdata.lines('zzz.py'), [])
     self.assertIsNone(covdata.lines('no_such_file.py'))