Ejemplo n.º 1
0
            def _print_traceback():
                self.indent += 2
                
                ### NOTE:
                ###     Commented out try/except; potential debugging hinderance
                
                #try:

                traceback_args = (test['error']['type'],
                                  test['error']['value'],
                                  test['error']['traceback'])
                self.print_traceback(*traceback_args)
                
                # except Exception:
                #     # should never occur!
                #     err_msg = '''Unexpected error in PyVows!
                #                  PyVows error occurred in: ({0!s})
                #                  Context was: {1!r}
                # 
                #               '''
                #     # from os.path import abspath
                #     raise VowsInternalError(err_msg, 'pyvows.reporting.test', ctx)

                # print file and line number
                if 'file' in test:
                    file_msg = 'found in {test[file]} at line {test[lineno]}'.format(test=test)
                    print('\n', 
                          self.indent_msg(red(file_msg)), 
                          '\n')

                self.indent -= 2
Ejemplo n.º 2
0
            def _print_traceback():
                self.indent += 2

                ### NOTE:
                ###     Commented out try/except; potential debugging hinderance

                #try:

                traceback_args = (test['error']['type'],
                                  test['error']['value'],
                                  test['error']['traceback'])
                self.print_traceback(*traceback_args, file=file)

                # except Exception:
                #     # should never occur!
                #     err_msg = '''Unexpected error in PyVows!
                #                  PyVows error occurred in: ({0!s})
                #                  Context was: {1!r}
                #
                #               '''
                #     # from os.path import abspath
                #     raise VowsInternalError(err_msg, 'pyvows.reporting.test', ctx)

                # print file and line number
                if 'file' in test:
                    file_msg = 'found in {test[file]} at line {test[lineno]}'.format(
                        test=test)
                    print('\n',
                          self.indent_msg(red(file_msg)),
                          '\n',
                          file=file)

                self.indent -= 2
Ejemplo n.º 3
0
    def print_traceback(self, err_type, err_obj, err_traceback):
        '''Prints a color-formatted traceback with appropriate indentation.'''
        if isinstance(err_obj, AssertionError):
            error_msg = err_obj
        else:
            error_msg = unicode(err_obj)

        print(self.indent_msg(red(error_msg)))

        if self.verbosity >= V_NORMAL:
            traceback_msg = traceback.format_exception(err_type, err_obj, err_traceback)
            traceback_msg = self.format_traceback(traceback_msg)
            traceback_msg = '\n{traceback}'.format(traceback=traceback_msg)
            traceback_msg = self.indent_msg(yellow(traceback_msg))
            print(traceback_msg)
Ejemplo n.º 4
0
    def print_traceback(self, err_type, err_obj, err_traceback):
        '''Prints a color-formatted traceback with appropriate indentation.'''
        if isinstance(err_obj, AssertionError):
            error_msg = err_obj
        else:
            error_msg = unicode(err_obj)

        print self.indent_msg(red(error_msg))

        if self.verbosity >= V_NORMAL:
            traceback_msg = traceback.format_exception(err_type, err_obj, err_traceback)
            traceback_msg = self.format_traceback(traceback_msg)
            traceback_msg = '\n{traceback}'.format(traceback=traceback_msg)
            traceback_msg = self.indent_msg(yellow(traceback_msg))
            print traceback_msg
Ejemplo n.º 5
0
    def print_traceback(self,
                        err_type,
                        err_obj,
                        err_traceback,
                        file=sys.stdout):
        '''Prints a color-formatted traceback with appropriate indentation.'''
        if isinstance(err_obj, AssertionError):
            error_msg = err_obj
        elif isinstance(err_obj, bytes):
            error_msg = err_obj.decode('utf8')
        else:
            error_msg = err_obj

        print(self.indent_msg(red(error_msg)), file=file)

        if self.verbosity >= V_NORMAL:
            traceback_msg = traceback.format_exception(err_type, err_obj,
                                                       err_traceback)
            traceback_msg = self.format_traceback(traceback_msg)
            traceback_msg = '\n{traceback}'.format(traceback=traceback_msg)
            traceback_msg = self.indent_msg(yellow(traceback_msg))
            print(traceback_msg, file=file)
Ejemplo n.º 6
0
            def _print_traceback():
                self.indent += 2

                ### NOTE:
                ###     Commented out try/except; potential debugging hinderance

                #try:

                traceback_args = None
                if (hasattr(test, 'topic') and hasattr(test['topic'], 'error')
                        and test['topic']['error'] is not None):
                    print '\n' + self.indent_msg(blue('Topic Error:'))
                    traceback_args = tuple(*test['topic'].error)
                else:
                    traceback_args = (test['error']['type'],
                                      test['error']['value'],
                                      test['error']['traceback'])
                self.print_traceback(*traceback_args)

                # except Exception:
                #     # should never occur!
                #     err_msg = '''Unexpected error in PyVows!
                #                  PyVows error occurred in: ({0!s})
                #                  Context was: {1!r}
                #
                #               '''
                #     # from os.path import abspath
                #     raise VowsInternalError(err_msg, 'pyvows.reporting.test', ctx)

                # print file and line number
                if 'file' in test:
                    file_msg = 'found in {test[file]} at line {test[lineno]}'.format(
                        test=test)
                    print
                    print self.indent_msg(red(file_msg))
                    print

                self.indent -= 2
