Exemplo n.º 1
0
def test_write_coverage():

    a = {
        'test1': {
            'a': [1]
        },
        'test2': {
            'a': [2]
        },
    }

    expected = {'lines': {os.path.abspath('a'): [1, 2]}}

    with tempdir() as base:
        path = os.path.join(base, '.coverage')

        cov = Coverage(data_file=path)

        smother = Smother(cov)
        smother.data = a
        smother.write_coverage()

        header_len = 63
        with open(path) as infile:
            infile.seek(header_len)
            result = json.load(infile)

        assert result == expected
Exemplo n.º 2
0
def test_convert_to_relative_paths():
    smother = Smother()
    smother.data = {'test1': {os.path.abspath('smother/tests/demo.py'): [8]}}

    expected_data = {'test1': {'smother/tests/demo.py': [8]}}

    assert Smother.convert_to_relative_paths(smother).data == expected_data
Exemplo n.º 3
0
 def configure(self, options, conf):
     super(SmotherNose, self).configure(options, conf)
     if self.enabled:
         self.first_test = True
         self.output = options.smother_output
         self.append = options.smother_append
         self.smother = Smother(self.coverInstance)
Exemplo n.º 4
0
class Plugin(object):
    def __init__(self, options):
        self.coverage = coverage.coverage(
            source=options.smother_source,
            config_file=options.smother_config,
        )

        # The unusual import statement placement is so that
        # smother's own test suite can measure coverage of
        # smother import statements
        self.coverage.start()
        from smother.control import Smother
        self.smother = Smother(self.coverage)

        self.output = options.smother_output
        self.append = options.smother_append
        self.cover_report = options.smother_cover
        self.first_test = True

    def pytest_runtest_setup(self, item):
        if self.first_test:
            self.first_test = False
            self.coverage.stop()
            self.smother.save_context("")
        self.smother.start()

    def pytest_runtest_teardown(self, item, nextitem):
        self.coverage.stop()
        self.smother.save_context(item.nodeid)

    def pytest_terminal_summary(self):
        self.smother.write(self.output, append=self.append)
        if self.cover_report:
            self.smother.write_coverage()
Exemplo n.º 5
0
class SmotherNose(Coverage):
    name = "smother"

    def afterTest(self, test):
        self.coverInstance.stop()
        self.smother.save_context("%s:%s" % test.address()[1:3])

    def beforeTest(self, test):

        # Save coverage from before first test as an unlabeled
        # context. This captures coverage during import.
        if self.first_test:
            self.coverInstance.stop()
            self.smother.save_context("")
            self.first_test = False

        self.smother.start()

    def configure(self, options, conf):
        super(SmotherNose, self).configure(options, conf)
        if self.enabled:
            self.first_test = True
            self.output = options.smother_output
            self.append = options.smother_append
            self.smother = Smother(self.coverInstance)

    def options(self, parser, env):
        super(Coverage, self).options(parser, env)
        parser.add_option("--smother-package",
                          action="append",
                          default=env.get('NOSE_COVER_PACKAGE'),
                          metavar="PACKAGE",
                          dest="cover_packages",
                          help="Restrict coverage output to selected packages "
                          "[NOSE_COVER_PACKAGE]")
        parser.add_option("--smother-erase",
                          action="store_true",
                          default=env.get('NOSE_COVER_ERASE'),
                          dest="cover_erase",
                          help="Erase previously collected coverage "
                          "statistics before run")
        parser.add_option("--smother-config-file",
                          action="store",
                          default=env.get('NOSE_COVER_CONFIG_FILE'),
                          dest="cover_config_file",
                          help="Location of coverage config file "
                          "[NOSE_COVER_CONFIG_FILE]")
        parser.add_option("--smother-output",
                          action="store",
                          default=env.get('NOSE_SMOTHER_OUTPUT', '.smother'),
                          dest="smother_output",
                          help="Location of output file")
        parser.add_option("--smother-append",
                          action="store_true",
                          default=env.get('NOSE_SMOTHER_APPEND'),
                          dest="smother_append",
                          help="Append to existing smother file")

    def report(self, stream):
        self.smother.write(self.output, append=self.append)
