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

Remove dependency on testfixtures #283

Merged
merged 5 commits into from
Jul 9, 2019
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
2 changes: 2 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ Release History
- New ``--execute`` option in Jupytext CLI (#231)
- The ``--set-formats`` option in Jupytext CLI also triggers ``--sync``, allowing shorter commands.
- ``jupytext``'s ``read`` and ``write`` functions can be used as drop-in replacements for ``nbformat``'s ones (#262).
- ``jupytext --sync`` will now skip unpaired notebooks (#281).
- The JupyterLab extension was updated. It now works on on text files (#213) and has a new option to include
(or not) the metadata in the text representation of the notebook.
- Jupytext's contents manager class is derived dynamically from the default CM class for compatibility with
``jupyter_server`` (#270)
- Removed dependency on ``testfixtures`` (#279)

**BugFixes**

Expand Down
21 changes: 20 additions & 1 deletion jupytext/compare.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
"""Compare two Jupyter notebooks"""

import re
import json
import difflib
from copy import copy
from testfixtures import compare
from .cell_metadata import _IGNORE_CELL_METADATA
from .header import _DEFAULT_NOTEBOOK_METADATA
from .metadata_filter import filter_metadata
Expand All @@ -13,6 +14,24 @@
_BLANK_LINE = re.compile(r'^\s*$')


def _multilines(obj):
try:
return obj.splitlines()
except AttributeError:
# Remove the final blank space on Python 2.7
# return json.dumps(obj, indent=True, sort_keys=True).splitlines()
return [line.rstrip() for line in json.dumps(obj, indent=True, sort_keys=True).splitlines()]


def compare(actual, expected):
"""Compare two strings, lists or dict-like objects"""
if actual != expected:
raise AssertionError('\n' + '\n'.join(difflib.unified_diff(
_multilines(actual),
_multilines(expected),
'first', 'second', lineterm='')))


def filtered_cell(cell, preserve_outputs, cell_metadata_filter):
"""Cell type, metadata and source from given cell"""
metadata = copy(cell.metadata)
Expand Down
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
nbformat>=4.0.0
pyyaml
mock
testfixtures
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
('share/jupyter/lab/extensions', ['packages/labextension/jupyterlab-jupytext-1.0.0.tgz'])],
entry_points={'console_scripts': ['jupytext = jupytext.cli:jupytext_cli']},
tests_require=['pytest'],
install_requires=['nbformat>=4.0.0', 'mock', 'pyyaml', 'testfixtures'],
install_requires=['nbformat>=4.0.0', 'mock', 'pyyaml'],
license='MIT',
classifiers=['Development Status :: 5 - Production/Stable',
'License :: OSI Approved :: MIT License',
Expand Down
2 changes: 1 addition & 1 deletion tests/test_active_cells.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import mock
import pytest
from testfixtures import compare
from jupytext.compare import compare
import jupytext
from .utils import skip_if_dict_is_not_ordered

Expand Down
2 changes: 1 addition & 1 deletion tests/test_black.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
import pytest
from shutil import copyfile
from testfixtures import compare
from jupytext.compare import compare
from nbformat.v4.nbbase import new_notebook, new_code_cell
from .utils import list_notebooks, requires_black, requires_flake8, requires_autopep8

Expand Down
2 changes: 1 addition & 1 deletion tests/test_cell_metadata.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import pytest
from testfixtures import compare
from jupytext.compare import compare
from jupytext.cell_metadata import rmd_options_to_metadata, metadata_to_rmd_options, parse_rmd_options
from jupytext.cell_metadata import _IGNORE_CELL_METADATA, RMarkdownOptionParsingError, try_eval_metadata
from jupytext.cell_metadata import json_options_to_metadata, metadata_to_json_options
Expand Down
2 changes: 1 addition & 1 deletion tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import nbformat
import itertools
from shutil import copyfile
from testfixtures import compare
from jupytext.compare import compare
from argparse import ArgumentTypeError
from nbformat.v4.nbbase import new_notebook, new_markdown_cell, new_code_cell
from jupyter_client.kernelspec import find_kernel_specs, get_kernel_spec
Expand Down
83 changes: 83 additions & 0 deletions tests/test_compare.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,91 @@
import pytest
from nbformat.v4.nbbase import new_notebook, new_markdown_cell, new_code_cell, new_raw_cell
from jupytext.compare import compare
from jupytext.compare import compare_notebooks, NotebookDifference, test_round_trip_conversion as round_trip_conversion


def notebook_metadata():
return {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": True,
"sideBar": True,
"skip_h1_title": False,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": False,
"toc_position": {},
"toc_section_display": True,
"toc_window_display": False
}
}


@pytest.fixture()
def notebook_1():
return new_notebook(
metadata=notebook_metadata(),
cells=[new_markdown_cell('First markdown cell'),
new_code_cell('1 + 1'),
new_markdown_cell('Second markdown cell')])


@pytest.fixture()
def notebook_2():
metadata = notebook_metadata()
metadata['language_info']['version'] = '3.6.8'
return new_notebook(
metadata=metadata,
cells=[new_markdown_cell('First markdown cell'),
new_code_cell('1 + 1'),
new_markdown_cell('Modified markdown cell')])


def test_compare_on_notebooks(notebook_1, notebook_2):
with pytest.raises(AssertionError) as err:
compare(notebook_1, notebook_2)

assert str(err.value) == """
--- first
+++ second
@@ -15,7 +15,7 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": "Second markdown cell"
+ "source": "Modified markdown cell"
}
],
"metadata": {
@@ -34,7 +34,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
- "version": "3.7.3"
+ "version": "3.6.8"
},
"toc": {
"base_numbering": 1,"""


def test_raise_on_different_metadata():
ref = new_notebook(metadata={'kernelspec': {'language': 'python', 'name': 'python', 'display_name': 'Python'}},
cells=[new_markdown_cell('Cell one')])
Expand Down
47 changes: 46 additions & 1 deletion tests/test_contentsmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import shutil
from nbformat.v4.nbbase import new_notebook, new_markdown_cell, new_code_cell
from tornado.web import HTTPError
from testfixtures import compare
from jupytext.compare import compare
import jupytext
from jupytext.jupytext import writes, write, read
from jupytext.compare import compare_notebooks
Expand Down Expand Up @@ -51,6 +51,51 @@ def test_rename_inconsistent_path(tmpdir):
assert os.path.isfile(org_file)


def test_pair_unpair_notebook(tmpdir):
tmp_ipynb = 'notebook.ipynb'
tmp_md = 'notebook.md'

nb = new_notebook(
metadata={'kernelspec': {'display_name': 'Python3', 'language': 'python', 'name': 'python3'}},
cells=[new_code_cell('1 + 1', outputs=[
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
])])

cm = jupytext.TextFileContentsManager()
cm.root_dir = str(tmpdir)

# save notebook
cm.save(model=dict(type='notebook', content=nb), path=tmp_ipynb)
assert not os.path.isfile(str(tmpdir.join(tmp_md)))

# pair notebook
nb['metadata']['jupytext'] = {'formats': 'ipynb,md'}
cm.save(model=dict(type='notebook', content=nb), path=tmp_ipynb)
assert os.path.isfile(str(tmpdir.join(tmp_md)))

# reload and get outputs
nb2 = cm.get(tmp_md)['content']
compare_notebooks(nb2, nb)

# unpair and save as md
del nb['metadata']['jupytext']
cm.save(model=dict(type='notebook', content=nb), path=tmp_md)
nb2 = cm.get(tmp_md)['content']

# we get no outputs here
compare_notebooks(nb2, nb, compare_outputs=False)
assert len(nb2.cells[0]['outputs']) == 0


@skip_if_dict_is_not_ordered
@pytest.mark.parametrize('nb_file', list_notebooks('ipynb', skip='66'))
def test_load_save_rename(nb_file, tmpdir):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_escape_magics.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest
import mock
from nbformat.v4.nbbase import new_code_cell, new_notebook
from testfixtures import compare
from jupytext.compare import compare
from jupytext.magics import comment_magic, uncomment_magic, unesc, is_magic
from jupytext.compare import compare_notebooks
import jupytext
Expand Down
2 changes: 1 addition & 1 deletion tests/test_formats.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import pytest
from testfixtures import compare
from jupytext.compare import compare
from nbformat.v4.nbbase import new_notebook
import jupytext
from jupytext.formats import guess_format, divine_format, read_format_from_metadata, rearrange_jupytext_metadata
Expand Down
2 changes: 1 addition & 1 deletion tests/test_header.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from nbformat.v4.nbbase import new_notebook, new_raw_cell, new_markdown_cell
import mock
from testfixtures import compare
from jupytext.compare import compare
import jupytext
from jupytext.header import uncomment_line, header_to_metadata_and_cell, metadata_and_cell_to_header
from jupytext.formats import get_format_implementation
Expand Down
2 changes: 1 addition & 1 deletion tests/test_mirror.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import mock
import pytest
from nbformat.v4.nbbase import new_notebook
from testfixtures import compare
from jupytext.compare import compare

import jupytext
from jupytext.compare import compare_notebooks, combine_inputs_with_outputs
Expand Down
14 changes: 7 additions & 7 deletions tests/test_paired_paths.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import pytest
from testfixtures import compare
from jupytext.compare import compare
from jupytext.contentsmanager import TextFileContentsManager
from jupytext.paired_paths import InconsistentPath, paired_paths, base_path
from jupytext.formats import long_form_one_format, long_form_multiple_formats, short_form_multiple_formats
Expand All @@ -8,8 +8,8 @@
def test_simple_pair():
formats = long_form_multiple_formats('ipynb,py')
expected_paths = ['notebook.ipynb', 'notebook.py']
compare(zip(expected_paths, formats), paired_paths('notebook.ipynb', 'ipynb', formats))
compare(zip(expected_paths, formats), paired_paths('notebook.py', 'py', formats))
compare(list(zip(expected_paths, formats)), paired_paths('notebook.ipynb', 'ipynb', formats))
compare(list(zip(expected_paths, formats)), paired_paths('notebook.py', 'py', formats))


def test_base_path():
Expand All @@ -23,7 +23,7 @@ def test_many_and_suffix():
formats = long_form_multiple_formats('ipynb,.pct.py,_lgt.py')
expected_paths = ['notebook.ipynb', 'notebook.pct.py', 'notebook_lgt.py']
for fmt, path in zip(formats, expected_paths):
compare(zip(expected_paths, formats), paired_paths(path, fmt, formats))
compare(list(zip(expected_paths, formats)), paired_paths(path, fmt, formats))

with pytest.raises(InconsistentPath):
paired_paths('wrong_suffix.py', 'py', formats)
Expand All @@ -41,12 +41,12 @@ def test_prefix_and_suffix():
'parent/script_folder/NOTEBOOK_NAME_in_percent_format.py',
'parent/script_folder/NOTEBOOK_NAME_in_light_format.py']
for fmt, path in zip(formats, expected_paths):
compare(zip(expected_paths, formats), paired_paths(path, fmt, formats))
compare(list(zip(expected_paths, formats)), paired_paths(path, fmt, formats))

# without the parent folder
expected_paths = [path[7:] for path in expected_paths]
for fmt, path in zip(formats, expected_paths):
compare(zip(expected_paths, formats), paired_paths(path, fmt, formats))
compare(list(zip(expected_paths, formats)), paired_paths(path, fmt, formats))

# Not the expected parent folder
with pytest.raises(InconsistentPath):
Expand All @@ -69,7 +69,7 @@ def test_prefix_on_root_174():

expected_paths = ['/Untitled.ipynb', '/python/Untitled.py']
for fmt, path in zip(formats, expected_paths):
compare(zip(expected_paths, formats), paired_paths(path, fmt, formats))
compare(list(zip(expected_paths, formats)), paired_paths(path, fmt, formats))


def test_duplicated_paths():
Expand Down
2 changes: 1 addition & 1 deletion tests/test_pep8.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import pytest
from testfixtures import compare
from jupytext.compare import compare
from jupytext import read, reads, writes
from jupytext.pep8 import next_instruction_is_function_or_class, cell_ends_with_function_or_class
from jupytext.pep8 import cell_ends_with_code, cell_has_code, pep8_lines_between_cells
Expand Down
2 changes: 1 addition & 1 deletion tests/test_preserve_empty_cells.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import pytest
from testfixtures import compare
from jupytext.compare import compare
from nbformat.v4.nbbase import new_notebook, new_code_cell, new_markdown_cell
import jupytext
from jupytext.compare import compare_notebooks
Expand Down
2 changes: 1 addition & 1 deletion tests/test_read_all_py.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import pytest
from testfixtures import compare
from jupytext.compare import compare
import jupytext
from .utils import list_notebooks

Expand Down
2 changes: 1 addition & 1 deletion tests/test_read_folding_markers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from testfixtures import compare
from jupytext.compare import compare
import jupytext


Expand Down
2 changes: 1 addition & 1 deletion tests/test_read_simple_R.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-

import pytest
from testfixtures import compare
from jupytext.compare import compare
import jupytext


Expand Down
2 changes: 1 addition & 1 deletion tests/test_read_simple_bare.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from nbformat.v4.nbbase import new_notebook, new_code_cell, new_markdown_cell
from testfixtures import compare
from jupytext.compare import compare
from jupytext import reads, writes
from jupytext.combine import combine_inputs_with_outputs

Expand Down
2 changes: 1 addition & 1 deletion tests/test_read_simple_clojure.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-

from testfixtures import compare
from jupytext.compare import compare
import jupytext


Expand Down
2 changes: 1 addition & 1 deletion tests/test_read_simple_hydrogen.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-

from testfixtures import compare
from jupytext.compare import compare
import jupytext


Expand Down
2 changes: 1 addition & 1 deletion tests/test_read_simple_ipynb.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import nbformat
from nbformat.v4.nbbase import new_notebook
from testfixtures import compare
from jupytext.compare import compare
import jupytext


Expand Down
Loading