Ejemplo n.º 7
0
            def _print_traceback():
                self.indent += 2
                
                ### NOTE:
                ###     Commented out try/except; potential debugging hinderance
                
                #try:
                
                traceback_args = None
                if (hasattr(test, 'topic') 
                        and hasattr(test['topic'], 'error')  
                        and test['topic']['error'] is not None):
                    print '\n' + self.indent_msg(blue('Topic Error:'))
                    traceback_args = tuple(*test['topic'].error)
                else:
                    traceback_args = (test['error']['type'],
                                      test['error']['value'],
                                      test['error']['traceback'])
                self.print_traceback(*traceback_args)
                
                # except Exception:
                #     # should never occur!
                #     err_msg = '''Unexpected error in PyVows!
                #                  PyVows error occurred in: ({0!s})
                #                  Context was: {1!r}
                # 
                #               '''
                #     # from os.path import abspath
                #     raise VowsInternalError(err_msg, 'pyvows.reporting.test', ctx)

                # print file and line number
                if 'file' in test:
                    file_msg = 'found in {test[file]} at line {test[lineno]}'.format(test=test)
                    print
                    print self.indent_msg(red(file_msg))
                    print

                self.indent -= 2
Ejemplo n.º 8
0
            def _print_traceback():
                self.indent += 2

                try:
                    traceback_args = None

                    if hasattr(test, 'topic') \
                            and hasattr(test['topic'], 'error')  \
                            and test['topic']['error'] is not None:
                        print '\n' + self.indent_msg(blue('Topic Error:'))
                        traceback_args = tuple(*test['topic'].error)
                    else:
                        traceback_args = (test['error']['type'],
                                          test['error']['value'],
                                          test['error']['traceback'])
                    self.print_traceback(*traceback_args)
                except Exception:
                    # should never occur!
                    err_msg = '''Unexpected error in PyVows!
                                 PyVows error occurred in: ({0!s})
                                 Context was: {1!r}

                              '''
                    # from os.path import abspath
                    raise VowsInternalError(err_msg,
                                            'pyvows.reporting.test',
                                            ctx)

                # print file and line number
                if 'file' in test:
                    file_msg = 'found in {test[file]} at line {test[lineno]}'.format(test=test)
                    print
                    print self.indent_msg(red(file_msg))
                    print

                self.indent -= 2
Ejemplo n.º 9
0
class VowsReporter(object):
    '''Base class for other Reporters to extend.  Contains common attributes
    and methods.
    '''
    #   Should *only* contain attributes and methods that aren't specific
    #   to a particular type of report.

    HONORED = green('✓')
    BROKEN = red('✗')
    SKIPPED = '?'
    TAB = '  '

    def __init__(self, result, verbosity):
        self.result = result
        self.verbosity = verbosity
        self.indent = 1

    #-------------------------------------------------------------------------
    #   String Formatting
    #-------------------------------------------------------------------------
    def camel_split(self, string):
        '''Splits camel-case `string` into separate words.

        Example:

            self.camel_split('SomeCamelCaseString')

        Returns:

            'Some camel case string'

        '''
        return re.sub('((?=[A-Z][a-z])|(?<=[a-z])(?=[A-Z])|(?=[0-9]\b))', ' ',
                      string).strip()

    def under_split(self, string):
        '''Replaces all underscores in `string` with spaces.'''
        return ' '.join(string.split('_'))

    def format_traceback(self, traceback_list):
        '''Adds the current level of indentation to a traceback (so it matches
        the current context's indentation).

        '''

        # TODO:
        #   ...Is this a decorator?  If so, please add a comment or docstring
        #   to make it explicit.
        def _indent(msg):
            if msg.strip().startswith('File'):
                return self.indent_msg(msg)
            return msg

        tb_list = [_indent(tb) for tb in traceback_list]
        return ''.join(tb_list)

    def format_python_constants(self, msg):
        '''Fixes capitalization of Python constants.

        Since developers are used to reading `True`, `False`, and `None`
        as capitalized words, it makes sense to match that capitalization
        in reports.

        '''
        msg = msg.replace('true', 'True')
        msg = msg.replace('false', 'False')
        msg = msg.replace('none', 'None')
        return msg

    def header(self, msg, ruler_character='='):
        '''Returns the string `msg` with a text "ruler".  Also colorizes as
        bright green (when color is available).

        '''
        ruler = ' {0}'.format(len(msg) * ruler_character)

        msg = ' {0}'.format(msg)
        msg = '{0}{ruler}{0}{msg}{0}{ruler}{0}'.format('\n',
                                                       ruler=ruler,
                                                       msg=msg)

        msg = green(bold(msg))

        return msg

    def indent_msg(self, msg, indentation=None):
        '''Returns `msg` with the indentation specified by `indentation`.

        '''
        if indentation is not None:
            indent = self.TAB * indentation
        else:
            indent = self.TAB * self.indent

        return '{indent}{msg}'.format(indent=indent, msg=msg)

    #-------------------------------------------------------------------------
    #   Printing Methods
    #-------------------------------------------------------------------------
    def humanized_print(self, msg, indentation=None, file=sys.stdout):
        '''Passes `msg` through multiple text filters to make the output
        appear more like normal text, then prints it (indented by
        `indentation`).

        '''
        msg = self.under_split(msg)
        msg = self.camel_split(msg)
        msg = msg.replace('  ', ' ')  # normalize spaces if inserted by
        # both of the above
        msg = msg.capitalize()
        msg = self.format_python_constants(msg)

        print(self.indent_msg(msg, indentation), file=file)

    def print_traceback(self,
                        err_type,
                        err_obj,
                        err_traceback,
                        file=sys.stdout):
        '''Prints a color-formatted traceback with appropriate indentation.'''
        if isinstance(err_obj, AssertionError):
            error_msg = err_obj
        elif isinstance(err_obj, bytes):
            error_msg = err_obj.decode('utf8')
        else:
            error_msg = err_obj

        print(self.indent_msg(red(error_msg)), file=file)

        if self.verbosity >= V_NORMAL:
            traceback_msg = traceback.format_exception(err_type, err_obj,
                                                       err_traceback)
            traceback_msg = self.format_traceback(traceback_msg)
            traceback_msg = '\n{traceback}'.format(traceback=traceback_msg)
            traceback_msg = self.indent_msg(yellow(traceback_msg))
            print(traceback_msg, file=file)
