Skip to content

Commit

Permalink
compare actual expected
Browse files Browse the repository at this point in the history
  • Loading branch information
mwouts committed Oct 29, 2019
1 parent 6bf09af commit 4d6bef1
Show file tree
Hide file tree
Showing 30 changed files with 148 additions and 148 deletions.
3 changes: 1 addition & 2 deletions jupytext/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ def parse_jupytext_args(args=None):
class RawTextArgumentDefaultsHelpFormatter(argparse.RawTextHelpFormatter,
argparse.ArgumentDefaultsHelpFormatter):
"""Keep the raw formatting in command line help, plus show the default values"""
pass

parser = argparse.ArgumentParser(
description='Jupyter notebooks as markdown documents, Julia, Python or R scripts',
Expand Down Expand Up @@ -398,7 +397,7 @@ def jupytext_single_file(nb_file, args, log):
notebook = reads(dest_text, fmt=dest_fmt)

text = writes(notebook, fmt=fmt)
compare(org_text, text)
compare(text, org_text)

except (NotebookDifference, AssertionError) as err:
sys.stdout.write('{}: {}'.format(nb_file, str(err)))
Expand Down
17 changes: 9 additions & 8 deletions jupytext/compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ def compare(actual, expected, return_diff=False):
diff = '\n'.join(difflib.unified_diff(
_multilines(expected),
_multilines(actual),
'first', 'second', lineterm=''))
'actual', 'expected', lineterm=''))
if return_diff:
return diff
raise AssertionError('\n' + diff)
return ''
if return_diff:
return ''


