def test_sync_with_pre_commit_hook(tmpdir): # Init git and create a pre-commit hook git = git_in_tmpdir(tmpdir) git('init') git('status') 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')
def test_mutiple_cells_differ(): nb1 = new_notebook(cells=[new_code_cell(""), new_code_cell("2")]) nb2 = new_notebook(cells=[new_code_cell("1+1"), new_code_cell("2\n2")]) with pytest.raises(NotebookDifference) as exception_info: compare_notebooks(nb2, nb1, raise_on_first_difference=False) assert "Cells 1,2 differ" in exception_info.value.args[0]
def test_store_line_numbers(): notebook = matches_mystnb(dedent("""\ --- a: 1 --- abc +++ def ```{{{0}}} --- b: 2 --- c = 3 ``` xyz """).format(CODE_DIRECTIVE), store_line_numbers=True, return_nb=True) expected = { "nbformat": 4, "nbformat_minor": 4, "metadata": { "a": 1 }, "cells": [ { "cell_type": "markdown", "source": "abc", "metadata": { "_source_lines": [3, 4] }, }, { "cell_type": "markdown", "source": "def", "metadata": { "_source_lines": [5, 6] }, }, { "cell_type": "code", "metadata": { "b": 2, "_source_lines": [7, 12] }, "execution_count": None, "source": "c = 3", "outputs": [], }, { "cell_type": "markdown", "source": "xyz", "metadata": { "_source_lines": [12, 13] }, }, ], } notebook.nbformat_minor = 4 compare_notebooks(notebook, from_dict(expected))
def test_raise_on_different_cell_metadata(): ref = new_notebook(cells=[new_code_cell('1+1')]) test = new_notebook(cells=[new_code_cell('1+1', metadata={'metakey': 'value'})]) with pytest.raises(NotebookDifference): compare_notebooks(test, ref, 'py:light')
def test_does_not_raise_on_blank_line_removed(): ref = new_notebook(cells=[new_code_cell("1+1\n ")]) test = new_notebook(cells=[new_code_cell("1+1")]) compare_notebooks(test, ref, "py:light")
def test_raise_on_incomplete_markdown_cell(): ref = new_notebook(cells=[new_markdown_cell('Cell one\n\n\nsecond line')]) test = new_notebook(cells=[new_markdown_cell('Cell one')]) with pytest.raises(NotebookDifference): compare_notebooks(ref, test, ext='.md')
def test_does_not_raise_on_blank_line_removed(): ref = new_notebook(cells=[new_code_cell('1+1\n ')]) test = new_notebook(cells=[new_code_cell('1+1')]) compare_notebooks(ref, test, ext='.py', format_name='light')
def test_dont_raise_on_split_markdown_cell(): ref = new_notebook(cells=[new_markdown_cell('Cell one\n\n\nsecond line')]) test = new_notebook(cells=[new_markdown_cell('Cell one'), new_markdown_cell('second line')]) compare_notebooks(ref, test, allow_split_markdown=True)
def test_raise_on_different_cell_metadata(): ref = new_notebook(cells=[new_code_cell('1+1')]) test = new_notebook( cells=[new_code_cell('1+1', metadata={'metakey': 'value'})]) with pytest.raises(AssertionError): compare_notebooks(ref, test)
def test_raise_on_different_cell_count(): ref = new_notebook(cells=[new_markdown_cell('Cell one'), new_code_cell('Cell two')]) test = new_notebook(cells=[new_markdown_cell('Cell one')]) with pytest.raises(AssertionError): compare_notebooks(ref, test)
def test_raise_on_incomplete_markdown_cell(): ref = new_notebook(cells=[new_markdown_cell('Cell one\n\n\nsecond line')]) test = new_notebook(cells=[new_markdown_cell('Cell one')]) with pytest.raises(AssertionError): compare_notebooks(ref, test, allow_split_markdown=True)
def test_read_magics(text="// :vars\n"): nb = jupytext.reads(text, 'rs') compare_notebooks(nb, new_notebook(cells=[new_code_cell(':vars')])) compare(jupytext.writes(nb, 'rs'), text)
def test_raise_on_different_cell_type(raise_on_first_difference): ref = new_notebook(cells=[new_markdown_cell('Cell one'), new_code_cell('Cell two')]) test = new_notebook(cells=[new_markdown_cell('Cell one'), new_raw_cell('Cell two')]) with pytest.raises(NotebookDifference): compare_notebooks(test, ref, 'md', raise_on_first_difference=raise_on_first_difference)
def test_strict_raise_on_blank_line_removed(): ref = new_notebook(cells=[new_code_cell('1+1\n')]) test = new_notebook(cells=[new_code_cell('1+1')]) with pytest.raises(NotebookDifference): compare_notebooks(test, ref, 'py:light', allow_expected_differences=False)
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")
def test_read_simple_file(script="""# --- # title: Simple file # --- # %% [markdown] # This is a markdown cell # %% [md] # This is also a markdown cell # %% [raw] # This is a raw cell # %%% sub-cell title # This is a sub-cell # %%%% sub-sub-cell title # This is a sub-sub-cell # %% And now a code cell 1 + 2 + 3 + 4 5 6 # %%magic # this is a commented magic, not a cell 7 """, ): nb = jupytext.reads(script, "py:percent") compare_notebooks( new_notebook(cells=[ new_raw_cell("---\ntitle: Simple file\n---"), new_markdown_cell("This is a markdown cell"), new_markdown_cell("This is also a markdown cell", metadata={"region_name": "md"}), new_raw_cell("This is a raw cell"), new_code_cell( "# This is a sub-cell", metadata={ "title": "sub-cell title", "cell_depth": 1 }, ), new_code_cell( "# This is a sub-sub-cell", metadata={ "title": "sub-sub-cell title", "cell_depth": 2 }, ), new_code_cell( """1 + 2 + 3 + 4 5 6 %%magic # this is a commented magic, not a cell 7""", metadata={"title": "And now a code cell"}, ), ]), nb, ) script2 = jupytext.writes(nb, "py:percent") compare(script2, script)
def test_raw_with_metadata_2(no_jupytext_version_number, text="""# + [raw] key="value" # Raw cell # # Commented line """, notebook=new_notebook(cells=[new_raw_cell('Raw cell\n# Commented line', metadata={'key': 'value'})])): nb2 = jupytext.reads(text, 'py') compare_notebooks(nb2, notebook)
def test_raise_on_incomplete_markdown_cell(): ref = new_notebook(cells=[new_markdown_cell("Cell one\n\n\nsecond line")]) test = new_notebook(cells=[new_markdown_cell("Cell one")]) with pytest.raises(NotebookDifference): compare_notebooks(test, ref, "md")
def test_raise_on_different_cell_metadata(): ref = new_notebook(cells=[new_code_cell('1+1')]) test = new_notebook( cells=[new_code_cell('1+1', metadata={'metakey': 'value'})]) with pytest.raises(NotebookDifference): compare_notebooks(ref, test, ext='.py', format_name='light')
def test_raise_on_different_cell_metadata(): ref = new_notebook(cells=[new_code_cell("1+1")]) test = new_notebook( cells=[new_code_cell("1+1", metadata={"metakey": "value"})]) with pytest.raises(NotebookDifference): compare_notebooks(test, ref, "py:light")
def test_round_trip_markdown_cell_with_magic(): notebook = new_notebook(cells=[new_markdown_cell('IPython has magic commands like\n%quickref')], metadata={'jupytext': {'main_language': 'python'}}) text = jupytext.writes(notebook, 'py') notebook2 = jupytext.reads(text, 'py') compare_notebooks(notebook2, notebook)
def test_does_raise_on_split_markdown_cell(): ref = new_notebook(cells=[new_markdown_cell('Cell one\n\n\nsecond line')]) test = new_notebook(cells=[new_markdown_cell('Cell one'), new_markdown_cell('second line')]) with pytest.raises(NotebookDifference): compare_notebooks(test, ref, 'md')