-
Notifications
You must be signed in to change notification settings - Fork 391
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
3 changed files
with
74 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters