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

Reproduce wrong paired paths when syncing with the --pre-commit flag #509

Merged
merged 3 commits into from
May 22, 2020
Merged
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
268 changes: 1 addition & 267 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import os
import sys
import time
import stat
from io import StringIO

try:
Expand All @@ -24,8 +23,7 @@
from jupytext.compare import compare_notebooks
from jupytext.paired_paths import paired_paths, InconsistentPath
from jupytext.formats import long_form_one_format, JupytextFormatError
from .utils import list_notebooks, skip_if_dict_is_not_ordered
from .utils import requires_black, requires_flake8, requires_sphinx_gallery
from .utils import list_notebooks, requires_sphinx_gallery
from .utils import requires_jupytext_installed, requires_pandoc, requires_myst


Expand Down Expand Up @@ -355,270 +353,6 @@ def test_convert_to_percent_format_and_keep_magics(nb_file, tmpdir):
compare_notebooks(nb2, nb1)


def git_in_tmpdir(tmpdir):
"""Return a function that will execute git instruction in the desired directory"""

def git(*args):
out = system("git", *args, cwd=str(tmpdir))
print(out)
return out

git("init")
git("status")
git("config", "user.name", "jupytext-test-cli")
git("config", "user.email", "jupytext@tests.com")

return git


@requires_jupytext_installed
@skip_if_dict_is_not_ordered
def test_pre_commit_hook(tmpdir):
tmp_ipynb = str(tmpdir.join("nb with spaces.ipynb"))
tmp_py = str(tmpdir.join("nb with spaces.py"))
nb = new_notebook(cells=[])

git = git_in_tmpdir(tmpdir)
hook = str(tmpdir.join(".git/hooks/pre-commit"))
with open(hook, "w") as fp:
fp.write("#!/bin/sh\n" "jupytext --to py:light --pre-commit\n")

st = os.stat(hook)
os.chmod(hook, st.st_mode | stat.S_IEXEC)

write(nb, tmp_ipynb)
assert os.path.isfile(tmp_ipynb)
assert not os.path.isfile(tmp_py)

git("add", "nb with spaces.ipynb")
git("status")
git("commit", "-m", "created")
git("status")

assert "nb with spaces.py" in git("ls-tree", "-r", "master", "--name-only")
assert os.path.isfile(tmp_py)


@requires_jupytext_installed
@skip_if_dict_is_not_ordered
def test_sync_with_pre_commit_hook(tmpdir):
# Init git and create a pre-commit hook
git = git_in_tmpdir(tmpdir)
hook = str(tmpdir.join(".git/hooks/pre-commit"))
with open(hook, "w") as fp:
fp.write("#!/bin/sh\n" "jupytext --sync --pre-commit\n")

st = os.stat(hook)
os.chmod(hook, st.st_mode | stat.S_IEXEC)

# Create a notebook that is not paired
tmp_ipynb = str(tmpdir.join("notebook.ipynb"))
tmp_md = str(tmpdir.join("notebook.md"))
nb = new_notebook(cells=[new_markdown_cell("A short notebook")])
write(nb, tmp_ipynb)
assert os.path.isfile(tmp_ipynb)
assert not os.path.isfile(tmp_md)

git("add", "notebook.ipynb")
git("status")
git("commit", "-m", "created")
git("status")

assert "notebook.ipynb" in git("ls-tree", "-r", "master", "--name-only")
assert "notebook.md" not in git("ls-tree", "-r", "master", "--name-only")

assert os.path.isfile(tmp_ipynb)
assert not os.path.exists(tmp_md)

# Pair the notebook
jupytext(["--set-formats", "ipynb,md", tmp_ipynb])

# Remove the md file (it will be regenerated by the pre-commit hook)
os.remove(tmp_md)

# Commit the ipynb file
git("add", "notebook.ipynb")
git("status")
git("commit", "-m", "paired")
git("status")

# The pre-commit script should have created and committed the md file
assert "notebook.ipynb" in git("ls-tree", "-r", "master", "--name-only")
assert "notebook.md" in git("ls-tree", "-r", "master", "--name-only")
assert os.path.isfile(tmp_md)
nb_md = read(tmp_md)
compare_notebooks(nb_md, nb)

# Edit the md file
with open(tmp_md) as fp:
md_text = fp.read()

with open(tmp_md, "w") as fp:
fp.write(md_text.replace("A short notebook", "Notebook was edited"))

# commit the md file
git("add", "notebook.md")
git("status")
git("commit", "-m", "edited md")
git("status")

# The pre-commit script should have sync and committed the ipynb file
assert "notebook.ipynb" in git("ls-tree", "-r", "master", "--name-only")
assert "notebook.md" in git("ls-tree", "-r", "master", "--name-only")

nb = read(tmp_ipynb)
compare(nb.cells, [new_markdown_cell("Notebook was edited")])

# create and commit a jpg file
tmp_jpg = str(tmpdir.join("image.jpg"))
with open(tmp_jpg, "wb") as fp:
fp.write(b"")
git("add", "image.jpg")
git("commit", "-m", "added image")


