From bf6049b9107fef171647345f84364cdb4c5aaf5e Mon Sep 17 00:00:00 2001 From: Marc Wouts Date: Sun, 13 Oct 2019 03:06:17 +0200 Subject: [PATCH] Round trip tested on the input file Rather than on its notebook representation (#339) --- HISTORY.rst | 1 + jupytext/cli.py | 28 ++++++++++++++++++++++------ tests/test_cli.py | 15 +++++++++++++++ 3 files changed, 38 insertions(+), 6 deletions(-) diff --git a/HISTORY.rst b/HISTORY.rst index 1659f2794..3294ba54f 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -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) diff --git a/jupytext/cli.py b/jupytext/cli.py index c1bdd9952..0982847ef 100644 --- a/jupytext/cli.py +++ b/jupytext/cli.py @@ -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__ @@ -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 diff --git a/tests/test_cli.py b/tests/test_cli.py index 1d58bca25..05acf7fb1 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -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