Skip to content

Commit

Permalink
Add a (failing) --execute example
Browse files Browse the repository at this point in the history
The example fails because Jupytext rewrites the ipynb file:
[jupytext] Updating 'test.ipynb' with this change:
E               @@ -5,10 +5,10 @@
E                   "execution_count": 1,
E                   "metadata": {
E                    "execution": {
E               -     "iopub.execute_input": "2021-01-26T22:24:31.954956Z",
E               -     "iopub.status.busy": "2021-01-26T22:24:31.949570Z",
E               -     "iopub.status.idle": "2021-01-26T22:24:31.979381Z",
E               -     "shell.execute_reply": "2021-01-26T22:24:31.978412Z"
E               +     "iopub.execute_input": "2021-01-26T22:24:34.617791Z",
E               +     "iopub.status.busy": "2021-01-26T22:24:34.614232Z",
E               +     "iopub.status.idle": "2021-01-26T22:24:34.619679Z",
E               +     "shell.execute_reply": "2021-01-26T22:24:34.619940Z"
E                    }
E                   },
E                   "outputs": [
  • Loading branch information
mwouts committed Jan 31, 2021
1 parent 6ad689d commit 5d1ba7d
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 2 deletions.
4 changes: 2 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def python_notebook():
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3",
"name": "python_kernel",
}
},
)
Expand All @@ -86,7 +86,7 @@ def notebook_with_outputs():
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3",
"name": "python_kernel",
}
},
)
68 changes: 68 additions & 0 deletions tests/test_pre_commit_4_sync_execute.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import pytest
from git.exc import HookExecutionError
from nbformat.v4.nbbase import new_code_cell
from pre_commit.main import main as pre_commit

from jupytext import read, write

from .utils import requires_user_kernel_python3, skip_pre_commit_tests_on_windows


@requires_user_kernel_python3
@skip_pre_commit_tests_on_windows
def test_pre_commit_hook_sync_execute(
tmpdir,
cwd_tmpdir,
tmp_repo,
capsys,
jupytext_repo_root,
jupytext_repo_rev,
notebook_with_outputs,
):
"""Here we sync the ipynb notebook with a py:percent file and execute it (this is probably not a very
recommendable hook!)"""
pre_commit_config_yaml = f"""
repos:
- repo: {jupytext_repo_root}
rev: {jupytext_repo_rev}
hooks:
- id: jupytext
args: [--sync, --execute, --diff]
additional_dependencies:
- nbconvert
"""
tmpdir.join(".pre-commit-config.yaml").write(pre_commit_config_yaml)

tmp_repo.git.add(".pre-commit-config.yaml")
pre_commit(["install", "--install-hooks", "-f"])

# simple notebook with kernel 'user_kernel_python3'
nb = notebook_with_outputs
nb.cells = [new_code_cell("3+4")]

# pair it to a py file and write it to disk
nb.metadata["jupytext"] = {"formats": "ipynb,py:percent"}
write(nb, "test.ipynb")

# try to commit it, should fail because
# 1. the py version hasn't been added
# 2. the outputs are missing
tmp_repo.git.add("test.ipynb")
with pytest.raises(
HookExecutionError,
match="files were modified by this hook",
):
tmp_repo.index.commit("failing")

# Add the two files
tmp_repo.git.add("test.ipynb")
tmp_repo.git.add("test.py")

# now the commit will succeed
tmp_repo.index.commit("passing")
assert "test.ipynb" in tmp_repo.tree()
assert "test.py" in tmp_repo.tree()

# the first cell has the expected output
nb = read("test.ipynb")
assert nb.cells[0].outputs[0]["test/plain"] == "7"
4 changes: 4 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ def isort_version():
not any(get_kernel_spec(name).language == "R" for name in find_kernel_specs()),
reason="irkernel is not installed",
)
requires_user_kernel_python3 = pytest.mark.skipif(
"python_kernel" not in find_kernel_specs(),
reason="Please run 'python -m ipykernel install --name python_kernel --user'",
)
requires_myst = pytest.mark.skipif(
not is_myst_available(), reason="myst_parser not found"
)
Expand Down

0 comments on commit 5d1ba7d

Please sign in to comment.