From c64538a09ca51fb181051e52217dc7b2adb0fcc6 Mon Sep 17 00:00:00 2001 From: Dan Yeaw Date: Sun, 21 Jan 2024 15:56:30 -0500 Subject: [PATCH 01/11] Fix encoding warnings with PEP 597 enabled --- .github/workflows/tests.yml | 3 +++ src/poetry/core/masonry/builders/wheel.py | 1 + src/poetry/core/vcs/__init__.py | 1 + tests/masonry/builders/test_builder.py | 4 +++- tests/pyproject/conftest.py | 6 +++--- tests/utils/test_helpers.py | 2 +- 6 files changed, 12 insertions(+), 5 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index fdc4019b9..1c9e1834c 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -5,6 +5,9 @@ on: push: branches: [main] +env: + PYTHONWARNDEFAULTENCODING: 'true' + jobs: tests: name: ${{ matrix.os }} / ${{ matrix.python-version }} diff --git a/src/poetry/core/masonry/builders/wheel.py b/src/poetry/core/masonry/builders/wheel.py index 9c00b0413..224ddb71f 100644 --- a/src/poetry/core/masonry/builders/wheel.py +++ b/src/poetry/core/masonry/builders/wheel.py @@ -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( diff --git a/src/poetry/core/vcs/__init__.py b/src/poetry/core/vcs/__init__.py index debd95760..0491669be 100644 --- a/src/poetry/core/vcs/__init__.py +++ b/src/poetry/core/vcs/__init__.py @@ -28,6 +28,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)) diff --git a/tests/masonry/builders/test_builder.py b/tests/masonry/builders/test_builder.py index c9502d2c3..b7a2421f4 100644 --- a/tests/masonry/builders/test_builder.py +++ b/tests/masonry/builders/test_builder.py @@ -307,7 +307,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 diff --git a/tests/pyproject/conftest.py b/tests/pyproject/conftest.py index 82ff21983..8aceff30d 100644 --- a/tests/pyproject/conftest.py +++ b/tests/pyproject/conftest.py @@ -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 @@ -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 @@ -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 diff --git a/tests/utils/test_helpers.py b/tests/utils/test_helpers.py index f0845bd44..ea8b03d48 100644 --- a/tests/utils/test_helpers.py +++ b/tests/utils/test_helpers.py @@ -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) From 47e35f0e3b485d78f59be91c7a6343cb9e284adf Mon Sep 17 00:00:00 2001 From: Dan Yeaw Date: Mon, 22 Jan 2024 22:04:19 -0500 Subject: [PATCH 02/11] Fix terminal commands may not return utf-8 --- src/poetry/core/vcs/__init__.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/poetry/core/vcs/__init__.py b/src/poetry/core/vcs/__init__.py index 0491669be..debd95760 100644 --- a/src/poetry/core/vcs/__init__.py +++ b/src/poetry/core/vcs/__init__.py @@ -28,7 +28,6 @@ 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)) From af618164dd9ba1388897edc6847849e8d1571a02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Randy=20D=C3=B6ring?= <30527984+radoering@users.noreply.github.com> Date: Sat, 27 Jan 2024 10:05:02 +0100 Subject: [PATCH 03/11] fix git encoding and add test --- src/poetry/core/vcs/__init__.py | 1 + tests/vcs/test_vcs.py | 12 ++++++++++++ 2 files changed, 13 insertions(+) diff --git a/src/poetry/core/vcs/__init__.py b/src/poetry/core/vcs/__init__.py index 2c2322053..3afa1de31 100644 --- a/src/poetry/core/vcs/__init__.py +++ b/src/poetry/core/vcs/__init__.py @@ -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)) diff --git a/tests/vcs/test_vcs.py b/tests/vcs/test_vcs.py index b4343e4b1..38034ae31 100644 --- a/tests/vcs/test_vcs.py +++ b/tests/vcs/test_vcs.py @@ -9,6 +9,7 @@ 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 @@ -474,3 +475,14 @@ def test_ensure_existing_git_executable_is_found(mocker: MockerFixture) -> None: 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() + subprocess.run(["git", "init"], cwd=repo_path) + assert repo_path.exists() + vcs = get_vcs(repo_path) + assert vcs is not None + assert vcs._work_dir is not None + assert vcs._work_dir.exists() From d421f04e85f7cfbd80e447c848559152c0b1c123 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Randy=20D=C3=B6ring?= <30527984+radoering@users.noreply.github.com> Date: Sat, 27 Jan 2024 10:31:38 +0100 Subject: [PATCH 04/11] try filesystemencoding --- src/poetry/core/vcs/__init__.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/poetry/core/vcs/__init__.py b/src/poetry/core/vcs/__init__.py index 3afa1de31..76f93da73 100644 --- a/src/poetry/core/vcs/__init__.py +++ b/src/poetry/core/vcs/__init__.py @@ -2,6 +2,7 @@ import os import subprocess +import sys from pathlib import Path @@ -30,7 +31,7 @@ def get_vcs(directory: Path) -> Git | None: [executable(), "rev-parse", "--show-toplevel"], stderr=subprocess.STDOUT, text=True, - encoding="utf-8", + encoding=sys.getfilesystemencoding(), ).strip() vcs = Git(Path(git_dir)) From 00924667ea31195f5c44323dc9b559b8b5f4db7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Randy=20D=C3=B6ring?= <30527984+radoering@users.noreply.github.com> Date: Sat, 27 Jan 2024 10:37:27 +0100 Subject: [PATCH 05/11] check if test succeeds without encoding --- src/poetry/core/vcs/__init__.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/poetry/core/vcs/__init__.py b/src/poetry/core/vcs/__init__.py index 76f93da73..2c2322053 100644 --- a/src/poetry/core/vcs/__init__.py +++ b/src/poetry/core/vcs/__init__.py @@ -2,7 +2,6 @@ import os import subprocess -import sys from pathlib import Path @@ -31,7 +30,6 @@ def get_vcs(directory: Path) -> Git | None: [executable(), "rev-parse", "--show-toplevel"], stderr=subprocess.STDOUT, text=True, - encoding=sys.getfilesystemencoding(), ).strip() vcs = Git(Path(git_dir)) From b6bc27ef708aad4f88f4101c821d3b541ae391d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Randy=20D=C3=B6ring?= <30527984+radoering@users.noreply.github.com> Date: Sat, 27 Jan 2024 10:52:25 +0100 Subject: [PATCH 06/11] improve test: check if "git init" succeeds --- tests/vcs/test_vcs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/vcs/test_vcs.py b/tests/vcs/test_vcs.py index 38034ae31..4fdbc5814 100644 --- a/tests/vcs/test_vcs.py +++ b/tests/vcs/test_vcs.py @@ -480,8 +480,8 @@ def test_ensure_existing_git_executable_is_found(mocker: MockerFixture) -> None: def test_get_vcs_encoding(tmp_path: Path) -> None: repo_path = tmp_path / "répö" repo_path.mkdir() - subprocess.run(["git", "init"], cwd=repo_path) assert repo_path.exists() + assert subprocess.check_call(["git", "init"], cwd=repo_path) == 0 vcs = get_vcs(repo_path) assert vcs is not None assert vcs._work_dir is not None From 0958d2507b5b2953559ccd0a9630ef5037b0b280 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Randy=20D=C3=B6ring?= <30527984+radoering@users.noreply.github.com> Date: Sat, 27 Jan 2024 11:24:56 +0100 Subject: [PATCH 07/11] fix tests, try again with utf-8 --- src/poetry/core/vcs/__init__.py | 1 + tests/vcs/test_vcs.py | 40 +++++++++++++++------------------ 2 files changed, 19 insertions(+), 22 deletions(-) diff --git a/src/poetry/core/vcs/__init__.py b/src/poetry/core/vcs/__init__.py index 2c2322053..3afa1de31 100644 --- a/src/poetry/core/vcs/__init__.py +++ b/src/poetry/core/vcs/__init__.py @@ -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)) diff --git a/tests/vcs/test_vcs.py b/tests/vcs/test_vcs.py index 4fdbc5814..602e49c30 100644 --- a/tests/vcs/test_vcs.py +++ b/tests/vcs/test_vcs.py @@ -15,12 +15,24 @@ 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", [ @@ -437,15 +449,12 @@ 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"" @@ -453,20 +462,8 @@ def checkout_output(cmd: list[str], *args: Any, **kwargs: Any) -> str | bytes: 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") @@ -474,14 +471,13 @@ def test_ensure_existing_git_executable_is_found(mocker: MockerFixture) -> None: cmd = Path(mock.call_args_list[-1][0][0][0]) 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(["git", "init"], cwd=repo_path) == 0 + 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 From bc23b52e14a2b7e7c2a31113e2589fa63a93f6fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Randy=20D=C3=B6ring?= <30527984+radoering@users.noreply.github.com> Date: Sat, 27 Jan 2024 11:36:46 +0100 Subject: [PATCH 08/11] no need for different checks --- tests/vcs/test_vcs.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/vcs/test_vcs.py b/tests/vcs/test_vcs.py index 602e49c30..fa0c88203 100644 --- a/tests/vcs/test_vcs.py +++ b/tests/vcs/test_vcs.py @@ -468,9 +468,7 @@ def checkout_output(cmd: list[str], *args: Any, **kwargs: Any) -> str | bytes: Git().run("config") - cmd = Path(mock.call_args_list[-1][0][0][0]) - - assert cmd.is_absolute() + assert mock.call_args_list[-1][0][0] == [r"C:\Git\cmd\git.exe", "config"] def test_get_vcs_encoding(tmp_path: Path) -> None: From e1afe0a98e623655b19c05bb9ede0869af33ece3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Randy=20D=C3=B6ring?= <30527984+radoering@users.noreply.github.com> Date: Sun, 28 Jan 2024 07:11:48 +0100 Subject: [PATCH 09/11] add one more missing encoding --- tests/integration/test_pep517_backend.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integration/test_pep517_backend.py b/tests/integration/test_pep517_backend.py index 031214821..7824bd0ae 100644 --- a/tests/integration/test_pep517_backend.py +++ b/tests/integration/test_pep517_backend.py @@ -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()) ) From 36b307fb09bb9829f664c8a54ff212caf6c7e7be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Randy=20D=C3=B6ring?= <30527984+radoering@users.noreply.github.com> Date: Sun, 28 Jan 2024 07:18:02 +0100 Subject: [PATCH 10/11] and another one --- tests/testutils.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/testutils.py b/tests/testutils.py index 93f3b6e12..dd6c0303e 100644 --- a/tests/testutils.py +++ b/tests/testutils.py @@ -59,7 +59,9 @@ 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) + result = subprocess.run( + args, text=True, encoding="utf-8", capture_output=True, **kwargs + ) assert result.returncode == 0 return result From 9dde0ad4d397af5dc8a8158b3c4e381f5e345b43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Randy=20D=C3=B6ring?= <30527984+radoering@users.noreply.github.com> Date: Sun, 28 Jan 2024 07:24:31 +0100 Subject: [PATCH 11/11] use locale encoding --- tests/testutils.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/testutils.py b/tests/testutils.py index dd6c0303e..f809f63bd 100644 --- a/tests/testutils.py +++ b/tests/testutils.py @@ -2,6 +2,7 @@ import shutil import subprocess +import sys import tarfile import tempfile import zipfile @@ -59,8 +60,9 @@ def subprocess_run(*args: str, **kwargs: Any) -> subprocess.CompletedProcess[str """ Helper method to run a subprocess. Asserts for success. """ + encoding = "locale" if sys.version_info >= (3, 10) else None result = subprocess.run( - args, text=True, encoding="utf-8", capture_output=True, **kwargs + args, text=True, encoding=encoding, capture_output=True, **kwargs ) assert result.returncode == 0 return result