Skip to content

Commit

Permalink
test: this test if stacktraces are cleared in output widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
maartenbreddels committed Nov 2, 2020
1 parent 29646f3 commit df6736b
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 0 deletions.
23 changes: 23 additions & 0 deletions share/jupyter/voila/templates/base/static/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,28 @@
* The full license is in the file LICENSE, distributed with this software. *
****************************************************************************/

window.VoilaDebug = VoilaDebug = {
_widgetManagerPromiseResolve: null,
_widgetManagerPromise: null,
async waitForWidgetManager() {
await _widgetManagerPromise;
},
async waitForViews(modelId) {
const widgetManager = await window.VoilaDebug._widgetManagerPromise;
const model = await widgetManager._models[modelId];
await Promise.all(Object.values(model.views))
},
async waitForAllViews() {
console.log(window.VoilaDebug._widgetManagerPromise)
const widgetManager = await window.VoilaDebug._widgetManagerPromise;
for(const modelId in widgetManager._models) {
await VoilaDebug.waitForViews(modelId);
}
}
}

window.VoilaDebug._widgetManagerPromise = new Promise((resolve) => window.VoilaDebug._widgetManagerPromiseResolve = resolve),

// NOTE: this file is not transpiled, async/await is the only modern feature we use here
require([window.voila_js_url || 'static/voila'], function(voila) {
// requirejs doesn't like to be passed an async function, so create one inside
Expand Down Expand Up @@ -45,6 +67,7 @@ require([window.voila_js_url || 'static/voila'], function(voila) {
});
await widgetManager.build_widgets();
voila.renderMathJax();
window.VoilaDebug._widgetManagerPromiseResolve(widgetManager);
}

if (document.readyState === 'complete') {
Expand Down
46 changes: 46 additions & 0 deletions tests/app/show_traceback_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import asyncio
import os

import pytest
import pyppeteer


@pytest.fixture(params=[True, False])
Expand All @@ -21,3 +25,45 @@ async def test_syntax_error(http_server_client, syntax_error_notebook_url, show_
else:
assert 'There was an error when executing cell' in output
assert 'This should not be executed' not in output


async def test_output_widget_traceback(http_server_client, exception_runtime_notebook_url, show_tracebacks):
options = dict(headless=False, devtools=True) if os.environ.get('VOILA_TEST_DEBUG_VISUAL', False) else {}
# with headless and gpu enabled, we get slightly different results on the same OS
# we can enable it if we need to, since we allow for a tolerance
launcher = pyppeteer.launcher.Launcher(options=options, autoClose=False, args=['--font-render-hinting=none', '--disable-gpu'])
browser = await launcher.launch()
page = await browser.newPage()
try:
await page.goto(http_server_client.get_url(exception_runtime_notebook_url), waitUntil='networkidle2')
await page.evaluate('async () => await VoilaDebug.widgetManagerPromise')
await page.evaluate('async () => await VoilaDebug.waitForAllViews()')
await page.evaluate("document.querySelector('.button2').click()")
for i in range(20):
await asyncio.sleep(0.05)
output_text = await page.evaluate("document.querySelector('.output_exception').innerText")
if output_text:
break
else:
assert False, f"Never received output"
if show_tracebacks:
assert 'this happens after the notebook is executed' in output_text
else:
assert 'this happens after the notebook is executed' not in output_text
except Exception as e: # noqa
if os.environ.get('VOILA_TEST_DEBUG_VISUAL', False):
import traceback
traceback.print_exc()
# may want to add --async-test-timeout=60 to pytest arguments
print("Waiting for 60 second for visual inspection (hit ctrl-c to break)")
import sys
sys.stdout.flush()
sys.stderr.flush()
try:
await asyncio.sleep(60)
except: # noqa ignore ctrl-c
pass
raise e
finally:
await browser.close()
await launcher.killChrome()
12 changes: 12 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ def syntax_error_notebook_url(base_url):
return base_url + "voila/render/syntax_error.ipynb"


@pytest.fixture
def exception_runtime_notebook_url(base_url):
return base_url + "voila/render/exception_runtime.ipynb"


@pytest.fixture
def voila_notebook(notebook_directory):
return os.path.join(notebook_directory, 'print.ipynb')


# this forces the event_loop fixture in pytest_asyncio to use the same ioloop as pytest_tornasync
@pytest.fixture()
def event_loop(io_loop):
import asyncio
return asyncio.get_event_loop()
2 changes: 2 additions & 0 deletions voila/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,8 @@ def start(self):
parent=self,
connection_dir=self.connection_dir,
kernel_spec_manager=self.kernel_spec_manager,
allow_tracebacks=self.voila_configuration.show_tracebacks,
traceback_replacement_message='An error occurred, run Voilà with --show_traceback=True or --debug to show the traceback.',
allowed_message_types=[
'comm_open',
'comm_close',
Expand Down

0 comments on commit df6736b

Please sign in to comment.