Skip to content

Commit

Permalink
Fix encoding warnings with PEP 597 enabled (#685)
Browse files Browse the repository at this point in the history
Co-authored-by: Randy Döring <[email protected]>
  • Loading branch information
danyeaw and radoering authored Jan 28, 2024
1 parent 14a5250 commit 2e122ea
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 30 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ on:
push:
branches: [main]

env:
PYTHONWARNDEFAULTENCODING: 'true'

jobs:
tests:
name: ${{ matrix.os }} / ${{ matrix.python-version }}
Expand Down
1 change: 1 addition & 0 deletions src/poetry/core/masonry/builders/wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ def _get_sys_tags(self) -> list[str]:
],
stderr=subprocess.STDOUT,
text=True,
encoding="utf-8",
)
except subprocess.CalledProcessError as e:
raise RuntimeError(
Expand Down
1 change: 1 addition & 0 deletions src/poetry/core/vcs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def get_vcs(directory: Path) -> Git | None:
[executable(), "rev-parse", "--show-toplevel"],
stderr=subprocess.STDOUT,
text=True,
encoding="utf-8",
).strip()

vcs = Git(Path(git_dir))
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/test_pep517_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def test_pip_install(

# Append dynamic `build-system` section to `pyproject.toml` in the temporary
# project directory.
with open(temp_pep_517_backend_path / "pyproject.toml", "a") as f:
with open(temp_pep_517_backend_path / "pyproject.toml", "a", encoding="utf-8") as f:
f.write(
BUILD_SYSTEM_TEMPLATE.format(project_path=project_source_root.as_posix())
)
Expand Down
4 changes: 3 additions & 1 deletion tests/masonry/builders/test_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,9 @@ def test_metadata_with_readme_files() -> None:

readme1 = test_path / "README-1.rst"
readme2 = test_path / "README-2.rst"
description = "\n".join([readme1.read_text(), readme2.read_text(), ""])
description = "\n".join(
[readme1.read_text(encoding="utf-8"), readme2.read_text(encoding="utf-8"), ""]
)

assert metadata.get_payload() == description

Expand Down
6 changes: 3 additions & 3 deletions tests/pyproject/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
@pytest.fixture
def pyproject_toml(tmp_path: Path) -> Path:
path = tmp_path / "pyproject.toml"
with path.open(mode="w"):
with path.open(mode="w", encoding="utf-8"):
pass
return path

Expand All @@ -24,7 +24,7 @@ def build_system_section(pyproject_toml: Path) -> str:
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
"""
with pyproject_toml.open(mode="a") as f:
with pyproject_toml.open(mode="a", encoding="utf-8") as f:
f.write(content)
return content

Expand All @@ -38,6 +38,6 @@ def poetry_section(pyproject_toml: Path) -> str:
[tool.poetry.dependencies]
python = "^3.5"
"""
with pyproject_toml.open(mode="a") as f:
with pyproject_toml.open(mode="a", encoding="utf-8") as f:
f.write(content)
return content
6 changes: 5 additions & 1 deletion tests/testutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import shutil
import subprocess
import sys
import tarfile
import tempfile
import zipfile
Expand Down Expand Up @@ -59,7 +60,10 @@ def subprocess_run(*args: str, **kwargs: Any) -> subprocess.CompletedProcess[str
"""
Helper method to run a subprocess. Asserts for success.
"""
result = subprocess.run(args, text=True, capture_output=True, **kwargs)
encoding = "locale" if sys.version_info >= (3, 10) else None
result = subprocess.run(
args, text=True, encoding=encoding, capture_output=True, **kwargs
)
assert result.returncode == 0
return result

Expand Down
2 changes: 1 addition & 1 deletion tests/utils/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def test_utils_helpers_combine_unicode() -> None:
def test_utils_helpers_temporary_directory_readonly_file() -> None:
with temporary_directory() as temp_dir:
readonly_filename = os.path.join(temp_dir, "file.txt")
with open(readonly_filename, "w+") as readonly_file:
with open(readonly_filename, "w+", encoding="utf-8") as readonly_file:
readonly_file.write("Poetry rocks!")
os.chmod(str(readonly_filename), S_IREAD)

Expand Down
52 changes: 29 additions & 23 deletions tests/vcs/test_vcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,30 @@
import pytest

from poetry.core.utils._compat import WINDOWS
from poetry.core.vcs import get_vcs
from poetry.core.vcs.git import Git
from poetry.core.vcs.git import GitError
from poetry.core.vcs.git import GitUrl
from poetry.core.vcs.git import ParsedUrl
from poetry.core.vcs.git import _reset_executable
from poetry.core.vcs.git import executable


if TYPE_CHECKING:
from collections.abc import Iterator

from pytest_mock import MockerFixture


@pytest.fixture
def reset_git() -> Iterator[None]:
_reset_executable()
try:
yield