Beispiel #1
0
def test_logger_thread(app, logger):
    """Test the logger thread.

    """
    queue = Queue()
    handler = QueueHandler(queue)
    logger.addHandler(handler)
    logger.info('test')
    logger.removeHandler(handler)

    model = LogModel()
    handler = GuiHandler(model)
    logger.addHandler(handler)

    thread = QueueLoggerThread(queue)
    thread.start()
    sleep(1)
    queue.put(None)
    thread.join(2)
    process_app_events()

    if thread.is_alive():
        raise

    assert model.text == 'test\n'
Beispiel #2
0
    def test_filter2(self):
        """Test adding a filter and removing the handler.

        """
        core = self.workbench.get_plugin(u'enaml.workbench.core')
        handler = GuiHandler(model=LogModel())
        core.invoke_command('ecpy.app.logging.add_handler', {
            'id': 'ui',
            'handler': handler,
            'logger': 'test'
        }, self)

        class Filter(object):
            def filter(self, record):
                return True

        test_filter = Filter()

        core.invoke_command('ecpy.app.logging.add_filter', {
            'id': 'filter',
            'filter': test_filter,
            'handler_id': 'ui'
        }, self)

        log_plugin = self.workbench.get_plugin(PLUGIN_ID)

        assert log_plugin.filter_ids == [u'filter']
        assert log_plugin._filters == {u'filter': (test_filter, u'ui')}

        core.invoke_command('ecpy.app.logging.remove_handler', {'id': 'ui'},
                            self)

        assert log_plugin.filter_ids == []
        assert log_plugin._filters == {}
Beispiel #3
0
def test_log_model():
    """Test the log model buff_size handling.

    """
    model = LogModel(buff_size=4)
    for i in range(5):
        model.add_message('%d\n' % i)

    check = ''.join(['%d\n' % i for i in range(5)])
    assert model.text == check
    model.add_message('%d' % 5)
    assert model.text == check.partition('\n')[-1] + '%d\n' % 5

    model.clean_text()
    for i in range(4):
        model.add_message('%d\n' % i)
    assert model.text == ''.join(['%d\n' % i for i in range(4)])
Beispiel #4
0
def test_log_model():
    """Test the log model buff_size handling.

    """
    model = LogModel(buff_size=4)
    for i in range(5):
        model.add_message('%d\n' % i)

    check = ''.join(['%d\n' % i for i in range(5)])
    assert model.text == check
    model.add_message('%d' % 5)
    assert model.text == check.partition('\n')[-1] + '%d\n' % 5

    model.clean_text()
    for i in range(4):
        model.add_message('%d\n' % i)
    assert model.text == ''.join(['%d\n' % i for i in range(4)])
Beispiel #5
0
def test_stdout_redirection(app, logger):
    """Test the redirection of stdout toward a logger.

    """
    model = LogModel()
    handler = GuiHandler(model)
    logger.addHandler(handler)
    stdout = sys.stdout
    sys.stdout = StreamToLogRedirector(logger)

    try:
        print('test')
        sys.stdout.flush()
    finally:
        sys.stdout = stdout

    process_app_events()
    assert model.text == 'test\n'
Beispiel #6
0
def test_stderr_redirection(app, logger):
    """Test the redirection of 'stderr' toward a logger.

    """
    model = LogModel()
    handler = GuiHandler(model)
    logger.addHandler(handler)
    stdout = sys.stdout
    sys.stdout = StreamToLogRedirector(logger, stream_type='stderr')

    try:
        print('test')
    finally:
        sys.stdout = stdout

    process_app_events()
    answer = 'An error occured please check the log file for more details.\n'
    assert model.text == answer
Beispiel #7
0
    def test_handler1(self, logger):
        """Test adding removing handler.

        """
        core = self.workbench.get_plugin(u'enaml.workbench.core')
        handler = GuiHandler(model=LogModel())
        core.invoke_command('ecpy.app.logging.add_handler', {
            'id': 'ui',
            'handler': handler,
            'logger': 'test'
        }, self)
        log_plugin = self.workbench.get_plugin(PLUGIN_ID)

        assert log_plugin.handler_ids == ['ui']
        assert handler in logger.handlers
        assert log_plugin._handlers == {'ui': (handler, 'test')}

        core.invoke_command('ecpy.app.logging.remove_handler', {'id': 'ui'},
                            self)

        assert log_plugin.handler_ids == []
        assert handler not in logger.handlers
        assert log_plugin._handlers == {}
Beispiel #8
0
    def test_formatter(self, logger, app):
        """Test setting the formatter of a handler.

        """
        core = self.workbench.get_plugin(u'enaml.workbench.core')
        model = LogModel()
        handler = GuiHandler(model=model)
        core.invoke_command('ecpy.app.logging.add_handler', {
            'id': 'ui',
            'handler': handler,
            'logger': 'test'
        }, self)

        formatter = logging.Formatter('test : %(message)s')
        core.invoke_command('ecpy.app.logging.set_formatter', {
            'formatter': formatter,
            'handler_id': 'ui'
        }, self)

        logger.info('test')

        process_app_events()

        assert model.text == 'test : test\n'
Beispiel #9
0
def test_gui_handler(app, logger, monkeypatch):
    """Test the gui handler.

    """
    model = LogModel()
    handler = GuiHandler(model)
    logger.addHandler(handler)

    logger.info('test')
    process_app_events()
    assert model.text == 'test\n'
    model.clean_text()

    logger.debug('test')
    process_app_events()
    assert model.text == 'DEBUG: test\n'
    model.clean_text()

    logger.warn('test')
    process_app_events()
    assert model.text == 'WARNING: test\n'
    model.clean_text()

    logger.error('test')
    process_app_events()
    assert model.text == 'ERROR: test\n'
    model.clean_text()

    logger.critical('test')
    process_app_events()
    answer = 'An error occured please check the log file for more details.\n'
    assert model.text == answer
    model.clean_text()

    def err(record):
        raise Exception

    monkeypatch.setattr(handler, 'format', err)
    logger.info('raise')
Beispiel #10
0
def test_gui_handler(app, logger, monkeypatch):
    """Test the gui handler.

    """
    model = LogModel()
    handler = GuiHandler(model)
    logger.addHandler(handler)

    logger.info('test')
    process_app_events()
    assert model.text == 'test\n'
    model.clean_text()

    logger.debug('test')
    process_app_events()
    assert model.text == 'DEBUG: test\n'
    model.clean_text()

    logger.warn('test')
    process_app_events()
    assert model.text == 'WARNING: test\n'
    model.clean_text()

    logger.error('test')
    process_app_events()
    assert model.text == 'ERROR: test\n'
    model.clean_text()

    logger.critical('test')
    process_app_events()
    answer = 'An error occured please check the log file for more details.\n'
    assert model.text == answer
    model.clean_text()

    def err(record):
        raise Exception

    monkeypatch.setattr(handler, 'format', err)
    logger.info('raise')