Skip to content

Commit

Permalink
Do not override test.ipynb when doing 'jupytext --set-formats' on tes…
Browse files Browse the repository at this point in the history
…t.py (#970)

* Confirm #969
* Fix #969
  • Loading branch information
mwouts committed Jul 3, 2022
1 parent 72afe5d commit ebb025b
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 17 deletions.
5 changes: 4 additions & 1 deletion docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Jupytext ChangeLog
==================

1.13.9 (2022-06-30)
1.13.9 (2022-07-??)
-------------------

**Changed**
Expand All @@ -10,6 +10,9 @@ Jupytext ChangeLog
With that value, the contents manager will log a line regarding the configuration file used only when the
config file is not the same as the one previously used ([#959](https://github.com/mwouts/jupytext/issues/959))

**Changed**
- `jupytext --set-formats ipynb,py test.py` will not override `test.ipynb` if the file exists already ([#969](https://github.com/mwouts/jupytext/issues/969)).


1.13.8 (2022-04-04)
-------------------
Expand Down
27 changes: 12 additions & 15 deletions jupytext/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -629,27 +629,24 @@ def jupytext_single_file(nb_file, args, log):

recursive_update(notebook.metadata, args.update_metadata)

# Read paired notebooks, except if the pair is being created
# Read paired notebooks
nb_files = [nb_file, nb_dest]
if args.sync:
formats = notebook_formats(
notebook, config, nb_file, fallback_on_current_fmt=False
)
set_prefix_and_suffix(fmt, formats, nb_file)
if args.set_formats is None:
try:
notebook, inputs_nb_file, outputs_nb_file = load_paired_notebook(
notebook, fmt, config, formats, nb_file, log, args.pre_commit_mode
)
nb_files = [inputs_nb_file, outputs_nb_file]
except NotAPairedNotebook as err:
sys.stderr.write("[jupytext] Warning: " + str(err) + "\n")
return 0
except InconsistentVersions as err:
sys.stderr.write("[jupytext] Error: " + str(err) + "\n")
return 1
else:
nb_files = [nb_file]
try:
notebook, inputs_nb_file, outputs_nb_file = load_paired_notebook(
notebook, fmt, config, formats, nb_file, log, args.pre_commit_mode
)
nb_files = [inputs_nb_file, outputs_nb_file]
except NotAPairedNotebook as err:
sys.stderr.write("[jupytext] Warning: " + str(err) + "\n")
return 0
except InconsistentVersions as err:
sys.stderr.write("[jupytext] Error: " + str(err) + "\n")
return 1

# II. ### Apply commands onto the notebook ###
# Pipe the notebook into the desired commands
Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def notebook_with_outputs():
execution_count=1,
outputs=[
new_output(
data={"text/plain": ["2"]},
data={"text/plain": "2"},
execution_count=1,
output_type="execute_result",
)
Expand Down
21 changes: 21 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -1459,3 +1459,24 @@ def test_set_shebang_with_update_metadata(tmp_path, python_notebook):
)

assert tmp_py.read_text().startswith("#!/usr/bin/python")


@pytest.mark.parametrize("compare_ids", [False, True])
@pytest.mark.parametrize("compare_outputs", [False, True])
def test_set_formats_does_not_override_existing_ipynb(
tmp_path, notebook_with_outputs, compare_ids, compare_outputs
):
tmp_py = tmp_path / "nb.py"
tmp_ipynb = tmp_path / "nb.ipynb"
write(notebook_with_outputs, tmp_ipynb)

jupytext([str(tmp_ipynb), "--set-formats", "ipynb,py:percent"])
jupytext([str(tmp_py), "--set-formats", "ipynb,py:percent"])

nb = read(tmp_ipynb)
compare_notebooks(
nb,
notebook_with_outputs,
compare_ids=compare_ids,
compare_outputs=compare_outputs,
)

0 comments on commit ebb025b

Please sign in to comment.