示例#1
0
    def opt_coverage(self):
        """
        Generate coverage information in the _trial_temp/coverage. Requires
        Python 2.3.3.
        """
        coverdir = 'coverage'
        print "Setting coverage directory to %s." % (coverdir, )
        import trace

        # begin monkey patch ---------------------------
        #   Before Python 2.4, this function asserted that 'filename' had
        #   to end with '.py'  This is wrong for at least two reasons:
        #   1.  We might be wanting to find executable line nos in a script
        #   2.  The implementation should use os.splitext
        #   This monkey patch is the same function as in the stdlib (v2.3)
        #   but with the assertion removed.
        def find_executable_linenos(filename):
            """Return dict where keys are line numbers in the line number
            table.
            """
            #assert filename.endswith('.py') # YOU BASTARDS
            try:
                prog = open(filename).read()
                prog = '\n'.join(prog.splitlines()) + '\n'
            except IOError, err:
                sys.stderr.write("Not printing coverage data for %r: %s\n" %
                                 (filename, err))
                sys.stderr.flush()
                return {}
            code = compile(prog, filename, "exec")
            strs = trace.find_strings(filename)
            return trace.find_lines(code, strs)
    def opt_coverage(self):
        """
        Generate coverage information in the _trial_temp/coverage. Requires
        Python 2.3.3.
        """
        coverdir = 'coverage'
        print "Setting coverage directory to %s." % (coverdir,)
        import trace

        # begin monkey patch ---------------------------
        #   Before Python 2.4, this function asserted that 'filename' had
        #   to end with '.py'  This is wrong for at least two reasons:
        #   1.  We might be wanting to find executable line nos in a script
        #   2.  The implementation should use os.splitext
        #   This monkey patch is the same function as in the stdlib (v2.3)
        #   but with the assertion removed.
        def find_executable_linenos(filename):
            """Return dict where keys are line numbers in the line number
            table.
            """
            #assert filename.endswith('.py') # YOU BASTARDS
            try:
                prog = open(filename).read()
                prog = '\n'.join(prog.splitlines()) + '\n'
            except IOError, err:
                sys.stderr.write("Not printing coverage data for %r: %s\n"
                                 % (filename, err))
                sys.stderr.flush()
                return {}
            code = compile(prog, filename, "exec")
            strs = trace.find_strings(filename)
            return trace.find_lines(code, strs)
示例#3
0
 def find_source_lines(self):
     """Mark all executable source lines in fn as executed 0 times."""
     strs = trace.find_strings(self.filename)
     lines = trace.find_lines_from_code(self.fn.__code__, strs)
     self.firstcodelineno = sys.maxsize
     for lineno in lines:
         self.firstcodelineno = min(self.firstcodelineno, lineno)
         self.sourcelines.setdefault(lineno, 0)
     if self.firstcodelineno == sys.maxsize:
         self.firstcodelineno = self.firstlineno
示例#4
0
 def find_source_lines(self):
     """Mark all executable source lines in fn as executed 0 times."""
     strs = trace.find_strings(self.filename)
     lines = trace.find_lines_from_code(self.fn.__code__, strs)
     self.firstcodelineno = sys.maxsize
     for lineno in lines:
         self.firstcodelineno = min(self.firstcodelineno, lineno)
         self.sourcelines.setdefault(lineno, 0)
     if self.firstcodelineno == sys.maxsize:
         self.firstcodelineno = self.firstlineno
 def find_source_lines(self):
     """Mark all executable source lines in fn as executed 0 times."""
     if self.filename is None:
         return
     strs = trace.find_strings(self.filename)
     lines = trace.find_lines_from_code(self.fn.__code__, strs)
     for lineno in lines:
         self.sourcelines.setdefault(lineno, 0)
     if lines:
         self.firstcodelineno = min(lines)
     else:
         self.firstcodelineno = self.firstlineno
示例#6
0
 def find_source_lines(self):
     """Mark all executable source lines in fn as executed 0 times."""
     if self.filename is None:
         return
     strs = trace.find_strings(self.filename)
     lines = trace.find_lines_from_code(self.fn.__code__, strs)
     for lineno in lines:
         self.sourcelines.setdefault(lineno, 0)
     if lines:
         self.firstcodelineno = min(lines)
     else:
         self.firstcodelineno = self.firstlineno
