Skip to content

Commit

Permalink
Test that Jupytext refuses to open a non-unicode file (api, cli & jup…
Browse files Browse the repository at this point in the history
…yter) (#897)
  • Loading branch information
mwouts authored Dec 27, 2021
1 parent f843c54 commit 703a729
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 2 deletions.
7 changes: 7 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
Jupytext ChangeLog
==================

1.13.5 (2021-12-27)
-------------------

**Fixed**
- Jupytext refuses to open a text notebook that is not UTF-8 ([#896](https://github.com/mwouts/jupytext/issues/896))


1.13.4 (2021-12-12)
-------------------

Expand Down
2 changes: 1 addition & 1 deletion jupytext/contentsmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ def get(
if ext == ".ipynb":
model = self.super.get(path, content, type="notebook", format=format)
else:
model = self.super.get(path, content, type="file", format=format)
model = self.super.get(path, content, type="file", format="text")
model["type"] = "notebook"
if content:
# We may need to update these keys, inherited from text files formats
Expand Down
2 changes: 1 addition & 1 deletion jupytext/version.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""Jupytext's version number"""

__version__ = "1.13.4"
__version__ = "1.13.5"
14 changes: 14 additions & 0 deletions tests/invalid_file_896.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
jupytext:
formats: ipynb,md:myst
text_representation:
extension: .md
format_name: myst
format_version: 0.13
jupytext_version: 1.11.5
kernelspec:
display_name: Python 3 (ipykernel)
language: python
name: python3
---
Et voilà!
36 changes: 36 additions & 0 deletions tests/test_invalid_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"""Jupytext should refuse to open a file with invalid content"""

from pathlib import Path

import pytest
from tornado.web import HTTPError

from jupytext import TextFileContentsManager, read
from jupytext.cli import jupytext as jupytext_cli

from .utils import skip_on_windows


@pytest.fixture
def invalid_md_file():
return Path(__file__).parent / "invalid_file_896.md"


@skip_on_windows
def test_read_invalid_md_file_fails(invalid_md_file):
with open(invalid_md_file) as fp:
with pytest.raises(UnicodeDecodeError):
read(fp)


def test_convert_invalid_md_file_fails(invalid_md_file):
with pytest.raises(UnicodeDecodeError):
jupytext_cli(["--to", "ipynb", str(invalid_md_file)])


def test_open_invalid_md_file_fails(invalid_md_file, tmp_path):
cm = TextFileContentsManager()
cm.root_dir = str(invalid_md_file.parent)

with pytest.raises(HTTPError, match="invalid_file_896.md is not UTF-8 encoded"):
cm.get(invalid_md_file.name)

0 comments on commit 703a729

Please sign in to comment.