Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion spyder_notebook/widgets/notebooktabwidget.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ def wait_and_check_if_empty(filename):
# Try reading the file
try:
nb_contents = nbformat.read(filename, as_version=4)
except FileNotFoundError:
except (FileNotFoundError, nbformat.reader.NotJSONError):
continue

# If empty, we are done
Expand Down
12 changes: 7 additions & 5 deletions spyder_notebook/widgets/tests/test_notebooktabwidget.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# Third party imports
import pytest
from qtpy.QtWidgets import QMessageBox
from nbformat.reader import NotJSONError

# Local imports
from spyder_notebook.utils.servermanager import ServerManager
Expand Down Expand Up @@ -138,15 +139,16 @@ def test_wait_and_check_if_empty_when_not_empty(mocker, tabwidget):
assert result is False


def test_wait_and_check_if_empty_with_delay(mocker, tabwidget):
@pytest.mark.parametrize('exception', [FileNotFoundError, NotJSONError])
def test_wait_and_check_if_empty_with_delay(mocker, tabwidget, exception):
"""Test that .wait_and_check_if_empty() on an empty notebook tries to read
it, and when that fails because the file does not exist, it tries again to
read it. When the read succeeds the second time and the notebook turns out
to be empty, the function returns True."""
it, and when that fails because the file does not exist or is not JSON, it
tries again to read it. When the read succeeds the second time and the
notebook turns out to be empty, the function returns True."""
contents = {'cells': []}
mock_read = mocker.patch(
'spyder_notebook.widgets.notebooktabwidget.nbformat.read',
side_effect=[FileNotFoundError, contents])
side_effect=[exception, contents])

result = tabwidget.wait_and_check_if_empty('ham.ipynb')

Expand Down