示例#7
0
 def find_source_lines(self):
     """Mark all executable source lines in fn as executed 0 times."""
     if self.filename is None:
         return
     strs = trace.find_strings(self.filename)
     lines = trace.find_lines_from_code(self.fn.__code__, strs)
     for lineno in lines:
         self.sourcelines.setdefault(lineno, 0)
     if lines:
         self.firstcodelineno = min(lines)
     else:  # pragma: nocover
         # This branch cannot be reached, I'm just being paranoid.
         self.firstcodelineno = self.firstlineno
示例#8
0
 def find_source_lines(self):
     """Mark all executable source lines in fn as executed 0 times."""
     if self.filename is None:
         return
     strs = trace.find_strings(self.filename)
     lines = trace.find_lines_from_code(self.fn.__code__, strs)
     for lineno in lines:
         self.sourcelines.setdefault(lineno, 0)
     if lines:
         self.firstcodelineno = min(lines)
     else:  # pragma: nocover
         # This branch cannot be reached, I'm just being paranoid.
         self.firstcodelineno = self.firstlineno
示例#9
0
    def opt_coverage(self, coverdir):
        """Generate coverage information in the given directory
        (relative to _trial_temp). Requires Python 2.3.3."""

        print "Setting coverage directory to %s." % (coverdir,)
        import trace

        # begin monkey patch --------------------------- 
        def find_executable_linenos(filename):
            """Return dict where keys are line numbers in the line number table."""
            #assert filename.endswith('.py') # YOU BASTARDS
            try:
                prog = open(filename).read()
                prog = '\n'.join(prog.splitlines()) + '\n'
            except IOError, err:
                sys.stderr.write("Not printing coverage data for %r: %s\n" % (filename, err))
                sys.stderr.flush()
                return {}
            code = compile(prog, filename, "exec")
            strs = trace.find_strings(filename)
            return trace.find_lines(code, strs)
示例#10
0
 def opt_coverage(self):
     """
     Generate coverage information in the given directory (relative to
     trial temporary working directory). Requires Python 2.3.3.
     """
     coverdir = 'coverage'
     print "Setting coverage directory to %s." % (coverdir,)
     import trace
     def find_executable_linenos(filename):
         """Return dict where keys are line numbers in the line number
         table.
         """
         try:
             prog = open(filename).read()
             prog = '\n'.join(prog.splitlines()) + '\n'
         except IOError, err:
             sys.stderr.write("Not printing coverage data for %r: %s\n"
                              % (filename, err))
             sys.stderr.flush()
             return {}
         code = compile(prog, filename, "exec")
         strs = trace.find_strings(filename)
         return trace.find_lines(code, strs)
示例#11
0
 def test_deprecated_find_strings(self):
     with open(TESTFN, 'w') as fd:
         self.addCleanup(unlink, TESTFN)
     with self.assertWarns(DeprecationWarning):
         trace.find_strings(fd.name)
示例#12
0
 def test_deprecated_find_strings(self):
     with open(TESTFN, 'w') as fd:
         self.addCleanup(unlink, TESTFN)
     with self.assertWarns(DeprecationWarning):
         trace.find_strings(fd.name)
示例#13
0
 def test_deprecated_find_strings(self):
     with self.assertWarns(DeprecationWarning):
         with tempfile.NamedTemporaryFile() as fd:
             trace.find_strings(fd.name)
示例#14
0
    A re-implementation of trace.find_executable_linenos working around
    compile's problems with missing EOLS
    """
    try:
        prog = open(filename, "rU").read()
    except IOError, err:
        print >> sys.stderr, ("Not printing coverage data for %r: %s"
                              % (filename, err))
        return {}

    # Adding trailing EOL if missing
    if not prog.endswith("\n"):
        prog += "\n"

    code = compile(prog, filename, "exec")
    strs = trace.find_strings(filename)
    return trace.find_lines(code, strs)

class Coverage:
    """
    Python code coverage module built specifically for checking code coverage
    in tests performed by Testoob.

    NOTE: This class depends on the 'trace' module.
    """
    def __init__(self, ignorepaths=()):
        """
        initialize the code coverage module, gets list of directories and files
        which's coverage is not needed.
        """
        # coverage is a dictionary mapping filenames to another dictionary with