Skip to content

Commit

Permalink
Added tests for config
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr committed Jan 22, 2020
1 parent ed9e020 commit d070832
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 3 deletions.
30 changes: 30 additions & 0 deletions panel/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@
"""
from __future__ import absolute_import, division, unicode_literals

import os
import re
import shutil

import pytest

from contextlib import contextmanager

from bokeh.document import Document
from bokeh.client import pull_session
from pyviz_comms import Comm

from panel.pane import HTML, Markdown
from panel.io import state


@pytest.fixture
Expand Down Expand Up @@ -45,6 +49,20 @@ def hv_bokeh():
hv.Store.current_backend = prev_backend


@pytest.yield_fixture
def get_display_handle():
cleanup = []
def display_handle(model):
cleanup.append(model.ref['id'])
handle = {}
state._handles[model.ref['id']] = (handle, [])
return handle
yield display_handle
for ref in cleanup:
if ref in state._handles:
del state._handles[ref]


@pytest.yield_fixture
def hv_mpl():
import holoviews as hv
Expand Down Expand Up @@ -97,3 +115,15 @@ def markdown_server_session():
server.stop()
except AssertionError:
pass # tests may already close this


@contextmanager
def set_env_var(env_var, value):
old_value = os.environ.get(env_var)
os.environ[env_var] = value
yield
if old_value is None:
del os.environ[env_var]
else:
os.environ[env_var] = old_value

127 changes: 127 additions & 0 deletions panel/tests/test_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
"""
Tests pn.config variables
"""
from __future__ import absolute_import, division, unicode_literals

from panel import config, state
from panel.pane import HTML

from panel.tests.conftest import set_env_var


def test_env_var_debug():
with set_env_var('PANEL_DEBUG', 'disable'):
assert config.debug == 'disable'
with set_env_var('PANEL_DEBUG', 'replace'):
assert config.debug == 'replace'
with set_env_var('PANEL_DOC_BUILD', 'true'):
assert config.debug == 'disable'


def test_debug_replace_stdout(document, comm, get_display_handle):
pane = HTML()
with set_env_var('PANEL_DEBUG', 'replace'):
model = pane.get_root(document, comm)
handle = get_display_handle(model)

pane._on_stdout(model.ref['id'], ['print output'])
assert handle == {'text/html': 'print output</br>', 'raw': True}

pane._on_stdout(model.ref['id'], ['new output'])
assert handle == {'text/html': 'new output</br>', 'raw': True}

pane._cleanup(model)
assert model.ref['id'] not in state._handles


def test_debug_accumulate_stdout(document, comm, get_display_handle):
pane = HTML()
model = pane.get_root(document, comm)
handle = get_display_handle(model)

pane._on_stdout(model.ref['id'], ['print output'])
assert handle == {'text/html': 'print output</br>', 'raw': True}

pane._on_stdout(model.ref['id'], ['new output'])
assert handle == {'text/html': 'print output</br>\nnew output</br>', 'raw': True}

pane._cleanup(model)
assert model.ref['id'] not in state._handles


def test_debug_disable_stdout(document, comm, get_display_handle):
pane = HTML()
with set_env_var('PANEL_DEBUG', 'disable'):
model = pane.get_root(document, comm)
handle = get_display_handle(model)

pane._on_stdout(model.ref['id'], ['print output'])
assert handle == {}

pane._cleanup(model)
assert model.ref['id'] not in state._handles


def test_debug_replace_error(document, comm, get_display_handle):
pane = HTML()
with set_env_var('PANEL_DEBUG', 'replace'):
model = pane.get_root(document, comm)
handle = get_display_handle(model)

try:
1/0
except Exception as e:
pane._on_error(model.ref['id'], e)
assert 'text/html' in handle
assert 'ZeroDivisionError' in handle['text/html']

try:
1 + '2'
except Exception as e:
pane._on_error(model.ref['id'], e)
assert 'text/html' in handle
assert 'ZeroDivisionError' not in handle['text/html']
assert 'TypeError' in handle['text/html']

pane._cleanup(model)
assert model.ref['id'] not in state._handles


def test_debug_accumulate_error(document, comm, get_display_handle):
pane = HTML()
model = pane.get_root(document, comm)
handle = get_display_handle(model)

try:
1/0
except Exception as e:
pane._on_error(model.ref['id'], e)
assert 'text/html' in handle
assert 'ZeroDivisionError' in handle['text/html']

try:
1 + '2'
except Exception as e:
pane._on_error(model.ref['id'], e)
assert 'text/html' in handle
assert 'ZeroDivisionError' in handle['text/html']
assert 'TypeError' in handle['text/html']

pane._cleanup(model)
assert model.ref['id'] not in state._handles


def test_debug_disable_error(document, comm, get_display_handle):
pane = HTML()
with set_env_var('PANEL_DEBUG', 'disable'):
model = pane.get_root(document, comm)
handle = get_display_handle(model)

try:
1/0
except Exception as e:
pane._on_error(model.ref['id'], e)
assert handle == {}

pane._cleanup(model)
assert model.ref['id'] not in state._handles
8 changes: 5 additions & 3 deletions panel/viewable.py
Original file line number Diff line number Diff line change
Expand Up @@ -693,12 +693,13 @@ def _on_error(self, ref, error):
if ref not in state._handles or config.debug in [None, 'disable']:
return
handle, accumulator = state._handles[ref]
formatted = '\n<PRE>'+escape(traceback.format_exc())+'</PRE>\n'
formatted = '\n<pre>'+escape(traceback.format_exc())+'</pre>\n'
if config.debug == 'accumulate':
accumulator.append(formatted)
elif config.debug == 'replace':
accumulator[:] = [formatted]
handle.update({'text/html': '\n'.join(accumulator)}, raw=True)
if accumulator:
handle.update({'text/html': '\n'.join(accumulator)}, raw=True)

def _on_stdout(self, ref, stdout):
if ref not in state._handles or config.debug is [None, 'disable']:
Expand All @@ -709,7 +710,8 @@ def _on_stdout(self, ref, stdout):
accumulator.extend(formatted)
elif config.debug == 'replace':
accumulator[:] = formatted
handle.update({'text/html': '\n'.join(accumulator)}, raw=True)
if accumulator:
handle.update({'text/html': '\n'.join(accumulator)}, raw=True)

def _link_props(self, model, properties, doc, root, comm=None):
ref = root.ref['id']
Expand Down

0 comments on commit d070832

Please sign in to comment.