Skip to content

Commit

Permalink
Implement compare using difflib
Browse files Browse the repository at this point in the history
  • Loading branch information
mwouts committed Jul 9, 2019
1 parent 00e784a commit 7a0d27f
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 1 deletion.
19 changes: 18 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,22 @@
_BLANK_LINE = re.compile(r'^\s*$')


def _multilines(obj):
try:
return obj.splitlines()
except AttributeError:
return 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
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

0 comments on commit 7a0d27f

Please sign in to comment.