Skip to content

Commit 04d887d

Browse files
committed
Force UTF-8 encoding when reading/writing text
1 parent 3d1420f commit 04d887d

File tree

4 files changed

+27
-25
lines changed

4 files changed

+27
-25
lines changed

src/juv/_add.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ def uv_pip_compile(
4242
exclude_newer: str | None,
4343
) -> list[str]:
4444
"""Use `pip compile` to generate exact versions of packages."""
45-
requirements_txt = "" if requirements is None else Path(requirements).read_text()
45+
requirements_txt = (
46+
"" if requirements is None else Path(requirements).read_text(encoding="utf-8")
47+
)
4648

4749
# just append the packages on to the requirements
4850
for package in packages:

src/juv/_run.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616

1717
def load_script_notebook(fp: Path) -> dict:
18-
script = fp.read_text()
18+
script = fp.read_text(encoding="utf-8")
1919
# we could read the whole thing with jupytext,
2020
# but is nice to ensure the script meta is at the top in it's own
2121
# cell (that we can hide by default in JupyterLab)

src/juv/_stamp.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,6 @@ def stamp( # noqa: PLR0913
174174
write_ipynb(nb, path)
175175
return action
176176

177-
script, action = update_inline_metadata(path.read_text(), dt)
177+
script, action = update_inline_metadata(path.read_text(encoding="utf-8"), dt)
178178
path.write_text(script)
179179
return action

tests/test_juv.py

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ def test_add_creates_inline_meta(
301301
result = invoke(["add", str(nb), "polars==1", "anywidget"], uv_python="3.11")
302302
assert result.exit_code == 0
303303
assert filter_tempfile_ipynb(result.stdout) == snapshot("Updated `foo.ipynb`\n")
304-
assert filter_ids(nb.read_text()) == snapshot("""\
304+
assert filter_ids(nb.read_text(encoding="utf-8")) == snapshot("""\
305305
{
306306
"cells": [
307307
{
@@ -344,7 +344,7 @@ def test_add_prepends_script_meta(
344344
result = invoke(["add", str(path), "polars==1", "anywidget"], uv_python="3.10")
345345
assert result.exit_code == 0
346346
assert filter_tempfile_ipynb(result.stdout) == snapshot("Updated `empty.ipynb`\n")
347-
assert filter_ids(path.read_text()) == snapshot("""\
347+
assert filter_ids(path.read_text(encoding="utf-8")) == snapshot("""\
348348
{
349349
"cells": [
350350
{
@@ -404,7 +404,7 @@ def test_add_updates_existing_meta(
404404
result = invoke(["add", str(path), "polars==1", "anywidget"], uv_python="3.13")
405405
assert result.exit_code == 0
406406
assert filter_tempfile_ipynb(result.stdout) == snapshot("Updated `empty.ipynb`\n")
407-
assert filter_ids(path.read_text()) == snapshot("""\
407+
assert filter_ids(path.read_text(encoding="utf-8")) == snapshot("""\
408408
{
409409
"cells": [
410410
{
@@ -444,7 +444,7 @@ def test_init_creates_notebook_with_inline_meta(
444444
assert filter_tempfile_ipynb(result.stdout) == snapshot(
445445
"Initialized notebook at `empty.ipynb`\n"
446446
)
447-
assert filter_ids(path.read_text()) == snapshot("""\
447+
assert filter_ids(path.read_text(encoding="utf-8")) == snapshot("""\
448448
{
449449
"cells": [
450450
{
@@ -497,7 +497,7 @@ def test_init_creates_notebook_with_specific_python_version(
497497
assert filter_tempfile_ipynb(result.stdout) == snapshot(
498498
"Initialized notebook at `empty.ipynb`\n"
499499
)
500-
assert filter_ids(path.read_text()) == snapshot("""\
500+
assert filter_ids(path.read_text(encoding="utf-8")) == snapshot("""\
501501
{
502502
"cells": [
503503
{
@@ -558,7 +558,7 @@ def test_init_with_deps(
558558
assert result.stdout == snapshot("Initialized notebook at `Untitled.ipynb`\n")
559559

560560
path = tmp_path / "Untitled.ipynb"
561-
assert filter_ids(path.read_text()) == snapshot("""\
561+
assert filter_ids(path.read_text(encoding="utf-8")) == snapshot("""\
562562
{
563563
"cells": [
564564
{
@@ -853,18 +853,18 @@ def test_stamp_script(
853853
tmp_path = pathlib.Path(tmpdir)
854854
monkeypatch.chdir(tmp_path)
855855

856-
with (tmp_path / "foo.py").open("w") as f:
856+
with (tmp_path / "foo.py").open("w", encoding="utf-8") as f:
857857
f.write("""# /// script
858858
# requires-python = ">=3.13"
859859
# dependencies = []
860860
# ///
861861
862862
863-
def main() -> None: |
864-
print("Hello from foo.py!") |
865-
|
866-
|
867-
if __name__ == "__main__": |
863+
def main() -> None:
864+
print("Hello from foo.py!")
865+
866+
867+
if __name__ == "__main__":
868868
main()
869869
""")
870870
result = invoke(["stamp", "foo.py", "--date", "2006-01-02"])
@@ -873,7 +873,7 @@ def main() -> None:
873873
assert result.stdout == snapshot(
874874
"Stamped `foo.py` with 2006-01-03T00:00:00-05:00\n"
875875
)
876-
assert (tmp_path / "foo.py").read_text() == snapshot("""\
876+
assert (tmp_path / "foo.py").read_text(encoding="utf-8") == snapshot("""\
877877
# /// script
878878
# requires-python = ">=3.13"
879879
# dependencies = []
@@ -883,11 +883,11 @@ def main() -> None:
883883
# ///
884884
885885
886-
def main() -> None: |
887-
print("Hello from foo.py!") |
888-
|
889-
|
890-
if __name__ == "__main__": |
886+
def main() -> None:
887+
print("Hello from foo.py!")
888+
889+
890+
if __name__ == "__main__":
891891
main()
892892
""")
893893

@@ -902,7 +902,7 @@ def test_stamp_clear(
902902
tmp_path = pathlib.Path(tmpdir)
903903
monkeypatch.chdir(tmp_path)
904904

905-
with (tmp_path / "foo.py").open("w") as f:
905+
with (tmp_path / "foo.py").open("w", encoding="utf-8") as f:
906906
f.write("""# /// script
907907
# requires-python = ">=3.13"
908908
# dependencies = []
@@ -916,7 +916,7 @@ def test_stamp_clear(
916916

917917
assert result.exit_code == 0
918918
assert result.stdout == snapshot("Removed blah from `foo.py`\n")
919-
assert (tmp_path / "foo.py").read_text() == snapshot("""\
919+
assert (tmp_path / "foo.py").read_text(encoding="utf-8") == snapshot("""\
920920
# /// script
921921
# requires-python = ">=3.13"
922922
# dependencies = []
@@ -950,7 +950,7 @@ def test_add_script_pinned(
950950
) -> None:
951951
monkeypatch.chdir(tmp_path)
952952

953-
with (tmp_path / "foo.py").open("w") as f:
953+
with (tmp_path / "foo.py").open("w", encoding="utf-8") as f:
954954
f.write("""# /// script
955955
# requires-python = ">=3.13"
956956
# dependencies = []
@@ -962,7 +962,7 @@ def test_add_script_pinned(
962962
result = invoke(["add", "foo.py", "anywidget", "--pin"])
963963
assert result.exit_code == 0
964964
assert result.stdout == snapshot("Updated `foo.py`\n")
965-
assert (tmp_path / "foo.py").read_text() == snapshot("""\
965+
assert (tmp_path / "foo.py").read_text(encoding="utf-8") == snapshot("""\
966966
# /// script
967967
# requires-python = ">=3.13"
968968
# dependencies = [

0 commit comments

Comments
 (0)