Skip to content

Commit cfc6909

Browse files
test: Use current env for pre-commit tests
* Use current test env to run pre-commit tests * This avoids creating a new env and installing jupytext in that env Signed-off-by: Mahendra Paipuri <[email protected]>
1 parent 1ef1dea commit cfc6909

9 files changed

+139
-110
lines changed

tests/conftest.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import os.path
33
import re
44
import sys
5+
import yaml
56
import unittest.mock as mock
67
from pathlib import Path
78

@@ -62,6 +63,34 @@ def jupytext_repo_rev(jupytext_repo_root):
6263
"""The local revision of this repo, to use in .pre-commit-config.yaml in tests"""
6364
return system("git", "rev-parse", "HEAD", cwd=jupytext_repo_root).strip()
6465

66+
@pytest.fixture
67+
def jupytext_pre_commit_config(jupytext_repo_root):
68+
"""The local revision of this repo, to use in .pre-commit-config.yaml in tests"""
69+
# Read pre-commit-hooks.yaml file
70+
#
71+
# Setting language to system will ensure that pre-commit assumes that we
72+
# provision correct environment. This is the case when we run unit tests as we
73+
# make a developmental install of jupytext before running unit tests. So there is
74+
# an "test" environment that is provisioned and we use this current environment
75+
# to run pre-commit tests.
76+
#
77+
# When there are additional_dependencies, we override this in individual test to
78+
# python so that pre-commit will create an environment and install those
79+
# additional dependencies in that environment.
80+
#
81+
# This strategy will enable us to directly test the pre-commit hook config with
82+
# current version of jupytext. It also avoid re-installing jupytext in pre-commit
83+
# config and thus tests run faster.
84+
with open(os.path.join(jupytext_repo_root, ".pre-commit-hooks.yaml"), 'r') as file:
85+
pre_commit_hooks = yaml.safe_load(file)
86+
pre_commit_hooks[0]["language"] = "system"
87+
pre_commit_config = {"repos": [
88+
{
89+
"repo": "local",
90+
"hooks": pre_commit_hooks,
91+
}
92+
]}
93+
return pre_commit_config
6594

6695
@pytest.fixture()
6796
def python_notebook():

tests/external/pre_commit/test_pre_commit_0_ipynb_to_py.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import os
2+
import yaml
13
import pytest
24
from git.exc import HookExecutionError
35
from nbformat.v4.nbbase import new_markdown_cell, new_notebook
@@ -9,25 +11,23 @@
911

1012

1113
def test_pre_commit_hook_ipynb_to_py(
12-
tmpdir, cwd_tmpdir, tmp_repo, jupytext_repo_root, jupytext_repo_rev
14+
tmpdir, cwd_tmpdir, tmp_repo, jupytext_repo_root, jupytext_repo_rev, jupytext_pre_commit_config
1315
):
1416
"""Here we document and test the expected behavior of the pre-commit hook in the
1517
directional (--to) mode. Note that here, the ipynb file is always the source for
1618
updates - i.e. changes on the .py file will not trigger the hook.
1719
"""
1820
# set up the tmpdir repo with pre-commit
19-
pre_commit_config_yaml = f"""
20-
repos:
21-
- repo: {jupytext_repo_root}
22-
rev: {jupytext_repo_rev}
23-
hooks:
24-
- id: jupytext
25-
args: [--from, ipynb, --to, "py:percent"]
26-
"""
21+
# Add args as if we will add in actual .pre-commit-config.yaml file
22+
jupytext_pre_commit_config["repos"][0]["hooks"][0]["args"] = [
23+
"--from", "ipynb", "--to", "py:percent"
24+
]
25+
with open(os.path.join(tmpdir, ".pre-commit-config.yaml"), 'w') as file:
26+
yaml.dump(jupytext_pre_commit_config, file)
2727

28-
tmpdir.join(".pre-commit-config.yaml").write(pre_commit_config_yaml)
28+
# tmpdir.join(".pre-commit-config.yaml").write(pre_commit_config_yaml)
2929
tmp_repo.git.add(".pre-commit-config.yaml")
30-
pre_commit(["install", "--install-hooks"])
30+
pre_commit(["install", "--install-hooks", "-f"])
3131

3232
# write test notebook and output file
3333
nb = new_notebook(cells=[new_markdown_cell("A short notebook")])

tests/external/pre_commit/test_pre_commit_1_sync.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import os
12
import shutil
3+
import yaml
24

