def test_end_comment(): """ Make sure that we can parse files with '#' at the very end. """ import figleaf filename = os.path.join(thisdir, "tst_end_comment.py") figleaf.get_lines(open(filename))
def build_python_coverage_info(coverage, exclude_patterns, files_list): keys = coverage.keys() line_info = {} for pyfile in filter_files(keys, exclude_patterns, files_list): try: fp = open(pyfile, 'rU') lines = figleaf.get_lines(fp) except KeyboardInterrupt: raise except IOError: logger.error('CANNOT OPEN: %s' % (pyfile,)) continue except Exception, e: logger.error('ERROR: file %s, exception %s' % (pyfile, str(e))) continue # retrieve the coverage and merge into a realpath-based filename. covered = coverage.get(pyfile, set()) realpath = os.path.realpath(pyfile) # be careful not to overwrite existing coverage for different # filenames that may have been canonicalized. (old_l, old_c) = line_info.get(realpath, (set(), set())) lines.update(old_l) covered.update(old_c) line_info[realpath] = (lines, covered)
def report_as_html(coverage, directory, exclude_patterns, files_list): """ Write an HTML report on all of the files, plus a summary. """ ### now, output. keys = coverage.keys() info_dict = {} for pyfile in filter_files(keys, exclude_patterns, files_list): try: fp = open(pyfile, 'rU') lines = figleaf.get_lines(fp) except KeyboardInterrupt: raise except IOError: logger.error('CANNOT OPEN: %s' % (pyfile,)) continue except Exception, e: logger.error('ERROR: file %s, exception %s' % (pyfile, str(e))) continue # # ok, we want to annotate this file. now annotate file ==> html. # # initialize covered = coverage.get(pyfile, set()) # rewind fp.seek(0) # annotate output, n_covered, n_lines, percent = annotate_file(fp, lines, covered) # summarize info_dict[pyfile] = (n_lines, n_covered, percent) # write out the file html_outfile = make_html_filename(pyfile) html_outfile = os.path.join(directory, html_outfile) html_outfp = open(html_outfile, 'w') html_outfp.write('source file: <b>%s</b><br>\n' % (pyfile,)) html_outfp.write(''' file stats: <b>%d lines, %d executed: %.1f%% covered</b> <pre> %s </pre> ''' % (n_lines, n_covered, percent, "\n".join(output))) html_outfp.close() logger.info('reported on %s' % (pyfile,))
def get_all_lines_from_path(self, path): if '<doctest' in path: return set() if not os.path.exists(path): return set() import figleaf try: return figleaf.get_lines(open(path)) except: return set()
def _getFileCoverage(self, filename, coverageMap=None): """Return coverage information about file specified by filename. """ if coverageMap is None: coverageMap = figleaf.get_info() num_total, num_covered = 0, 0, lines = figleaf.get_lines(open(filename)) for i, line in enumerate(open(filename)): if (i + 1) in coverageMap[filename]: num_total += 1 num_covered += 1 elif (i + 1) in lines: num_total += 1 return num_covered, num_total
def report_as_cover( coverage, exclude_patterns=[], ): ### now, output. keys = coverage.keys() info_dict = {} for k in filter_files(keys): try: pyfile = open(k, 'rU') lines = figleaf.get_lines(pyfile) except IOError: logger.warning('CANNOT OPEN: %s' % k) continue except KeyboardInterrupt: raise except Exception, e: logger.error('ERROR: file %s, exception %s' % (pyfile, str(e))) continue # ok, got all the info. now annotate file ==> html. covered = coverage[k] pyfile = open(k) (n_covered, n_lines, output) = make_cover_lines(lines, covered, pyfile) try: pcnt = n_covered * 100. / n_lines except ZeroDivisionError: pcnt = 100 info_dict[k] = (n_lines, n_covered, pcnt) outfile = make_cover_filename(k) try: outfp = open(outfile, 'w') outfp.write("\n".join(output)) outfp.write("\n") outfp.close() except IOError: logger.warning('cannot open filename %s' % (outfile, )) continue logger.info('reported on %s' % (outfile, ))
def report_as_cover(coverage, exclude_patterns=[], ): ### now, output. keys = coverage.keys() info_dict = {} for k in filter_files(keys): try: pyfile = open(k, 'rU') lines = figleaf.get_lines(pyfile) except IOError: logger.warning('CANNOT OPEN: %s' % k) continue except KeyboardInterrupt: raise except Exception, e: logger.error('ERROR: file %s, exception %s' % (pyfile, str(e))) continue # ok, got all the info. now annotate file ==> html. covered = coverage[k] pyfile = open(k) (n_covered, n_lines, output) = make_cover_lines(lines, covered, pyfile) try: pcnt = n_covered * 100. / n_lines except ZeroDivisionError: pcnt = 100 info_dict[k] = (n_lines, n_covered, pcnt) outfile = make_cover_filename(k) try: outfp = open(outfile, 'w') outfp.write("\n".join(output)) outfp.write("\n") outfp.close() except IOError: logger.warning('cannot open filename %s' % (outfile,)) continue logger.info('reported on %s' % (outfile,))
from figleaf import get_lines coverage = xmlio.Fragment() try: fileobj = open(ctxt.resolve(summary)) except IOError, e: log.warning('Error opening coverage summary file (%s)', e) return coverage_data = pickle.load(fileobj) fileset = FileSet(ctxt.basedir, include, exclude) for filename in fileset: base, ext = os.path.splitext(filename) if ext != '.py': continue modname = base.replace(os.path.sep, '.') realfilename = ctxt.resolve(filename) interesting_lines = get_lines(open(realfilename)) covered_lines = coverage_data.get(realfilename, set()) percentage = int(round(len(covered_lines) * 100 / len(interesting_lines))) line_hits = [] for lineno in xrange(1, max(interesting_lines)+1): if lineno not in interesting_lines: line_hits.append('-') elif lineno in covered_lines: line_hits.append('1') else: line_hits.append('0') module = xmlio.Element('coverage', name=modname, file=filename.replace(os.sep, '/'), percentage=percentage, lines=len(interesting_lines), line_hits=' '.join(line_hits))
def report_as_html(coverage, directory, exclude_patterns=[], root=None): ### now, output. keys = coverage.keys() info_dict = {} for k in keys: skip = False for pattern in exclude_patterns: if pattern.search(k): logger.debug('SKIPPING %s -- matches exclusion pattern' % k) skip = True break if skip: continue if k.endswith('figleaf.py'): continue display_filename = k if root: if not k.startswith(root): continue display_filename = k[len(root):] assert not display_filename.startswith("/") assert display_filename.endswith(".py") display_filename = display_filename[:-3] # trim .py display_filename = display_filename.replace("/", ".") if not k.startswith("/"): continue try: pyfile = open(k) # print 'opened', k except IOError: logger.warning('CANNOT OPEN: %s' % k) continue try: lines = figleaf.get_lines(pyfile) except KeyboardInterrupt: raise except Exception, e: pyfile.close() logger.warning('ERROR: %s %s' % (k, str(e))) continue # ok, got all the info. now annotate file ==> html. covered = coverage[k] n_covered = n_lines = 0 pyfile = open(k) output = [] for i, line in enumerate(pyfile): is_covered = False is_line = False i += 1 if i in covered: is_covered = True n_covered += 1 n_lines += 1 elif i in lines: is_line = True n_lines += 1 color = 'black' if is_covered: color = 'green' elif is_line: color = 'red' line = escape_html(line.rstrip()) output.append('<font color="%s">%4d. %s</font>' % (color, i, line.rstrip())) try: pcnt = n_covered * 100. / n_lines except ZeroDivisionError: pcnt = 0 info_dict[k] = (n_lines, n_covered, pcnt, display_filename) html_outfile = make_html_filename(display_filename) html_outfp = open(os.path.join(directory, html_outfile), 'w') html_outfp.write('source file: <b>%s</b><br>\n' % (k,)) html_outfp.write('file stats: <b>%d lines, %d executed: %.1f%% covered</b>\n' % (n_lines, n_covered, pcnt)) html_outfp.write('<pre>\n') html_outfp.write("\n".join(output)) html_outfp.close()
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 examine_source(filename): f = open(filename, "r") lines = figleaf.get_lines(f) f.close() return lines