diff --git a/nbstripout/_nbstripout.py b/nbstripout/_nbstripout.py index 7168e39..41dfe4d 100644 --- a/nbstripout/_nbstripout.py +++ b/nbstripout/_nbstripout.py @@ -378,6 +378,9 @@ def process_jupyter_notebook( ) any_change = nb_orig != nb_stripped + # Early exit when writing in-place and nothing changes. + if not any_change and output_stream is input_stream: + return any_change if args.dry_run: if any_change: @@ -410,6 +413,9 @@ def process_zeppelin_notebook( nb_stripped = strip_zeppelin_output(nb) any_change = nb_orig != nb_stripped + # Early exit when writing in-place and nothing changes. + if not any_change and output_stream is input_stream: + return any_change if args.dry_run: if any_change: diff --git a/tests/test_end_to_end.py b/tests/test_end_to_end.py index 555b843..f0bfb83 100644 --- a/tests/test_end_to_end.py +++ b/tests/test_end_to_end.py @@ -184,3 +184,21 @@ def test_make_errors(input_file: str, expected_errs: List[Union[str, Pattern]], assert e.search(err_output) else: assert e in err_output + + +def test_nochange_notebook_unchanged(): + ipynb_file = NOTEBOOKS_FOLDER / 'test_nochange.ipynb' + ipynb_mtime_before = ipynb_file.stat().st_mtime_ns + + run([nbstripout_exe(), ipynb_file]) + ipynb_mtime_after = ipynb_file.stat().st_mtime_ns + + assert ipynb_mtime_after == ipynb_mtime_before + + zpln_file = NOTEBOOKS_FOLDER / 'test_zeppelin.zpln.expected' + zpln_mtime_before = zpln_file.stat().st_mtime_ns + + run([nbstripout_exe(), '--force', '--mode', 'zeppelin', zpln_file]) + zpln_mtime_after = zpln_file.stat().st_mtime_ns + + assert zpln_mtime_after == zpln_mtime_before