2009-03-30 06:10:24 +08:00
|
|
|
import py
|
|
|
|
|
|
|
|
class FigleafPlugin:
|
|
|
|
def pytest_addoption(self, parser):
|
|
|
|
group = parser.addgroup('figleaf options')
|
|
|
|
group.addoption('-F', action='store_true', default=False,
|
|
|
|
dest = 'figleaf',
|
2009-04-01 01:58:02 +08:00
|
|
|
help=('trace coverage with figleaf and write HTML '
|
|
|
|
'for files below the current working dir'))
|
2009-03-30 06:10:24 +08:00
|
|
|
group.addoption('--figleaf-data', action='store', default='.figleaf',
|
|
|
|
dest='figleafdata',
|
|
|
|
help='path coverage tracing file.')
|
|
|
|
group.addoption('--figleaf-html', action='store', default='html',
|
|
|
|
dest='figleafhtml',
|
|
|
|
help='path to the coverage html dir.')
|
|
|
|
|
|
|
|
def pytest_configure(self, config):
|
|
|
|
if config.getvalue('figleaf'):
|
|
|
|
try:
|
|
|
|
import figleaf
|
|
|
|
import figleaf.annotate_html
|
|
|
|
except ImportError:
|
|
|
|
raise config.Error('Could not import figleaf module')
|
|
|
|
self.figleaf = figleaf
|
|
|
|
self.figleaf.start()
|
|
|
|
|
|
|
|
def pytest_terminal_summary(self, terminalreporter):
|
|
|
|
if hasattr(self, 'figleaf'):
|
2009-04-01 01:58:02 +08:00
|
|
|
config = terminalreporter.config
|
|
|
|
datafile = py.path.local(config.getvalue('figleafdata'))
|
2009-03-30 06:10:24 +08:00
|
|
|
tw = terminalreporter._tw
|
|
|
|
tw.sep('-', 'figleaf')
|
2009-03-30 07:12:41 +08:00
|
|
|
tw.line('Writing figleaf data to %s' % (datafile))
|
2009-03-30 06:10:24 +08:00
|
|
|
self.figleaf.stop()
|
2009-03-30 07:12:41 +08:00
|
|
|
self.figleaf.write_coverage(str(datafile))
|
2009-04-01 01:58:02 +08:00
|
|
|
coverage = self.get_coverage(datafile, config)
|
2009-03-30 06:10:24 +08:00
|
|
|
|
2009-04-01 01:58:02 +08:00
|
|
|
reportdir = py.path.local(config.getvalue('figleafhtml'))
|
2009-03-30 06:10:24 +08:00
|
|
|
tw.line('Writing figleaf html to file://%s' % (reportdir))
|
|
|
|
self.figleaf.annotate_html.prepare_reportdir(str(reportdir))
|
2009-03-30 07:12:41 +08:00
|
|
|
exclude = []
|
2009-03-30 06:10:24 +08:00
|
|
|
self.figleaf.annotate_html.report_as_html(coverage,
|
|
|
|
str(reportdir), exclude, {})
|
|
|
|
|
2009-04-01 01:58:02 +08:00
|
|
|
def get_coverage(self, datafile, config):
|
|
|
|
# basepath = config.topdir
|
|
|
|
basepath = py.path.local()
|
2009-03-30 07:12:41 +08:00
|
|
|
data = self.figleaf.read_coverage(str(datafile))
|
|
|
|
d = {}
|
|
|
|
coverage = self.figleaf.combine_coverage(d, data)
|
|
|
|
for path in coverage.keys():
|
2009-04-01 01:58:02 +08:00
|
|
|
if not py.path.local(path).relto(basepath):
|
2009-03-30 07:12:41 +08:00
|
|
|
del coverage[path]
|
|
|
|
return coverage
|
|
|
|
|
|
|
|
|
2009-03-30 06:40:44 +08:00
|
|
|
def test_generic(plugintester):
|
2009-05-08 00:01:53 +08:00
|
|
|
plugintester.hookcheck(FigleafPlugin)
|
2009-03-30 06:10:24 +08:00
|
|
|
|
2009-03-30 06:40:44 +08:00
|
|
|
def test_functional(testdir):
|
2009-04-02 21:57:42 +08:00
|
|
|
py.test.importorskip("figleaf")
|
2009-03-30 06:40:44 +08:00
|
|
|
testdir.plugins.append('figleaf')
|
|
|
|
testdir.makepyfile("""
|
|
|
|
def f():
|
|
|
|
x = 42
|
|
|
|
def test_whatever():
|
|
|
|
pass
|
|
|
|
""")
|
|
|
|
result = testdir.runpytest('-F')
|
|
|
|
assert result.ret == 0
|
|
|
|
assert result.stdout.fnmatch_lines([
|
|
|
|
'*figleaf html*'
|
|
|
|
])
|
2009-04-01 01:58:02 +08:00
|
|
|
#print result.stdout.str()
|