Ejemplo n.º 1
0
def test_logger_thread(exopy_qtbot, 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)

    if thread.is_alive():
        raise RuntimeError()

    def assert_text():
        assert model.text == 'test\n'

    exopy_qtbot.wait_until(assert_text)
Ejemplo n.º 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('exopy.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('exopy.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('exopy.app.logging.remove_handler',
                            {'id': 'ui'}, self)

        assert log_plugin.filter_ids == []
        assert log_plugin._filters == {}
Ejemplo n.º 3
0
    def test_formatter(self, logger, exopy_qtbot):
        """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('exopy.app.logging.add_handler', {
            'id': 'ui',
            'handler': handler,
            'logger': 'test'
        }, self)

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

        logger.info('test')

        def assert_text():
            assert model.text == 'test : test\n'

        exopy_qtbot.wait_until(assert_text)
Ejemplo n.º 4
0
def test_gui_handler(exopy_qtbot, logger, monkeypatch):
    """Test the gui handler.

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

    logger.info('test')

    def assert_text():
        assert model.text == 'test\n'

    exopy_qtbot.wait_until(assert_text)
    model.clean_text()

    logger.debug('test')

    def assert_text():
        assert model.text == 'DEBUG: test\n'

    exopy_qtbot.wait_until(assert_text)
    model.clean_text()

    logger.warning('test')

    def assert_text():
        assert model.text == 'WARNING: test\n'

    exopy_qtbot.wait_until(assert_text)
    model.clean_text()

    logger.error('test')

    def assert_text():
        assert model.text == 'ERROR: test\n'

    exopy_qtbot.wait_until(assert_text)
    model.clean_text()

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

    def assert_text():
        assert model.text == answer

    exopy_qtbot.wait_until(assert_text)
    model.clean_text()

    def err(record):
        raise Exception

    monkeypatch.setattr(handler, 'format', err)
    logger.info('raise')
Ejemplo n.º 5
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)])
Ejemplo n.º 6
0
def test_stdout_redirection(exopy_qtbot, 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

    def assert_text():
        assert model.text == 'test\n'

    exopy_qtbot.wait_until(assert_text)
Ejemplo n.º 7
0
def test_stderr_redirection(exopy_qtbot, 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

    answer = 'An error occured please check the log file for more details.\n'

    def assert_text():
        assert model.text == answer

    exopy_qtbot.wait_until(assert_text)
Ejemplo n.º 8
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('exopy.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('exopy.app.logging.remove_handler',
                            {'id': 'ui'}, self)

        assert log_plugin.handler_ids == []
        assert handler not in logger.handlers
        assert log_plugin._handlers == {}