Skip to content

Commit

Permalink
Test that notebooks can be trusted (2) (#826)
Browse files Browse the repository at this point in the history
* Test that a simple notebook can be trusted

* Cell ids were added to JupyterLab in 3.0.13

* No need to add the "trusted" metadata manually
  • Loading branch information
mwouts authored Aug 31, 2021
1 parent be04fe5 commit efe9cb0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
2 changes: 1 addition & 1 deletion environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ channels:
- conda-forge
dependencies:
- python>=3.6
- jupyterlab>=3.0
- jupyterlab>=3.0.13
- nbformat>=5.1.2
- jupyter-packaging
- pyyaml
Expand Down
9 changes: 0 additions & 9 deletions jupytext/contentsmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,15 +343,6 @@ def read_one_file(alt_path, alt_fmt):
if not outputs.timestamp:
set_kernelspec_from_language(model["content"])

# Trust code cells when they have no output
for cell in model["content"].cells:
if (
cell.cell_type == "code"
and not cell.outputs
and cell.metadata.get("trusted") is False
):
cell.metadata["trusted"] = True

return model

def new_untitled(self, path="", type="", ext=""):
Expand Down
20 changes: 20 additions & 0 deletions tests/test_trust_notebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,23 @@ def test_text_notebooks_can_be_trusted(nb_file, tmpdir, no_jupytext_version_numb
model = cm.get(file)
for cell in model["content"].cells:
assert cell.metadata.get("trusted", True)


def test_simple_notebook_is_trusted(tmpdir, python_notebook):
cm = TextFileContentsManager()
cm.root_dir = str(tmpdir)

nb = python_notebook
cm.notary.unsign(nb)

# All cells are trusted in this notebook
assert cm.notary.check_cells(nb)
# Yet the notebook is not in the database of trusted notebooks
assert not cm.notary.check_signature(nb)

# Save the notebook using the CM
cm.save(dict(type="notebook", content=nb), "test.ipynb")

# The notebook is safe so it should have been trusted before getting saved to disk
nb = cm.get("test.ipynb")["content"]
assert cm.notary.check_signature(nb)

0 comments on commit efe9cb0

Please sign in to comment.