Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Change write_bytes to write_text to preserve newlines #157

Merged
merged 3 commits into from
Dec 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## Unreleased

* Fixed:
* Line ending style was not preserved in some cases because of the default behavior of `pathlib.Path.read_text`.
To avoid this, `pathlib.Path.read_bytes` is used instead now.
([Contributed by nardi](https://github.com/mtkennerly/poetry-dynamic-versioning/pull/157))

## v1.1.1 (2023-10-27)

* Fixed:
Expand Down
14 changes: 7 additions & 7 deletions poetry_dynamic_versioning/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ def _get_config_from_path(start: Optional[Path] = None) -> Mapping:
pyproject_path = _get_pyproject_path(start)
if pyproject_path is None:
return _default_config()["tool"]["poetry-dynamic-versioning"]
pyproject = tomlkit.parse(pyproject_path.read_text(encoding="utf-8"))
pyproject = tomlkit.parse(pyproject_path.read_bytes().decode("utf-8"))
result = _get_config(pyproject)
return result

Expand All @@ -287,7 +287,7 @@ def _validate_config(config: Optional[Mapping] = None) -> Sequence[str]:
pyproject_path = _get_pyproject_path()
if pyproject_path is None:
raise RuntimeError("Unable to find pyproject.toml")
config = tomlkit.parse(pyproject_path.read_text(encoding="utf-8"))
config = tomlkit.parse(pyproject_path.read_bytes().decode("utf-8"))

return _validate_config_section(
config.get("tool", {}).get("poetry-dynamic-versioning", {}),
Expand Down Expand Up @@ -469,7 +469,7 @@ def _substitute_version(name: str, version: str, folders: Sequence[_FolderConfig
files[resolved] = folder

for file, config in files.items():
original_content = file.read_text(encoding="utf-8")
original_content = file.read_bytes().decode("utf-8")
new_content = _substitute_version_in_text(version, original_content, config.patterns)
if original_content != new_content:
_state.projects[name].substitutions[file] = original_content
Expand Down Expand Up @@ -509,7 +509,7 @@ def _substitute_version_in_text(version: str, content: str, patterns: Sequence[_
def _apply_version(
version: str, config: _Config, pyproject_path: Path, retain: bool = False
) -> None:
pyproject = tomlkit.parse(pyproject_path.read_text(encoding="utf-8"))
pyproject = tomlkit.parse(pyproject_path.read_bytes().decode("utf-8"))

pyproject["tool"]["poetry"]["version"] = version # type: ignore

Expand Down Expand Up @@ -559,7 +559,7 @@ def _get_and_apply_version(
raise RuntimeError("Unable to find pyproject.toml")

if pyproject is None:
pyproject = tomlkit.parse(pyproject_path.read_text(encoding="utf-8"))
pyproject = tomlkit.parse(pyproject_path.read_bytes().decode("utf-8"))

if name is None or original is None:
name = pyproject["tool"]["poetry"]["name"]
Expand Down Expand Up @@ -590,7 +590,7 @@ def _get_and_apply_version(

def _revert_version(retain: bool = False) -> None:
for project, state in _state.projects.items():
pyproject = tomlkit.parse(state.path.read_text(encoding="utf-8"))
pyproject = tomlkit.parse(state.path.read_bytes().decode("utf-8"))

if state.substitutions:
config = _get_config(pyproject)
Expand All @@ -607,7 +607,7 @@ def _revert_version(retain: bool = False) -> None:
file.write_bytes(content.encode("utf-8"))

# Reread pyproject.toml in case the substitutions affected it.
pyproject = tomlkit.parse(state.path.read_text(encoding="utf-8"))
pyproject = tomlkit.parse(state.path.read_bytes().decode("utf-8"))

pyproject["tool"]["poetry"]["version"] = state.original_version # type: ignore

Expand Down
2 changes: 1 addition & 1 deletion poetry_dynamic_versioning/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def enable() -> None:
pyproject_path = _get_pyproject_path()
if pyproject_path is None:
raise RuntimeError("Unable to find pyproject.toml")
config = tomlkit.parse(pyproject_path.read_text(encoding="utf-8"))
config = tomlkit.parse(pyproject_path.read_bytes().decode("utf-8"))

config = _enable_in_doc(config)
pyproject_path.write_bytes(tomlkit.dumps(config).encode("utf-8"))
Expand Down
54 changes: 33 additions & 21 deletions tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def test_plugin_enabled():


def test_plugin_disabled():
data = DUMMY_PYPROJECT.read_text("utf8")
data = DUMMY_PYPROJECT.read_bytes().decode("utf-8")
data = data.replace("enable = true", "enable = false")
DUMMY_PYPROJECT.write_bytes(data.encode("utf-8"))

Expand All @@ -98,7 +98,7 @@ def test_plugin_disabled():


def test_plugin_disabled_without_plugin_section():
data = DUMMY_PYPROJECT.read_text("utf8")
data = DUMMY_PYPROJECT.read_bytes().decode("utf-8")
data = data.replace("[tool.poetry-dynamic-versioning]", "[tool.poetry-dynamic-versioning-x]")
DUMMY_PYPROJECT.write_bytes(data.encode("utf-8"))

Expand All @@ -113,7 +113,7 @@ def test_plugin_disabled_without_pyproject_file():


def test_invalid_config_for_vcs():
data = DUMMY_PYPROJECT.read_text("utf8")
data = DUMMY_PYPROJECT.read_bytes().decode("utf-8")
data = data.replace('vcs = "git"', 'vcs = "invalid"')
DUMMY_PYPROJECT.write_bytes(data.encode("utf-8"))

Expand All @@ -125,64 +125,76 @@ def test_keep_pyproject_modifications():
# Using --optional to avoid actually installing the package
run(f"poetry add --optional {package}", where=DUMMY)
# Make sure pyproject.toml contains the new package dependency
data = DUMMY_PYPROJECT.read_text("utf8")
data = DUMMY_PYPROJECT.read_bytes().decode("utf-8")
assert package in data


def test_poetry_run():
# The original version is restored before the command runs:
run(f"poetry run grep 'version = \"{DUMMY_VERSION}\"' pyproject.toml", where=DUMMY)
# Make sure original version number is still in place:
data = DUMMY_PYPROJECT.read_text("utf8")
data = DUMMY_PYPROJECT.read_bytes().decode("utf-8")
assert f'version = "{DUMMY_VERSION}"' in data


@pytest.mark.skipif("CI" in os.environ, reason="Avoid error: 'Inappropriate ioctl for device'")
def test_poetry_shell():
# Make sure original version number is still in place afterwards:
run("poetry shell", where=DUMMY)
data = DUMMY_PYPROJECT.read_text("utf8")
data = DUMMY_PYPROJECT.read_bytes().decode("utf-8")
assert f'version = "{DUMMY_VERSION}"' in data


def test_plugin_cli_mode_and_substitution():
run("poetry dynamic-versioning", where=DUMMY)
# Changes persist after the command is done:
assert f'version = "{DUMMY_VERSION}"' not in DUMMY_PYPROJECT.read_text("utf8")
assert '__version__: str = "0.0.0"' not in (DUMMY / "project" / "__init__.py").read_text("utf8")
assert '__version__ = "0.0.0"' not in (DUMMY / "project" / "__init__.py").read_text("utf8")
assert f'version = "{DUMMY_VERSION}"' not in DUMMY_PYPROJECT.read_bytes().decode("utf-8")
assert '__version__: str = "0.0.0"' not in (
DUMMY / "project" / "__init__.py"
).read_bytes().decode("utf-8")
assert '__version__ = "0.0.0"' not in (DUMMY / "project" / "__init__.py").read_bytes().decode(
"utf-8"
)
assert "__version_tuple__ = (0, 0, 0)" not in (DUMMY / "project" / "__init__.py").read_text(
"utf8"
)
assert "<0.0.0>" not in (DUMMY / "project" / "__init__.py").read_text("utf8")
assert "<0.0.0>" not in (DUMMY / "project" / "__init__.py").read_bytes().decode("utf-8")


def test_standalone_cli_mode_and_substitution():
run("poetry-dynamic-versioning", where=DUMMY)
# Changes persist after the command is done:
assert f'version = "{DUMMY_VERSION}"' not in DUMMY_PYPROJECT.read_text("utf8")
assert '__version__: str = "0.0.0"' not in (DUMMY / "project" / "__init__.py").read_text("utf8")
assert '__version__ = "0.0.0"' not in (DUMMY / "project" / "__init__.py").read_text("utf8")
assert f'version = "{DUMMY_VERSION}"' not in DUMMY_PYPROJECT.read_bytes().decode("utf-8")
assert '__version__: str = "0.0.0"' not in (
DUMMY / "project" / "__init__.py"
).read_bytes().decode("utf-8")
assert '__version__ = "0.0.0"' not in (DUMMY / "project" / "__init__.py").read_bytes().decode(
"utf-8"
)
assert "__version_tuple__ = (0, 0, 0)" not in (DUMMY / "project" / "__init__.py").read_text(
"utf8"
)
assert "<0.0.0>" not in (DUMMY / "project" / "__init__.py").read_text("utf8")
assert "<0.0.0>" not in (DUMMY / "project" / "__init__.py").read_bytes().decode("utf-8")


def test_cli_mode_and_substitution_without_enable():
data = DUMMY_PYPROJECT.read_text("utf8")
data = DUMMY_PYPROJECT.read_bytes().decode("utf-8")
data = data.replace("enable = true", "enable = false")
DUMMY_PYPROJECT.write_bytes(data.encode("utf-8"))

run("poetry dynamic-versioning", where=DUMMY)
# Changes persist after the command is done:
assert f'version = "{DUMMY_VERSION}"' not in DUMMY_PYPROJECT.read_text("utf8")
assert '__version__: str = "0.0.0"' not in (DUMMY / "project" / "__init__.py").read_text("utf8")
assert '__version__ = "0.0.0"' not in (DUMMY / "project" / "__init__.py").read_text("utf8")
assert f'version = "{DUMMY_VERSION}"' not in DUMMY_PYPROJECT.read_bytes().decode("utf-8")
assert '__version__: str = "0.0.0"' not in (
DUMMY / "project" / "__init__.py"
).read_bytes().decode("utf-8")
assert '__version__ = "0.0.0"' not in (DUMMY / "project" / "__init__.py").read_bytes().decode(
"utf-8"
)
assert "__version_tuple__ = (0, 0, 0)" not in (DUMMY / "project" / "__init__.py").read_text(
"utf8"
)
assert "<0.0.0>" not in (DUMMY / "project" / "__init__.py").read_text("utf8")
assert "<0.0.0>" not in (DUMMY / "project" / "__init__.py").read_bytes().decode("utf-8")


def test_cli_mode_plus_build_will_disable_plugin():
Expand Down Expand Up @@ -210,7 +222,7 @@ def test_poetry_core_as_build_system():
dist = project / "dist"
pyproject = project / "pyproject.toml"

data = pyproject.read_text("utf8")
data = pyproject.read_bytes().decode("utf-8")
data = re.sub(
r"requires = .*",
'requires = ["poetry-core>=1.0.0", "poetry-dynamic-versioning"]',
Expand All @@ -229,7 +241,7 @@ def test_poetry_core_as_build_system():


def test_bumping_enabled():
data = DUMMY_PYPROJECT.read_text("utf8")
data = DUMMY_PYPROJECT.read_bytes().decode("utf-8")
data = data.replace('vcs = "git"', "bump = true")
data = data.replace('style = "semver"', 'style = "pep440"')
DUMMY_PYPROJECT.write_bytes(data.encode("utf-8"))
Expand Down