Ejemplo n.º 10
0
    def print_context(self, name, context):
        #   FIXME: Add Docstring
        #
        #       *   Is this only used in certain cases?
        #           *   If so, which?
        self.indent += 1

        if (self.verbosity >= V_VERBOSE or
                not self.result.eval_context(context)):
            self.humanized_print(name)

        def _print_successful_context():
            honored = ensure_encoded(VowsReporter.HONORED)
            topic = ensure_encoded(test['topic'])
            name = ensure_encoded(test['name'])

            if self.verbosity == V_VERBOSE:
                self.humanized_print('{0} {1}'.format(honored, name))
            elif self.verbosity >= V_EXTRA_VERBOSE:
                if test['enumerated']:
                    self.humanized_print('{0} {1} - {2}'.format(honored, topic, name))
                else:
                    self.humanized_print('{0} {1}'.format(honored, name))

        def _print_failed_context():
            ctx = test['context_instance']

            def _print_traceback():
                self.indent += 2
                
                ### NOTE:
                ###     Commented out try/except; potential debugging hinderance
                
                #try:

                traceback_args = (test['error']['type'],
                                  test['error']['value'],
                                  test['error']['traceback'])
                self.print_traceback(*traceback_args)
                
                # except Exception:
                #     # should never occur!
                #     err_msg = '''Unexpected error in PyVows!
                #                  PyVows error occurred in: ({0!s})
                #                  Context was: {1!r}
                # 
                #               '''
                #     # from os.path import abspath
                #     raise VowsInternalError(err_msg, 'pyvows.reporting.test', ctx)

                # print file and line number
                if 'file' in test:
                    file_msg = 'found in {test[file]} at line {test[lineno]}'.format(test=test)
                    print('\n', 
                          self.indent_msg(red(file_msg)), 
                          '\n')

                self.indent -= 2

            self.humanized_print('{0} {test}'.format(
                VowsReporter.BROKEN,
                test=test['name']))

            # print generated topic (if applicable)
            if ctx.generated_topic:
                value = yellow(test['topic'])
                self.humanized_print('')
                self.humanized_print('\tTopic value:')
                self.humanized_print('\t{value}'.format(value=value))
                self.humanized_print('\n' * 2)

            # print traceback
            _print_traceback()

        # Show any error raised by the setup, topic or teardown functions
        if context.get('error', None):
            e = context['error']
            print('\n', self.indent_msg(blue("Error in {0!s}:".format(e.source))))
            self.print_traceback(*e.exc_info)
            print(self.indent_msg(red("Nested tests following this error have not been run.")))

        else:
            for test in context['tests']:
                if test['succeeded']:
                    _print_successful_context()
                else:
                    _print_failed_context()

        # I hereby (re)curse you...!
        for context in context['contexts']:
            self.print_context(context['name'], context)

        self.indent -= 1