コード例 #1
0
def test_format_exceptions(cgitb, as_html, monkeypatch):
    if cgitb:
        monkeypatch.setitem(sys.modules, 'IPython.core.ultratb', None)
    monkeypatch.setattr(
        exceptions,
        'standard_metadata',
        lambda x: {
            'package': 'test-package',
            'version': '0.1.0'
        },
    )

    # we make sure to actually raise the exceptions,
    # otherwise they will miss the __traceback__ attributes.
    try:
        try:
            raise ValueError('cause')
        except ValueError as e:
            raise PluginError(
                'some error',
                plugin_name='test_plugin',
                plugin="mock",
                cause=e,
            )
    except PluginError:
        pass

    formatted = exceptions.format_exceptions('test_plugin', as_html=as_html)
    assert "some error" in formatted
    assert "version: 0.1.0" in formatted
    assert "plugin package: test-package" in formatted

    assert exceptions.format_exceptions('nonexistent', as_html=as_html) == ''
コード例 #2
0
def test_format_exceptions(plugin_manager):
    """Test that format_exceptions returns a string with traceback info."""

    # check both as_html=True and False
    assert 'Traceback' in format_exceptions('invalid', as_html=False)
    assert 'Traceback' in format_exceptions('invalid', as_html=True)

    # formatting exceptions for a plugin that has not errored returns None.
    assert not format_exceptions('working')
コード例 #3
0
def test_iter_reader_plugins(plugin_manager):
    """Test safe iteration through reader plugins even with errors.

    `napari_bad_plugin2` is a plugin that loads fine but throws an error during
    file-reading.  this tests that we can gracefully handle that.
    """

    # the plugin loads fine, so there should be no exceptions yet.
    assert 'napari_bad_plugin2' not in PLUGIN_ERRORS

    # make sure 'napari_bad_plugin2' gets called first
    plugin_manager.hooks.napari_get_reader.bring_to_front(
        ['napari_bad_plugin2'])

    # but when we try to read an image path, it will raise an IOError.
    # we want to catch and store that IOError, and then move on to give other
    # plugins chance to return layer_data
    layer_data = read_data_with_plugins('image.ext', plugin_manager)

    # the good plugins (like "napari_test_plugin") should return layer_data
    assert layer_data

    # but the exception from `bad_plugin2` should now be stored.
    assert 'napari_bad_plugin2' in PLUGIN_ERRORS
    # we can print out a string that should have the explanation of the error.
    exception_string = format_exceptions('napari_bad_plugin2')
    assert 'IOError' in exception_string
    assert "napari_get_reader" in exception_string
コード例 #4
0
def test_format_exceptions(cgitb, as_html, monkeypatch):
    if cgitb:
        monkeypatch.setitem(sys.modules, 'IPython.core.ultratb', None)
    monkeypatch.setattr(
        exceptions,
        'standard_metadata',
        lambda x: {'package': 'test-package', 'version': '0.1.0'},
    )
    _ = PluginError(
        'some error',
        plugin_name='test_plugin',
        plugin="mock",
        cause=ValueError("cause"),
    )
    formatted = exceptions.format_exceptions('test_plugin', as_html=as_html)
    assert "some error" in formatted
    assert "version: 0.1.0" in formatted
    assert "plugin package: test-package" in formatted

    assert exceptions.format_exceptions('nonexistent', as_html=as_html) == ''