diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 536295104..42a7b9a56 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 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/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()) ) diff --git a/tests/masonry/builders/test_builder.py b/tests/masonry/builders/test_builder.py index 779bfd14b..e0f673626 100644 --- a/tests/masonry/builders/test_builder.py +++ b/tests/masonry/builders/test_builder.py @@ -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 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/testutils.py b/tests/testutils.py index 93f3b6e12..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,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 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) diff --git a/tests/vcs/test_vcs.py b/tests/vcs/test_vcs.py index b4343e4b1..fa0c88203 100644 --- a/tests/vcs/test_vcs.py +++ b/tests/vcs/test_vcs.py @@ -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", [ @@ -436,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"" @@ -452,25 +462,21 @@ 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") - 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()