35
import pytest
46
from git.exc import HookExecutionError
@@ -15,17 +17,13 @@ def test_pre_commit_hook_sync(
1517
tmp_repo,
1618
jupytext_repo_root,
1719
jupytext_repo_rev,
20+
jupytext_pre_commit_config,
1821
python_notebook,
1922
):
20-
pre_commit_config_yaml = f"""
21-
repos:
22-
- repo: {jupytext_repo_root}
23-
rev: {jupytext_repo_rev}
24-
hooks:
25-
- id: jupytext
26-
args: [--sync]
27-
"""
28-
tmpdir.join(".pre-commit-config.yaml").write(pre_commit_config_yaml)
23+
# Add args as if we will add in actual .pre-commit-config.yaml file
24+
jupytext_pre_commit_config["repos"][0]["hooks"][0]["args"] = ["--sync"]
25+
with open(os.path.join(tmpdir, ".pre-commit-config.yaml"), 'w') as file:
26+
yaml.dump(jupytext_pre_commit_config, file)
2927

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

tests/external/pre_commit/test_pre_commit_1_sync_with_config.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import os
2+
import yaml
13
import pytest
24
from git.exc import HookExecutionError
35
from nbformat.v4.nbbase import new_markdown_cell
@@ -12,17 +14,12 @@ def test_pre_commit_hook_sync_with_config(
1214
tmp_repo,
1315
jupytext_repo_root,
1416
jupytext_repo_rev,
17+
jupytext_pre_commit_config,
1518
python_notebook,
1619
):
17-
pre_commit_config_yaml = f"""
18-
repos:
19-
- repo: {jupytext_repo_root}
20-
rev: {jupytext_repo_rev}
21-
hooks:
22-
- id: jupytext
23-
args: [--sync]
24-
"""
25-
tmpdir.join(".pre-commit-config.yaml").write(pre_commit_config_yaml)
20+
jupytext_pre_commit_config["repos"][0]["hooks"][0]["args"] = ["--sync"]
21+
with open(os.path.join(tmpdir, ".pre-commit-config.yaml"), 'w') as file:
22+
yaml.dump(jupytext_pre_commit_config, file)
2623

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

tests/external/pre_commit/test_pre_commit_1_sync_with_no_config.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import os
2+
import yaml
13
from copy import deepcopy
24