Exemplo n.º 6
0
class Plugin(object):

    def __init__(self, options):
        self.coverage = coverage.coverage(
            source=options.smother_source,
            config_file=options.smother_config,
        )

        # The unusual import statement placement is so that
        # smother's own test suite can measure coverage of
        # smother import statements
        self.coverage.start()
        from smother.control import Smother
        self.smother = Smother(self.coverage)

        self.output = options.smother_output
        self.append = options.smother_append
        self.cover_report = options.smother_cover
        self.first_test = True

    def pytest_runtest_setup(self, item):
        if self.first_test:
            self.first_test = False
            self.coverage.stop()
            self.smother.save_context("")
        self.smother.start()

    def pytest_runtest_teardown(self, item, nextitem):
        self.coverage.stop()
        self.smother.save_context(item.nodeid)

    def pytest_terminal_summary(self):
        self.smother.write(self.output, append=self.append)
        if self.cover_report:
            self.smother.write_coverage()
Exemplo n.º 7
0
def convert_to_relative_paths(src, dst):
    """
    Converts all file paths in a smother report to relative paths, relative
    to the current directory.
    """
    result = Smother.convert_to_relative_paths(Smother.load(src))
    result.write(dst)
Exemplo n.º 8
0
def test_iter_records_semantic():

    # Default coverage behavior is to emit absolute file paths.
    smother = Smother()
    smother.data = {'test1': {os.path.abspath('smother/tests/demo.py'): [8]}}

    expected = [('smother.tests.demo:foo', 'test1')]
    assert list(smother.iter_records(semantic=True)) == expected
Exemplo n.º 9
0
def test_iter_records_semantic():

    # Default coverage behavior is to emit absolute file paths.
    smother = Smother()
    smother.data = {
        'test1': {os.path.abspath('smother/tests/demo.py'): [8]}
    }

    expected = [('smother.tests.demo:foo', 'test1')]
    assert list(smother.iter_records(semantic=True)) == expected
Exemplo n.º 10
0
def test_append():
    a = {
        'test1': {'a': [1]},
    }

    b = {
        'test1': {'a': [2]},
        'test2': {'a': [3]}
    }

    combine = {
        'test1': {'a': [1, 2]},
        'test2': {'a': [3]},
    }

    with tempdir() as base:
        outpath = os.path.join(base, '.smother')

        smother = Smother()
        smother.data = a
        smother.write(outpath, append=True)

        smother = Smother()
        smother.data = b
        smother.write(outpath, append=True)

        assert Smother.load(outpath).data == combine
Exemplo n.º 11
0
class SmotherNose(Coverage):
    name = "smother"

    def afterTest(self, test):
        self.coverInstance.stop()
        self.smother.save_context("%s:%s" % test.address()[1:3])

    def beforeTest(self, test):

        # Save coverage from before first test as an unlabeled
        # context. This captures coverage during import.
        if self.first_test:
            self.coverInstance.stop()
            self.smother.save_context("")
            self.first_test = False

        self.smother.start()

    def configure(self, options, conf):
        super(SmotherNose, self).configure(options, conf)
        if self.enabled:
            self.first_test = True
            self.output = options.smother_output
            self.append = options.smother_append
            self.smother = Smother(self.coverInstance)

    def options(self, parser, env):
        super(Coverage, self).options(parser, env)
        parser.add_option("--smother-package", action="append",
                          default=env.get('NOSE_COVER_PACKAGE'),
                          metavar="PACKAGE",
                          dest="cover_packages",
                          help="Restrict coverage output to selected packages "
                          "[NOSE_COVER_PACKAGE]")
        parser.add_option("--smother-erase", action="store_true",
                          default=env.get('NOSE_COVER_ERASE'),
                          dest="cover_erase",
                          help="Erase previously collected coverage "
                          "statistics before run")
        parser.add_option("--smother-config-file", action="store",
                          default=env.get('NOSE_COVER_CONFIG_FILE'),
                          dest="cover_config_file",
                          help="Location of coverage config file "
                          "[NOSE_COVER_CONFIG_FILE]")
        parser.add_option("--smother-output", action="store",
                          default=env.get('NOSE_SMOTHER_OUTPUT', '.smother'),
                          dest="smother_output",
                          help="Location of output file")
        parser.add_option("--smother-append", action="store_true",
                          default=env.get('NOSE_SMOTHER_APPEND'),
                          dest="smother_append",
                          help="Append to existing smother file")

    def report(self, stream):
        self.smother.write(self.output, append=self.append)
