Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test that notebooks can be trusted (2) #826

Merged
merged 3 commits into from
Aug 31, 2021
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 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)