def filtered_cell(cell, preserve_outputs, cell_metadata_filter):
Expand Down Expand Up @@ -145,7 +146,7 @@ def compare_notebooks(notebook_actual, notebook_expected, fmt=None, allow_expect
if ref_cell.metadata != test_cell.metadata:
if raise_on_first_difference:
try:
compare(ref_cell.metadata, test_cell.metadata)
compare(test_cell.metadata, ref_cell.metadata)
except AssertionError as error:
raise NotebookDifference("Metadata differ on {} cell #{}: {}\nCell content:\n{}"
.format(test_cell.cell_type, i, str(error), ref_cell.source))
Expand All @@ -162,7 +163,7 @@ def compare_notebooks(notebook_actual, notebook_expected, fmt=None, allow_expect
if ref_lines != test_lines:
if raise_on_first_difference:
try:
compare('\n'.join(ref_lines), '\n'.join(test_lines))
compare('\n'.join(test_lines), '\n'.join(ref_lines))
except AssertionError as error:
raise NotebookDifference("Cell content differ on {} cell #{}: {}"
.format(test_cell.cell_type, i, str(error)))
Expand All @@ -173,7 +174,7 @@ def compare_notebooks(notebook_actual, notebook_expected, fmt=None, allow_expect
if not same_content(ref_cell.source, test_cell.source, allow_removed_final_blank_line):
if ref_cell.source != test_cell.source:
if raise_on_first_difference:
diff = compare(ref_cell.source, test_cell.source, return_diff=True)
diff = compare(test_cell.source, ref_cell.source, return_diff=True)
raise NotebookDifference("Cell content differ on {} cell #{}: {}"
.format(test_cell.cell_type, i, diff))
modified_cells.add(i)
Expand All @@ -192,7 +193,7 @@ def compare_notebooks(notebook_actual, notebook_expected, fmt=None, allow_expect
cell_metadata_filter=cell_metadata_filter)

try:
compare(ref_cell, test_cell)
compare(test_cell, ref_cell)
except AssertionError as error:
if raise_on_first_difference:
raise NotebookDifference("Cell outputs differ on {} cell #{}: {}"
Expand All @@ -217,8 +218,8 @@ def compare_notebooks(notebook_actual, notebook_expected, fmt=None, allow_expect
# Compare notebook metadata
modified_metadata = False
try:
compare(filtered_notebook_metadata(notebook_expected),
filtered_notebook_metadata(notebook_actual))
compare(filtered_notebook_metadata(notebook_actual),
filtered_notebook_metadata(notebook_expected))
except AssertionError as error:
if raise_on_first_difference:
raise NotebookDifference("Notebook metadata differ: {}".format(str(error)))
Expand Down
16 changes: 8 additions & 8 deletions tests/test_active_cells.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
def test_active_all(ext, no_jupytext_version_number):
nb = jupytext.reads(HEADER[ext] + ACTIVE_ALL[ext], ext)
assert len(nb.cells) == 1
compare(ACTIVE_ALL[ext], jupytext.writes(nb, ext))
compare(jupytext.writes(nb, ext), ACTIVE_ALL[ext])
compare(nb.cells[0], ACTIVE_ALL['.ipynb'])


Expand Down Expand Up @@ -92,7 +92,7 @@ def test_active_all(ext, no_jupytext_version_number):
def test_active_ipynb(ext, no_jupytext_version_number):
nb = jupytext.reads(HEADER[ext] + ACTIVE_IPYNB[ext], ext)
assert len(nb.cells) == 1
compare(ACTIVE_IPYNB[ext], jupytext.writes(nb, ext))
compare(jupytext.writes(nb, ext), ACTIVE_IPYNB[ext])
compare(nb.cells[0], ACTIVE_IPYNB['.ipynb'])


Expand Down Expand Up @@ -127,7 +127,7 @@ def test_active_ipynb(ext, no_jupytext_version_number):
def test_active_ipynb_rmd_using_tags(ext, no_jupytext_version_number):
nb = jupytext.reads(HEADER[ext] + ACTIVE_IPYNB_RMD_USING_TAG[ext], ext)
assert len(nb.cells) == 1
compare(ACTIVE_IPYNB_RMD_USING_TAG[ext], jupytext.writes(nb, ext))
compare(jupytext.writes(nb, ext), ACTIVE_IPYNB_RMD_USING_TAG[ext])
compare(nb.cells[0], ACTIVE_IPYNB_RMD_USING_TAG['.ipynb'])


Expand All @@ -147,7 +147,7 @@ def test_active_ipynb_rmd_using_tags(ext, no_jupytext_version_number):
def test_active_ipynb_rspin(no_jupytext_version_number):
nb = jupytext.reads(ACTIVE_IPYNB_RSPIN['.R'], 'R:spin')
assert len(nb.cells) == 1
compare(ACTIVE_IPYNB_RSPIN['.R'], jupytext.writes(nb, 'R:spin'))
compare(jupytext.writes(nb, 'R:spin'), ACTIVE_IPYNB_RSPIN['.R'])
compare(nb.cells[0], ACTIVE_IPYNB_RSPIN['.ipynb'])


Expand Down Expand Up @@ -178,7 +178,7 @@ def test_active_ipynb_rspin(no_jupytext_version_number):
def test_active_py_ipynb(ext, no_jupytext_version_number):
nb = jupytext.reads(HEADER[ext] + ACTIVE_PY_IPYNB[ext], ext)
assert len(nb.cells) == 1
compare(ACTIVE_PY_IPYNB[ext], jupytext.writes(nb, ext))
compare(jupytext.writes(nb, ext), ACTIVE_PY_IPYNB[ext])
compare(nb.cells[0], ACTIVE_PY_IPYNB['.ipynb'])


Expand All @@ -205,7 +205,7 @@ def test_active_py_ipynb(ext, no_jupytext_version_number):
def test_active_py_r_ipynb(ext, no_jupytext_version_number):
nb = jupytext.reads(HEADER[ext] + ACTIVE_PY_R_IPYNB[ext], ext)
assert len(nb.cells) == 1
compare(ACTIVE_PY_R_IPYNB[ext], jupytext.writes(nb, ext))
compare(jupytext.writes(nb, ext), ACTIVE_PY_R_IPYNB[ext])
compare(nb.cells[0], ACTIVE_PY_R_IPYNB['.ipynb'])


Expand All @@ -229,7 +229,7 @@ def test_active_py_r_ipynb(ext, no_jupytext_version_number):
def test_active_rmd(ext, no_jupytext_version_number):
nb = jupytext.reads(HEADER[ext] + ACTIVE_RMD[ext], ext)
assert len(nb.cells) == 1
compare(ACTIVE_RMD[ext], jupytext.writes(nb, ext))
compare(jupytext.writes(nb, ext), ACTIVE_RMD[ext])
compare(nb.cells[0], ACTIVE_RMD['.ipynb'])


Expand All @@ -254,5 +254,5 @@ def test_active_rmd(ext, no_jupytext_version_number):
def test_active_not_include_rmd(ext, no_jupytext_version_number):
nb = jupytext.reads(ACTIVE_NOT_INCLUDE_RMD[ext], ext)
assert len(nb.cells) == 1
compare(ACTIVE_NOT_INCLUDE_RMD[ext], jupytext.writes(nb, ext))
compare(jupytext.writes(nb, ext), ACTIVE_NOT_INCLUDE_RMD[ext])
compare(nb.cells[0], ACTIVE_NOT_INCLUDE_RMD['.ipynb'])
14 changes: 7 additions & 7 deletions tests/test_black.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def test_pipe_into_black():
nb_dest = new_notebook(cells=[new_code_cell('1 + 1')])

nb_pipe = pipe_notebook(nb_org, 'black')
compare(nb_dest, nb_pipe)
compare(nb_pipe, nb_dest)


@requires_autopep8
Expand All @@ -68,7 +68,7 @@ def test_pipe_into_autopep8():
nb_dest = new_notebook(cells=[new_code_cell('1 + 1')])

nb_pipe = pipe_notebook(nb_org, 'autopep8 -')
compare(nb_dest, nb_pipe)
compare(nb_pipe, nb_dest)


@requires_flake8
Expand Down Expand Up @@ -102,7 +102,7 @@ def test_apply_black_through_jupytext(tmpdir, nb_file):
write(nb_org, tmp_ipynb)
jupytext([tmp_ipynb, '--pipe', 'black'])
nb_now = read(tmp_ipynb)
compare(nb_black, nb_now)
compare(nb_now, nb_black)

# Write to another folder using dots
script_fmt = os.path.join('..', 'script_folder//py:percent')
Expand All @@ -111,7 +111,7 @@ def test_apply_black_through_jupytext(tmpdir, nb_file):
assert os.path.isfile(tmp_py)
nb_now = read(tmp_py)
nb_now.metadata = metadata
compare(nb_black, nb_now)
compare(nb_now, nb_black)
os.remove(tmp_py)

# Map to another folder based on file name
Expand All @@ -121,7 +121,7 @@ def test_apply_black_through_jupytext(tmpdir, nb_file):
assert os.path.isfile(tmp_py)
nb_now = read(tmp_py)
nb_now.metadata = metadata
compare(nb_black, nb_now)
compare(nb_now, nb_black)


@requires_black
Expand All @@ -143,14 +143,14 @@ def test_apply_black_and_sync_on_paired_notebook(tmpdir, nb_file):
jupytext([tmp_ipynb, '--pipe', 'black', '--sync'])

nb_now = read(tmp_ipynb)
compare(nb_black, nb_now)
compare(nb_now, nb_black)
assert 'language_info' in nb_now.metadata

nb_now = read(tmp_py)
nb_now.metadata['jupytext'].pop('text_representation')
nb_black.metadata = {key: nb_black.metadata[key] for key in nb_black.metadata
if key in _DEFAULT_NOTEBOOK_METADATA}
compare(nb_black, nb_now)
compare(nb_now, nb_black)


@requires_black
Expand Down
2 changes: 1 addition & 1 deletion tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -919,7 +919,7 @@ def test_convert_and_update_preserves_notebook(nb_file, fmt, tmpdir):

nb_org = read(nb_file)
nb_now = read(tmp_ipynb)
compare(nb_org, nb_now)
compare(nb_now, nb_org)


def test_incorrect_notebook_causes_early_exit(tmpdir):
Expand Down
4 changes: 2 additions & 2 deletions tests/test_compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ def test_compare_on_notebooks(notebook_actual, notebook_expected):
compare(notebook_actual, notebook_expected)

assert str(err.value) == """
--- first
+++ second
--- actual
+++ expected
@@ -15,7 +15,7 @@
{
"cell_type": "markdown",
Expand Down
24 changes: 12 additions & 12 deletions tests/test_contentsmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ def test_pair_plain_script(py_file, tmpdir):
with open(str(tmpdir.join(tmp_py))) as fp:
script2 = fp.read()

compare(script, script2)
compare(script2, script)

# reopen py file with the cm
nb2 = cm.get(tmp_py)['content']
Expand Down Expand Up @@ -278,7 +278,7 @@ def test_load_save_py_freeze_metadata(script, tmpdir):
with open(str(tmpdir.join(tmp_nbpy))) as fp:
text_py2 = fp.read()

compare(text_py, text_py2)
compare(text_py2, text_py)


@skip_if_dict_is_not_ordered
Expand Down Expand Up @@ -487,7 +487,7 @@ def test_load_save_percent_format(nb_file, tmpdir):
# Remove the YAML header
text_py2 = text_py2[-len(text_py):]

compare(text_py, text_py2)
compare(text_py2, text_py)


@skip_if_dict_is_not_ordered
Expand Down Expand Up @@ -1011,7 +1011,7 @@ def test_no_metadata_added_to_scripts_139(tmpdir):
cm.save(model=model, path='script.py')

with open(tmp_script) as fp:
compare(text, fp.read())
compare(fp.read(), text)


@pytest.mark.parametrize('nb_file,ext', itertools.product(list_notebooks('ipynb_py'), ['.py', '.ipynb']))
Expand Down Expand Up @@ -1087,7 +1087,7 @@ def test_python_kernel_preserves_R_files(nb_file, tmpdir):
with open(tmp_r_file) as fp:
script2 = fp.read()

compare(script, script2)
compare(script2, script)


def test_pair_notebook_in_another_folder(tmpdir):
Expand Down Expand Up @@ -1174,7 +1174,7 @@ def test_split_at_heading_option(tmpdir):

nb.metadata['jupytext']['notebook_metadata_filter'] = '-all'
text2 = writes(nb, 'md')
compare(text, text2)
compare(text2, text)


def test_load_then_change_formats(tmpdir):
Expand Down Expand Up @@ -1343,7 +1343,7 @@ def test_vim_folding_markers(tmpdir):
# Remove YAML header
text = re.sub(re.compile(r'# ---.*# ---\n\n', re.DOTALL), '', text)

compare("""# region
compare(text, """# region
'''Sample cell with region markers'''
'''End of the cell'''
# end region
Expand All @@ -1354,7 +1354,7 @@ def test_vim_folding_markers(tmpdir):
b = 1
# }}}
""", text)
""")


def test_vscode_pycharm_folding_markers(tmpdir):
Expand Down Expand Up @@ -1390,7 +1390,7 @@ def test_vscode_pycharm_folding_markers(tmpdir):
# Remove YAML header
text = re.sub(re.compile(r'# ---.*# ---\n\n', re.DOTALL), '', text)

compare("""# {{{
compare(text, """# {{{
'''Sample cell with region markers'''
'''End of the cell'''
# }}}
Expand All @@ -1401,7 +1401,7 @@ def test_vscode_pycharm_folding_markers(tmpdir):
b = 1
# endregion
""", text)
""")


def test_open_file_with_default_cell_markers(tmpdir):
Expand Down Expand Up @@ -1439,7 +1439,7 @@ def test_open_file_with_default_cell_markers(tmpdir):
# endregion
"""

compare(expected, text2)
compare(text2, expected)


def test_save_file_with_default_cell_markers(tmpdir):
Expand Down Expand Up @@ -1471,7 +1471,7 @@ def test_save_file_with_default_cell_markers(tmpdir):
with open(tmp_py) as fp:
text2 = fp.read()

compare('\n'.join(text.splitlines()), '\n'.join(text2.splitlines()[-len(text.splitlines()):]))
compare('\n'.join(text2.splitlines()[-len(text.splitlines()):]), '\n'.join(text.splitlines()))

nb2 = cm.get('nb.py')['content']
compare_notebooks(nb2, nb)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_escape_magics.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,12 @@ def g(x):
return x+1""")])

text = jupytext.writes(nb, 'py')
compare("""# +
compare(text, """# +
# %load_ext watermark
# %watermark -u -n -t -z \\
# -p jupytext -v
def g(x):
return x+1
""", text)
""")
compare_notebooks(jupytext.reads(text, 'py'), nb)
16 changes: 8 additions & 8 deletions tests/test_formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,31 +152,31 @@ def test_compress_formats():
def test_rearrange_jupytext_metadata():
metadata = {'nbrmd_formats': 'ipynb,py'}
rearrange_jupytext_metadata(metadata)
compare({'jupytext': {'formats': 'ipynb,py'}}, metadata)
compare(metadata, {'jupytext': {'formats': 'ipynb,py'}})

metadata = {'jupytext_formats': 'ipynb,py'}
rearrange_jupytext_metadata(metadata)
compare({'jupytext': {'formats': 'ipynb,py'}}, metadata)
compare(metadata, {'jupytext': {'formats': 'ipynb,py'}})

metadata = {'executable': '#!/bin/bash'}
rearrange_jupytext_metadata(metadata)
compare({'jupytext': {'executable': '#!/bin/bash'}}, metadata)
compare(metadata, {'jupytext': {'executable': '#!/bin/bash'}})


def test_rearrange_jupytext_metadata_metadata_filter():
metadata = {'jupytext': {'metadata_filter': {'notebook': {'additional': ['one', 'two'], 'excluded': 'all'},
'cells': {'additional': 'all', 'excluded': ['three', 'four']}}}}
rearrange_jupytext_metadata(metadata)
compare({'jupytext': {'notebook_metadata_filter': 'one,two,-all',
'cell_metadata_filter': 'all,-three,-four'}}, metadata)
compare(metadata, {'jupytext': {'notebook_metadata_filter': 'one,two,-all',
'cell_metadata_filter': 'all,-three,-four'}})


def test_rearrange_jupytext_metadata_add_dot_in_suffix():
metadata = {'jupytext': {'text_representation': {'jupytext_version': '0.8.6'},
'formats': 'ipynb,pct.py,lgt.py'}}
rearrange_jupytext_metadata(metadata)
compare({'jupytext': {'text_representation': {'jupytext_version': '0.8.6'},
'formats': 'ipynb,.pct.py,.lgt.py'}}, metadata)
compare(metadata, {'jupytext': {'text_representation': {'jupytext_version': '0.8.6'},
'formats': 'ipynb,.pct.py,.lgt.py'}})


def test_fix_139():
Expand Down Expand Up @@ -229,4 +229,4 @@ def test_pandoc_format_is_preserved():
long = long_form_multiple_formats(formats_org)
formats_new = short_form_multiple_formats(long)

compare(formats_org, formats_new)
compare(formats_new, formats_org)
Loading

0 comments on commit 4d6bef1

Please sign in to comment.