Exemplo n.º 12
0
def test_convert_to_relative_paths():
    smother = Smother()
    smother.data = {
        'test1': {os.path.abspath('smother/tests/demo.py'): [8]}
    }

    expected_data = {
        'test1': {'smother/tests/demo.py': [8]}
    }

    assert Smother.convert_to_relative_paths(smother).data == expected_data
Exemplo n.º 13
0
def combine(ctx, src, dst):
    """
    Combine several smother reports.
    """
    c = coverage.Coverage(config_file=ctx.obj['rcfile'])
    result = Smother(c)

    for infile in src:
        result |= Smother.load(infile)

    result.write(dst)
Exemplo n.º 14
0
 def configure(self, options, conf):
     super(SmotherNose, self).configure(options, conf)
     if self.enabled:
         self.first_test = True
         self.output = options.smother_output
         self.append = options.smother_append
         self.smother = Smother(self.coverInstance)
Exemplo n.º 15
0
def to_coverage(ctx):
    """
    Produce a .coverage file from a smother file
    """
    sm = Smother.load(ctx.obj['report'])
    sm.coverage = coverage.coverage()
    sm.write_coverage()
Exemplo n.º 16
0
    def __init__(self, options):
        self.coverage = coverage.coverage(
            source=options.smother_source,
            config_file=options.smother_config,
        )

        # The unusual import statement placement is so that
        # smother's own test suite can measure coverage of
        # smother import statements
        self.coverage.start()
        from smother.control import Smother
        self.smother = Smother(self.coverage)

        self.output = options.smother_output
        self.append = options.smother_append
        self.cover_report = options.smother_cover
        self.first_test = True
Exemplo n.º 17
0
def csv(ctx, dst):
    """
    Flatten a coverage file into a CSV
    of source_context, testname
    """
    sm = Smother.load(ctx.obj['report'])
    semantic = ctx.obj['semantic']
    writer = _csv.writer(dst, lineterminator='\n')
    dst.write("source_context, test_context\n")
    writer.writerows(sm.iter_records(semantic=semantic))
Exemplo n.º 18
0
def test_write_coverage():

    a = {
        'test1': {'a': [1]},
        'test2': {'a': [2]},
    }

    expected = {'lines': {os.path.abspath('a'): [1, 2]}}

    with tempdir() as base:
        path = os.path.join(base, '.coverage')

        cov = Coverage(data_file=path)

        smother = Smother(cov)
        smother.data = a
        smother.write_coverage()

        header_len = 63
        with open(path) as infile:
            infile.seek(header_len)
            result = json.load(infile)

        assert result == expected
Exemplo n.º 19
0
    def __init__(self, options):
        self.coverage = coverage.coverage(
            source=options.smother_source,
            config_file=options.smother_config,
        )

        # The unusual import statement placement is so that
        # smother's own test suite can measure coverage of
        # smother import statements
        self.coverage.start()
        from smother.control import Smother
        self.smother = Smother(self.coverage)

        self.output = options.smother_output
        self.append = options.smother_append
        self.cover_report = options.smother_cover
        self.first_test = True
Exemplo n.º 20
0
def test_append():
    a = {
        'test1': {
            'a': [1]
        },
    }

    b = {'test1': {'a': [2]}, 'test2': {'a': [3]}}

    combine = {
        'test1': {
            'a': [1, 2]
        },
        'test2': {
            'a': [3]
        },
    }

    with tempdir() as base:
        outpath = os.path.join(base, '.smother')

        smother = Smother()
        smother.data = a
        smother.write(outpath, append=True)

        smother = Smother()
        smother.data = b
        smother.write(outpath, append=True)

        assert Smother.load(outpath).data == combine
Exemplo n.º 21
0
def _report_from_regions(regions, opts, **kwargs):
    report_file = opts['report']
    smother = Smother.load(report_file)
    result = smother.query_context(regions, **kwargs)
    result.report()