Skip to content

Commit 6cb53bc

Browse files
committed
add a create_notebook fixture
1 parent 4bcd12d commit 6cb53bc

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

jupyter_server/pytest_plugin.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
from jupyter_server.serverapp import ServerApp
1717
from jupyter_server.utils import url_path_join
1818

19+
import nbformat
20+
1921

2022
pytest_plugins = "pytest_tornasync"
2123

@@ -184,7 +186,6 @@ def base_url(http_server_port):
184186
@pytest.fixture
185187
def fetch(http_server_client, auth_header, base_url):
186188
"""fetch fixture that handles auth, base_url, and path"""
187-
188189
def client_fetch(*parts, headers={}, params={}, **kwargs):
189190
# Handle URL strings
190191
path_url = url_escape(url_path_join(base_url, *parts), plus=False)
@@ -196,5 +197,22 @@ def client_fetch(*parts, headers={}, params={}, **kwargs):
196197
return http_server_client.fetch(
197198
url, headers=headers, request_timeout=20, **kwargs
198199
)
200+
return client_fetch
199201

200-
return client_fetch
202+
203+
@pytest.fixture
204+
def create_notebook(root_dir):
205+
"""Create a notebook in the test's home directory."""
206+
def inner(nbpath):
207+
nbpath = root_dir.joinpath(nbpath)
208+
# Check that the notebook has the correct file extension.
209+
if nbpath.suffix != '.ipynb':
210+
raise Exception("File extension for notebook must be .ipynb")
211+
# If the notebook path has a parent directory, make sure it's created.
212+
parent = nbpath.parent
213+
parent.mkdir(parents=True, exist_ok=True)
214+
# Create a notebook string and write to file.
215+
nb = nbformat.v4.new_notebook()
216+
nbtext = nbformat.writes(nb, version=4)
217+
nbpath.write_text(nbtext)
218+
return inner

0 commit comments

Comments
 (0)