From c3a64cd0341abef7f1bf92cc8ae2f5e1ddb1f217 Mon Sep 17 00:00:00 2001 From: Federico Di Gioia Date: Thu, 15 Aug 2024 21:16:24 +0200 Subject: [PATCH] Fix inconsistent encoding in config files, among different functions and OSs. On Windows the default Python text encoding is "cp1252", while on Unix is "utf-8". This affects open(), pathlib.Path().read_text() and pathlib.Path().write_text(). This explicitly sets the encoding on all read/write operations on text files. Before some function calls had an explicit encoding, other not. --- scripts/generate_coverage_summary.py | 2 +- src/hatch/cli/fmt/core.py | 8 +- src/hatch/cli/run/__init__.py | 2 +- src/hatch/cli/test/core.py | 6 +- src/hatch/env/virtual.py | 3 +- src/hatch/project/env.py | 4 +- src/hatch/project/frontend/core.py | 26 +- src/hatch/publish/auth.py | 6 +- src/hatch/python/core.py | 4 +- tests/backend/builders/hooks/test_custom.py | 9 +- tests/backend/builders/hooks/test_version.py | 31 +- .../backend/builders/plugin/test_interface.py | 2 +- tests/backend/builders/test_config.py | 14 +- tests/backend/builders/test_custom.py | 15 +- tests/backend/builders/test_sdist.py | 15 +- tests/backend/builders/test_wheel.py | 116 +++++--- tests/backend/metadata/test_core.py | 48 +-- tests/backend/metadata/test_custom_hook.py | 9 +- tests/backend/test_build.py | 9 +- tests/backend/version/source/test_code.py | 13 +- tests/backend/version/source/test_regex.py | 10 +- tests/cli/build/test_build.py | 52 ++-- tests/cli/clean/test_clean.py | 6 +- tests/cli/dep/show/test_requirements.py | 3 +- tests/cli/dep/show/test_table.py | 3 +- tests/cli/dep/test_hash.py | 3 +- tests/cli/env/test_create.py | 39 ++- tests/cli/env/test_find.py | 3 +- tests/cli/env/test_prune.py | 3 +- tests/cli/env/test_remove.py | 3 +- tests/cli/env/test_run.py | 26 +- tests/cli/env/test_show.py | 3 +- tests/cli/fmt/test_fmt.py | 56 ++-- tests/cli/new/test_new.py | 10 +- tests/cli/project/test_metadata.py | 7 +- tests/cli/publish/test_publish.py | 4 +- tests/cli/python/test_install.py | 2 +- tests/cli/run/test_run.py | 278 ++++++++++++------ tests/cli/test/test_test.py | 14 +- tests/cli/version/test_version.py | 5 +- tests/conftest.py | 6 +- tests/env/collectors/test_custom.py | 9 +- tests/helpers/helpers.py | 6 +- tests/project/test_config.py | 6 +- tests/project/test_frontend.py | 39 ++- tests/python/test_core.py | 4 +- 46 files changed, 581 insertions(+), 361 deletions(-) diff --git a/scripts/generate_coverage_summary.py b/scripts/generate_coverage_summary.py index 26569f97d..8f2947d6d 100644 --- a/scripts/generate_coverage_summary.py +++ b/scripts/generate_coverage_summary.py @@ -13,7 +13,7 @@ def main(): coverage_report = ROOT / 'coverage.xml' - root = etree.fromstring(coverage_report.read_text()) # nosec B320 # noqa: S320 + root = etree.fromstring(coverage_report.read_text(encoding='utf-8')) # nosec B320 # noqa: S320 raw_package_data = defaultdict(lambda: {'hits': 0, 'misses': 0}) for package in root.find('packages'): diff --git a/src/hatch/cli/fmt/core.py b/src/hatch/cli/fmt/core.py index 8b079d89a..733de9c58 100644 --- a/src/hatch/cli/fmt/core.py +++ b/src/hatch/cli/fmt/core.py @@ -84,7 +84,7 @@ def write_config_file(self, *, preview: bool) -> None: return self.internal_config_file.parent.ensure_dir_exists() - self.internal_config_file.write_text(config_contents) + self.internal_config_file.write_text(config_contents, encoding='utf-8') # TODO: remove everything below once this is fixed https://github.com/astral-sh/ruff/issues/8737 if self.internal_user_config_file is None: @@ -93,7 +93,7 @@ def write_config_file(self, *, preview: bool) -> None: if self.user_config_file is None: return - old_contents = self.user_config_file.read_text() + old_contents = self.user_config_file.read_text(encoding='utf-8') config_path = str(self.internal_config_file).replace('\\', '\\\\') if self.user_config_file.name == 'pyproject.toml': lines = old_contents.splitlines() @@ -112,7 +112,7 @@ def write_config_file(self, *, preview: bool) -> None: else: contents = f'extend = "{config_path}"\n{old_contents}' - self.internal_user_config_file.write_text(contents) + self.internal_user_config_file.write_text(contents, encoding='utf-8') @cached_property def internal_user_config_file(self) -> Path | None: @@ -137,7 +137,7 @@ def user_config(self) -> dict[str, Any]: from hatch.utils.toml import load_toml_data - return load_toml_data(self.user_config_file.read_text()) + return load_toml_data(self.user_config_file.read_text(encoding='utf-8')) @cached_property def linter_preview(self) -> bool: diff --git a/src/hatch/cli/run/__init__.py b/src/hatch/cli/run/__init__.py index db5b13ed6..c7589e46a 100644 --- a/src/hatch/cli/run/__init__.py +++ b/src/hatch/cli/run/__init__.py @@ -73,7 +73,7 @@ def run(ctx: click.Context, args: tuple[str, ...]): script = script.resolve() try: - metadata = parse_inline_script_metadata(script.read_text()) + metadata = parse_inline_script_metadata(script.read_text(encoding='utf-8')) except ValueError as e: app.abort(f'{e}, {first_arg}') diff --git a/src/hatch/cli/test/core.py b/src/hatch/cli/test/core.py index 1de06965c..d71428573 100644 --- a/src/hatch/cli/test/core.py +++ b/src/hatch/cli/test/core.py @@ -33,7 +33,7 @@ def write_config_file(self) -> None: from configparser import ConfigParser cfg = ConfigParser() - cfg.read(str(self.user_config_path)) + cfg.read(str(self.user_config_path), encoding='utf-8') if 'run' not in cfg: cfg['run'] = {'parallel': 'true'} @@ -47,9 +47,9 @@ def write_config_file(self) -> None: from hatch.utils.toml import load_toml_data - project_data = load_toml_data(self.user_config_path.read_text()) + project_data = load_toml_data(self.user_config_path.read_text(encoding='utf-8')) project_data.setdefault('tool', {}).setdefault('coverage', {}).setdefault('run', {})['parallel'] = True - self.internal_config_path.write_text(tomli_w.dumps(project_data)) + self.internal_config_path.write_text(tomli_w.dumps(project_data), encoding='utf-8') def _write_ini(self, cfg: ConfigParser) -> None: with self.internal_config_path.open('w', encoding='utf-8') as f: diff --git a/src/hatch/env/virtual.py b/src/hatch/env/virtual.py index dab5a7ee7..428036582 100644 --- a/src/hatch/env/virtual.py +++ b/src/hatch/env/virtual.py @@ -151,7 +151,8 @@ def create(self): """\ # This file was automatically created by Hatch * -""" +""", + encoding='utf-8', ) with self.expose_uv(): diff --git a/src/hatch/project/env.py b/src/hatch/project/env.py index 27420cc51..32d9cdd92 100644 --- a/src/hatch/project/env.py +++ b/src/hatch/project/env.py @@ -398,14 +398,14 @@ def _read(self, environment: EnvironmentInterface) -> dict[str, Any]: if not metadata_file.is_file(): return {} - return json.loads(metadata_file.read_text()) + return json.loads(metadata_file.read_text(encoding='utf-8')) def _write(self, environment: EnvironmentInterface, metadata: dict[str, Any]) -> None: import json metadata_file = self._metadata_file(environment) metadata_file.parent.ensure_dir_exists() - metadata_file.write_text(json.dumps(metadata)) + metadata_file.write_text(json.dumps(metadata), encoding='utf-8') def _metadata_file(self, environment: EnvironmentInterface) -> Path: from hatch.env.internal import is_isolated_environment diff --git a/src/hatch/project/frontend/core.py b/src/hatch/project/frontend/core.py index 702923731..8918b1c83 100644 --- a/src/hatch/project/frontend/core.py +++ b/src/hatch/project/frontend/core.py @@ -36,7 +36,7 @@ def build_sdist(self, directory: Path) -> Path: script_context = fs_context.join('build_sdist.py') script_context.local_path.parent.ensure_dir_exists() - script_context.local_path.write_text(script) + script_context.local_path.write_text(script, encoding='utf-8') script_context.sync_env() context = ExecutionContext(self.__env) @@ -45,7 +45,7 @@ def build_sdist(self, directory: Path) -> Path: output_context.sync_local() output_path = output_context.local_path / 'output.json' - output = json.loads(output_path.read_text()) + output = json.loads(output_path.read_text(encoding='utf-8')) work_dir = output_context.local_path / 'work' artifact_path = Path(work_dir / output['return_val']) @@ -60,7 +60,7 @@ def build_wheel(self, directory: Path) -> Path: script_context = fs_context.join('build_wheel.py') script_context.local_path.parent.ensure_dir_exists() - script_context.local_path.write_text(script) + script_context.local_path.write_text(script, encoding='utf-8') script_context.sync_env() context = ExecutionContext(self.__env) @@ -69,7 +69,7 @@ def build_wheel(self, directory: Path) -> Path: output_context.sync_local() output_path = output_context.local_path / 'output.json' - output = json.loads(output_path.read_text()) + output = json.loads(output_path.read_text(encoding='utf-8')) work_dir = output_context.local_path / 'work' artifact_path = Path(work_dir / output['return_val']) @@ -86,7 +86,7 @@ def get_requires(self, build: Literal['sdist', 'wheel', 'editable']) -> list[str script_context = fs_context.join(f'get_requires_{build}.py') script_context.local_path.parent.ensure_dir_exists() - script_context.local_path.write_text(script) + script_context.local_path.write_text(script, encoding='utf-8') script_context.sync_env() context = ExecutionContext(self.__env) @@ -95,7 +95,7 @@ def get_requires(self, build: Literal['sdist', 'wheel', 'editable']) -> list[str output_context.sync_local() output_path = output_context.local_path / 'output.json' - output = json.loads(output_path.read_text()) + output = json.loads(output_path.read_text(encoding='utf-8')) return output['return_val'] def get_core_metadata(self, *, editable: bool = False) -> dict[str, Any]: @@ -110,7 +110,7 @@ def get_core_metadata(self, *, editable: bool = False) -> dict[str, Any]: script_context = fs_context.join('get_core_metadata.py') script_context.local_path.parent.ensure_dir_exists() - script_context.local_path.write_text(script) + script_context.local_path.write_text(script, encoding='utf-8') script_context.sync_env() context = ExecutionContext(self.__env) @@ -119,11 +119,11 @@ def get_core_metadata(self, *, editable: bool = False) -> dict[str, Any]: output_context.sync_local() output_path = output_context.local_path / 'output.json' - output = json.loads(output_path.read_text()) + output = json.loads(output_path.read_text(encoding='utf-8')) work_dir = output_context.local_path / 'work' metadata_file = Path(work_dir) / output['return_val'] / 'METADATA' - return project_metadata_from_core_metadata(metadata_file.read_text()) + return project_metadata_from_core_metadata(metadata_file.read_text(encoding='utf-8')) class HatchBuildFrontend: @@ -146,7 +146,7 @@ def get_build_deps(self, targets: list[str]) -> list[str]: script_context = fs_context.join(f'get_build_deps_{"_".join(targets)}.py') script_context.local_path.parent.ensure_dir_exists() - script_context.local_path.write_text(script) + script_context.local_path.write_text(script, encoding='utf-8') script_context.sync_env() context = ExecutionContext(self.__env) @@ -155,7 +155,7 @@ def get_build_deps(self, targets: list[str]) -> list[str]: output_context.sync_local() output_path = output_context.local_path / 'output.json' - output: list[str] = json.loads(output_path.read_text()) + output: list[str] = json.loads(output_path.read_text(encoding='utf-8')) return output def get_core_metadata(self) -> dict[str, Any]: @@ -168,7 +168,7 @@ def get_core_metadata(self) -> dict[str, Any]: script_context = fs_context.join('get_core_metadata.py') script_context.local_path.parent.ensure_dir_exists() - script_context.local_path.write_text(script) + script_context.local_path.write_text(script, encoding='utf-8') script_context.sync_env() context = ExecutionContext(self.__env) @@ -177,7 +177,7 @@ def get_core_metadata(self) -> dict[str, Any]: output_context.sync_local() output_path = output_context.local_path / 'output.json' - output: dict[str, Any] = json.loads(output_path.read_text()) + output: dict[str, Any] = json.loads(output_path.read_text(encoding='utf-8')) return output def get_required_build_deps(self, targets: list[str]) -> list[str]: diff --git a/src/hatch/publish/auth.py b/src/hatch/publish/auth.py index 79790a543..589c87e90 100644 --- a/src/hatch/publish/auth.py +++ b/src/hatch/publish/auth.py @@ -74,7 +74,7 @@ def __get_username(self) -> str: def _read_previous_working_user_data(self) -> str | None: if self._pwu_path.is_file(): - contents = self._pwu_path.read_text() + contents = self._pwu_path.read_text(encoding='utf-8') if contents: import json @@ -85,7 +85,7 @@ def _read_pypirc(self) -> str | None: import configparser pypirc = configparser.ConfigParser() - pypirc.read(Path.home() / '.pypirc') + pypirc.read(Path.home() / '.pypirc', encoding='utf-8') repo = self._repo or 'pypi' if pypirc.has_section(repo): @@ -106,7 +106,7 @@ def write_updated_data(self): self.__pwu_data[self._repo] = self.__username self._pwu_path.ensure_parent_dir_exists() - self._pwu_path.write_text(json.dumps(self.__pwu_data)) + self._pwu_path.write_text(json.dumps(self.__pwu_data), encoding='utf-8') if self.__password_was_read: import keyring diff --git a/src/hatch/python/core.py b/src/hatch/python/core.py index b0914b7db..2828a6700 100644 --- a/src/hatch/python/core.py +++ b/src/hatch/python/core.py @@ -69,7 +69,7 @@ def get_installed(self) -> dict[str, InstalledDistribution]: if not metadata_file.is_file(): continue - metadata = json.loads(metadata_file.read_text()) + metadata = json.loads(metadata_file.read_text(encoding='utf-8')) distribution = get_distribution(path.name, source=metadata.get('source', '')) if not (path / distribution.python_path).is_file(): continue @@ -117,7 +117,7 @@ def install(self, identifier: str) -> InstalledDistribution: metadata = {'source': dist.source, 'python_path': dist.python_path} metadata_file = path / InstalledDistribution.metadata_filename() - metadata_file.write_text(json.dumps(metadata, indent=2)) + metadata_file.write_text(json.dumps(metadata, indent=2), encoding='utf-8') return InstalledDistribution(path, dist, metadata) diff --git a/tests/backend/builders/hooks/test_custom.py b/tests/backend/builders/hooks/test_custom.py index 1fc918c13..381ac1957 100644 --- a/tests/backend/builders/hooks/test_custom.py +++ b/tests/backend/builders/hooks/test_custom.py @@ -40,7 +40,8 @@ class CustomHook(BuildHookInterface): def foo(self): return self.PLUGIN_NAME, self.root """ - ) + ), + encoding='utf-8', ) with temp_dir.as_cwd(): @@ -63,7 +64,8 @@ class CustomHook(BuildHookInterface): def foo(self): return self.PLUGIN_NAME, self.root """ - ) + ), + encoding='utf-8', ) with temp_dir.as_cwd(): @@ -88,7 +90,8 @@ def test_no_subclass(temp_dir, helpers): class CustomHook: pass """ - ) + ), + encoding='utf-8', ) with pytest.raises( diff --git a/tests/backend/builders/hooks/test_version.py b/tests/backend/builders/hooks/test_version.py index b03500292..727d7f5c3 100644 --- a/tests/backend/builders/hooks/test_version.py +++ b/tests/backend/builders/hooks/test_version.py @@ -80,7 +80,8 @@ class CustomHook(MetadataHookInterface): def update(self, metadata): metadata['version'] = '1.2.3' """ - ) + ), + encoding='utf-8', ) build_data = {'artifacts': []} @@ -89,7 +90,7 @@ def update(self, metadata): expected_file = temp_dir / 'baz.py' assert expected_file.is_file() - assert expected_file.read_text() == helpers.dedent( + assert expected_file.read_text(encoding='utf-8') == helpers.dedent( """ # This file is auto-generated by Hatchling. As such, do not: # - modify @@ -120,7 +121,8 @@ class CustomHook(MetadataHookInterface): def update(self, metadata): metadata['version'] = '1.2.3' """ - ) + ), + encoding='utf-8', ) build_data = {'artifacts': []} @@ -129,7 +131,7 @@ def update(self, metadata): expected_file = temp_dir / 'bar' / 'baz.py' assert expected_file.is_file() - assert expected_file.read_text() == helpers.dedent( + assert expected_file.read_text(encoding='utf-8') == helpers.dedent( """ # This file is auto-generated by Hatchling. As such, do not: # - modify @@ -160,7 +162,8 @@ class CustomHook(MetadataHookInterface): def update(self, metadata): metadata['version'] = '1.2.3' """ - ) + ), + encoding='utf-8', ) build_data = {'artifacts': []} @@ -169,7 +172,7 @@ def update(self, metadata): expected_file = temp_dir / 'baz.py' assert expected_file.is_file() - assert expected_file.read_text() == helpers.dedent( + assert expected_file.read_text(encoding='utf-8') == helpers.dedent( """ VER = '1.2.3' """ @@ -199,7 +202,8 @@ class CustomHook(MetadataHookInterface): def update(self, metadata): metadata['version'] = '1.2.3' """ - ) + ), + encoding='utf-8', ) version_file = temp_dir / 'baz.py' version_file.write_text( @@ -207,14 +211,15 @@ def update(self, metadata): """ __version__ = '0.0.0' """ - ) + ), + encoding='utf-8', ) build_data = {'artifacts': []} hook = VersionBuildHook(str(temp_dir), config, None, metadata, '', '') hook.initialize([], build_data) - assert version_file.read_text() == helpers.dedent( + assert version_file.read_text(encoding='utf-8') == helpers.dedent( """ __version__ = '1.2.3' """ @@ -242,7 +247,8 @@ class CustomHook(MetadataHookInterface): def update(self, metadata): metadata['version'] = '1.2.3' """ - ) + ), + encoding='utf-8', ) version_file = temp_dir / 'baz.py' version_file.write_text( @@ -250,14 +256,15 @@ def update(self, metadata): """ v = "0.0.0" """ - ) + ), + encoding='utf-8', ) build_data = {'artifacts': []} hook = VersionBuildHook(str(temp_dir), config, None, metadata, '', '') hook.initialize([], build_data) - assert version_file.read_text() == helpers.dedent( + assert version_file.read_text(encoding='utf-8') == helpers.dedent( """ v = "1.2.3" """ diff --git a/tests/backend/builders/plugin/test_interface.py b/tests/backend/builders/plugin/test_interface.py index afaa33de7..999e0e5dc 100644 --- a/tests/backend/builders/plugin/test_interface.py +++ b/tests/backend/builders/plugin/test_interface.py @@ -42,7 +42,7 @@ def test_reuse(self, isolation): def test_read(self, temp_dir): project_file = temp_dir / 'pyproject.toml' - project_file.write_text('foo = 5') + project_file.write_text('foo = 5', encoding='utf-8') with temp_dir.as_cwd(): builder = MockBuilder(str(temp_dir)) diff --git a/tests/backend/builders/test_config.py b/tests/backend/builders/test_config.py index 0fb358e04..782b0680a 100644 --- a/tests/backend/builders/test_config.py +++ b/tests/backend/builders/test_config.py @@ -1832,7 +1832,7 @@ def test_vcs_git(self, temp_dir, separator, platform): builder = MockBuilder(str(temp_dir), config=config) vcs_ignore_file = temp_dir / '.gitignore' - vcs_ignore_file.write_text('/bar\n*.pyc') + vcs_ignore_file.write_text('/bar\n*.pyc', encoding='utf-8') assert builder.config.exclude_spec.match_file(f'foo{separator}file.py') assert builder.config.exclude_spec.match_file(f'bar{separator}file.py') @@ -1849,7 +1849,7 @@ def test_ignore_vcs_git(self, temp_dir, separator, platform): builder = MockBuilder(str(temp_dir), config=config) vcs_ignore_file = temp_dir / '.gitignore' - vcs_ignore_file.write_text('/bar\n*.pyc') + vcs_ignore_file.write_text('/bar\n*.pyc', encoding='utf-8') assert builder.config.exclude_spec.match_file(f'foo{separator}file.py') assert not builder.config.exclude_spec.match_file(f'bar{separator}file.py') @@ -1868,7 +1868,7 @@ def test_vcs_git_boundary(self, temp_dir, separator, platform): builder = MockBuilder(str(project_dir), config=config) vcs_ignore_file = temp_dir / '.gitignore' - vcs_ignore_file.write_text('/bar\n*.pyc') + vcs_ignore_file.write_text('/bar\n*.pyc', encoding='utf-8') assert builder.config.exclude_spec.match_file(f'foo{separator}file.py') assert not builder.config.exclude_spec.match_file(f'bar{separator}file.py') @@ -1883,7 +1883,7 @@ def test_vcs_git_exclude_whitelisted_file(self, temp_dir, separator, platform): builder = MockBuilder(str(temp_dir), config=config) vcs_ignore_file = temp_dir / '.gitignore' - vcs_ignore_file.write_text('foo/*\n!foo/bar') + vcs_ignore_file.write_text('foo/*\n!foo/bar', encoding='utf-8') assert builder.config.path_is_excluded(f'foo{separator}deb') is True assert builder.config.path_is_excluded(f'foo{separator}bar') is True @@ -1898,7 +1898,7 @@ def test_vcs_mercurial(self, temp_dir, separator, platform): builder = MockBuilder(str(temp_dir), config=config) vcs_ignore_file = temp_dir / '.hgignore' - vcs_ignore_file.write_text('syntax: glob\n/bar\n*.pyc') + vcs_ignore_file.write_text('syntax: glob\n/bar\n*.pyc', encoding='utf-8') assert builder.config.exclude_spec.match_file(f'foo{separator}file.py') assert builder.config.exclude_spec.match_file(f'bar{separator}file.py') @@ -1915,7 +1915,7 @@ def test_ignore_vcs_mercurial(self, temp_dir, separator, platform): builder = MockBuilder(str(temp_dir), config=config) vcs_ignore_file = temp_dir / '.hgignore' - vcs_ignore_file.write_text('syntax: glob\n/bar\n*.pyc') + vcs_ignore_file.write_text('syntax: glob\n/bar\n*.pyc', encoding='utf-8') assert builder.config.exclude_spec.match_file(f'foo{separator}file.py') assert not builder.config.exclude_spec.match_file(f'bar{separator}file.py') @@ -1934,7 +1934,7 @@ def test_vcs_mercurial_boundary(self, temp_dir, separator, platform): builder = MockBuilder(str(project_dir), config=config) vcs_ignore_file = temp_dir / '.hgignore' - vcs_ignore_file.write_text('syntax: glob\n/bar\n*.pyc') + vcs_ignore_file.write_text('syntax: glob\n/bar\n*.pyc', encoding='utf-8') assert builder.config.exclude_spec.match_file(f'foo{separator}file.py') assert not builder.config.exclude_spec.match_file(f'bar{separator}file.py') diff --git a/tests/backend/builders/test_custom.py b/tests/backend/builders/test_custom.py index 02c43d43f..d32c4769a 100644 --- a/tests/backend/builders/test_custom.py +++ b/tests/backend/builders/test_custom.py @@ -83,7 +83,8 @@ def build(self, **kwargs): os.replace(artifact, new_path) yield new_path """ - ) + ), + encoding='utf-8', ) builder = CustomBuilder(str(project_path), config=config) @@ -162,7 +163,8 @@ def build(self, **kwargs): os.replace(artifact, new_path) yield new_path """ - ) + ), + encoding='utf-8', ) builder = CustomBuilder(str(project_path), config=config) @@ -231,7 +233,8 @@ def test_no_subclass(hatch, helpers, temp_dir): class CustomBuilder: pass """ - ) + ), + encoding='utf-8', ) with pytest.raises( @@ -273,7 +276,8 @@ def test_multiple_subclasses(hatch, helpers, temp_dir): class CustomWheelBuilder(WheelBuilder): pass """ - ) + ), + encoding='utf-8', ) with pytest.raises( @@ -328,7 +332,8 @@ class CustomHook(BuildHookInterface): def dependencies(self): return ['baz'] """ - ) + ), + encoding='utf-8', ) builder = CustomBuilder(str(project_path), config=config) diff --git a/tests/backend/builders/test_sdist.py b/tests/backend/builders/test_sdist.py index 8e42f97ff..a72300ca5 100644 --- a/tests/backend/builders/test_sdist.py +++ b/tests/backend/builders/test_sdist.py @@ -818,7 +818,7 @@ def test_default_build_script_artifacts(self, hatch, helpers, temp_dir, config_f project_path = temp_dir / 'my-app' vcs_ignore_file = project_path / '.gitignore' - vcs_ignore_file.write_text('*.pyc\n*.so\n*.h\n') + vcs_ignore_file.write_text('*.pyc\n*.so\n*.h\n', encoding='utf-8') build_script = project_path / DEFAULT_BUILD_SCRIPT build_script.write_text( @@ -833,7 +833,8 @@ def initialize(self, version, build_data): pathlib.Path('my_app', 'lib.so').touch() pathlib.Path('my_app', 'lib.h').touch() """ - ) + ), + encoding='utf-8', ) config = { @@ -892,7 +893,7 @@ def test_default_build_script_extra_dependencies(self, hatch, helpers, temp_dir, project_path = temp_dir / 'my-app' vcs_ignore_file = project_path / '.gitignore' - vcs_ignore_file.write_text('*.pyc\n*.so\n*.h\n') + vcs_ignore_file.write_text('*.pyc\n*.so\n*.h\n', encoding='utf-8') build_script = project_path / DEFAULT_BUILD_SCRIPT build_script.write_text( @@ -908,7 +909,8 @@ def initialize(self, version, build_data): pathlib.Path('my_app', 'lib.h').touch() build_data['dependencies'].append('binary') """ - ) + ), + encoding='utf-8', ) config = { @@ -1357,7 +1359,7 @@ def test_default_vcs_git_exclusion_files(self, hatch, helpers, temp_dir, config_ project_path = temp_dir / 'my-app' vcs_ignore_file = temp_dir / '.gitignore' - vcs_ignore_file.write_text('*.pyc\n*.so\n*.h\n') + vcs_ignore_file.write_text('*.pyc\n*.so\n*.h\n', encoding='utf-8') (project_path / 'my_app' / 'lib.so').touch() (project_path / 'my_app' / 'lib.h').touch() @@ -1428,7 +1430,8 @@ def test_default_vcs_mercurial_exclusion_files(self, hatch, helpers, temp_dir, c *.so *.h """ - ) + ), + encoding='utf-8', ) (project_path / 'my_app' / 'lib.so').touch() diff --git a/tests/backend/builders/test_wheel.py b/tests/backend/builders/test_wheel.py index bdc4e99dd..c9ab0cd8a 100644 --- a/tests/backend/builders/test_wheel.py +++ b/tests/backend/builders/test_wheel.py @@ -1188,7 +1188,8 @@ def test_default_build_script_default_tag(self, hatch, helpers, temp_dir, config class CustomHook(BuildHookInterface): pass """ - ) + ), + encoding='utf-8', ) config = { @@ -1256,7 +1257,8 @@ class CustomHook(BuildHookInterface): def initialize(self, version, build_data): build_data['tag'] = 'foo-bar-baz' """ - ) + ), + encoding='utf-8', ) config = { @@ -1315,7 +1317,7 @@ def test_default_build_script_known_artifacts(self, hatch, helpers, temp_dir, co project_path = temp_dir / 'my-app' vcs_ignore_file = project_path / '.gitignore' - vcs_ignore_file.write_text('*.pyc\n*.so\n*.h') + vcs_ignore_file.write_text('*.pyc\n*.so\n*.h', encoding='utf-8') build_script = project_path / DEFAULT_BUILD_SCRIPT build_script.write_text( @@ -1333,7 +1335,8 @@ def initialize(self, version, build_data): pathlib.Path('my_app', 'lib.so').touch() pathlib.Path('my_app', 'lib.h').touch() """ - ) + ), + encoding='utf-8', ) config = { @@ -1397,7 +1400,7 @@ def test_default_build_script_configured_build_hooks(self, hatch, helpers, temp_ project_path = temp_dir / 'my-app' vcs_ignore_file = project_path / '.gitignore' - vcs_ignore_file.write_text('*.pyc\n*.so\n*.h') + vcs_ignore_file.write_text('*.pyc\n*.so\n*.h', encoding='utf-8') build_script = project_path / DEFAULT_BUILD_SCRIPT build_script.write_text( @@ -1412,10 +1415,11 @@ def initialize(self, version, build_data): build_data['pure_python'] = False build_data['infer_tag'] = True - pathlib.Path('my_app', 'lib.so').write_text(','.join(build_data['build_hooks'])) - pathlib.Path('my_app', 'lib.h').write_text(','.join(build_data['build_hooks'])) + pathlib.Path('my_app', 'lib.so').write_text(','.join(build_data['build_hooks']), encoding='utf-8') + pathlib.Path('my_app', 'lib.h').write_text(','.join(build_data['build_hooks']), encoding='utf-8') """ - ) + ), + encoding='utf-8', ) config = { @@ -1479,7 +1483,7 @@ def test_default_build_script_extra_dependencies(self, hatch, helpers, temp_dir, project_path = temp_dir / 'my-app' vcs_ignore_file = project_path / '.gitignore' - vcs_ignore_file.write_text('*.pyc\n*.so\n*.h') + vcs_ignore_file.write_text('*.pyc\n*.so\n*.h', encoding='utf-8') build_script = project_path / DEFAULT_BUILD_SCRIPT build_script.write_text( @@ -1498,7 +1502,8 @@ def initialize(self, version, build_data): pathlib.Path('my_app', 'lib.so').touch() pathlib.Path('my_app', 'lib.h').touch() """ - ) + ), + encoding='utf-8', ) config = { @@ -1562,7 +1567,7 @@ def test_default_build_script_dynamic_artifacts(self, hatch, helpers, temp_dir, project_path = temp_dir / 'my-app' vcs_ignore_file = project_path / '.gitignore' - vcs_ignore_file.write_text('*.pyc\n*.so\n*.h') + vcs_ignore_file.write_text('*.pyc\n*.so\n*.h', encoding='utf-8') build_script = project_path / DEFAULT_BUILD_SCRIPT build_script.write_text( @@ -1581,7 +1586,8 @@ def initialize(self, version, build_data): pathlib.Path('my_app', 'lib.so').touch() pathlib.Path('my_app', 'lib.h').touch() """ - ) + ), + encoding='utf-8', ) config = { @@ -1644,7 +1650,7 @@ def test_default_build_script_dynamic_force_include(self, hatch, helpers, temp_d project_path = temp_dir / 'my-app' vcs_ignore_file = project_path / '.gitignore' - vcs_ignore_file.write_text('*.pyc\n*.so\n*.h') + vcs_ignore_file.write_text('*.pyc\n*.so\n*.h', encoding='utf-8') build_script = project_path / DEFAULT_BUILD_SCRIPT build_script.write_text( @@ -1666,7 +1672,8 @@ def initialize(self, version, build_data): (artifact_path / 'lib.so').touch() (artifact_path / 'lib.h').touch() """ - ) + ), + encoding='utf-8', ) config = { @@ -1729,10 +1736,10 @@ def test_default_build_script_dynamic_force_include_duplicate(self, hatch, helpe project_path = temp_dir / 'my-app' vcs_ignore_file = project_path / '.gitignore' - vcs_ignore_file.write_text('*.pyc\n*.so\n*.h') + vcs_ignore_file.write_text('*.pyc\n*.so\n*.h', encoding='utf-8') target_file = project_path / 'my_app' / 'z.py' - target_file.write_text('print("hello world")') + target_file.write_text('print("hello world")', encoding='utf-8') build_script = project_path / DEFAULT_BUILD_SCRIPT build_script.write_text( @@ -1752,7 +1759,8 @@ def initialize(self, version, build_data): tmp_path.mkdir() (tmp_path / 'new_z.py').write_bytes(pathlib.Path('my_app/z.py').read_bytes()) """ - ) + ), + encoding='utf-8', ) config = { @@ -1812,7 +1820,7 @@ def test_default_build_script_dynamic_artifacts_with_src_layout(self, hatch, hel project_path = temp_dir / 'my-app' vcs_ignore_file = project_path / '.gitignore' - vcs_ignore_file.write_text('*.pyc\n*.so\n*.pyd\n*.h') + vcs_ignore_file.write_text('*.pyc\n*.so\n*.pyd\n*.h', encoding='utf-8') build_script = project_path / DEFAULT_BUILD_SCRIPT build_script.write_text( @@ -1833,7 +1841,8 @@ def initialize(self, version, build_data): pathlib.Path('src', 'lib.h').touch() pathlib.Path('src', 'zlib.pyd').touch() """ - ) + ), + encoding='utf-8', ) config = { @@ -1974,7 +1983,8 @@ class CustomHook(BuildHookInterface): def initialize(self, version, build_data): build_data['shared_data']['../data'] = '/' """ - ) + ), + encoding='utf-8', ) config = { @@ -2047,7 +2057,8 @@ def test_default_shared_scripts(self, hatch, platform, helpers, temp_dir, config #!/bin/sh arg1 arg2 echo "Hello, World!" """ - ) + ), + encoding='utf-8', ) (shared_data_path / 'python_script.sh').write_text( helpers.dedent( @@ -2056,7 +2067,8 @@ def test_default_shared_scripts(self, hatch, platform, helpers, temp_dir, config #!/usr/bin/env python3.11 arg1 arg2 print("Hello, World!") """ - ) + ), + encoding='utf-8', ) (shared_data_path / 'pythonw_script.sh').write_text( helpers.dedent( @@ -2065,7 +2077,8 @@ def test_default_shared_scripts(self, hatch, platform, helpers, temp_dir, config #!/usr/bin/pythonw3.11 arg1 arg2 print("Hello, World!") """ - ) + ), + encoding='utf-8', ) (shared_data_path / 'pypy_script.sh').write_text( helpers.dedent( @@ -2074,7 +2087,8 @@ def test_default_shared_scripts(self, hatch, platform, helpers, temp_dir, config #!/usr/bin/env pypy print("Hello, World!") """ - ) + ), + encoding='utf-8', ) (shared_data_path / 'pypyw_script.sh').write_text( helpers.dedent( @@ -2083,7 +2097,8 @@ def test_default_shared_scripts(self, hatch, platform, helpers, temp_dir, config #!pypyw3.11 arg1 arg2 print("Hello, World!") """ - ) + ), + encoding='utf-8', ) config = { @@ -2158,7 +2173,8 @@ def test_default_shared_scripts_from_build_data(self, hatch, platform, helpers, #!/bin/sh arg1 arg2 echo "Hello, World!" """ - ) + ), + encoding='utf-8', ) (shared_data_path / 'python_script.sh').write_text( helpers.dedent( @@ -2167,7 +2183,8 @@ def test_default_shared_scripts_from_build_data(self, hatch, platform, helpers, #!/usr/bin/env python3.11 arg1 arg2 print("Hello, World!") """ - ) + ), + encoding='utf-8', ) (shared_data_path / 'pythonw_script.sh').write_text( helpers.dedent( @@ -2176,7 +2193,8 @@ def test_default_shared_scripts_from_build_data(self, hatch, platform, helpers, #!/usr/bin/pythonw3.11 arg1 arg2 print("Hello, World!") """ - ) + ), + encoding='utf-8', ) (shared_data_path / 'pypy_script.sh').write_text( helpers.dedent( @@ -2185,7 +2203,8 @@ def test_default_shared_scripts_from_build_data(self, hatch, platform, helpers, #!/usr/bin/env pypy print("Hello, World!") """ - ) + ), + encoding='utf-8', ) (shared_data_path / 'pypyw_script.sh').write_text( helpers.dedent( @@ -2194,7 +2213,8 @@ def test_default_shared_scripts_from_build_data(self, hatch, platform, helpers, #!pypyw3.11 arg1 arg2 print("Hello, World!") """ - ) + ), + encoding='utf-8', ) build_script = project_path / DEFAULT_BUILD_SCRIPT @@ -2209,7 +2229,8 @@ class CustomHook(BuildHookInterface): def initialize(self, version, build_data): build_data['shared_scripts']['../data'] = '/' """ - ) + ), + encoding='utf-8', ) config = { @@ -2344,7 +2365,8 @@ class CustomHook(BuildHookInterface): def initialize(self, version, build_data): build_data['extra_metadata']['../data'] = '/' """ - ) + ), + encoding='utf-8', ) config = { @@ -2400,7 +2422,7 @@ def test_default_symlink(self, hatch, helpers, temp_dir, config_file): project_path = temp_dir / 'my-app' vcs_ignore_file = project_path / '.gitignore' - vcs_ignore_file.write_text('*.pyc\n*.so\n*.h') + vcs_ignore_file.write_text('*.pyc\n*.so\n*.h', encoding='utf-8') (temp_dir / 'foo.so').write_bytes(b'data') @@ -2421,7 +2443,8 @@ def initialize(self, version, build_data): pathlib.Path('my_app', 'lib.so').symlink_to(os.path.abspath(os.path.join('..', 'foo.so'))) pathlib.Path('my_app', 'lib.h').touch() """ - ) + ), + encoding='utf-8', ) config = { @@ -2551,7 +2574,8 @@ class CustomHook(BuildHookInterface): def initialize(self, version, build_data): build_data['dependencies'].append('binary') """ - ) + ), + encoding='utf-8', ) config = { @@ -2624,7 +2648,8 @@ def initialize(self, version, build_data): # Prefix z just to satisfy our ordering test assertion build_data['force_include_editable']['src/my_app/__about__.py'] = 'zfoo.py' """ - ) + ), + encoding='utf-8', ) config = { @@ -2883,7 +2908,8 @@ class CustomHook(BuildHookInterface): def initialize(self, version, build_data): build_data['dependencies'].append('binary') """ - ) + ), + encoding='utf-8', ) config = { @@ -2963,7 +2989,8 @@ def initialize(self, version, build_data): # Prefix z just to satisfy our ordering test assertion build_data['force_include_editable']['my_app/__about__.py'] = 'zfoo.py' """ - ) + ), + encoding='utf-8', ) config = { @@ -3111,7 +3138,8 @@ def initialize(self, version, build_data): # Prefix z just to satisfy our ordering test assertion build_data['force_include_editable']['my_app/__about__.py'] = 'zfoo.py' """ - ) + ), + encoding='utf-8', ) config = { @@ -3548,7 +3576,7 @@ def test_macos_archflags(self, hatch, helpers, temp_dir, config_file, archflags, project_path = temp_dir / 'my-app' vcs_ignore_file = project_path / '.gitignore' - vcs_ignore_file.write_text('*.pyc\n*.so\n*.h') + vcs_ignore_file.write_text('*.pyc\n*.so\n*.h', encoding='utf-8') build_script = project_path / DEFAULT_BUILD_SCRIPT build_script.write_text( @@ -3566,7 +3594,8 @@ def initialize(self, version, build_data): pathlib.Path('my_app', 'lib.so').touch() pathlib.Path('my_app', 'lib.h').touch() """ - ) + ), + encoding='utf-8', ) config = { @@ -3634,7 +3663,7 @@ def test_macos_max_compat(self, hatch, helpers, temp_dir, config_file, macos_max project_path = temp_dir / 'my-app' vcs_ignore_file = project_path / '.gitignore' - vcs_ignore_file.write_text('*.pyc\n*.so\n*.h') + vcs_ignore_file.write_text('*.pyc\n*.so\n*.h', encoding='utf-8') build_script = project_path / DEFAULT_BUILD_SCRIPT build_script.write_text( @@ -3652,7 +3681,8 @@ def initialize(self, version, build_data): pathlib.Path('my_app', 'lib.so').touch() pathlib.Path('my_app', 'lib.h').touch() """ - ) + ), + encoding='utf-8', ) config = { diff --git a/tests/backend/metadata/test_core.py b/tests/backend/metadata/test_core.py index 76fd6601f..4fad53544 100644 --- a/tests/backend/metadata/test_core.py +++ b/tests/backend/metadata/test_core.py @@ -30,7 +30,7 @@ def test_reuse(self, isolation): def test_read(self, temp_dir): project_file = temp_dir / 'pyproject.toml' - project_file.write_text('foo = 5') + project_file.write_text('foo = 5', encoding='utf-8') with temp_dir.as_cwd(): metadata = ProjectMetadata(str(temp_dir), None) @@ -120,7 +120,7 @@ def test_cache_correct(self, temp_dir, helpers): file_path = temp_dir / 'a' / 'b' file_path.ensure_parent_dir_exists() - file_path.write_text('__version__ = "0.0.1"') + file_path.write_text('__version__ = "0.0.1"', encoding='utf-8') file_path = temp_dir / DEFAULT_BUILD_SCRIPT file_path.write_text( @@ -132,7 +132,8 @@ class CustomHook(MetadataHookInterface): def update(self, metadata): metadata['description'] = metadata['name'] + 'bar' """ - ) + ), + encoding='utf-8', ) # Trigger hooks with `metadata.core` first @@ -279,7 +280,7 @@ def test_dynamic_source_regex(self, temp_dir): file_path = temp_dir / 'a' / 'b' file_path.ensure_parent_dir_exists() - file_path.write_text('__version__ = "0.0.1"') + file_path.write_text('__version__ = "0.0.1"', encoding='utf-8') assert metadata.hatch.version.source is metadata.hatch.version.source assert isinstance(metadata.hatch.version.source, RegexSource) @@ -294,7 +295,7 @@ def test_dynamic_source_regex_invalid(self, temp_dir): file_path = temp_dir / 'a' / 'b' file_path.ensure_parent_dir_exists() - file_path.write_text('__version__ = "0..0"') + file_path.write_text('__version__ = "0..0"', encoding='utf-8') with pytest.raises( ValueError, match='Invalid version `0..0` from source `regex`, see https://peps.python.org/pep-0440/' @@ -394,7 +395,7 @@ def test_string_correct(self, extension, content_type, temp_dir): file_path = temp_dir / 'foo' / f'bar{extension}' file_path.ensure_parent_dir_exists() - file_path.write_text('test content') + file_path.write_text('test content', encoding='utf-8') assert metadata.core.readme == metadata.core.readme == 'test content' assert metadata.core.readme_content_type == metadata.core.readme_content_type == content_type @@ -461,7 +462,7 @@ def test_table_file_correct(self, temp_dir): file_path = temp_dir / 'foo' / 'bar.markdown' file_path.ensure_parent_dir_exists() - file_path.write_text('test content') + file_path.write_text('test content', encoding='utf-8') assert metadata.core.readme == metadata.core.readme == 'test content' assert metadata.core.readme_content_type == metadata.core.readme_content_type == 'text/markdown' @@ -590,7 +591,7 @@ def test_file_correct(self, temp_dir): file_path = temp_dir / 'foo' / 'bar.md' file_path.ensure_parent_dir_exists() - file_path.write_text('test content') + file_path.write_text('test content', encoding='utf-8') assert metadata.core.license == 'test content' @@ -1461,7 +1462,7 @@ def test_custom(self, temp_dir, helpers): file_path = temp_dir / 'a' / 'b' file_path.ensure_parent_dir_exists() - file_path.write_text('__version__ = "0.0.1"') + file_path.write_text('__version__ = "0.0.1"', encoding='utf-8') file_path = temp_dir / DEFAULT_BUILD_SCRIPT file_path.write_text( @@ -1477,7 +1478,8 @@ def update(self, metadata): def get_known_classifiers(self): return ['Framework :: Foo'] """ - ) + ), + encoding='utf-8', ) assert 'custom' in metadata.hatch.metadata.hooks @@ -1504,7 +1506,7 @@ def test_custom_missing_dynamic(self, temp_dir, helpers): file_path = temp_dir / 'a' / 'b' file_path.ensure_parent_dir_exists() - file_path.write_text('__version__ = "0.0.1"') + file_path.write_text('__version__ = "0.0.1"', encoding='utf-8') file_path = temp_dir / DEFAULT_BUILD_SCRIPT file_path.write_text( @@ -1516,7 +1518,8 @@ class CustomHook(MetadataHookInterface): def update(self, metadata): metadata['description'] = metadata['name'] + 'bar' """ - ) + ), + encoding='utf-8', ) with pytest.raises( @@ -1539,7 +1542,7 @@ def test_correct(self, temp_dir, helpers): file_path = temp_dir / 'a' / 'b' file_path.ensure_parent_dir_exists() - file_path.write_text('__version__ = "0.0.1"') + file_path.write_text('__version__ = "0.0.1"', encoding='utf-8') (temp_dir / 'pyproject.toml').touch() file_path = temp_dir / 'hatch.toml' @@ -1549,7 +1552,8 @@ def test_correct(self, temp_dir, helpers): [version] path = 'a/b' """ - ) + ), + encoding='utf-8', ) assert metadata.version == '0.0.1' @@ -1567,11 +1571,11 @@ def test_precedence(self, temp_dir, helpers): file_path = temp_dir / 'a' / 'b' file_path.ensure_parent_dir_exists() - file_path.write_text('__version__ = "0.0.1"') + file_path.write_text('__version__ = "0.0.1"', encoding='utf-8') file_path = temp_dir / 'c' / 'd' file_path.ensure_parent_dir_exists() - file_path.write_text('__version__ = "0.0.2"') + file_path.write_text('__version__ = "0.0.2"', encoding='utf-8') (temp_dir / 'pyproject.toml').touch() file_path = temp_dir / 'hatch.toml' @@ -1581,7 +1585,8 @@ def test_precedence(self, temp_dir, helpers): [version] path = 'c/d' """ - ) + ), + encoding='utf-8', ) assert metadata.version == '0.0.2' @@ -1746,7 +1751,8 @@ def test_basic_persistence(self, temp_dir, helpers): Keywords: foo,bar Requires-Dist: bar==5 """ - ) + ), + encoding='utf-8', ) with temp_dir.as_cwd(): assert metadata.core.config == { @@ -1783,7 +1789,8 @@ def update(self, metadata): metadata['description'] = metadata['name'] + ' bar' metadata['scripts'] = {'foo': 'bar'} """ - ) + ), + encoding='utf-8', ) pkg_info = temp_dir / 'PKG-INFO' @@ -1797,7 +1804,8 @@ def update(self, metadata): Keywords: foo,bar Requires-Dist: bar==5 """ - ) + ), + encoding='utf-8', ) with temp_dir.as_cwd(): assert metadata.core.config == { diff --git a/tests/backend/metadata/test_custom_hook.py b/tests/backend/metadata/test_custom_hook.py index ad83c0e7b..2e02dc717 100644 --- a/tests/backend/metadata/test_custom_hook.py +++ b/tests/backend/metadata/test_custom_hook.py @@ -43,7 +43,8 @@ def update(self, metadata): def foo(self): return self.PLUGIN_NAME, self.root """ - ) + ), + encoding='utf-8', ) with temp_dir.as_cwd(): @@ -69,7 +70,8 @@ def update(self, metadata): def foo(self): return self.PLUGIN_NAME, self.root """ - ) + ), + encoding='utf-8', ) with temp_dir.as_cwd(): @@ -94,7 +96,8 @@ def test_no_subclass(temp_dir, helpers): class CustomHook: pass """ - ) + ), + encoding='utf-8', ) with pytest.raises( diff --git a/tests/backend/test_build.py b/tests/backend/test_build.py index 810956dab..1ba8c4a14 100644 --- a/tests/backend/test_build.py +++ b/tests/backend/test_build.py @@ -27,7 +27,8 @@ def test_sdist(hatch, helpers, temp_dir, config_file): [tool.hatch.build.targets.sdist] versions = '9000' """ - ) + ), + encoding='utf-8', ) build_path = project_path / 'dist' @@ -68,7 +69,8 @@ def test_wheel(hatch, helpers, temp_dir, config_file): [tool.hatch.build.targets.wheel] versions = '9000' """ - ) + ), + encoding='utf-8', ) build_path = project_path / 'dist' @@ -109,7 +111,8 @@ def test_editable(hatch, helpers, temp_dir, config_file): [tool.hatch.build.targets.wheel] versions = '9000' """ - ) + ), + encoding='utf-8', ) build_path = project_path / 'dist' diff --git a/tests/backend/version/source/test_code.py b/tests/backend/version/source/test_code.py index 1e1cf639e..ae7e33006 100644 --- a/tests/backend/version/source/test_code.py +++ b/tests/backend/version/source/test_code.py @@ -62,7 +62,7 @@ def test_match_default_expression(temp_dir): file_path = temp_dir / 'a' / 'b.py' file_path.ensure_parent_dir_exists() - file_path.write_text('__version__ = "0.0.1"') + file_path.write_text('__version__ = "0.0.1"', encoding='utf-8') with temp_dir.as_cwd(): assert source.get_version_data()['version'] == '0.0.1' @@ -73,7 +73,7 @@ def test_match_custom_expression_basic(temp_dir): file_path = temp_dir / 'a' / 'b.py' file_path.ensure_parent_dir_exists() - file_path.write_text('VER = "0.0.1"') + file_path.write_text('VER = "0.0.1"', encoding='utf-8') with temp_dir.as_cwd(): assert source.get_version_data()['version'] == '0.0.1' @@ -92,7 +92,8 @@ def test_match_custom_expression_complex(temp_dir, helpers): def foo(): return '.'.join(str(part) for part in __version_info__) """ - ) + ), + encoding='utf-8', ) with temp_dir.as_cwd(): @@ -112,7 +113,8 @@ def test_search_paths(temp_dir, helpers): __version__ = foo((1, 0, 0, 1, 'dev0')) """ - ) + ), + encoding='utf-8', ) (parent_dir / 'c.py').write_text( helpers.dedent( @@ -120,7 +122,8 @@ def test_search_paths(temp_dir, helpers): def foo(version_info): return '.'.join(str(part) for part in version_info) """ - ) + ), + encoding='utf-8', ) with temp_dir.as_cwd(): diff --git a/tests/backend/version/source/test_regex.py b/tests/backend/version/source/test_regex.py index 9eecf3be2..b87926273 100644 --- a/tests/backend/version/source/test_regex.py +++ b/tests/backend/version/source/test_regex.py @@ -55,7 +55,7 @@ def test_pattern_no_version_group(temp_dir): file_path = temp_dir / 'a' / 'b' file_path.ensure_parent_dir_exists() - file_path.write_text('foo') + file_path.write_text('foo', encoding='utf-8') with temp_dir.as_cwd(), pytest.raises(ValueError, match='no group named `version` was defined in the pattern'): source.get_version_data() @@ -66,7 +66,7 @@ def test_match_custom_pattern(temp_dir): file_path = temp_dir / 'a' / 'b' file_path.ensure_parent_dir_exists() - file_path.write_text('VER = "0.0.1"') + file_path.write_text('VER = "0.0.1"', encoding='utf-8') with temp_dir.as_cwd(): assert source.get_version_data()['version'] == '0.0.1' @@ -88,7 +88,8 @@ def test_match_default_pattern(temp_dir, helpers, variable, quote, prefix): def foo(): return {quote}bar{quote} """ - ) + ), + encoding='utf-8', ) with temp_dir.as_cwd(): @@ -111,7 +112,8 @@ def test_set_default_pattern(temp_dir, helpers, variable, quote, prefix): def foo(): return {quote}bar{quote} """ - ) + ), + encoding='utf-8', ) with temp_dir.as_cwd(): diff --git a/tests/cli/build/test_build.py b/tests/cli/build/test_build.py index 5f31c12fa..de994f2d9 100644 --- a/tests/cli/build/test_build.py +++ b/tests/cli/build/test_build.py @@ -109,18 +109,21 @@ def test_legacy(self, hatch, temp_dir, helpers): [build-system] requires = ["setuptools"] build-backend = "setuptools.build_meta" -""" +""", + encoding='utf-8', ) (path / 'setup.py').write_text( """\ import setuptools setuptools.setup(name="tmp", version="0.0.1") -""" +""", + encoding='utf-8', ) (path / 'tmp.py').write_text( """\ print("Hello World!") -""" +""", + encoding='utf-8', ) (path / 'README.md').touch() @@ -454,7 +457,8 @@ def initialize(self, version, build_data): if self.target_name == 'wheel': pathlib.Path('my_app', 'lib.so').touch() """ - ) + ), + encoding='utf-8', ) project = Project(path) @@ -594,7 +598,8 @@ def initialize(self, version, build_data): if self.target_name == 'wheel': pathlib.Path('my_app', 'lib.so').touch() """ - ) + ), + encoding='utf-8', ) project = Project(path) @@ -660,7 +665,8 @@ def initialize(self, version, build_data): if self.target_name == 'wheel': pathlib.Path('my_app', 'lib.so').touch() """ - ) + ), + encoding='utf-8', ) project = Project(path) @@ -726,7 +732,8 @@ def initialize(self, version, build_data): if self.target_name == 'wheel': pathlib.Path('my_app', 'lib.so').touch() """ - ) + ), + encoding='utf-8', ) project = Project(path) @@ -791,7 +798,8 @@ def initialize(self, version, build_data): if self.target_name == 'wheel': pathlib.Path('my_app', 'lib.so').touch() """ - ) + ), + encoding='utf-8', ) project = Project(path) @@ -856,7 +864,8 @@ def initialize(self, version, build_data): if self.target_name == 'wheel': pathlib.Path('my_app', 'lib.so').touch() """ - ) + ), + encoding='utf-8', ) project = Project(path) @@ -919,7 +928,8 @@ def initialize(self, version, build_data): if self.target_name == 'wheel': pathlib.Path('my_app', 'lib.so').touch() """ - ) + ), + encoding='utf-8', ) project = Project(path) @@ -979,7 +989,8 @@ def initialize(self, version, build_data): if self.target_name == 'wheel': pathlib.Path('my_app', 'lib.so').touch() """ - ) + ), + encoding='utf-8', ) project = Project(path) @@ -1039,7 +1050,8 @@ def initialize(self, version, build_data): if self.target_name == 'wheel': pathlib.Path('my_app', 'lib.so').touch() """ - ) + ), + encoding='utf-8', ) project = Project(path) @@ -1096,7 +1108,8 @@ def initialize(self, version, build_data): if self.target_name == 'wheel': pathlib.Path('my_app', 'lib.so').touch() """ - ) + ), + encoding='utf-8', ) project = Project(path) @@ -1151,7 +1164,8 @@ def initialize(self, version, build_data): if self.target_name == 'wheel': pathlib.Path('my_app', 'lib.so').touch() """ - ) + ), + encoding='utf-8', ) project = Project(path) @@ -1314,10 +1328,11 @@ def get_builder(): class CustomWheelBuilder(WheelBuilder): def build(self, **kwargs): - pathlib.Path('test.txt').write_text(str(binary.convert_units(1024))) + pathlib.Path('test.txt').write_text(str(binary.convert_units(1024)), encoding='utf-8') yield from super().build(**kwargs) """ - ) + ), + encoding='utf-8', ) project = Project(project_path) @@ -1340,7 +1355,7 @@ def build(self, **kwargs): output_file = project_path / 'test.txt' assert output_file.is_file() - assert str(output_file.read_text()) == "(1.0, 'KiB')" + assert str(output_file.read_text(encoding='utf-8')) == "(1.0, 'KiB')" assert result.output == helpers.dedent( """ @@ -1370,7 +1385,8 @@ def test_plugin_dependencies_unmet(hatch, temp_dir, helpers, mock_plugin_install [env] requires = ["{dependency}"] """ - ) + ), + encoding='utf-8', ) with path.as_cwd(): diff --git a/tests/cli/clean/test_clean.py b/tests/cli/clean/test_clean.py index e5c4d2558..f74ee6801 100644 --- a/tests/cli/clean/test_clean.py +++ b/tests/cli/clean/test_clean.py @@ -36,7 +36,8 @@ def initialize(self, version, build_data): if self.target_name == 'wheel': pathlib.Path('my_app', 'lib.so').touch() """ - ) + ), + encoding='utf-8', ) project = Project(path) @@ -63,7 +64,8 @@ def initialize(self, version, build_data): [env] requires = ["{dependency}"] """ - ) + ), + encoding='utf-8', ) with path.as_cwd(): diff --git a/tests/cli/dep/show/test_requirements.py b/tests/cli/dep/show/test_requirements.py index 1471fdd15..44a556a31 100644 --- a/tests/cli/dep/show/test_requirements.py +++ b/tests/cli/dep/show/test_requirements.py @@ -248,7 +248,8 @@ def test_plugin_dependencies_unmet(hatch, helpers, temp_dir, config_file, mock_p [env] requires = ["{dependency}"] """ - ) + ), + encoding='utf-8', ) project = Project(project_path) diff --git a/tests/cli/dep/show/test_table.py b/tests/cli/dep/show/test_table.py index 9748d1a16..2d0d35bad 100644 --- a/tests/cli/dep/show/test_table.py +++ b/tests/cli/dep/show/test_table.py @@ -237,7 +237,8 @@ def test_plugin_dependencies_unmet(hatch, helpers, temp_dir, config_file, mock_p [env] requires = ["{dependency}"] """ - ) + ), + encoding='utf-8', ) project = Project(project_path) diff --git a/tests/cli/dep/test_hash.py b/tests/cli/dep/test_hash.py index 19904a391..ffe839cc6 100644 --- a/tests/cli/dep/test_hash.py +++ b/tests/cli/dep/test_hash.py @@ -108,7 +108,8 @@ def test_plugin_dependencies_unmet(hatch, helpers, temp_dir, mock_plugin_install [env] requires = ["{dependency}"] """ - ) + ), + encoding='utf-8', ) project = Project(project_path) diff --git a/tests/cli/env/test_create.py b/tests/cli/env/test_create.py index 7106f4479..66674249a 100644 --- a/tests/cli/env/test_create.py +++ b/tests/cli/env/test_create.py @@ -1138,7 +1138,9 @@ def test_pre_install_commands(hatch, helpers, temp_dir, config_file): project, 'default', { - 'pre-install-commands': ["python -c \"with open('test.txt', 'w') as f: f.write('content')\""], + 'pre-install-commands': [ + "python -c \"with open('test.txt', 'w', encoding='utf-8') as f: f.write('content')\"" + ], **project.config.envs['default'], }, ) @@ -1216,7 +1218,9 @@ def test_post_install_commands(hatch, helpers, temp_dir, config_file): project, 'default', { - 'post-install-commands': ["python -c \"with open('test.txt', 'w') as f: f.write('content')\""], + 'post-install-commands': [ + "python -c \"with open('test.txt', 'w', encoding='utf-8') as f: f.write('content')\"" + ], **project.config.envs['default'], }, ) @@ -1294,7 +1298,9 @@ def test_sync_dependencies_uv(hatch, helpers, temp_dir, platform, uv_on_path, ex 'default', { 'dependencies': ['binary'], - 'post-install-commands': ["python -c \"with open('test.txt', 'w') as f: f.write('content')\""], + 'post-install-commands': [ + "python -c \"with open('test.txt', 'w', encoding='utf-8') as f: f.write('content')\"" + ], **project.config.envs['default'], }, ) @@ -1364,7 +1370,9 @@ def test_sync_dependencies_pip(hatch, helpers, temp_dir, platform, extract_insta 'default', { 'dependencies': ['binary'], - 'post-install-commands': ["python -c \"with open('test.txt', 'w') as f: f.write('content')\""], + 'post-install-commands': [ + "python -c \"with open('test.txt', 'w', encoding='utf-8') as f: f.write('content')\"" + ], **project.config.envs['default'], }, ) @@ -1508,7 +1516,9 @@ def test_sync_dynamic_dependencies(hatch, helpers, temp_dir, platform, uv_on_pat { 'dependencies': ['my-app1 @ {root:uri}/../my-app1'], 'features': ['foo'], - 'post-install-commands': ["python -c \"with open('test.txt', 'w') as f: f.write('content')\""], + 'post-install-commands': [ + "python -c \"with open('test.txt', 'w', encoding='utf-8') as f: f.write('content')\"" + ], **project.config.envs['default'], }, ) @@ -1525,7 +1535,8 @@ def update(self, metadata): metadata['dependencies'] = ['my-app0 @ {root:uri}/../my-app0'] metadata['optional-dependencies'] = {'foo': ['binary']} """ - ) + ), + encoding='utf-8', ) with project_path.as_cwd(env_vars={ConfigEnvVars.DATA: str(data_path)}): @@ -1612,7 +1623,9 @@ def test_unknown_dynamic_feature(hatch, helpers, temp_dir, config_file): 'default', { 'features': ['foo'], - 'post-install-commands': ["python -c \"with open('test.txt', 'w') as f: f.write('content')\""], + 'post-install-commands': [ + "python -c \"with open('test.txt', 'w', encoding='utf-8') as f: f.write('content')\"" + ], **project.config.envs['default'], }, ) @@ -1628,7 +1641,8 @@ class CustomHook(MetadataHookInterface): def update(self, metadata): metadata['optional-dependencies'] = {'bar': ['binary']} """ - ) + ), + encoding='utf-8', ) with project_path.as_cwd(env_vars={ConfigEnvVars.DATA: str(data_path)}), pytest.raises( @@ -1711,7 +1725,8 @@ def test_plugin_dependencies_unmet(hatch, config_file, helpers, temp_dir, mock_p [env] requires = ["{dependency}"] """ - ) + ), + encoding='utf-8', ) project = Project(project_path) @@ -1773,7 +1788,8 @@ def test_plugin_dependencies_met(hatch, config_file, helpers, temp_dir): [env] requires = ["{dependency}"] """ - ) + ), + encoding='utf-8', ) project = Project(project_path) @@ -1833,7 +1849,8 @@ def test_plugin_dependencies_met_as_app(hatch, config_file, helpers, temp_dir): [env] requires = ["{dependency}"] """ - ) + ), + encoding='utf-8', ) project = Project(project_path) diff --git a/tests/cli/env/test_find.py b/tests/cli/env/test_find.py index 250f56dc1..7ecf9a7cb 100644 --- a/tests/cli/env/test_find.py +++ b/tests/cli/env/test_find.py @@ -187,7 +187,8 @@ def test_plugin_dependencies_unmet(hatch, helpers, temp_dir_data, config_file, m [env] requires = ["{dependency}"] """ - ) + ), + encoding='utf-8', ) with project_path.as_cwd(): diff --git a/tests/cli/env/test_prune.py b/tests/cli/env/test_prune.py index 6270bc563..2f406ef9b 100644 --- a/tests/cli/env/test_prune.py +++ b/tests/cli/env/test_prune.py @@ -160,7 +160,8 @@ def test_plugin_dependencies_unmet(hatch, helpers, temp_dir_data, config_file, m [env] requires = ["{dependency}"] """ - ) + ), + encoding='utf-8', ) project = Project(project_path) diff --git a/tests/cli/env/test_remove.py b/tests/cli/env/test_remove.py index 8b3ab2308..12c4a459e 100644 --- a/tests/cli/env/test_remove.py +++ b/tests/cli/env/test_remove.py @@ -431,7 +431,8 @@ def test_plugin_dependencies_unmet(hatch, helpers, temp_dir_data, config_file, m [env] requires = ["{dependency}"] """ - ) + ), + encoding='utf-8', ) with project_path.as_cwd(): diff --git a/tests/cli/env/test_run.py b/tests/cli/env/test_run.py index 4a9721e58..494256f93 100644 --- a/tests/cli/env/test_run.py +++ b/tests/cli/env/test_run.py @@ -29,7 +29,7 @@ def test_filter_not_mapping(hatch, helpers, temp_dir, config_file): 'scripts': { 'error': [ 'python -c "import sys;sys.exit(3)"', - 'python -c "import pathlib,sys;pathlib.Path(\'test.txt\').write_text(sys.executable)"', + "python -c \"import pathlib,sys;pathlib.Path('test.txt').write_text(sys.executable, encoding='utf-8')\"", ], }, **project.config.envs['default'], @@ -84,7 +84,7 @@ def test_filter(hatch, helpers, temp_dir, config_file): '--', 'python', '-c', - "import os,sys;open('test.txt', 'a').write(sys.executable+os.linesep[-1])", + "import os,sys;open('test.txt', 'a', encoding='utf-8').write(sys.executable+os.linesep[-1])", ) assert result.exit_code == 0, result.output @@ -116,7 +116,7 @@ def test_filter(hatch, helpers, temp_dir, config_file): env_path = env_dirs[0] assert env_path.name == 'test.42' - python_path = str(output_file.read_text()).strip() + python_path = str(output_file.read_text(encoding='utf-8')).strip() assert str(env_path) in python_path @@ -146,7 +146,7 @@ def test_force_continue(hatch, helpers, temp_dir, config_file): 'python -c "import sys;sys.exit(2)"', '- python -c "import sys;sys.exit(3)"', 'python -c "import sys;sys.exit(1)"', - 'python -c "import pathlib,sys;pathlib.Path(\'test.txt\').write_text(sys.executable)"', + "python -c \"import pathlib,sys;pathlib.Path('test.txt').write_text(sys.executable, encoding='utf-8')\"", ], }, **project.config.envs['default'], @@ -164,7 +164,7 @@ def test_force_continue(hatch, helpers, temp_dir, config_file): cmd [1] | python -c "import sys;sys.exit(2)" cmd [2] | - python -c "import sys;sys.exit(3)" cmd [3] | python -c "import sys;sys.exit(1)" - cmd [4] | python -c "import pathlib,sys;pathlib.Path('test.txt').write_text(sys.executable)" + cmd [4] | python -c "import pathlib,sys;pathlib.Path('test.txt').write_text(sys.executable, encoding='utf-8')" """ ) output_file = project_path / 'test.txt' @@ -189,7 +189,7 @@ def test_force_continue(hatch, helpers, temp_dir, config_file): assert env_path.name == project_path.name - assert str(env_path) in str(output_file.read_text()) + assert str(env_path) in str(output_file.read_text(encoding='utf-8')) def test_ignore_compatibility(hatch, helpers, temp_dir, config_file): @@ -223,7 +223,7 @@ def test_ignore_compatibility(hatch, helpers, temp_dir, config_file): '--', 'python', '-c', - "import os,sys;open('test.txt', 'a').write(sys.executable+os.linesep[-1])", + "import os,sys;open('test.txt', 'a', encoding='utf-8').write(sys.executable+os.linesep[-1])", ) assert result.exit_code == 0 @@ -262,7 +262,8 @@ def test_plugin_dependencies_unmet(hatch, helpers, temp_dir, config_file, mock_p [env] requires = ["{dependency}"] """ - ) + ), + encoding='utf-8', ) project = Project(project_path) @@ -270,7 +271,12 @@ def test_plugin_dependencies_unmet(hatch, helpers, temp_dir, config_file, mock_p with project_path.as_cwd(env_vars={ConfigEnvVars.DATA: str(data_path)}): result = hatch( - 'env', 'run', '--', 'python', '-c', "import pathlib,sys;pathlib.Path('test.txt').write_text(sys.executable)" + 'env', + 'run', + '--', + 'python', + '-c', + "import pathlib,sys;pathlib.Path('test.txt').write_text(sys.executable, encoding='utf-8')", ) assert result.exit_code == 0, result.output @@ -305,4 +311,4 @@ def test_plugin_dependencies_unmet(hatch, helpers, temp_dir, config_file, mock_p assert env_path.name == project_path.name - assert str(env_path) in str(output_file.read_text()) + assert str(env_path) in str(output_file.read_text(encoding='utf-8')) diff --git a/tests/cli/env/test_show.py b/tests/cli/env/test_show.py index e7c3e0956..1045339e0 100644 --- a/tests/cli/env/test_show.py +++ b/tests/cli/env/test_show.py @@ -485,7 +485,8 @@ def test_plugin_dependencies_unmet(hatch, helpers, temp_dir, config_file, mock_p [env] requires = ["{dependency}"] """ - ) + ), + encoding='utf-8', ) with project_path.as_cwd(): diff --git a/tests/cli/fmt/test_fmt.py b/tests/cli/fmt/test_fmt.py index b3450507d..18afb3b3f 100644 --- a/tests/cli/fmt/test_fmt.py +++ b/tests/cli/fmt/test_fmt.py @@ -102,12 +102,12 @@ def test_fix(self, hatch, helpers, temp_dir, config_file, env_run, mocker, platf mocker.call(f'ruff format --config {user_config_path} .', shell=True), ] - assert default_config.read_text() == defaults_file_stable + assert default_config.read_text(encoding='utf-8') == defaults_file_stable - old_contents = (project_path / 'pyproject.toml').read_text() + old_contents = (project_path / 'pyproject.toml').read_text(encoding='utf-8') config_path = str(default_config).replace('\\', '\\\\') assert ( - user_config.read_text() + user_config.read_text(encoding='utf-8') == f"""\ {old_contents} [tool.ruff] @@ -150,12 +150,12 @@ def test_check(self, hatch, helpers, temp_dir, config_file, env_run, mocker, pla mocker.call(f'ruff format --config {user_config_path} --check --diff .', shell=True), ] - assert default_config.read_text() == defaults_file_stable + assert default_config.read_text(encoding='utf-8') == defaults_file_stable - old_contents = (project_path / 'pyproject.toml').read_text() + old_contents = (project_path / 'pyproject.toml').read_text(encoding='utf-8') config_path = str(default_config).replace('\\', '\\\\') assert ( - user_config.read_text() + user_config.read_text(encoding='utf-8') == f"""\ {old_contents} [tool.ruff] @@ -185,8 +185,8 @@ def test_existing_config( user_config_path = platform.join_command_args([str(user_config)]) project_file = project_path / 'pyproject.toml' - old_contents = project_file.read_text() - project_file.write_text(f'[tool.ruff]\n{old_contents}') + old_contents = project_file.read_text(encoding='utf-8') + project_file.write_text(f'[tool.ruff]\n{old_contents}', encoding='utf-8') with project_path.as_cwd(env_vars={ConfigEnvVars.DATA: str(data_path)}): result = hatch('fmt', '--check') @@ -204,11 +204,11 @@ def test_existing_config( mocker.call(f'ruff format --config {user_config_path} --check --diff .', shell=True), ] - assert default_config.read_text() == defaults_file_stable + assert default_config.read_text(encoding='utf-8') == defaults_file_stable config_path = str(default_config).replace('\\', '\\\\') assert ( - user_config.read_text() + user_config.read_text(encoding='utf-8') == f"""\ [tool.ruff] extend = "{config_path}\" @@ -253,12 +253,12 @@ def test_fix_flag(self, hatch, helpers, temp_dir, config_file, env_run, mocker, mocker.call(f'ruff format --config {user_config_path} --preview .', shell=True), ] - assert default_config.read_text() == defaults_file_preview + assert default_config.read_text(encoding='utf-8') == defaults_file_preview - old_contents = (project_path / 'pyproject.toml').read_text() + old_contents = (project_path / 'pyproject.toml').read_text(encoding='utf-8') config_path = str(default_config).replace('\\', '\\\\') assert ( - user_config.read_text() + user_config.read_text(encoding='utf-8') == f"""\ {old_contents} [tool.ruff] @@ -301,12 +301,12 @@ def test_check_flag(self, hatch, helpers, temp_dir, config_file, env_run, mocker mocker.call(f'ruff format --config {user_config_path} --preview --check --diff .', shell=True), ] - assert default_config.read_text() == defaults_file_preview + assert default_config.read_text(encoding='utf-8') == defaults_file_preview - old_contents = (project_path / 'pyproject.toml').read_text() + old_contents = (project_path / 'pyproject.toml').read_text(encoding='utf-8') config_path = str(default_config).replace('\\', '\\\\') assert ( - user_config.read_text() + user_config.read_text(encoding='utf-8') == f"""\ {old_contents} [tool.ruff] @@ -346,12 +346,12 @@ def test_only_linter(self, hatch, temp_dir, config_file, env_run, mocker, platfo mocker.call(f'ruff check --config {user_config_path} --fix .', shell=True), ] - assert default_config.read_text() == defaults_file_stable + assert default_config.read_text(encoding='utf-8') == defaults_file_stable - old_contents = (project_path / 'pyproject.toml').read_text() + old_contents = (project_path / 'pyproject.toml').read_text(encoding='utf-8') config_path = str(default_config).replace('\\', '\\\\') assert ( - user_config.read_text() + user_config.read_text(encoding='utf-8') == f"""\ {old_contents} [tool.ruff] @@ -389,12 +389,12 @@ def test_only_formatter(self, hatch, temp_dir, config_file, env_run, mocker, pla mocker.call(f'ruff format --config {user_config_path} .', shell=True), ] - assert default_config.read_text() == defaults_file_stable + assert default_config.read_text(encoding='utf-8') == defaults_file_stable - old_contents = (project_path / 'pyproject.toml').read_text() + old_contents = (project_path / 'pyproject.toml').read_text(encoding='utf-8') config_path = str(default_config).replace('\\', '\\\\') assert ( - user_config.read_text() + user_config.read_text(encoding='utf-8') == f"""\ {old_contents} [tool.ruff] @@ -465,12 +465,12 @@ def test_forwarding(self, hatch, helpers, temp_dir, config_file, env_run, mocker mocker.call(f'ruff format --config {user_config_path} --foo bar', shell=True), ] - assert default_config.read_text() == defaults_file_stable + assert default_config.read_text(encoding='utf-8') == defaults_file_stable - old_contents = (project_path / 'pyproject.toml').read_text() + old_contents = (project_path / 'pyproject.toml').read_text(encoding='utf-8') config_path = str(default_config).replace('\\', '\\\\') assert ( - user_config.read_text() + user_config.read_text(encoding='utf-8') == f"""\ {old_contents} [tool.ruff] @@ -547,7 +547,7 @@ def test_sync(self, hatch, helpers, temp_dir, config_file, env_run, mocker, defa mocker.call('ruff format .', shell=True), ] - assert default_config_file.read_text() == defaults_file_stable + assert default_config_file.read_text(encoding='utf-8') == defaults_file_stable def test_no_sync(self, hatch, helpers, temp_dir, config_file, env_run, mocker): config_file.model.template.plugins['default']['tests'] = False @@ -591,7 +591,7 @@ def test_no_sync(self, hatch, helpers, temp_dir, config_file, env_run, mocker): mocker.call('ruff format .', shell=True), ] - assert not default_config_file.read_text() + assert not default_config_file.read_text(encoding='utf-8') def test_sync_legacy_config(self, hatch, helpers, temp_dir, config_file, env_run, mocker, defaults_file_stable): config_file.model.template.plugins['default']['tests'] = False @@ -636,7 +636,7 @@ def test_sync_legacy_config(self, hatch, helpers, temp_dir, config_file, env_run mocker.call('ruff format .', shell=True), ] - assert default_config_file.read_text() == defaults_file_stable + assert default_config_file.read_text(encoding='utf-8') == defaults_file_stable class TestCustomScripts: diff --git a/tests/cli/new/test_new.py b/tests/cli/new/test_new.py index 0791fcfe9..29a8d10c9 100644 --- a/tests/cli/new/test_new.py +++ b/tests/cli/new/test_new.py @@ -542,7 +542,8 @@ def test_initialize_update(hatch, helpers, temp_dir): [tool.hatch.version] path = "o/__init__.py" -""" +""", + encoding='utf-8', ) with temp_dir.as_cwd(): @@ -558,7 +559,7 @@ def test_initialize_update(hatch, helpers, temp_dir): """ ) assert len(list(temp_dir.iterdir())) == 1 - assert project_file.read_text() == ( + assert project_file.read_text(encoding='utf-8') == ( f"""\ [build-system] requires = ["hatchling"] @@ -593,7 +594,8 @@ def test_initialize_setup_cfg_only(hatch, helpers, temp_dir): author_email = void@some.where url = https://example.com license = MIT -""" +""", + encoding='utf-8', ) with temp_dir.as_cwd(): @@ -607,7 +609,7 @@ def test_initialize_setup_cfg_only(hatch, helpers, temp_dir): ) project_file = temp_dir / 'pyproject.toml' - assert project_file.read_text() == ( + assert project_file.read_text(encoding='utf-8') == ( """\ [build-system] requires = ["hatchling"] diff --git a/tests/cli/project/test_metadata.py b/tests/cli/project/test_metadata.py index f93dd8af5..9b4e9e94e 100644 --- a/tests/cli/project/test_metadata.py +++ b/tests/cli/project/test_metadata.py @@ -10,7 +10,7 @@ def read_readme(project_dir): - return repr((project_dir / 'README.txt').read_text())[1:-1] + return repr((project_dir / 'README.txt').read_text(encoding='utf-8'))[1:-1] def test_other_backend(hatch, temp_dir, helpers): @@ -176,7 +176,7 @@ def test_field_readme(hatch, temp_dir): Checking dependencies Syncing dependencies Inspecting build dependencies -{(path / 'README.txt').read_text()} +{(path / 'README.txt').read_text(encoding='utf-8')} """ ) @@ -281,7 +281,8 @@ def test_plugin_dependencies_unmet(hatch, temp_dir, helpers, mock_plugin_install [env] requires = ["{dependency}"] """ - ) + ), + encoding='utf-8', ) (path / 'README.md').replace(path / 'README.txt') diff --git a/tests/cli/publish/test_publish.py b/tests/cli/publish/test_publish.py index c23c7195f..a0f9178e4 100644 --- a/tests/cli/publish/test_publish.py +++ b/tests/cli/publish/test_publish.py @@ -692,7 +692,9 @@ def test_missing_required_metadata_field(self, hatch, temp_dir_cache, helpers, p tar_archive.extractall(extraction_directory, **helpers.tarfile_extraction_compat_options()) metadata_file_path = extraction_directory / f'{published_project_name}-{current_version}' / 'PKG-INFO' - metadata_file_path.write_text(remove_metadata_field(field, metadata_file_path.read_text())) + metadata_file_path.write_text( + remove_metadata_field(field, metadata_file_path.read_text(encoding='utf-8')), encoding='utf-8' + ) with tarfile.open(artifact_path, 'w:gz') as tar_archive: tar_archive.add(extraction_directory, arcname='') diff --git a/tests/cli/python/test_install.py b/tests/cli/python/test_install.py index f8a462771..4cf19a75b 100644 --- a/tests/cli/python/test_install.py +++ b/tests/cli/python/test_install.py @@ -69,7 +69,7 @@ def test_installation( install_dir = temp_dir_data / 'data' / 'pythons' / dist_name metadata_file = install_dir / InstalledDistribution.metadata_filename() - python_path = install_dir / json.loads(metadata_file.read_text())['python_path'] + python_path = install_dir / json.loads(metadata_file.read_text(encoding='utf-8'))['python_path'] assert result.exit_code == 0, result.output assert result.output == helpers.dedent( diff --git a/tests/cli/run/test_run.py b/tests/cli/run/test_run.py index 072ba9833..548b8bde4 100644 --- a/tests/cli/run/test_run.py +++ b/tests/cli/run/test_run.py @@ -52,7 +52,12 @@ def test_automatic_creation(hatch, helpers, temp_dir, config_file): helpers.update_project_environment(project, 'default', {'skip-install': True, **project.config.envs['default']}) with project_path.as_cwd(env_vars={ConfigEnvVars.DATA: str(data_path)}): - result = hatch('run', 'python', '-c', "import pathlib,sys;pathlib.Path('test.txt').write_text(sys.executable)") + result = hatch( + 'run', + 'python', + '-c', + "import pathlib,sys;pathlib.Path('test.txt').write_text(sys.executable, encoding='utf-8')", + ) assert result.exit_code == 0, result.output assert result.output == helpers.dedent( @@ -83,7 +88,7 @@ def test_automatic_creation(hatch, helpers, temp_dir, config_file): assert env_path.name == project_path.name - assert str(env_path) in str(output_file.read_text()) + assert str(env_path) in str(output_file.read_text(encoding='utf-8')) def test_no_compatibility_check_if_exists(hatch, helpers, temp_dir, config_file, mocker): @@ -105,7 +110,12 @@ def test_no_compatibility_check_if_exists(hatch, helpers, temp_dir, config_file, helpers.update_project_environment(project, 'default', {'skip-install': True, **project.config.envs['default']}) with project_path.as_cwd(env_vars={ConfigEnvVars.DATA: str(data_path)}): - result = hatch('run', 'python', '-c', "import pathlib,sys;pathlib.Path('test.txt').write_text(sys.executable)") + result = hatch( + 'run', + 'python', + '-c', + "import pathlib,sys;pathlib.Path('test.txt').write_text(sys.executable, encoding='utf-8')", + ) assert result.exit_code == 0, result.output assert result.output == helpers.dedent( @@ -136,16 +146,21 @@ def test_no_compatibility_check_if_exists(hatch, helpers, temp_dir, config_file, assert env_path.name == project_path.name - assert str(env_path) in str(output_file.read_text()) + assert str(env_path) in str(output_file.read_text(encoding='utf-8')) output_file.unlink() mocker.patch('hatch.env.virtual.VirtualEnvironment.check_compatibility', side_effect=Exception('incompatible')) with project_path.as_cwd(env_vars={ConfigEnvVars.DATA: str(data_path)}): - result = hatch('run', 'python', '-c', "import pathlib,sys;pathlib.Path('test.txt').write_text(sys.executable)") + result = hatch( + 'run', + 'python', + '-c', + "import pathlib,sys;pathlib.Path('test.txt').write_text(sys.executable, encoding='utf-8')", + ) assert result.exit_code == 0, result.output assert not result.output - assert str(env_path) in str(output_file.read_text()) + assert str(env_path) in str(output_file.read_text(encoding='utf-8')) def test_enter_project_directory(hatch, config_file, helpers, temp_dir): @@ -173,7 +188,12 @@ def test_enter_project_directory(hatch, config_file, helpers, temp_dir): helpers.update_project_environment(project, 'default', {'skip-install': True, **project.config.envs['default']}) with EnvVars({ConfigEnvVars.DATA: str(data_path)}): - result = hatch('run', 'python', '-c', "import pathlib,sys;pathlib.Path('test.txt').write_text(sys.executable)") + result = hatch( + 'run', + 'python', + '-c', + "import pathlib,sys;pathlib.Path('test.txt').write_text(sys.executable, encoding='utf-8')", + ) assert result.exit_code == 0, result.output assert result.output == helpers.dedent( @@ -204,7 +224,7 @@ def test_enter_project_directory(hatch, config_file, helpers, temp_dir): assert env_path.name == project_path.name - assert str(env_path) in str(output_file.read_text()) + assert str(env_path) in str(output_file.read_text(encoding='utf-8')) @pytest.mark.requires_internet @@ -266,7 +286,7 @@ def test_sync_dependencies(hatch, helpers, temp_dir, config_file): 'run', 'python', '-c', - "import binary,pathlib,sys;pathlib.Path('test.txt').write_text(str(binary.convert_units(1024)))", + "import binary,pathlib,sys;pathlib.Path('test.txt').write_text(str(binary.convert_units(1024)), encoding='utf-8')", ) assert result.exit_code == 0, result.output @@ -279,7 +299,7 @@ def test_sync_dependencies(hatch, helpers, temp_dir, config_file): output_file = project_path / 'test.txt' assert output_file.is_file() - assert str(output_file.read_text()) == "(1.0, 'KiB')" + assert str(output_file.read_text(encoding='utf-8')) == "(1.0, 'KiB')" @pytest.mark.requires_internet @@ -339,7 +359,7 @@ def test_sync_project_dependencies(hatch, helpers, temp_dir, config_file): 'run', 'python', '-c', - "import binary,pathlib,sys;pathlib.Path('test.txt').write_text(str(binary.convert_units(1024)))", + "import binary,pathlib,sys;pathlib.Path('test.txt').write_text(str(binary.convert_units(1024)), encoding='utf-8')", ) assert result.exit_code == 0, result.output @@ -352,7 +372,7 @@ def test_sync_project_dependencies(hatch, helpers, temp_dir, config_file): output_file = project_path / 'test.txt' assert output_file.is_file() - assert str(output_file.read_text()) == "(1.0, 'KiB')" + assert str(output_file.read_text(encoding='utf-8')) == "(1.0, 'KiB')" @pytest.mark.requires_internet @@ -418,7 +438,7 @@ def test_sync_project_features(hatch, helpers, temp_dir, config_file): 'run', 'python', '-c', - "import binary,pathlib,sys;pathlib.Path('test.txt').write_text(str(binary.convert_units(1024)))", + "import binary,pathlib,sys;pathlib.Path('test.txt').write_text(str(binary.convert_units(1024)), encoding='utf-8')", ) assert result.exit_code == 0, result.output @@ -431,7 +451,7 @@ def test_sync_project_features(hatch, helpers, temp_dir, config_file): output_file = project_path / 'test.txt' assert output_file.is_file() - assert str(output_file.read_text()) == "(1.0, 'KiB')" + assert str(output_file.read_text(encoding='utf-8')) == "(1.0, 'KiB')" @pytest.mark.requires_internet @@ -493,7 +513,7 @@ def test_dependency_hash_checking(hatch, helpers, temp_dir, config_file, mocker) 'run', 'python', '-c', - "import binary,pathlib,sys;pathlib.Path('test.txt').write_text(str(binary.convert_units(1024)))", + "import binary,pathlib,sys;pathlib.Path('test.txt').write_text(str(binary.convert_units(1024)), encoding='utf-8')", ) assert result.exit_code == 0, result.output @@ -506,7 +526,7 @@ def test_dependency_hash_checking(hatch, helpers, temp_dir, config_file, mocker) output_file = project_path / 'test.txt' assert output_file.is_file() - assert str(output_file.read_text()) == "(1.0, 'KiB')" + assert str(output_file.read_text(encoding='utf-8')) == "(1.0, 'KiB')" output_file.unlink() # Now there should be no output because there is no dependency checking @@ -515,7 +535,7 @@ def test_dependency_hash_checking(hatch, helpers, temp_dir, config_file, mocker) 'run', 'python', '-c', - "import binary,pathlib,sys;pathlib.Path('test.txt').write_text(str(binary.convert_units(1024)))", + "import binary,pathlib,sys;pathlib.Path('test.txt').write_text(str(binary.convert_units(1024)), encoding='utf-8')", ) assert result.exit_code == 0, result.output @@ -524,7 +544,7 @@ def test_dependency_hash_checking(hatch, helpers, temp_dir, config_file, mocker) output_file = project_path / 'test.txt' assert output_file.is_file() - assert str(output_file.read_text()) == "(1.0, 'KiB')" + assert str(output_file.read_text(encoding='utf-8')) == "(1.0, 'KiB')" output_file.unlink() mocker.patch('hatch.env.virtual.VirtualEnvironment.dependencies_in_sync', return_value=False) @@ -536,7 +556,7 @@ def test_dependency_hash_checking(hatch, helpers, temp_dir, config_file, mocker) 'run', 'python', '-c', - "import binary,pathlib,sys;pathlib.Path('test.txt').write_text(str(binary.convert_units(1024)))", + "import binary,pathlib,sys;pathlib.Path('test.txt').write_text(str(binary.convert_units(1024)), encoding='utf-8')", ) assert result.exit_code == 0, result.output @@ -550,7 +570,7 @@ def test_dependency_hash_checking(hatch, helpers, temp_dir, config_file, mocker) output_file = project_path / 'test.txt' assert output_file.is_file() - assert str(output_file.read_text()) == "(1.0, 'KiB')" + assert str(output_file.read_text(encoding='utf-8')) == "(1.0, 'KiB')" output_file.unlink() with project_path.as_cwd(env_vars={ConfigEnvVars.DATA: str(data_path)}): @@ -558,7 +578,7 @@ def test_dependency_hash_checking(hatch, helpers, temp_dir, config_file, mocker) 'run', 'python', '-c', - "import binary,pathlib,sys;pathlib.Path('test.txt').write_text(str(binary.convert_units(1024)))", + "import binary,pathlib,sys;pathlib.Path('test.txt').write_text(str(binary.convert_units(1024)), encoding='utf-8')", ) assert result.exit_code == 0, result.output @@ -567,7 +587,7 @@ def test_dependency_hash_checking(hatch, helpers, temp_dir, config_file, mocker) output_file = project_path / 'test.txt' assert output_file.is_file() - assert str(output_file.read_text()) == "(1.0, 'KiB')" + assert str(output_file.read_text(encoding='utf-8')) == "(1.0, 'KiB')" def test_scripts(hatch, helpers, temp_dir, config_file): @@ -593,7 +613,9 @@ def test_scripts(hatch, helpers, temp_dir, config_file): ) with project_path.as_cwd(env_vars={ConfigEnvVars.DATA: str(data_path)}): - result = hatch('run', 'py', "import pathlib,sys;pathlib.Path('test.txt').write_text(sys.executable)") + result = hatch( + 'run', 'py', "import pathlib,sys;pathlib.Path('test.txt').write_text(sys.executable, encoding='utf-8')" + ) assert result.exit_code == 0, result.output assert result.output == helpers.dedent( @@ -624,7 +646,7 @@ def test_scripts(hatch, helpers, temp_dir, config_file): assert env_path.name == project_path.name - assert str(env_path) in str(output_file.read_text()) + assert str(env_path) in str(output_file.read_text(encoding='utf-8')) def test_scripts_specific_environment(hatch, helpers, temp_dir, config_file): @@ -655,7 +677,7 @@ def test_scripts_specific_environment(hatch, helpers, temp_dir, config_file): 'run', 'test:py', "import os,pathlib,sys;pathlib.Path('test.txt').write_text(" - "sys.executable+os.linesep[-1]+os.environ['foo'])", + "sys.executable+os.linesep[-1]+os.environ['foo'], encoding='utf-8')", ) assert result.exit_code == 0, result.output @@ -687,7 +709,7 @@ def test_scripts_specific_environment(hatch, helpers, temp_dir, config_file): assert env_path.name == 'test' - python_executable_path, env_var_value = str(output_file.read_text()).splitlines() + python_executable_path, env_var_value = str(output_file.read_text(encoding='utf-8')).splitlines() assert str(env_path) in python_executable_path assert env_var_value == 'bar' @@ -714,7 +736,9 @@ def test_scripts_no_environment(hatch, helpers, temp_dir, config_file): project.save_config(config) with project_path.as_cwd(env_vars={ConfigEnvVars.DATA: str(data_path)}): - result = hatch('run', ':py', "import pathlib,sys;pathlib.Path('test.txt').write_text(sys.executable)") + result = hatch( + 'run', ':py', "import pathlib,sys;pathlib.Path('test.txt').write_text(sys.executable, encoding='utf-8')" + ) assert result.exit_code == 0, result.output assert result.output == helpers.dedent( @@ -729,7 +753,10 @@ def test_scripts_no_environment(hatch, helpers, temp_dir, config_file): env_data_path = data_path / 'env' / 'virtual' assert not env_data_path.exists() - assert os.path.realpath(output_file.read_text().strip()).lower() == os.path.realpath(sys.executable).lower() + assert ( + os.path.realpath(output_file.read_text(encoding='utf-8').strip()).lower() + == os.path.realpath(sys.executable).lower() + ) def test_error(hatch, helpers, temp_dir, config_file): @@ -756,7 +783,7 @@ def test_error(hatch, helpers, temp_dir, config_file): 'scripts': { 'error': [ 'python -c "import sys;sys.exit(3)"', - 'python -c "import pathlib,sys;pathlib.Path(\'test.txt\').write_text(sys.executable)"', + "python -c \"import pathlib,sys;pathlib.Path('test.txt').write_text(sys.executable, encoding='utf-8')\"", ], }, **project.config.envs['default'], @@ -802,7 +829,7 @@ def test_ignore_error(hatch, helpers, temp_dir, config_file): 'scripts': { 'error': [ '- python -c "import sys;sys.exit(3)"', - 'python -c "import pathlib,sys;pathlib.Path(\'test.txt\').write_text(sys.executable)"', + "python -c \"import pathlib,sys;pathlib.Path('test.txt').write_text(sys.executable, encoding='utf-8')\"", ], }, **project.config.envs['default'], @@ -818,7 +845,7 @@ def test_ignore_error(hatch, helpers, temp_dir, config_file): Creating environment: default Checking dependencies cmd [1] | - python -c "import sys;sys.exit(3)" - cmd [2] | python -c "import pathlib,sys;pathlib.Path('test.txt').write_text(sys.executable)" + cmd [2] | python -c "import pathlib,sys;pathlib.Path('test.txt').write_text(sys.executable, encoding='utf-8')" """ ) output_file = project_path / 'test.txt' @@ -843,7 +870,7 @@ def test_ignore_error(hatch, helpers, temp_dir, config_file): assert env_path.name == project_path.name - assert str(env_path) in str(output_file.read_text()) + assert str(env_path) in str(output_file.read_text(encoding='utf-8')) def test_command_expansion_error(hatch, helpers, temp_dir, config_file): @@ -906,9 +933,9 @@ def test_verbosity(hatch, helpers, temp_dir, config_file): 'skip-install': True, 'scripts': { 'write-exe': [ - 'python -c "import pathlib,sys;pathlib.Path(\'{args}1.txt\').write_text(sys.executable)"', - 'python -c "import pathlib,sys;pathlib.Path(\'{args}2.txt\').write_text(sys.executable)"', - 'python -c "import pathlib,sys;pathlib.Path(\'{args}3.txt\').write_text(sys.executable)"', + "python -c \"import pathlib,sys;pathlib.Path('{args}1.txt').write_text(sys.executable, encoding='utf-8')\"", + "python -c \"import pathlib,sys;pathlib.Path('{args}2.txt').write_text(sys.executable, encoding='utf-8')\"", + "python -c \"import pathlib,sys;pathlib.Path('{args}3.txt').write_text(sys.executable, encoding='utf-8')\"", ], }, **project.config.envs['default'], @@ -924,9 +951,9 @@ def test_verbosity(hatch, helpers, temp_dir, config_file): ─────────────────────────────────── default ──────────────────────────────────── Creating environment: default Checking dependencies - cmd [1] | python -c "import pathlib,sys;pathlib.Path('test1.txt').write_text(sys.executable)" - cmd [2] | python -c "import pathlib,sys;pathlib.Path('test2.txt').write_text(sys.executable)" - cmd [3] | python -c "import pathlib,sys;pathlib.Path('test3.txt').write_text(sys.executable)" + cmd [1] | python -c "import pathlib,sys;pathlib.Path('test1.txt').write_text(sys.executable, encoding='utf-8')" + cmd [2] | python -c "import pathlib,sys;pathlib.Path('test2.txt').write_text(sys.executable, encoding='utf-8')" + cmd [3] | python -c "import pathlib,sys;pathlib.Path('test3.txt').write_text(sys.executable, encoding='utf-8')" """ ) output_files = [] @@ -955,7 +982,7 @@ def test_verbosity(hatch, helpers, temp_dir, config_file): assert env_path.name == project_path.name for output_file in output_files: - assert str(env_path) in str(output_file.read_text()) + assert str(env_path) in str(output_file.read_text(encoding='utf-8')) def test_matrix_no_environments(hatch, helpers, temp_dir, config_file): @@ -979,7 +1006,10 @@ def test_matrix_no_environments(hatch, helpers, temp_dir, config_file): with project_path.as_cwd(env_vars={ConfigEnvVars.DATA: str(data_path)}): result = hatch( - 'run', 'test:python', '-c', "import os,sys;open('test.txt', 'a').write(sys.executable+os.linesep[-1])" + 'run', + 'test:python', + '-c', + "import os,sys;open('test.txt', 'a', encoding='utf-8').write(sys.executable+os.linesep[-1])", ) assert result.exit_code == 1, result.output @@ -1011,7 +1041,10 @@ def test_matrix(hatch, helpers, temp_dir, config_file): with project_path.as_cwd(env_vars={ConfigEnvVars.DATA: str(data_path)}): result = hatch( - 'run', 'test:python', '-c', "import os,sys;open('test.txt', 'a').write(sys.executable+os.linesep[-1])" + 'run', + 'test:python', + '-c', + "import os,sys;open('test.txt', 'a', encoding='utf-8').write(sys.executable+os.linesep[-1])", ) assert result.exit_code == 0, result.output @@ -1043,7 +1076,7 @@ def test_matrix(hatch, helpers, temp_dir, config_file): env_dirs = sorted(storage_path.iterdir(), key=lambda d: d.name)[::-1] assert len(env_dirs) == 2 - python_path1, python_path2 = str(output_file.read_text()).splitlines() + python_path1, python_path2 = str(output_file.read_text(encoding='utf-8')).splitlines() for python_path, env_dir, env_name in zip((python_path1, python_path2), env_dirs, ('test.9000', 'test.42')): assert env_dir.name == env_name assert str(env_dir) in python_path @@ -1072,7 +1105,10 @@ def test_incompatible_single(hatch, helpers, temp_dir, config_file): with project_path.as_cwd(env_vars={ConfigEnvVars.DATA: str(data_path)}): result = hatch( - 'run', 'test:python', '-c', "import os,sys;open('test.txt', 'a').write(sys.executable+os.linesep[-1])" + 'run', + 'test:python', + '-c', + "import os,sys;open('test.txt', 'a', encoding='utf-8').write(sys.executable+os.linesep[-1])", ) assert result.exit_code == 1 @@ -1111,7 +1147,10 @@ def test_incompatible_matrix_full(hatch, helpers, temp_dir, config_file): with project_path.as_cwd(env_vars={ConfigEnvVars.DATA: str(data_path)}): result = hatch( - 'run', 'test:python', '-c', "import os,sys;open('test.txt', 'a').write(sys.executable+os.linesep[-1])" + 'run', + 'test:python', + '-c', + "import os,sys;open('test.txt', 'a', encoding='utf-8').write(sys.executable+os.linesep[-1])", ) assert result.exit_code == 0, result.output @@ -1157,7 +1196,10 @@ def test_incompatible_matrix_partial(hatch, helpers, temp_dir, config_file): with project_path.as_cwd(env_vars={ConfigEnvVars.DATA: str(data_path)}): result = hatch( - 'run', 'test:python', '-c', "import os,sys;open('test.txt', 'a').write(sys.executable+os.linesep[-1])" + 'run', + 'test:python', + '-c', + "import os,sys;open('test.txt', 'a', encoding='utf-8').write(sys.executable+os.linesep[-1])", ) assert result.exit_code == 0, result.output @@ -1192,7 +1234,7 @@ def test_incompatible_matrix_partial(hatch, helpers, temp_dir, config_file): env_path = env_dirs[0] assert env_path.name == 'test.42' - python_path = str(output_file.read_text()).strip() + python_path = str(output_file.read_text(encoding='utf-8')).strip() assert str(env_path) in python_path @@ -1218,7 +1260,10 @@ def test_incompatible_missing_python(hatch, helpers, temp_dir, config_file): with project_path.as_cwd(env_vars={ConfigEnvVars.DATA: str(data_path)}): result = hatch( - 'run', 'test:python', '-c', "import os,sys;open('test.txt', 'a').write(sys.executable+os.linesep[-1])" + 'run', + 'test:python', + '-c', + "import os,sys;open('test.txt', 'a', encoding='utf-8').write(sys.executable+os.linesep[-1])", ) padding = '─' @@ -1257,7 +1302,7 @@ def test_incompatible_missing_python(hatch, helpers, temp_dir, config_file): env_path = env_dirs[0] assert env_path.name == f'test.py{known_version}' - python_path = str(output_file.read_text()).strip() + python_path = str(output_file.read_text(encoding='utf-8')).strip() assert str(env_path) in python_path @@ -1321,14 +1366,19 @@ def test_env_detection(hatch, helpers, temp_dir, config_file): assert env_dirs[1].name == 'my-app' with project_path.as_cwd(env_vars={ConfigEnvVars.DATA: str(data_path), AppEnvVars.ENV_ACTIVE: 'foo'}): - result = hatch('run', 'python', '-c', "import pathlib,sys;pathlib.Path('test.txt').write_text(sys.executable)") + result = hatch( + 'run', + 'python', + '-c', + "import pathlib,sys;pathlib.Path('test.txt').write_text(sys.executable, encoding='utf-8')", + ) assert result.exit_code == 0, result.output output_file = project_path / 'test.txt' assert output_file.is_file() - python_path = str(output_file.read_text()).strip() + python_path = str(output_file.read_text(encoding='utf-8')).strip() assert str(env_dirs[0]) in python_path @@ -1393,7 +1443,10 @@ def test_env_detection_override(hatch, helpers, temp_dir, config_file): with project_path.as_cwd(env_vars={ConfigEnvVars.DATA: str(data_path), AppEnvVars.ENV_ACTIVE: 'foo'}): result = hatch( - 'run', 'default:python', '-c', "import pathlib,sys;pathlib.Path('test.txt').write_text(sys.executable)" + 'run', + 'default:python', + '-c', + "import pathlib,sys;pathlib.Path('test.txt').write_text(sys.executable, encoding='utf-8')", ) assert result.exit_code == 0, result.output @@ -1401,7 +1454,7 @@ def test_env_detection_override(hatch, helpers, temp_dir, config_file): output_file = project_path / 'test.txt' assert output_file.is_file() - python_path = str(output_file.read_text()).strip() + python_path = str(output_file.read_text(encoding='utf-8')).strip() assert str(env_dirs[1]) in python_path @@ -1461,7 +1514,7 @@ def test_matrix_variable_selection_duplicate_inclusion(hatch, helpers, temp_dir, '+version=42', 'python', '-c', - "import pathlib,sys;pathlib.Path('test.txt').write_text(sys.executable)", + "import pathlib,sys;pathlib.Path('test.txt').write_text(sys.executable, encoding='utf-8')", ) assert result.exit_code == 1, result.output @@ -1498,7 +1551,7 @@ def test_matrix_variable_selection_duplicate_exclusion(hatch, helpers, temp_dir, '-version=42', 'python', '-c', - "import pathlib,sys;pathlib.Path('test.txt').write_text(sys.executable)", + "import pathlib,sys;pathlib.Path('test.txt').write_text(sys.executable, encoding='utf-8')", ) assert result.exit_code == 1, result.output @@ -1535,7 +1588,7 @@ def test_matrix_variable_selection_python_alias(hatch, helpers, temp_dir, config '+python=42', 'python', '-c', - "import pathlib,sys;pathlib.Path('test.txt').write_text(sys.executable)", + "import pathlib,sys;pathlib.Path('test.txt').write_text(sys.executable, encoding='utf-8')", ) assert result.exit_code == 1, result.output @@ -1571,7 +1624,7 @@ def test_matrix_variable_selection_not_matrix(hatch, helpers, temp_dir, config_f '+version=9000', 'python', '-c', - "import os,sys;open('test.txt', 'a').write(sys.executable+os.linesep[-1])", + "import os,sys;open('test.txt', 'a', encoding='utf-8').write(sys.executable+os.linesep[-1])", ) assert result.exit_code == 1, result.output @@ -1607,7 +1660,7 @@ def test_matrix_variable_selection_inclusion(hatch, helpers, temp_dir, config_fi '+version=9000', 'test:python', '-c', - "import os,sys;open('test.txt', 'a').write(sys.executable+os.linesep[-1])", + "import os,sys;open('test.txt', 'a', encoding='utf-8').write(sys.executable+os.linesep[-1])", ) assert result.exit_code == 0, result.output @@ -1639,7 +1692,7 @@ def test_matrix_variable_selection_inclusion(hatch, helpers, temp_dir, config_fi env_path = env_dirs[0] assert env_path.name == 'test.9000' - python_path = str(output_file.read_text()).strip() + python_path = str(output_file.read_text(encoding='utf-8')).strip() assert str(env_path) in python_path @@ -1668,7 +1721,7 @@ def test_matrix_variable_selection_exclusion(hatch, helpers, temp_dir, config_fi '-version=9000', 'test:python', '-c', - "import os,sys;open('test.txt', 'a').write(sys.executable+os.linesep[-1])", + "import os,sys;open('test.txt', 'a', encoding='utf-8').write(sys.executable+os.linesep[-1])", ) assert result.exit_code == 0, result.output @@ -1700,7 +1753,7 @@ def test_matrix_variable_selection_exclusion(hatch, helpers, temp_dir, config_fi env_path = env_dirs[0] assert env_path.name == 'test.42' - python_path = str(output_file.read_text()).strip() + python_path = str(output_file.read_text(encoding='utf-8')).strip() assert str(env_path) in python_path @@ -1729,7 +1782,7 @@ def test_matrix_variable_selection_exclude_all(hatch, helpers, temp_dir, config_ '-version', 'test:python', '-c', - "import os,sys;open('test.txt', 'a').write(sys.executable+os.linesep[-1])", + "import os,sys;open('test.txt', 'a', encoding='utf-8').write(sys.executable+os.linesep[-1])", ) assert result.exit_code == 1, result.output @@ -1765,7 +1818,7 @@ def test_matrix_variable_selection_include_none(hatch, helpers, temp_dir, config '+version=3.14', 'test:python', '-c', - "import os,sys;open('test.txt', 'a').write(sys.executable+os.linesep[-1])", + "import os,sys;open('test.txt', 'a', encoding='utf-8').write(sys.executable+os.linesep[-1])", ) assert result.exit_code == 1, result.output @@ -1803,7 +1856,7 @@ def test_matrix_variable_selection_inclusion_multiple_variables(hatch, helpers, '+version1=9000', 'test:python', '-c', - "import os,sys;open('test.txt', 'a').write(sys.executable+os.linesep[-1])", + "import os,sys;open('test.txt', 'a', encoding='utf-8').write(sys.executable+os.linesep[-1])", ) assert result.exit_code == 0, result.output @@ -1835,7 +1888,7 @@ def test_matrix_variable_selection_inclusion_multiple_variables(hatch, helpers, env_path = env_dirs[0] assert env_path.name == 'test.9000-3.14' - python_path = str(output_file.read_text()).strip() + python_path = str(output_file.read_text(encoding='utf-8')).strip() assert str(env_path) in python_path @@ -1868,7 +1921,7 @@ def test_context_formatting_recursion(hatch, helpers, temp_dir, config_file): with project_path.as_cwd( env_vars={ ConfigEnvVars.DATA: str(data_path), - 'BAZ': "import pathlib,sys;pathlib.Path('test.txt').write_text(sys.executable)", + 'BAZ': "import pathlib,sys;pathlib.Path('test.txt').write_text(sys.executable, encoding='utf-8')", } ): result = hatch('run', 'py') @@ -1902,7 +1955,7 @@ def test_context_formatting_recursion(hatch, helpers, temp_dir, config_file): assert env_path.name == project_path.name - assert str(env_path) in str(output_file.read_text()) + assert str(env_path) in str(output_file.read_text(encoding='utf-8')) def test_plugin_dependencies_unmet(hatch, helpers, temp_dir, config_file, mock_plugin_installation): @@ -1927,14 +1980,20 @@ def test_plugin_dependencies_unmet(hatch, helpers, temp_dir, config_file, mock_p [env] requires = ["{dependency}"] """ - ) + ), + encoding='utf-8', ) project = Project(project_path) helpers.update_project_environment(project, 'default', {'skip-install': True, **project.config.envs['default']}) with project_path.as_cwd(env_vars={ConfigEnvVars.DATA: str(data_path)}): - result = hatch('run', 'python', '-c', "import pathlib,sys;pathlib.Path('test.txt').write_text(sys.executable)") + result = hatch( + 'run', + 'python', + '-c', + "import pathlib,sys;pathlib.Path('test.txt').write_text(sys.executable, encoding='utf-8')", + ) assert result.exit_code == 0, result.output assert result.output == helpers.dedent( @@ -1968,7 +2027,7 @@ def test_plugin_dependencies_unmet(hatch, helpers, temp_dir, config_file, mock_p assert env_path.name == project_path.name - assert str(env_path) in str(output_file.read_text()) + assert str(env_path) in str(output_file.read_text(encoding='utf-8')) @pytest.mark.requires_internet @@ -2000,7 +2059,10 @@ def test_install_python_specific(hatch, helpers, temp_dir, config_file, mocker, with project_path.as_cwd(env_vars={ConfigEnvVars.DATA: str(data_path)}): result = hatch( - 'run', 'python', '-c', "import os,sys;open('test.txt', 'a').write(sys.executable+os.linesep[-1])" + 'run', + 'python', + '-c', + "import os,sys;open('test.txt', 'a', encoding='utf-8').write(sys.executable+os.linesep[-1])", ) assert result.exit_code == 0, result.output @@ -2033,7 +2095,7 @@ def test_install_python_specific(hatch, helpers, temp_dir, config_file, mocker, assert env_path.name == project_path.name - assert str(env_path) in str(output_file.read_text()) + assert str(env_path) in str(output_file.read_text(encoding='utf-8')) assert list(manager.get_installed()) == [available_python_version] @@ -2072,7 +2134,10 @@ def test_update_python_specific(hatch, helpers, temp_dir, config_file, mocker, a with project_path.as_cwd(env_vars={ConfigEnvVars.DATA: str(data_path)}): result = hatch( - 'run', 'python', '-c', "import os,sys;open('test.txt', 'a').write(sys.executable+os.linesep[-1])" + 'run', + 'python', + '-c', + "import os,sys;open('test.txt', 'a', encoding='utf-8').write(sys.executable+os.linesep[-1])", ) assert result.exit_code == 0, result.output @@ -2105,7 +2170,7 @@ def test_update_python_specific(hatch, helpers, temp_dir, config_file, mocker, a assert env_path.name == project_path.name - assert str(env_path) in str(output_file.read_text()) + assert str(env_path) in str(output_file.read_text(encoding='utf-8')) @pytest.mark.requires_internet @@ -2133,7 +2198,10 @@ def test_install_python_max_compatible(hatch, helpers, temp_dir, config_file, mo with project_path.as_cwd(env_vars={ConfigEnvVars.DATA: str(data_path)}): result = hatch( - 'run', 'python', '-c', "import os,sys;open('test.txt', 'a').write(sys.executable+os.linesep[-1])" + 'run', + 'python', + '-c', + "import os,sys;open('test.txt', 'a', encoding='utf-8').write(sys.executable+os.linesep[-1])", ) assert result.exit_code == 0, result.output @@ -2166,7 +2234,7 @@ def test_install_python_max_compatible(hatch, helpers, temp_dir, config_file, mo assert env_path.name == project_path.name - assert str(env_path) in str(output_file.read_text()) + assert str(env_path) in str(output_file.read_text(encoding='utf-8')) assert list(manager.get_installed()) == [available_python_version] @@ -2201,7 +2269,10 @@ def test_update_python_max_compatible(hatch, helpers, temp_dir, config_file, moc with project_path.as_cwd(env_vars={ConfigEnvVars.DATA: str(data_path)}): result = hatch( - 'run', 'python', '-c', "import os,sys;open('test.txt', 'a').write(sys.executable+os.linesep[-1])" + 'run', + 'python', + '-c', + "import os,sys;open('test.txt', 'a', encoding='utf-8').write(sys.executable+os.linesep[-1])", ) assert result.exit_code == 0, result.output @@ -2234,7 +2305,7 @@ def test_update_python_max_compatible(hatch, helpers, temp_dir, config_file, moc assert env_path.name == project_path.name - assert str(env_path) in str(output_file.read_text()) + assert str(env_path) in str(output_file.read_text(encoding='utf-8')) @pytest.mark.requires_internet @@ -2277,7 +2348,8 @@ class CustomMetadataHook(MetadataHookInterface): def update(self, metadata): import binary """ - ) + ), + encoding='utf-8', ) mocker.patch('hatch.env.virtual.VirtualEnvironment._interpreter_is_compatible', return_value=False) @@ -2286,7 +2358,10 @@ def update(self, metadata): with project_path.as_cwd(env_vars={ConfigEnvVars.DATA: str(data_path)}): result = hatch( - 'run', 'python', '-c', "import os,sys;open('test.txt', 'a').write(sys.executable+os.linesep[-1])" + 'run', + 'python', + '-c', + "import os,sys;open('test.txt', 'a', encoding='utf-8').write(sys.executable+os.linesep[-1])", ) assert result.exit_code == 0, result.output @@ -2319,7 +2394,7 @@ def update(self, metadata): assert env_path.name == project_path.name - assert str(env_path) in str(output_file.read_text()) + assert str(env_path) in str(output_file.read_text(encoding='utf-8')) assert list(manager.get_installed()) == [available_python_version] @@ -2346,7 +2421,11 @@ def test_not_file(self, hatch, helpers, temp_dir): ) with project_path.as_cwd(env_vars={ConfigEnvVars.DATA: str(data_path)}): - result = hatch('run', 'script.py', "import pathlib,sys;pathlib.Path('test.txt').write_text(sys.executable)") + result = hatch( + 'run', + 'script.py', + "import pathlib,sys;pathlib.Path('test.txt').write_text(sys.executable, encoding='utf-8')", + ) assert result.exit_code == 0, result.output assert result.output == helpers.dedent( @@ -2377,7 +2456,7 @@ def test_not_file(self, hatch, helpers, temp_dir): assert env_path.name == project_path.name - assert str(env_path) in str(output_file.read_text()) + assert str(env_path) in str(output_file.read_text(encoding='utf-8')) @pytest.mark.requires_internet def test_dependencies(self, hatch, helpers, temp_dir): @@ -2398,10 +2477,12 @@ def test_dependencies(self, hatch, helpers, temp_dir): import binary pathlib.Path('test.txt').write_text( - f'{sys.executable}\\n{str(binary.convert_units(1024))}' + f'{sys.executable}\\n{str(binary.convert_units(1024))}', + encoding='utf-8' ) """ - ) + ), + encoding='utf-8', ) with temp_dir.as_cwd(env_vars={ConfigEnvVars.DATA: str(data_path)}): @@ -2425,7 +2506,7 @@ def test_dependencies(self, hatch, helpers, temp_dir): assert env_path.is_dir() assert env_path.name == script.id - executable_path, unit_conversion = output_file.read_text().splitlines() + executable_path, unit_conversion = output_file.read_text(encoding='utf-8').splitlines() executable = Path(executable_path) assert executable.is_file() @@ -2454,10 +2535,12 @@ def test_dependencies_from_tool_config(self, hatch, helpers, temp_dir): import binary pathlib.Path('test.txt').write_text( - f'{sys.executable}\\n{str(binary.convert_units(1024))}' + f'{sys.executable}\\n{str(binary.convert_units(1024))}', + encoding='utf-8' ) """ - ) + ), + encoding='utf-8', ) with temp_dir.as_cwd(env_vars={ConfigEnvVars.DATA: str(data_path)}): @@ -2481,7 +2564,7 @@ def test_dependencies_from_tool_config(self, hatch, helpers, temp_dir): assert env_path.is_dir() assert env_path.name == script.id - executable_path, unit_conversion = output_file.read_text().splitlines() + executable_path, unit_conversion = output_file.read_text(encoding='utf-8').splitlines() executable = Path(executable_path) assert executable.is_file() @@ -2501,9 +2584,10 @@ def test_unsupported_python_version(self, hatch, helpers, temp_dir): import pathlib import sys - pathlib.Path('test.txt').write_text(sys.executable) + pathlib.Path('test.txt').write_text(sys.executable, encoding='utf-8') """ - ) + ), + encoding='utf-8', ) with temp_dir.as_cwd(env_vars={ConfigEnvVars.DATA: str(data_path)}): @@ -2535,9 +2619,10 @@ def test_python_version_constraint(self, hatch, helpers, temp_dir): import pathlib import sys - pathlib.Path('test.txt').write_text(sys.executable) + pathlib.Path('test.txt').write_text(sys.executable, encoding='utf-8') """ - ) + ), + encoding='utf-8', ) with temp_dir.as_cwd(env_vars={ConfigEnvVars.DATA: str(data_path)}): @@ -2560,7 +2645,7 @@ def test_python_version_constraint(self, hatch, helpers, temp_dir): assert env_path.is_dir() assert env_path.name == script.id - executable = Path(output_file.read_text()) + executable = Path(output_file.read_text(encoding='utf-8')) assert executable.is_file() assert data_path in executable.parents @@ -2585,9 +2670,10 @@ def test_python_version_constraint_from_tool_config(self, hatch, helpers, temp_d import pathlib import sys - pathlib.Path('test.txt').write_text(sys.executable) + pathlib.Path('test.txt').write_text(sys.executable, encoding='utf-8') """ - ) + ), + encoding='utf-8', ) with temp_dir.as_cwd(env_vars={ConfigEnvVars.DATA: str(data_path)}): @@ -2610,6 +2696,6 @@ def test_python_version_constraint_from_tool_config(self, hatch, helpers, temp_d assert env_path.is_dir() assert env_path.name == script.id - executable = Path(output_file.read_text()) + executable = Path(output_file.read_text(encoding='utf-8')) assert executable.is_file() assert data_path in executable.parents diff --git a/tests/cli/test/test_test.py b/tests/cli/test/test_test.py index 2177c012d..0bf1dcec3 100644 --- a/tests/cli/test/test_test.py +++ b/tests/cli/test/test_test.py @@ -203,7 +203,7 @@ def test_flag(self, hatch, temp_dir, config_file, env_run, mocker): config_dir = next(root_config_path.iterdir()) coverage_config_file = next(config_dir.iterdir()) - assert coverage_config_file.read_text().strip().splitlines()[-2:] == [ + assert coverage_config_file.read_text(encoding='utf-8').strip().splitlines()[-2:] == [ '[tool.coverage.run]', 'parallel = true', ] @@ -239,7 +239,7 @@ def test_flag_with_arguments(self, hatch, temp_dir, config_file, env_run, mocker config_dir = next(root_config_path.iterdir()) coverage_config_file = next(config_dir.iterdir()) - assert coverage_config_file.read_text().strip().splitlines()[-2:] == [ + assert coverage_config_file.read_text(encoding='utf-8').strip().splitlines()[-2:] == [ '[tool.coverage.run]', 'parallel = true', ] @@ -274,7 +274,7 @@ def test_quiet_implicitly_enables(self, hatch, temp_dir, config_file, env_run, m config_dir = next(root_config_path.iterdir()) coverage_config_file = next(config_dir.iterdir()) - assert coverage_config_file.read_text().strip().splitlines()[-2:] == [ + assert coverage_config_file.read_text(encoding='utf-8').strip().splitlines()[-2:] == [ '[tool.coverage.run]', 'parallel = true', ] @@ -312,7 +312,7 @@ def test_legacy_config_define_section(self, hatch, temp_dir, config_file, env_ru config_dir = next(root_config_path.iterdir()) coverage_config_file = next(config_dir.iterdir()) - assert coverage_config_file.read_text().strip().splitlines() == [ + assert coverage_config_file.read_text(encoding='utf-8').strip().splitlines() == [ '[run]', 'parallel = true', ] @@ -332,7 +332,7 @@ def test_legacy_config_enable_parallel(self, hatch, temp_dir, config_file, env_r data_path = temp_dir / 'data' data_path.mkdir() - (project_path / '.coveragerc').write_text('[run]\nparallel = false\nbranch = true\n') + (project_path / '.coveragerc').write_text('[run]\nparallel = false\nbranch = true\n', encoding='utf-8') with project_path.as_cwd(env_vars={ConfigEnvVars.DATA: str(data_path)}): result = hatch('test', '--cover') @@ -350,7 +350,7 @@ def test_legacy_config_enable_parallel(self, hatch, temp_dir, config_file, env_r config_dir = next(root_config_path.iterdir()) coverage_config_file = next(config_dir.iterdir()) - assert coverage_config_file.read_text().strip().splitlines() == [ + assert coverage_config_file.read_text(encoding='utf-8').strip().splitlines() == [ '[run]', 'parallel = true', 'branch = true', @@ -818,7 +818,7 @@ def test_coverage(self, hatch, temp_dir, config_file, env_run, mocker): config_dir = next(root_config_path.iterdir()) coverage_config_file = next(config_dir.iterdir()) - assert coverage_config_file.read_text().strip().splitlines()[-2:] == [ + assert coverage_config_file.read_text(encoding='utf-8').strip().splitlines()[-2:] == [ '[tool.coverage.run]', 'parallel = true', ] diff --git a/tests/cli/version/test_version.py b/tests/cli/version/test_version.py index c13e5f27f..c19a79b71 100644 --- a/tests/cli/version/test_version.py +++ b/tests/cli/version/test_version.py @@ -48,7 +48,7 @@ def test_other_backend_show(hatch, temp_dir, helpers): data_path = temp_dir / 'data' data_path.mkdir() - (path / 'src' / 'my_app' / '__init__.py').write_text('__version__ = "9000.42"') + (path / 'src' / 'my_app' / '__init__.py').write_text('__version__ = "9000.42"', encoding='utf-8') project = Project(path) config = dict(project.raw_config) @@ -173,7 +173,8 @@ def test_plugin_dependencies_unmet(hatch, helpers, temp_dir, mock_plugin_install [env] requires = ["{dependency}"] """ - ) + ), + encoding='utf-8', ) with path.as_cwd(env_vars={ConfigEnvVars.DATA: str(data_path)}): diff --git a/tests/conftest.py b/tests/conftest.py index a2fbb9fdf..61d659330 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -81,8 +81,8 @@ def isolation(uv_on_path) -> Generator[Path, None, None]: licenses_dir = cache_dir / 'licenses' licenses_dir.mkdir() - licenses_dir.joinpath('Apache-2.0.txt').write_text(Apache_2_0) - licenses_dir.joinpath('MIT.txt').write_text(MIT) + licenses_dir.joinpath('Apache-2.0.txt').write_text(Apache_2_0, encoding='utf-8') + licenses_dir.joinpath('MIT.txt').write_text(MIT, encoding='utf-8') default_env_vars = { AppEnvVars.NO_COLOR: '1', @@ -255,7 +255,7 @@ def devpi(tmp_path_factory, worker_id): devpi_docker_data = devpi_data / 'docker' with FileLock(lock_file): if devpi_data_file.is_file(): - data = json.loads(devpi_data_file.read_text()) + data = json.loads(devpi_data_file.read_text(encoding='utf-8')) else: import trustme diff --git a/tests/env/collectors/test_custom.py b/tests/env/collectors/test_custom.py index 2b53e6f2b..4b8716a46 100644 --- a/tests/env/collectors/test_custom.py +++ b/tests/env/collectors/test_custom.py @@ -42,7 +42,8 @@ class CustomHook(EnvironmentCollectorInterface): def foo(self): return self.PLUGIN_NAME, self.root """ - ) + ), + encoding='utf-8', ) with temp_dir.as_cwd(): @@ -65,7 +66,8 @@ class CustomHook(EnvironmentCollectorInterface): def foo(self): return self.PLUGIN_NAME, self.root """ - ) + ), + encoding='utf-8', ) with temp_dir.as_cwd(): @@ -90,7 +92,8 @@ def test_no_subclass(temp_dir, helpers): class CustomHook: pass """ - ) + ), + encoding='utf-8', ) with pytest.raises( diff --git a/tests/helpers/helpers.py b/tests/helpers/helpers.py index 01d1bade5..996a033ef 100644 --- a/tests/helpers/helpers.py +++ b/tests/helpers/helpers.py @@ -148,14 +148,14 @@ def write_distribution(directory: Path, name: str): metadata = {'source': dist.source, 'python_path': dist.python_path} metadata_file = path / InstalledDistribution.metadata_filename() - metadata_file.write_text(json.dumps(metadata)) + metadata_file.write_text(json.dumps(metadata), encoding='utf-8') return InstalledDistribution(path, dist, metadata) def downgrade_distribution_metadata(dist_dir: Path): metadata_file = dist_dir / InstalledDistribution.metadata_filename() - metadata = json.loads(metadata_file.read_text()) + metadata = json.loads(metadata_file.read_text(encoding='utf-8')) dist = InstalledDistribution(dist_dir, get_distribution(dist_dir.name), metadata) source = metadata['source'] @@ -173,7 +173,7 @@ def downgrade_distribution_metadata(dist_dir: Path): new_python_path.parent.ensure_dir_exists() (dist_dir / python_path).rename(new_python_path) - metadata_file.write_text(json.dumps(metadata)) + metadata_file.write_text(json.dumps(metadata), encoding='utf-8') return metadata diff --git a/tests/project/test_config.py b/tests/project/test_config.py index 1edd48d49..1e97acf2d 100644 --- a/tests/project/test_config.py +++ b/tests/project/test_config.py @@ -2511,7 +2511,8 @@ class CustomHook(EnvironmentCollectorInterface): def finalize_config(self, config): config['default']['type'] = 'foo' """ - ) + ), + encoding='utf-8', ) env_config = { @@ -2546,7 +2547,8 @@ class CustomHook(EnvironmentCollectorInterface): def finalize_environments(self, config): config['foo.42']['type'] = 'foo' """ - ) + ), + encoding='utf-8', ) env_config = { diff --git a/tests/project/test_frontend.py b/tests/project/test_frontend.py index f7570856c..3ba8ddfb5 100644 --- a/tests/project/test_frontend.py +++ b/tests/project/test_frontend.py @@ -57,7 +57,8 @@ def test_wheel(self, temp_dir, temp_dir_data, platform, global_application, back name = "foo" version = "9000.42" description = "text" -""" +""", + encoding='utf-8', ) package_dir = project_dir / 'foo' @@ -85,10 +86,10 @@ def test_wheel(self, temp_dir, temp_dir_data, platform, global_application, back ) platform.check_command([sys.executable, '-c', script]) work_dir = output_dir / 'work' - output = json.loads((output_dir / 'output.json').read_text()) + output = json.loads((output_dir / 'output.json').read_text(encoding='utf-8')) metadata_file = work_dir / output['return_val'] / 'METADATA' - assert project_metadata_from_core_metadata(metadata_file.read_text()) == { + assert project_metadata_from_core_metadata(metadata_file.read_text(encoding='utf-8')) == { 'name': 'foo', 'version': '9000.42', 'description': 'text', @@ -111,7 +112,8 @@ def test_editable(self, temp_dir, temp_dir_data, platform, global_application, b name = "foo" version = "9000.42" description = "text" -""" +""", + encoding='utf-8', ) package_dir = project_dir / 'foo' @@ -139,10 +141,10 @@ def test_editable(self, temp_dir, temp_dir_data, platform, global_application, b ) platform.check_command([sys.executable, '-c', script]) work_dir = output_dir / 'work' - output = json.loads((output_dir / 'output.json').read_text()) + output = json.loads((output_dir / 'output.json').read_text(encoding='utf-8')) metadata_file = work_dir / output['return_val'] / 'METADATA' - assert project_metadata_from_core_metadata(metadata_file.read_text()) == { + assert project_metadata_from_core_metadata(metadata_file.read_text(encoding='utf-8')) == { 'name': 'foo', 'version': '9000.42', 'description': 'text', @@ -167,7 +169,8 @@ def test_standard(self, temp_dir, temp_dir_data, platform, global_application, b name = "foo" version = "9000.42" description = "text" -""" +""", + encoding='utf-8', ) package_dir = project_dir / 'foo' @@ -193,7 +196,7 @@ def test_standard(self, temp_dir, temp_dir_data, platform, global_application, b script = project.build_frontend.scripts.build_wheel(output_dir=str(output_dir), project_root=str(project_dir)) platform.check_command([sys.executable, '-c', script]) work_dir = output_dir / 'work' - output = json.loads((output_dir / 'output.json').read_text()) + output = json.loads((output_dir / 'output.json').read_text(encoding='utf-8')) wheel_path = work_dir / output['return_val'] assert wheel_path.is_file() @@ -217,7 +220,8 @@ def test_editable(self, temp_dir, temp_dir_data, platform, global_application, b name = "foo" version = "9000.42" description = "text" -""" +""", + encoding='utf-8', ) package_dir = project_dir / 'foo' @@ -245,7 +249,7 @@ def test_editable(self, temp_dir, temp_dir_data, platform, global_application, b ) platform.check_command([sys.executable, '-c', script]) work_dir = output_dir / 'work' - output = json.loads((output_dir / 'output.json').read_text()) + output = json.loads((output_dir / 'output.json').read_text(encoding='utf-8')) wheel_path = work_dir / output['return_val'] assert wheel_path.is_file() @@ -271,7 +275,8 @@ def test_standard(self, temp_dir, temp_dir_data, platform, global_application, b name = "foo" version = "9000.42" description = "text" -""" +""", + encoding='utf-8', ) package_dir = project_dir / 'foo' @@ -297,7 +302,7 @@ def test_standard(self, temp_dir, temp_dir_data, platform, global_application, b script = project.build_frontend.scripts.build_sdist(output_dir=str(output_dir), project_root=str(project_dir)) platform.check_command([sys.executable, '-c', script]) work_dir = output_dir / 'work' - output = json.loads((output_dir / 'output.json').read_text()) + output = json.loads((output_dir / 'output.json').read_text(encoding='utf-8')) sdist_path = work_dir / output['return_val'] assert sdist_path.is_file() @@ -323,7 +328,8 @@ def test_default(self, temp_dir, temp_dir_data, platform, global_application, ba name = "foo" version = "9000.42" description = "text" -""" +""", + encoding='utf-8', ) package_dir = project_dir / 'foo' @@ -350,7 +356,7 @@ def test_default(self, temp_dir, temp_dir_data, platform, global_application, ba output_dir=str(output_dir), project_root=str(project_dir), build=build ) platform.check_command([sys.executable, '-c', script]) - output = json.loads((output_dir / 'output.json').read_text()) + output = json.loads((output_dir / 'output.json').read_text(encoding='utf-8')) assert output['return_val'] == ( [EDITABLES_REQUIREMENT] if backend_pkg == 'hatchling' and build == 'editable' else [] @@ -370,7 +376,8 @@ def test_default(self, temp_dir, temp_dir_data, platform, global_application): [project] name = "foo" version = "9000.42" -""" +""", + encoding='utf-8', ) package_dir = project_dir / 'foo' @@ -397,6 +404,6 @@ def test_default(self, temp_dir, temp_dir_data, platform, global_application): output_dir=str(output_dir), project_root=str(project_dir), targets=['sdist', 'wheel'] ) platform.check_command([sys.executable, '-c', script]) - output = json.loads((output_dir / 'output.json').read_text()) + output = json.loads((output_dir / 'output.json').read_text(encoding='utf-8')) assert output == [] diff --git a/tests/python/test_core.py b/tests/python/test_core.py index 1b0eae919..2a0ffcaf6 100644 --- a/tests/python/test_core.py +++ b/tests/python/test_core.py @@ -75,7 +75,7 @@ def test_no_python_path(self, temp_dir): path = temp_dir / dist.name path.mkdir() metadata_file = path / InstalledDistribution.metadata_filename() - metadata_file.write_text(json.dumps({'source': dist.source})) + metadata_file.write_text(json.dumps({'source': dist.source}), encoding='utf-8') assert manager.get_installed() == {} @@ -87,7 +87,7 @@ def test_order(self, temp_dir, compatible_python_distributions): path = temp_dir / dist.name path.mkdir() metadata_file = path / InstalledDistribution.metadata_filename() - metadata_file.write_text(json.dumps({'source': dist.source})) + metadata_file.write_text(json.dumps({'source': dist.source}), encoding='utf-8') python_path = path / dist.python_path python_path.parent.ensure_dir_exists() python_path.touch()