@requires_jupytext_installed
@skip_if_dict_is_not_ordered
def test_pre_commit_hook_in_subfolder(tmpdir):
tmp_ipynb = str(tmpdir.join("nb with spaces.ipynb"))
tmp_py = str(tmpdir.join("python", "nb with spaces.py"))
nb = new_notebook(cells=[])

git = git_in_tmpdir(tmpdir)
hook = str(tmpdir.join(".git/hooks/pre-commit"))
with open(hook, "w") as fp:
fp.write(
"#!/bin/sh\n" "jupytext --from ipynb --to python//py:light --pre-commit\n"
)

st = os.stat(hook)
os.chmod(hook, st.st_mode | stat.S_IEXEC)

write(nb, tmp_ipynb)
assert os.path.isfile(tmp_ipynb)
assert not os.path.isfile(tmp_py)

git("add", "nb with spaces.ipynb")
git("status")
git("commit", "-m", "created")
git("status")

assert "nb with spaces.py" in git("ls-tree", "-r", "master", "--name-only")
assert os.path.isfile(tmp_py)


@requires_jupytext_installed
@skip_if_dict_is_not_ordered
def test_pre_commit_hook_py_to_ipynb_and_md(tmpdir):
tmp_ipynb = str(tmpdir.join("nb with spaces.ipynb"))
tmp_py = str(tmpdir.join("nb with spaces.py"))
tmp_md = str(tmpdir.join("nb with spaces.md"))
nb = new_notebook(cells=[])

git = git_in_tmpdir(tmpdir)
hook = str(tmpdir.join(".git/hooks/pre-commit"))
with open(hook, "w") as fp:
fp.write(
"#!/bin/sh\n"
"jupytext --from py:light --to ipynb --pre-commit\n"
"jupytext --from py:light --to md --pre-commit\n"
)

st = os.stat(hook)
os.chmod(hook, st.st_mode | stat.S_IEXEC)

write(nb, tmp_py)
assert os.path.isfile(tmp_py)
assert not os.path.isfile(tmp_ipynb)
assert not os.path.isfile(tmp_md)

git("add", "nb with spaces.py")
git("status")
git("commit", "-m", "created")
git("status")

assert "nb with spaces.ipynb" in git("ls-tree", "-r", "master", "--name-only")
assert "nb with spaces.md" in git("ls-tree", "-r", "master", "--name-only")

assert os.path.isfile(tmp_ipynb)
assert os.path.isfile(tmp_md)


@requires_black
@requires_flake8
@requires_jupytext_installed
@pytest.mark.parametrize("nb_file", list_notebooks("ipynb_py")[:1])
def test_pre_commit_hook_sync_black_flake8(tmpdir, nb_file):
# Load real notebook metadata to get the 'auto' extension in --pipe-fmt to work
metadata = read(nb_file).metadata

git = git_in_tmpdir(tmpdir)
hook = str(tmpdir.join(".git/hooks/pre-commit"))
with open(hook, "w") as fp:
fp.write(
"#!/bin/sh\n"
"# Pair ipynb notebooks to a python file, reformat content with black, and run flake8\n"
"# Note: this hook only acts on ipynb files. When pulling, run 'jupytext --sync' to "
"update the ipynb file.\n"
"jupytext --pre-commit --from ipynb --set-formats ipynb,py --pipe black --check flake8\n"
)

st = os.stat(hook)
os.chmod(hook, st.st_mode | stat.S_IEXEC)

tmp_ipynb = str(tmpdir.join("notebook.ipynb"))
tmp_py = str(tmpdir.join("notebook.py"))
nb = new_notebook(cells=[new_code_cell(source="1+ 1")], metadata=metadata)

write(nb, tmp_ipynb)
git("add", "notebook.ipynb")
git("status")
git("commit", "-m", "created")
git("status")
assert os.path.isfile(tmp_py)
assert os.path.isfile(tmp_ipynb)
with open(tmp_py) as fp:
assert fp.read().splitlines()[-1] == "1 + 1"

nb = new_notebook(
cells=[new_code_cell(source='"""trailing \nwhitespace"""')], metadata=metadata
)
write(nb, tmp_ipynb)
git("add", "notebook.ipynb")
git("status")
with pytest.raises(SystemExit): # not flake8
git("commit", "-m", "created")


def test_manual_call_of_pre_commit_hook(tmpdir):
tmp_ipynb = str(tmpdir.join("notebook.ipynb"))
tmp_py = str(tmpdir.join("notebook.py"))
nb = new_notebook(cells=[])
os.chdir(str(tmpdir))

def system_in_tmpdir(*args):
return system(*args, cwd=str(tmpdir))

git = git_in_tmpdir(tmpdir)

def hook():
with mock.patch("jupytext.cli.system", system_in_tmpdir):
jupytext(["--to", "py", "--pre-commit"])

write(nb, tmp_ipynb)
assert os.path.isfile(tmp_ipynb)
assert not os.path.isfile(tmp_py)

git("add", "notebook.ipynb")
git("status")
hook()
git("commit", "-m", "created")
git("status")

assert "notebook.py" in git("ls-tree", "-r", "master", "--name-only")
assert os.path.isfile(tmp_py)


@pytest.mark.parametrize("py_file", list_notebooks("python"))
def test_set_formats(py_file, tmpdir):
tmp_py = str(tmpdir.join("notebook.py"))
Expand Down
Loading