-
Notifications
You must be signed in to change notification settings - Fork 414
Expand file tree
/
Copy pathtest_pre_commit_1_sync.py
More file actions
106 lines (83 loc) · 3.2 KB
/
test_pre_commit_1_sync.py
File metadata and controls
106 lines (83 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import os
import shutil
import pytest
import yaml
from git.exc import HookExecutionError
from nbformat.v4.nbbase import new_markdown_cell
from pre_commit.main import main as pre_commit
from jupytext import read, write
from jupytext.cli import jupytext
def test_pre_commit_hook_sync(
tmpdir,
cwd_tmpdir,
tmp_repo,
jupytext_repo_root,
jupytext_repo_rev,
jupytext_pre_commit_config,
python_notebook,
):
# Add args as if we will add in actual .pre-commit-config.yaml file
jupytext_pre_commit_config["repos"][0]["hooks"][0]["args"] = ["--sync"]
with open(os.path.join(tmpdir, ".pre-commit-config.yaml"), "w") as file:
yaml.dump(jupytext_pre_commit_config, file)
tmp_repo.git.add(".pre-commit-config.yaml")
pre_commit(["install", "--install-hooks", "-f"])
# write a test notebook and sync it to py:percent
nb = python_notebook
write(nb, "test.ipynb")
jupytext(["--set-formats", "ipynb,py:percent", "test.ipynb"])
# try to commit it, should fail because the py version hasn't been added
tmp_repo.git.add("test.ipynb")
with pytest.raises(
HookExecutionError,
match="git add test.py",
):
tmp_repo.index.commit("failing")
# add the py file, now the commit will succeed
tmp_repo.git.add("test.py")
tmp_repo.index.commit("passing")
assert "test.ipynb" in tmp_repo.tree()
assert "test.py" in tmp_repo.tree()
# modify the ipynb file
nb = read("test.ipynb")
nb.cells.append(new_markdown_cell("A new cell"))
write(nb, "test.ipynb")
tmp_repo.git.add("test.ipynb")
# We try to commit one more time, this updates the py file
with pytest.raises(
HookExecutionError,
match="files were modified by this hook",
):
tmp_repo.index.commit("failing")
# the text file has been updated
assert "A new cell" in tmpdir.join("test.py").read()
# trying to commit should fail again because we forgot to add the .py version
with pytest.raises(HookExecutionError, match="git add test.py"):
tmp_repo.index.commit("still failing")
nb = read("test.ipynb")
assert len(nb.cells) == 2
# add the text file, now the commit will succeed
tmp_repo.git.add("test.py")
tmp_repo.index.commit("passing")
# modify the .py file
nb.cells.append(new_markdown_cell("A third cell"))
write(nb, "test.py", fmt="py:percent")
tmp_repo.git.add("test.py")
# the pre-commit hook will update the .ipynb file
with pytest.raises(HookExecutionError, match="git add test.ipynb"):
tmp_repo.index.commit("failing")
tmp_repo.git.add("test.ipynb")
tmp_repo.index.commit("passing")
nb = read("test.ipynb")
assert len(nb.cells) == 3
# finally we move the paired notebook to a subfolder
tmpdir.mkdir("subfolder")
shutil.move("test.py", "subfolder")
shutil.move("test.ipynb", "subfolder")
# adding both files works on the first commit as notebooks are in sync
tmp_repo.git.add("subfolder/test.ipynb")
tmp_repo.git.add("subfolder/test.py")
tmp_repo.index.commit("passing")
# and we don't get any error or message when we run 'pre-commit run --all'
status = pre_commit(["run", "--all"])
assert status == 0