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

Use test env for pre-commit tests #1214

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
33 changes: 33 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from pathlib import Path

import pytest
import yaml
from jupyter_client.kernelspec import find_kernel_specs, get_kernel_spec
from nbformat.v4 import nbbase
from nbformat.v4.nbbase import (
Expand Down Expand Up @@ -63,6 +64,38 @@ def jupytext_repo_rev(jupytext_repo_root):
return system("git", "rev-parse", "HEAD", cwd=jupytext_repo_root).strip()


@pytest.fixture
def jupytext_pre_commit_config(jupytext_repo_root):
"""The local revision of this repo, to use in .pre-commit-config.yaml in tests"""
# Read pre-commit-hooks.yaml file
#
# Setting language to system will ensure that pre-commit assumes that we
# provision correct environment. This is the case when we run unit tests as we
# make a developmental install of jupytext before running unit tests. So there is
# an "test" environment that is provisioned and we use this current environment
# to run pre-commit tests.
#
# When there are additional_dependencies, we override this in individual test to
# python so that pre-commit will create an environment and install those
# additional dependencies in that environment.
#
# This strategy will enable us to directly test the pre-commit hook config with
# current version of jupytext. It also avoid re-installing jupytext in pre-commit
# config and thus tests run faster.
with open(os.path.join(jupytext_repo_root, ".pre-commit-hooks.yaml")) as file:
pre_commit_hooks = yaml.safe_load(file)
pre_commit_hooks[0]["language"] = "system"
pre_commit_config = {
"repos": [
{
"repo": "local",
"hooks": pre_commit_hooks,
}
]
}
return pre_commit_config


@pytest.fixture()
def python_notebook():
return new_notebook(
Expand Down
31 changes: 20 additions & 11 deletions tests/external/pre_commit/test_pre_commit_0_ipynb_to_py.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import os

import pytest
import yaml
from git.exc import HookExecutionError
from nbformat.v4.nbbase import new_markdown_cell, new_notebook
from pre_commit.main import main as pre_commit
Expand All @@ -9,25 +12,31 @@


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

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

# write test notebook and output file
nb = new_notebook(cells=[new_markdown_cell("A short notebook")])
Expand Down
16 changes: 7 additions & 9 deletions tests/external/pre_commit/test_pre_commit_1_sync.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
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
Expand All @@ -15,17 +17,13 @@ def test_pre_commit_hook_sync(
tmp_repo,
jupytext_repo_root,
jupytext_repo_rev,
jupytext_pre_commit_config,
python_notebook,
):
pre_commit_config_yaml = f"""
repos:
- repo: {jupytext_repo_root}
rev: {jupytext_repo_rev}
hooks:
- id: jupytext
args: [--sync]
"""
tmpdir.join(".pre-commit-config.yaml").write(pre_commit_config_yaml)
# 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"])
Expand Down
16 changes: 7 additions & 9 deletions tests/external/pre_commit/test_pre_commit_1_sync_with_config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import os

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
Expand All @@ -12,17 +15,12 @@ def test_pre_commit_hook_sync_with_config(
tmp_repo,
jupytext_repo_root,
jupytext_repo_rev,
jupytext_pre_commit_config,
python_notebook,
):
pre_commit_config_yaml = f"""
repos:
- repo: {jupytext_repo_root}
rev: {jupytext_repo_rev}
hooks:
- id: jupytext
args: [--sync]
"""
tmpdir.join(".pre-commit-config.yaml").write(pre_commit_config_yaml)
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"])
Expand Down
26 changes: 12 additions & 14 deletions tests/external/pre_commit/test_pre_commit_1_sync_with_no_config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import os
from copy import deepcopy

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
Expand All @@ -16,23 +18,18 @@ def test_pre_commit_hook_sync_with_no_config(
tmp_repo,
jupytext_repo_root,
jupytext_repo_rev,
jupytext_pre_commit_config,
notebook_with_outputs,
):
"""In this test we reproduce the setting from https://github.com/mwouts/jupytext/issues/967"""
pre_commit_config_yaml = f"""
repos:
- repo: {jupytext_repo_root}
rev: {jupytext_repo_rev}
hooks:
- id: jupytext
args: [
'--sync',
'--set-formats',
'ipynb,py:percent',
'--show-changes',
'--'
jupytext_pre_commit_config["repos"][0]["hooks"][0]["args"] = [
"--sync",
"--set-formats",
"ipynb,py:percent",
"--show-changes",
"--",
]
"""

# Save a sample notebook with outputs in Jupyter
nb = deepcopy(notebook_with_outputs)
(tmpdir / "notebooks").mkdir()
Expand All @@ -45,7 +42,8 @@ def test_pre_commit_hook_sync_with_no_config(
tmp_repo.index.commit("Notebook with outputs")

# Configure the pre-commit hook
tmpdir.join(".pre-commit-config.yaml").write(pre_commit_config_yaml)
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"])

Expand Down
28 changes: 14 additions & 14 deletions tests/external/pre_commit/test_pre_commit_2_sync_nbstripout.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import os

import pytest
import yaml
from git.exc import HookExecutionError
from pre_commit.main import main as pre_commit

Expand All @@ -12,24 +15,21 @@ def test_pre_commit_hook_sync_nbstripout(
tmp_repo,
jupytext_repo_root,
jupytext_repo_rev,
jupytext_pre_commit_config,
notebook_with_outputs,
):
"""Here we sync the ipynb notebook with a Markdown file and also apply nbstripout."""
pre_commit_config_yaml = f"""
repos:
- repo: {jupytext_repo_root}
rev: {jupytext_repo_rev}
hooks:
- id: jupytext
args: [--sync]

- repo: https://github.com/kynan/nbstripout
rev: 0.5.0
hooks:
- id: nbstripout
"""
jupytext_pre_commit_config["repos"][0]["hooks"][0]["args"] = ["--sync"]
jupytext_pre_commit_config["repos"].append(
{
"repo": "https://github.com/kynan/nbstripout",
"rev": "0.5.0",
"hooks": [{"id": "nbstripout"}],
}
)

tmpdir.join(".pre-commit-config.yaml").write(pre_commit_config_yaml)
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"])
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import os

import pytest
import yaml
from git.exc import HookExecutionError
from pre_commit.main import main as pre_commit

Expand All @@ -11,30 +14,37 @@ def test_pre_commit_hook_sync_black_nbstripout(
tmp_repo,
jupytext_repo_root,
jupytext_repo_rev,
jupytext_pre_commit_config,
notebook_with_outputs,
):
"""Here we sync the ipynb notebook with a py:percent file and also apply black and nbstripout."""
pre_commit_config_yaml = f"""
repos:
- repo: {jupytext_repo_root}
rev: {jupytext_repo_rev}
hooks:
- id: jupytext
args: [--sync, --pipe, black]
additional_dependencies:
- black==22.3.0 # Matches hook

- repo: https://github.com/psf/black
rev: 22.3.0
hooks:
- id: black
jupytext_pre_commit_config["repos"][0]["hooks"][0]["args"] = [
"--sync",
"--pipe",
"black",
]
jupytext_pre_commit_config["repos"][0]["hooks"][0]["additional_dependencies"] = [
"black==22.3.0",
]
# Use python as language as we will need to install additional dependencies
jupytext_pre_commit_config["repos"][0]["hooks"][0]["language"] = "python"
jupytext_pre_commit_config["repos"].append(
{
"repo": "https://github.com/kynan/nbstripout",
"rev": "0.5.0",
"hooks": [{"id": "nbstripout"}],
}
)
jupytext_pre_commit_config["repos"].append(
{
"repo": "https://github.com/psf/black",
"rev": "22.3.0",
"hooks": [{"id": "black"}],
}
)

- repo: https://github.com/kynan/nbstripout
rev: 0.5.0
hooks:
- id: nbstripout
"""
tmpdir.join(".pre-commit-config.yaml").write(pre_commit_config_yaml)
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"])
Expand Down
28 changes: 17 additions & 11 deletions tests/external/pre_commit/test_pre_commit_4_sync_execute.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import os

import pytest
import yaml
from git.exc import HookExecutionError
from nbformat.v4.nbbase import new_code_cell
from pre_commit.main import main as pre_commit
Expand All @@ -13,21 +16,24 @@ def test_pre_commit_hook_sync_execute(
tmp_repo,
jupytext_repo_root,
jupytext_repo_rev,
jupytext_pre_commit_config,
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, --show-changes]
additional_dependencies:
- nbconvert
"""
tmpdir.join(".pre-commit-config.yaml").write(pre_commit_config_yaml)
jupytext_pre_commit_config["repos"][0]["hooks"][0]["args"] = [
"--sync",
"--execute",
"--show-changes",
]
jupytext_pre_commit_config["repos"][0]["hooks"][0]["additional_dependencies"] = [
"nbconvert"
]
# Use python as language as we will need to install additional dependencies
jupytext_pre_commit_config["repos"][0]["hooks"][0]["language"] = "python"

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"])
Expand Down
Loading
Loading