Skip to content

Commit

Permalink
Round trip tested on the input file
Browse files Browse the repository at this point in the history
Rather than on its notebook representation (#339)
  • Loading branch information
mwouts committed Oct 13, 2019
1 parent 14dbac7 commit bf6049b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
1 change: 1 addition & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Release History

- Fixed an inconsistent round trip (code cell with ``"cat"`` being converted to a markdown cell) in the ``py:light`` format (#339)
- Commands like ``cat = x`` are not magic commands {#339)
- ``jupytext --test`` now really compares the text (rather than the corresponding notebook) when run on text files (#339)


1.2.4 (2019-09-19)
Expand Down
28 changes: 22 additions & 6 deletions jupytext/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from .header import recursive_update
from .paired_paths import paired_paths, base_path, full_path, InconsistentPath
from .combine import combine_inputs_with_outputs
from .compare import test_round_trip_conversion, NotebookDifference
from .compare import test_round_trip_conversion, NotebookDifference, compare
from .kernels import kernelspec_from_language, find_kernel_specs, get_kernel_spec
from .reraise import reraise
from .version import __version__
Expand Down Expand Up @@ -351,11 +351,27 @@ def jupytext_single_file(nb_file, args, log):
# a. Test round trip conversion
if args.test or args.test_strict:
try:
test_round_trip_conversion(notebook, dest_fmt,
update=args.update,
allow_expected_differences=not args.test_strict,
stop_on_first_error=args.stop_on_first_error)
except NotebookDifference as err:
# Round trip from an ipynb document
if fmt['extension'] == '.ipynb':
test_round_trip_conversion(notebook, dest_fmt,
update=args.update,
allow_expected_differences=not args.test_strict,
stop_on_first_error=args.stop_on_first_error)

# Round trip from a text file
else:
with open(nb_file) as fp:
org_text = fp.read()

# If the destination is not ipynb, we convert to/back that format
if dest_fmt['extension'] != '.ipynb':
dest_text = writes(notebook, fmt=dest_fmt)
notebook = reads(dest_text, fmt=dest_fmt)

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

except (NotebookDifference, AssertionError) as err:
sys.stdout.write('{}: {}'.format(nb_file, str(err)))
return 1
return 0
Expand Down
15 changes: 15 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -967,3 +967,18 @@ def test_339_ipynb(tmpdir, fmt):
nbformat.write(nb, tmp_ipynb)

assert jupytext([tmp_ipynb, '--to', fmt, '--test-strict']) == 0


def test_339_py(tmpdir):
"""Test that an incorrect round trip conversion on the text file is detected"""
tmp_py = str(tmpdir.join('test.py'))
with open(tmp_py, 'w') as fp:
fp.write("""# %%
cat = 42
""")

def erroneous_is_magic(line, language, comment_magics):
return 'cat' in line

with mock.patch('jupytext.magics.is_magic', erroneous_is_magic):
assert jupytext([tmp_py, '--to', 'ipynb', '--test-strict']) != 0

0 comments on commit bf6049b

Please sign in to comment.