def test_noexclude_stdlib(self): """Check that stdlib files are covered if not excluded. Only works for python >= 2.5, because of some changes in the way frame objs f_code.co_filename is reported. """ if version_string in ('2.3', '2.4'): # CTB return figleaf.start(ignore_python_lib=False) os.path.dirname('/some/weird/path') # use some stdlib code figleaf.stop() coverage = figleaf.get_data().gather_files() print 'sysdir is:', sysdir found = False for k in coverage: print 'checking:', k if k.startswith(sysdir): found = True break assert found
def stopCoverage(): if figleaf_started is not None: figleaf.stop() figleaf.write_coverage('.figleaf') print "Figleaf stats were written to .figleaf" else: print "Warning: NO coverage written as the debugger was active"
def generate_coverage_report(self): """ Generates a coverage report in HTML format. Returns the absolute path to the report file. Note that it is generated in a temp directory, so be sure to either move or delete the report. """ if not has_figleaf: return figleaf.stop() tempdir = tempfile.mkdtemp() coverage_file = os.path.join(tempdir, "coverage.txt") logging.info("Writing coverage to %s" % coverage_file) logging.info("coverage info = %r" % figleaf.get_data()) figleaf.write_coverage(coverage_file) coverage = {} d = figleaf.read_coverage(coverage_file) coverage_data = figleaf.combine_coverage(coverage, d) logging.info("Preparing to write report...") report_dir = os.path.join(tempdir, "figleaf-html-report") if not os.path.exists(report_dir): os.makedirs(report_dir) html_report.report_as_html(coverage_data, report_dir, [ re.compile(".*site-packages.*"), re.compile(".*pubsub.*"), re.compile(".*pew/.*") ], {}) logging.info("Writing report to %s" % report_dir) return os.path.join(report_dir, "index.html")
def finalize(self, result): """ Finalize: stop recording coverage info, save & exit. """ figleaf.stop() figleaf.write_coverage(self.figleaf_file, append=False)
def _run_with_figleaf(self): import figleaf figleaf.start() try: self._run_tests() finally: figleaf.stop() figleaf.write_coverage(self.coverage_summary)
def run(self): if self.coverage: import figleaf figleaf.start() nose.run(argv=['nosetests']) if self.coverage: figleaf.stop() figleaf.write_coverage('.figleaf')
def finalize(self, result): """ Finalize: stop recording coverage info, save & exit. """ figleaf.stop() fp = open(self.figleaf_file, 'w') figleaf.dump_pickled_coverage(fp) fp.close()
def pytest_sessionfinish(session, exitstatus): # pylint: disable=W0613 """ Write out figleaf coverage report for the test session. """ figleaf.stop() filepath_coverage_log = session.config.known_args_namespace.coverage_log with open(filepath_coverage_log, 'wb') as file: figleaf.dump_pickled_coverage(file)
def test(): figleaf.start() common() figleaf.start_section('a') a() figleaf.stop_section() common2() figleaf.start_section('b') b() figleaf.start_section('c') c() figleaf.stop() in_common = set() union = set() expected = dict(a = set([17, 18, 11, 14]), b = set([22, 11, 21, 14]), c = set([25, 26, 11, 14])) for i in ('a', 'b', 'c'): actual = figleaf.get_info(section_name=i).get(thisfile()) print '******', thisfile() print 'section:', i, '; lines:', actual print expected[i] x = list(expected[i]) x.sort() y = list(actual) y.sort() print x, y assert x == y # note: depends on absolute line no. # so don't shift lines in this file ;) if not in_common: in_common.update(actual) else: in_common.intersection_update(actual) union.update(actual) in_common = list(in_common) in_common.sort() print 'common:', in_common assert in_common == [11, 14] union = list(union) union.sort() print 'union:', union assert union == [11, 14, 17, 18, 21, 22, 25, 26]
def run_tests(test_labels, verbosity=1, interactive=True, extra_tests=[]): setup_test_environment() figleaf.start() test_results = django_test_runner(test_labels, verbosity, interactive, extra_tests) figleaf.stop() if not os.path.isdir(os.path.join("temp", "figleaf")): os.mkdir(os.path.join("temp", "figleaf")) file_name = "temp/figleaf/test_output.figleaf" figleaf.write_coverage(file_name) output = commands.getoutput("figleaf2html " + file_name + " --output-directory=temp/figleaf") print output return test_results
def test_noexclude_misc_path(self): "Check that tests/tst_exclude1.py is covered, if no excl specified" figleaf.init(None, None) figleaf.start() execfile('tests/tst_exclude1.py') figleaf.stop() coverage = figleaf.get_data().gather_files() assert 'tests/tst_exclude1.py' in coverage.keys()
def stopCoverage (self): try: import figleaf figleaf.stop() self._log.info("Coverage tracking stopped") path = "/opt/ofelia/ofam/local/log/figleaf.%f.log" % (time.time()) figleaf.write_coverage(path) self._log.info("Coverage written to %s" % (path)) return jsonify({"output-path" : path}) except Exception, e: self._log.exception("Exception") return jsonify(None, code = 2, msg = traceback.format_exc())
def stopCoverage(self): try: import figleaf figleaf.stop() self._log.info("Coverage tracking stopped") path = "/opt/ofelia/ofam/local/log/figleaf.%f.log" % (time.time()) figleaf.write_coverage(path) self._log.info("Coverage written to %s" % (path)) return jsonify({"output-path": path}) except Exception, e: self._log.exception("Exception") return jsonify(None, code=2, msg=traceback.format_exc())
def stop(self): """Stops code coverage.""" try: if self.running: logger.info('Stopping code coverage') figleaf.stop() figleaf.write_coverage(self.filename) self.running = False for k in ['FIGLEAF_DIR', 'USE_FIGLEAF', 'FIGLEAF_PID']: del ExecutionContext.propagate_env_map[k] except Exception, e: logger.error('Error stopping code coverage: %s' % e)
def test_include_misc_path(self): "Check that tests/tst_include1.py is the only thing covered, if set" figleaf.init(None, 'tests') figleaf.start() execfile('tests/tst_include1.py') figleaf.stop() coverage = figleaf.get_data().gather_files() assert 'tests/tst_include1.py' in coverage.keys() assert len(coverage) == 1
def cover_program(self, program_name): import figleaf cur_trace = sys.gettrace() if os.path.exists('.figleaf_test'): os.remove('.figleaf_test') figleaf.start() self.run_program(program_name) figleaf.stop() figleaf.write_coverage('.figleaf_test', append=False) if hasattr(cur_trace, '__call__'): sys.settrace(cur_trace) elif hasattr(cur_trace, 'start'): cur_trace.start()
def main(test_dict, test_order): figleaf.start() try: for test in test_order: print '[+]', test_dict[test] Tester(test) except Exception as e: return False else: return True finally: figleaf.stop() figleaf.write_coverage('.figleaf')
def cover_program(self, program_name): import figleaf cur_trace = sys.gettrace() if os.path.exists(".figleaf_test"): os.remove(".figleaf_test") figleaf.start() self.run_program(program_name) figleaf.stop() figleaf.write_coverage(".figleaf_test", append=False) if hasattr(cur_trace, "__call__"): sys.settrace(cur_trace) elif hasattr(cur_trace, "start"): cur_trace.start()
def run_tests(test_labels, verbosity=1, interactive=True, extra_tests=[]): setup_test_environment() figleaf.start() test_results = django_test_runner(test_labels, verbosity, interactive, extra_tests) figleaf.stop() if not os.path.isdir(os.path.join("temp", "figleaf")): os.makedirs(os.path.join("temp", "figleaf")) file_name = "temp/figleaf/test_output.figleaf" figleaf.write_coverage(file_name) output = commands.getoutput("figleaf2html " + file_name + " --output-directory=temp/figleaf") print output return test_results
def pytest_terminal_summary(terminalreporter): config = terminalreporter.config datafile = py.path.local(config.getvalue('figleafdata')) tw = terminalreporter._tw tw.sep('-', 'figleaf') tw.line('Writing figleaf data to %s' % (datafile)) figleaf.stop() figleaf.write_coverage(str(datafile)) coverage = get_coverage(datafile, config) reportdir = py.path.local(config.getvalue('figleafhtml')) tw.line('Writing figleaf html to file://%s' % (reportdir)) figleaf.annotate_html.prepare_reportdir(str(reportdir)) exclude = [] figleaf.annotate_html.report_as_html(coverage, str(reportdir), exclude, {})
def test_exclude_misc_path(self): "Check that tests/tst_exclude1.py is not covered, if excl specified" testspath = os.path.abspath('tests/') print 'IGNORING', testspath figleaf.init(testspath, None) figleaf.start() execfile('tests/tst_exclude1.py') figleaf.stop() coverage = figleaf.get_data().gather_files() print coverage.keys() assert not 'tests/tst_exclude1.py' in coverage.keys()
def post_run(self, runner): """ stops figleaf and returns a report """ figleaf.stop() coverageOutput = figleaf.get_data().gather_files() self.coverageDir = os.path.join(runner.setupDir, 'report', 'pycoverage') # check if there's a dir first if not os.path.exists(self.coverageDir): os.mkdir(self.coverageDir) figleaf.annotate_html.report_as_html(coverageOutput, self.coverageDir, [], {}) # open a browser window with the report openBrowser(runner.config.getBrowserPath(), os.path.join( self.coverageDir, "index.html"))
def test_exclude_stdlib(self): "Check that stdlib files are not covered if excluded." if version_string in ('2.3', '2.4'): # CTB return ### first check that they would be covered ordinarily! figleaf.start(ignore_python_lib=False) os.path.dirname('/some/weird/path') # use some stdlib code figleaf.stop() coverage = figleaf.get_data().gather_files() print 'sysdir is:', sysdir found = False for k in coverage: print 'checking:', k if k.startswith(sysdir): found = True break assert found # this will fail in 2.3, 2.4 ### ok, now check that they're ignored properly. figleaf._t = None # RESET figleaf.start(ignore_python_lib=True) os.path.dirname('/some/weird/path') # use some stdlib code figleaf.stop() coverage = figleaf.get_data().gather_files() for k in coverage: # posixpath & threading somehow evade the discrimination!? if k.startswith(sysdir) and (k.endswith('posixpath.py') or k.endswith('threading.py')): continue assert not k.startswith(sysdir), '%s starts with %s' % (k, sysdir,)
def generate_coverage(func, path, *args, **kwds): """ Generates code coverage for the function and places the results in the path """ import figleaf from figleaf import annotate_html # Fix for figleaf misbehaving. It is adding a logger at root level # and that will add a handler to all subloggers (ours as well) # needs to be fixed in figleaf import logging root = logging.getLogger() # remove all root handlers for hand in root.handlers: root.removeHandler(hand) if os.path.isdir(path): shutil.rmtree(path) info("collecting coverage information") figleaf.start() # execute the function itself return_vals = func(*args, **kwds) figleaf.stop() info('generating coverage') coverage = figleaf.get_data().gather_files() annotate_html.prepare_reportdir(path) # skip python modules and the test modules regpatt = lambda patt: re.compile(patt, re.IGNORECASE) patterns = map(regpatt, ['python', 'tests', 'django', 'path*']) annotate_html.report_as_html(coverage, path, exclude_patterns=patterns, files_list='') return return_vals
def test_dos_eol(): """ Make sure that we can annotate files with DOS EOL characters in them. """ import figleaf, figleaf.annotate_html figleaf.start() execfile(os.path.join(thisdir, "tst_dos_eol.py")) figleaf.stop() coverage = figleaf.get_data().gather_files() tmpdir = tempfile.mkdtemp(".figleaf") try: figleaf.annotate_html.report_as_html(coverage, tmpdir, [], {}) finally: files = glob.glob("%s/*" % (tmpdir,)) for f in files: os.unlink(f) os.rmdir(tmpdir)
def generate_coverage( func, path, *args, **kwds): """ Generates code coverage for the function and places the results in the path """ import figleaf from figleaf import annotate_html # Fix for figleaf misbehaving. It is adding a logger at root level # and that will add a handler to all subloggers (ours as well) # needs to be fixed in figleaf import logging root = logging.getLogger() # remove all root handlers for hand in root.handlers: root.removeHandler(hand) if os.path.isdir( path ): shutil.rmtree( path ) info( "collecting coverage information" ) figleaf.start() # execute the function itself return_vals = func( *args, **kwds) figleaf.stop() info( 'generating coverage' ) coverage = figleaf.get_data().gather_files() annotate_html.prepare_reportdir( path ) # skip python modules and the test modules regpatt = lambda patt: re.compile( patt, re.IGNORECASE ) patterns = map( regpatt, [ 'python', 'tests', 'django', 'path*' ] ) annotate_html.report_as_html( coverage, path, exclude_patterns=patterns, files_list='') return return_vals
def test_exclude_stdlib(self): "Check that stdlib files are not covered if excluded." if version_string in ('2.3', '2.4'): # CTB return ### first check that they would be covered ordinarily! figleaf.start(ignore_python_lib=False) os.path.dirname('/some/weird/path') # use some stdlib code figleaf.stop() coverage = figleaf.get_data().gather_files() found = False for k in coverage: print k if k.startswith(sysdir): found = True break assert found # this will fail in 2.3, 2.4 ### ok, now check that they're ignored properly. figleaf._t = None # RESET figleaf.start(ignore_python_lib=True) os.path.dirname('/some/weird/path') # use some stdlib code figleaf.stop() coverage = figleaf.get_data().gather_files() for k in coverage: assert not k.startswith(sysdir)
def run_tests(test_labels, verbosity=1, interactive=True, extra_tests=[]): figleaf.start() total_tests = simple.run_tests(test_labels, verbosity, interactive, extra_tests) figleaf.stop() figleaf.write_coverage('.figleaf') return total_tests
from glob import glob from doctest import testmod from sys import modules import figleaf files = glob('datagrid/*.py') + glob('datagrid/*/*.py') figleaf.start() for f in files: name = f.replace('.py','').replace('/','.') __import__(name) testmod(modules[name]) import tests.runner figleaf.stop() figleaf.write_coverage('.figleaf')
error = None result = None figleaf.start() try: if hasattr(handler, "process"): result = handler(args, remoteHost, session).process() else: result = handler(args, remoteHost, session) except Exception, e: error = "%s: %s" % (e.__class__, str(e)) results[methodName] = { 'parameters': args, 'result': result, 'error': error } coverage = figleaf.get_info() fData = cPickle.dumps(coverage) figleaf.stop() return {"results": results, "figLeafData": fData} methodMap = {"login": SystemLogin, "echo": echo, "runCoverage": runCoverage}
def close_figleaf(): import figleaf print "Figleaf cleaning up" figleaf.stop() figleaf.write_coverage('.unittest_coverage', append=False)
def OnExit(self): if has_figleaf: figleaf.stop() figleaf.write_coverage("eclass_library_run.figleaf") return True #wx.PySimpleApp.OnExit(self)
def teardown(): os.chdir(cwd) sys.path.remove(testdir) figleaf.stop() figleaf._t = None
def tearDown(self): figleaf.stop()
def compare_coverage(filename): print filename fullpath = os.path.abspath(os.path.join(testdir, filename)) ### run file & record coverage maindict = {} maindict.update(__main__.__dict__) figleaf.start() try: execfile(fullpath, maindict) except: pass figleaf.stop() d = figleaf.get_data().gather_files() coverage_info = d.get(fullpath, None) ### ok, now get the interesting lines from our just-executed python script f1 = open(fullpath) SYNTAX_ERROR = False try: line_info = figleaf.get_lines(f1) except SyntaxError: SYNTAX_ERROR = True # not supported in this ver? ### load in the previously annotated lines coverage_file = filename + '.cover.' + version_string try: f2 = open(os.path.join(testdir, coverage_file)) assert not SYNTAX_ERROR except IOError: assert SYNTAX_ERROR # it's ok, skip this test return ########## No syntax error, check aginst previously annotated stuff. actual_lines = f2.readlines() f2.close() ### annotate lines with coverage style 'cover' f1.seek(0) (_, _, output) = figleaf.annotate_cover.make_cover_lines(line_info, coverage_info, f1) ### compare! f1.close() for (i, (check_line, recorded_line)) in enumerate(zip(output, actual_lines)): check_line = check_line.strip() recorded_line = recorded_line.strip() assert check_line == recorded_line, "regression mismatch, file %s:%d\ncurrent '%s'\nactual '%s'\n" % (fullpath, i, check_line, recorded_line,)
def tearDown(self): figleaf.stop() figleaf._t = None