35
import pytest
@@ -16,23 +18,14 @@ def test_pre_commit_hook_sync_with_no_config(
1618
tmp_repo,
1719
jupytext_repo_root,
1820
jupytext_repo_rev,
21+
jupytext_pre_commit_config,
1922
notebook_with_outputs,
2023
):
2124
"""In this test we reproduce the setting from https://github.com/mwouts/jupytext/issues/967"""
22-
pre_commit_config_yaml = f"""
23-
repos:
24-
- repo: {jupytext_repo_root}
25-
rev: {jupytext_repo_rev}
26-
hooks:
27-
- id: jupytext
28-
args: [
29-
'--sync',
30-
'--set-formats',
31-
'ipynb,py:percent',
32-
'--show-changes',
33-
'--'
25+
jupytext_pre_commit_config["repos"][0]["hooks"][0]["args"] = [
26+
"--sync", "--set-formats", "ipynb,py:percent", "--show-changes", "--"
3427
]
35-
"""
28+
3629
# Save a sample notebook with outputs in Jupyter
3730
nb = deepcopy(notebook_with_outputs)
3831
(tmpdir / "notebooks").mkdir()
@@ -45,7 +38,8 @@ def test_pre_commit_hook_sync_with_no_config(
4538
tmp_repo.index.commit("Notebook with outputs")
4639

4740
# Configure the pre-commit hook
48-
tmpdir.join(".pre-commit-config.yaml").write(pre_commit_config_yaml)
41+
with open(os.path.join(tmpdir, ".pre-commit-config.yaml"), 'w') as file:
42+
yaml.dump(jupytext_pre_commit_config, file)
4943
tmp_repo.git.add(".pre-commit-config.yaml")
5044
pre_commit(["install", "--install-hooks", "-f"])
5145

tests/external/pre_commit/test_pre_commit_2_sync_nbstripout.py

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import os
2+
import yaml
13
import pytest
24
from git.exc import HookExecutionError
35
from pre_commit.main import main as pre_commit
@@ -12,24 +14,19 @@ def test_pre_commit_hook_sync_nbstripout(
1214
tmp_repo,
1315
jupytext_repo_root,
1416
jupytext_repo_rev,
17+
jupytext_pre_commit_config,
1518
notebook_with_outputs,
1619
):
1720
"""Here we sync the ipynb notebook with a Markdown file and also apply nbstripout."""
18-
pre_commit_config_yaml = f"""
19-
repos:
20-
- repo: {jupytext_repo_root}
21-
rev: {jupytext_repo_rev}
22-
hooks:
23-
- id: jupytext
24-
args: [--sync]
25-
26-
- repo: https://github.com/kynan/nbstripout
27-
rev: 0.5.0
28-
hooks:
29-
- id: nbstripout
30-
"""
31-
32-
tmpdir.join(".pre-commit-config.yaml").write(pre_commit_config_yaml)
21+
jupytext_pre_commit_config["repos"][0]["hooks"][0]["args"] = ["--sync"]
22+
jupytext_pre_commit_config["repos"].append({
23+
"repo": "https://github.com/kynan/nbstripout",
24+
"rev": "0.5.0",
25+
"hooks": [{"id": "nbstripout"}]
26+
})
27+
28+
with open(os.path.join(tmpdir, ".pre-commit-config.yaml"), 'w') as file:
29+
yaml.dump(jupytext_pre_commit_config, file)
3330

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

tests/external/pre_commit/test_pre_commit_3_sync_black_nbstripout.py

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import os
2+
import yaml
13
import pytest
24
from git.exc import HookExecutionError
35
from pre_commit.main import main as pre_commit
@@ -11,30 +13,31 @@ def test_pre_commit_hook_sync_black_nbstripout(
1113
tmp_repo,
1214
jupytext_repo_root,
1315
jupytext_repo_rev,
16+
jupytext_pre_commit_config,
1417
notebook_with_outputs,
1518
):
1619
"""Here we sync the ipynb notebook with a py:percent file and also apply black and nbstripout."""
17-
pre_commit_config_yaml = f"""
18-
repos:
19-
- repo: {jupytext_repo_root}
20-
rev: {jupytext_repo_rev}
21-
hooks:
22-
- id: jupytext
23-
args: [--sync, --pipe, black]
24-
additional_dependencies:
25-
- black==22.3.0 # Matches hook
20+
jupytext_pre_commit_config["repos"][0]["hooks"][0]["args"] = [
21+
"--sync", "--pipe", "black",
22+
]
23+
jupytext_pre_commit_config["repos"][0]["hooks"][0]["additional_dependencies"] = [
24+
"black==22.3.0",
25+
]
26+
# Use python as language as we will need to install additional dependencies
27+
jupytext_pre_commit_config["repos"][0]["hooks"][0]["language"] = "python"
28+
jupytext_pre_commit_config["repos"].append({
29+
"repo": "https://github.com/kynan/nbstripout",
30+
"rev": "0.5.0",
31+
"hooks": [{"id": "nbstripout"}]
32+
})
33+
jupytext_pre_commit_config["repos"].append({
34+
"repo": "https://github.com/psf/black",
35+
"rev": "22.3.0",
36+
"hooks": [{"id": "black"}]
37+
})
2638

27-
- repo: https://github.com/psf/black
28-
rev: 22.3.0
29-
hooks:
30-
- id: black
31-
32-
- repo: https://github.com/kynan/nbstripout
33-
rev: 0.5.0
34-
hooks:
35-
- id: nbstripout
36-
"""
37-
tmpdir.join(".pre-commit-config.yaml").write(pre_commit_config_yaml)
39+
with open(os.path.join(tmpdir, ".pre-commit-config.yaml"), 'w') as file:
40+
yaml.dump(jupytext_pre_commit_config, file)
3841

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

tests/external/pre_commit/test_pre_commit_4_sync_execute.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import os
2+
import yaml
13
import pytest
24
from git.exc import HookExecutionError
35
from nbformat.v4.nbbase import new_code_cell
@@ -13,21 +15,22 @@ def test_pre_commit_hook_sync_execute(
1315
tmp_repo,
1416
jupytext_repo_root,
1517
jupytext_repo_rev,
18+
jupytext_pre_commit_config,
1619
notebook_with_outputs,
1720
):
1821
"""Here we sync the ipynb notebook with a py:percent file and execute it (this is probably not a very
1922
recommendable hook!)"""
20-
pre_commit_config_yaml = f"""
21-
repos:
22-
- repo: {jupytext_repo_root}
23-
rev: {jupytext_repo_rev}
24-
hooks:
25-
- id: jupytext
26-
args: [--sync, --execute, --show-changes]
27-
additional_dependencies:
28-
- nbconvert
29-
"""
30-
tmpdir.join(".pre-commit-config.yaml").write(pre_commit_config_yaml)
23+
jupytext_pre_commit_config["repos"][0]["hooks"][0]["args"] = [
24+
"--sync", "--execute", "--show-changes",
25+
]
26+
jupytext_pre_commit_config["repos"][0]["hooks"][0]["additional_dependencies"] = [
27+
"nbconvert"
28+
]
29+
# Use python as language as we will need to install additional dependencies
30+
jupytext_pre_commit_config["repos"][0]["hooks"][0]["language"] = "python"
31+
32+
with open(os.path.join(tmpdir, ".pre-commit-config.yaml"), 'w') as file:
33+
yaml.dump(jupytext_pre_commit_config, file)
3134

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

tests/external/pre_commit/test_pre_commit_5_reformat_markdown.py

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import os
2+
import yaml
13
import pytest
24
from git.exc import HookExecutionError
35
from nbformat.v4.nbbase import new_code_cell, new_markdown_cell, new_notebook
@@ -13,6 +15,7 @@ def test_pre_commit_hook_sync_reformat_code_and_markdown(
1315
tmp_repo,
1416
jupytext_repo_root,
1517
jupytext_repo_rev,
18+
jupytext_pre_commit_config,
1619
notebook_with_outputs,
1720
):
1821
"""Here we sync the ipynb notebook with a py:percent file and also apply black and pandoc to reformat both
@@ -22,27 +25,32 @@ def test_pre_commit_hook_sync_reformat_code_and_markdown(
2225
ipynb files. Consequently we pin the version of nbformat to 5.0.8 in all the environments below (and you
2326
will have to do the same on the environment in which you edit the notebooks).
2427
"""
25-
pre_commit_config_yaml = f"""
26-
repos:
27-
- repo: {jupytext_repo_root}
28-
rev: {jupytext_repo_rev}
29-
hooks:
30-
- id: jupytext
31-
args: [--sync, --pipe-fmt, ipynb, --pipe, 'pandoc --from ipynb --to ipynb --markdown-headings=atx', --show-changes]
32-
additional_dependencies:
33-
- nbformat==5.0.8 # because pandoc 2.11.4 does not preserve yet the new cell ids
34-
- id: jupytext
35-
args: [--sync, --pipe, black, --show-changes]
36-
additional_dependencies:
37-
- black==22.3.0 # Matches black hook below
38-
- nbformat==5.0.8 # for compatibility with the pandoc hook above
39-
40-
- repo: https://github.com/psf/black
41-
rev: 22.3.0
42-
hooks:
43-
- id: black
44-
"""
45-
tmpdir.join(".pre-commit-config.yaml").write(pre_commit_config_yaml)
28+
jupytext_pre_commit_config["repos"][0]["hooks"][0]["args"] = [
29+
"--sync", "--pipe-fmt", "ipynb", "--pipe", "pandoc", "--from",
30+
"ipynb", "--to", "ipynb", "--markdown-headings=atx", "--show-changes",
31+
]
32+
jupytext_pre_commit_config["repos"][0]["hooks"][0]["additional_dependencies"] = [
33+
"nbconvert==5.0.8"
34+
]
35+
jupytext_pre_commit_config["repos"][0]["hooks"][1] = jupytext_pre_commit_config["repos"][0]["hooks"][0]
36+
jupytext_pre_commit_config["repos"][0]["hooks"][1]["args"] = [
37+
"--sync", "--pipe", "black", "--show-changes",
38+
]
39+
jupytext_pre_commit_config["repos"][0]["hooks"][1]["additional_dependencies"] = [
40+
"black==22.3.0", # Matches black hook below
41+
"nbformat==5.0.8", # for compatibility with the pandoc hook above
42+
]
43+
# Use python as language as we will need to install additional dependencies
44+
jupytext_pre_commit_config["repos"][0]["hooks"][0]["language"] = "python"
45+
jupytext_pre_commit_config["repos"][0]["hooks"][1]["language"] = "python"
46+
jupytext_pre_commit_config["repos"][1] = {
47+
"repo": "https://github.com/psf/black",
48+
"rev": "22.3.0",
49+
"hooks": [{"id": "black"}]
50+
}
51+
52+
with open(os.path.join(tmpdir, ".pre-commit-config.yaml"), 'w') as file:
53+
yaml.dump(jupytext_pre_commit_config, file)
4654

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

0 commit comments

Comments
 (0)