Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix encoding warnings with PEP 597 enabled #685

Merged
merged 13 commits into from
Jan 28, 2024
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",
radoering marked this conversation as resolved.
Show resolved Hide resolved
).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
finally:
_reset_executable()


@pytest.mark.parametrize(
"url, normalized",
[
Expand Down Expand Up @@ -436,41 +449,34 @@ def test_git_rev_parse_raises_error_on_invalid_repository() -> None:
" reasons"
),
)
def test_ensure_absolute_path_to_git(mocker: MockerFixture) -> None:
_reset_executable()

def test_ensure_absolute_path_to_git(reset_git: None, mocker: MockerFixture) -> None:
def checkout_output(cmd: list[str], *args: Any, **kwargs: Any) -> str | bytes:
if Path(cmd[0]).name == "where.exe":
return "\n".join([
str(Path.cwd().joinpath("git.exe")),
"C:\\Git\\cmd\\git.exe",
])
return "\n".join(
[str(Path.cwd().joinpath("git.exe")), r"C:\Git\cmd\git.exe"]
)

return b""

mock = mocker.patch.object(subprocess, "check_output", side_effect=checkout_output)

Git().run("config")

assert mock.call_args_list[-1][0][0] == [
"C:\\Git\\cmd\\git.exe",
"config",
]
assert mock.call_args_list[-1][0][0] == [r"C:\Git\cmd\git.exe", "config"]


@pytest.mark.skipif(
not WINDOWS,
reason=(
"Retrieving the complete path to git is only necessary on Windows, for security"
" reasons"
),
)
def test_ensure_existing_git_executable_is_found(mocker: MockerFixture) -> None:
mock = mocker.patch.object(subprocess, "check_output", return_value=b"")

Git().run("config")

cmd = Path(mock.call_args_list[-1][0][0][0])
assert mock.call_args_list[-1][0][0] == [r"C:\Git\cmd\git.exe", "config"]


assert cmd.is_absolute()
assert cmd.name == "git.exe"
def test_get_vcs_encoding(tmp_path: Path) -> None:
repo_path = tmp_path / "répö"
repo_path.mkdir()
assert repo_path.exists()
assert subprocess.check_call([executable(), "init"], cwd=repo_path) == 0
vcs = get_vcs(repo_path)
assert vcs is not None
assert vcs._work_dir is not None
assert vcs._work_dir.exists()