Skip to content
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
10 changes: 5 additions & 5 deletions commitizen/commands/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import re
import sys
from pathlib import Path
from typing import TYPE_CHECKING, TypedDict

from commitizen import factory, git, out
Expand Down Expand Up @@ -116,11 +117,10 @@ def _get_commit_message(self) -> str | None:
# Get commit message from command line (--message)
return self.commit_msg

with open(
self.commit_msg_file, encoding=self.config.settings["encoding"]
) as commit_file:
# Get commit message from file (--commit-msg-file)
return commit_file.read()
# Get commit message from file (--commit-msg-file)
return Path(self.commit_msg_file).read_text(
encoding=self.config.settings["encoding"]
)

def _get_commits(self) -> list[git.GitCommit]:
if (msg := self._get_commit_message()) is not None:
Expand Down
11 changes: 5 additions & 6 deletions commitizen/commands/commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import shutil
import subprocess
import tempfile
from pathlib import Path
from typing import TYPE_CHECKING, TypedDict

import questionary
Expand All @@ -26,8 +27,6 @@
from commitizen.git import smart_open

if TYPE_CHECKING:
from pathlib import Path

from commitizen.config import BaseConfig


Expand Down Expand Up @@ -61,8 +60,9 @@ def _read_backup_message(self) -> str | None:
return None

# Read commit message from backup
with self.backup_file_path.open(encoding=self.config.settings["encoding"]) as f:
return f.read().strip()
return self.backup_file_path.read_text(
encoding=self.config.settings["encoding"]
).strip()

def _get_message_by_prompt_commit_questions(self) -> str:
# Prompt user for the commit message
Expand Down Expand Up @@ -112,8 +112,7 @@ def manual_edit(self, message: str) -> str:
file_path = file.name
argv = [exec_path, file_path]
subprocess.call(argv)
with open(file_path) as temp_file:
message = temp_file.read().strip()
message = Path(file_path).read_text().strip()
os.unlink(file.name)
return message

Expand Down
3 changes: 1 addition & 2 deletions commitizen/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ def _resolve_config_candidates() -> list[BaseConfig]:


def _create_config_from_path(path: Path) -> BaseConfig:
with path.open("rb") as f:
return create_config(data=f.read(), path=path)
return create_config(data=path.read_bytes(), path=path)


def read_cfg(filepath: str | None = None) -> BaseConfig:
Expand Down
12 changes: 4 additions & 8 deletions commitizen/config/toml_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,13 @@ def __init__(self, *, data: bytes | str, path: Path) -> None:
self._parse_setting(data)

def contains_commitizen_section(self) -> bool:
with self.path.open("rb") as f:
config_doc = parse(f.read())
config_doc = parse(self.path.read_bytes())
return config_doc.get("tool", {}).get("commitizen") is not None

def init_empty_config_content(self) -> None:
config_doc = TOMLDocument()
if self.path.is_file():
with self.path.open("rb") as input_toml_file:
config_doc = parse(input_toml_file.read())
config_doc = parse(self.path.read_bytes())

if config_doc.get("tool") is None:
config_doc["tool"] = table()
Expand All @@ -46,12 +44,10 @@ def init_empty_config_content(self) -> None:
)

def set_key(self, key: str, value: object) -> Self:
with self.path.open("rb") as f:
config_doc = parse(f.read())
config_doc = parse(self.path.read_bytes())

config_doc["tool"]["commitizen"][key] = value # type: ignore[index]
with self.path.open("wb") as f:
f.write(config_doc.as_string().encode(self._settings["encoding"]))
self.path.write_bytes(config_doc.as_string().encode(self._settings["encoding"]))

return self

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,5 +215,4 @@ def schema_pattern(self) -> str:

def info(self) -> str:
filepath = Path(__file__).parent / "conventional_commits_info.txt"
with filepath.open(encoding=self.config.settings["encoding"]) as f:
return f.read()
return filepath.read_text(encoding=self.config.settings["encoding"])
4 changes: 2 additions & 2 deletions commitizen/cz/customize/customize.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

from pathlib import Path
from typing import TYPE_CHECKING, Any

if TYPE_CHECKING:
Expand Down Expand Up @@ -68,6 +69,5 @@ def schema(self) -> str:

def info(self) -> str:
if info_path := self.custom_settings.get("info_path"):
with open(info_path, encoding=self.config.settings["encoding"]) as f:
return f.read()
return Path(info_path).read_text(encoding=self.config.settings["encoding"])
return self.custom_settings.get("info") or ""
3 changes: 1 addition & 2 deletions commitizen/cz/jira/jira.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,4 @@ def schema_pattern(self) -> str:

def info(self) -> str:
filepath = Path(__file__).parent / "jira_info.txt"
with filepath.open(encoding=self.config.settings["encoding"]) as f:
return f.read()
return filepath.read_text(encoding=self.config.settings["encoding"])
94 changes: 38 additions & 56 deletions tests/commands/test_bump_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ def test_bump_minor_increment_signed(commit_msg: str, util: UtilFixture):
def test_bump_minor_increment_annotated_config_file(
commit_msg: str, util: UtilFixture, pyproject: Path
):
pyproject.write_text(pyproject.read_text() + "\nannotated_tag = 1")
with pyproject.open("a", encoding="utf-8") as f:
f.write("\nannotated_tag = 1")
util.create_file_and_commit(commit_msg)
util.run_cli("bump", "--yes")
assert git.tag_exist("0.2.0") is True
Expand All @@ -117,7 +118,8 @@ def test_bump_minor_increment_signed_config_file(
commit_msg: str, util: UtilFixture, tmp_commitizen_project_with_gpg
):
tmp_commitizen_cfg_file = tmp_commitizen_project_with_gpg.join("pyproject.toml")
tmp_commitizen_cfg_file.write(f"{tmp_commitizen_cfg_file.read()}\ngpg_sign = 1")
with Path(tmp_commitizen_cfg_file).open("a", encoding="utf-8") as f:
f.write("\ngpg_sign = 1")
util.create_file_and_commit(commit_msg)
util.run_cli("bump", "--yes")
assert git.tag_exist("0.2.0") is True
Expand Down Expand Up @@ -362,10 +364,8 @@ def test_bump_when_version_inconsistent_in_version_files(
tmp_version_file.write("100.999.10000")
tmp_commitizen_cfg_file = tmp_commitizen_project.join("pyproject.toml")
tmp_version_file_string = str(tmp_version_file).replace("\\", "/")
tmp_commitizen_cfg_file.write(
f"{tmp_commitizen_cfg_file.read()}\n"
f'version_files = ["{tmp_version_file_string}"]'
)
with Path(tmp_commitizen_cfg_file).open("a", encoding="utf-8") as f:
f.write(f'\nversion_files = ["{tmp_version_file_string}"]')

util.create_file_and_commit("feat: new file")

Expand Down Expand Up @@ -407,10 +407,8 @@ def test_bump_files_only(tmp_commitizen_project, util: UtilFixture):
tmp_version_file.write("0.1.0")
tmp_commitizen_cfg_file = tmp_commitizen_project.join("pyproject.toml")
tmp_version_file_string = str(tmp_version_file).replace("\\", "/")
tmp_commitizen_cfg_file.write(
f"{tmp_commitizen_cfg_file.read()}\n"
f'version_files = ["{tmp_version_file_string}"]'
)
with Path(tmp_commitizen_cfg_file).open("a", encoding="utf-8") as f:
f.write(f'\nversion_files = ["{tmp_version_file_string}"]')

util.create_file_and_commit("feat: new user interface")
util.run_cli("bump", "--yes")
Expand All @@ -422,11 +420,9 @@ def test_bump_files_only(tmp_commitizen_project, util: UtilFixture):

assert git.tag_exist("0.3.0") is False

with tmp_version_file.open(encoding="utf-8") as f:
assert "0.3.0" in f.read()
assert "0.3.0" in Path(tmp_version_file).read_text(encoding="utf-8")

with tmp_commitizen_cfg_file.open(encoding="utf-8") as f:
assert "0.3.0" in f.read()
assert "0.3.0" in Path(tmp_commitizen_cfg_file).read_text(encoding="utf-8")


def test_bump_local_version(tmp_commitizen_project, util: UtilFixture):
Expand All @@ -444,8 +440,7 @@ def test_bump_local_version(tmp_commitizen_project, util: UtilFixture):
util.run_cli("bump", "--yes", "--local-version")
assert git.tag_exist("4.5.1+0.2.0") is True

with tmp_version_file.open(encoding="utf-8") as f:
assert "4.5.1+0.2.0" in f.read()
assert "4.5.1+0.2.0" in Path(tmp_version_file).read_text(encoding="utf-8")


@pytest.mark.usefixtures("tmp_commitizen_project")
Expand Down Expand Up @@ -504,8 +499,7 @@ def test_bump_with_changelog_arg(util: UtilFixture, changelog_path):
util.run_cli("bump", "--yes", "--changelog")
assert git.tag_exist("0.2.0") is True

with changelog_path.open(encoding="utf-8") as f:
out = f.read()
out = changelog_path.read_text(encoding="utf-8")
assert out.startswith("#")
assert "0.2.0" in out

Expand All @@ -519,8 +513,7 @@ def test_bump_with_changelog_config(util: UtilFixture, changelog_path, config_pa
util.run_cli("bump", "--yes")
assert git.tag_exist("0.2.0") is True

with changelog_path.open(encoding="utf-8") as f:
out = f.read()
out = changelog_path.read_text(encoding="utf-8")
assert out.startswith("#")
assert "0.2.0" in out

Expand Down Expand Up @@ -557,8 +550,7 @@ def test_bump_with_changelog_to_stdout_arg(
assert "this should appear in stdout" in out
assert git.tag_exist("0.2.0") is True

with changelog_path.open(encoding="utf-8") as f:
out = f.read()
out = changelog_path.read_text(encoding="utf-8")
assert out.startswith("#")
assert "0.2.0" in out

Expand Down Expand Up @@ -800,8 +792,9 @@ def test_bump_version_with_less_components_in_config(
tmp_commitizen_project.join("__version__.py"),
tmp_commitizen_project.join("pyproject.toml"),
]:
with version_file.open() as f:
assert expected_version_after_bump in f.read()
assert expected_version_after_bump in Path(version_file).read_text(
encoding="utf-8"
)


@pytest.mark.parametrize("commit_msg", ["feat: new file", "feat(user): new file"])
Expand All @@ -812,11 +805,11 @@ def test_bump_with_pre_bump_hooks(
post_bump_hook = "scripts/post_bump_hook.sh"

tmp_commitizen_cfg_file = tmp_commitizen_project.join("pyproject.toml")
tmp_commitizen_cfg_file.write(
f"{tmp_commitizen_cfg_file.read()}\n"
f'pre_bump_hooks = ["{pre_bump_hook}"]\n'
f'post_bump_hooks = ["{post_bump_hook}"]\n'
)
with Path(tmp_commitizen_cfg_file).open("a", encoding="utf-8") as f:
f.write(
f'\npre_bump_hooks = ["{pre_bump_hook}"]\n'
f'post_bump_hooks = ["{post_bump_hook}"]\n'
)

run_mock = mocker.Mock()
mocker.patch.object(hooks, "run", run_mock)
Expand Down Expand Up @@ -863,11 +856,11 @@ def test_bump_with_hooks_and_increment(
post_bump_hook = "scripts/post_bump_hook.sh"

tmp_commitizen_cfg_file = tmp_commitizen_project.join("pyproject.toml")
tmp_commitizen_cfg_file.write(
f"{tmp_commitizen_cfg_file.read()}\n"
f'pre_bump_hooks = ["{pre_bump_hook}"]\n'
f'post_bump_hooks = ["{post_bump_hook}"]\n'
)
with Path(tmp_commitizen_cfg_file).open("a", encoding="utf-8") as f:
f.write(
f'\npre_bump_hooks = ["{pre_bump_hook}"]\n'
f'post_bump_hooks = ["{post_bump_hook}"]\n'
)

run_mock = mocker.Mock()
mocker.patch.object(hooks, "run", run_mock)
Expand Down Expand Up @@ -913,16 +906,14 @@ def test_bump_command_prerelease_scheme_via_cli(
assert git.tag_exist("0.2.0-a0") is True

for version_file in [tmp_version_file, tmp_commitizen_cfg_file]:
with version_file.open() as f:
assert "0.2.0-a0" in f.read()
assert "0.2.0-a0" in Path(version_file).read_text(encoding="utf-8")

# PRERELEASE BUMP CREATES VERSION WITHOUT PRERELEASE
util.run_cli("bump", "--yes")
assert git.tag_exist("0.2.0") is True

for version_file in [tmp_version_file, tmp_commitizen_cfg_file]:
with version_file.open() as f:
assert "0.2.0" in f.read()
assert "0.2.0" in Path(version_file).read_text(encoding="utf-8")


def test_bump_command_prerelease_scheme_via_config(
Expand All @@ -938,23 +929,20 @@ def test_bump_command_prerelease_scheme_via_config(
assert git.tag_exist("0.2.0-a0") is True

for version_file in [tmp_version_file, tmp_commitizen_cfg_file]:
with version_file.open() as f:
assert "0.2.0-a0" in f.read()
assert "0.2.0-a0" in Path(version_file).read_text(encoding="utf-8")

util.run_cli("bump", "--prerelease", "alpha", "--yes")
assert git.tag_exist("0.2.0-a1") is True

for version_file in [tmp_version_file, tmp_commitizen_cfg_file]:
with version_file.open() as f:
assert "0.2.0-a1" in f.read()
assert "0.2.0-a1" in Path(version_file).read_text(encoding="utf-8")

# PRERELEASE BUMP CREATES VERSION WITHOUT PRERELEASE
util.run_cli("bump", "--yes")
assert git.tag_exist("0.2.0") is True

for version_file in [tmp_version_file, tmp_commitizen_cfg_file]:
with version_file.open() as f:
assert "0.2.0" in f.read()
assert "0.2.0" in Path(version_file).read_text(encoding="utf-8")


def test_bump_command_prerelease_scheme_check_old_tags(
Expand All @@ -970,23 +958,20 @@ def test_bump_command_prerelease_scheme_check_old_tags(
assert git.tag_exist("v0.2.0-a0") is True

for version_file in [tmp_version_file, tmp_commitizen_cfg_file]:
with version_file.open() as f:
assert "0.2.0-a0" in f.read()
assert "0.2.0-a0" in Path(version_file).read_text(encoding="utf-8")

util.run_cli("bump", "--prerelease", "alpha")
assert git.tag_exist("v0.2.0-a1") is True

for version_file in [tmp_version_file, tmp_commitizen_cfg_file]:
with version_file.open() as f:
assert "0.2.0-a1" in f.read()
assert "0.2.0-a1" in Path(version_file).read_text(encoding="utf-8")

# PRERELEASE BUMP CREATES VERSION WITHOUT PRERELEASE
util.run_cli("bump")
assert git.tag_exist("v0.2.0") is True

for version_file in [tmp_version_file, tmp_commitizen_cfg_file]:
with version_file.open() as f:
assert "0.2.0" in f.read()
assert "0.2.0" in Path(version_file).read_text(encoding="utf-8")


@pytest.mark.usefixtures("tmp_commitizen_project")
Expand Down Expand Up @@ -1458,8 +1443,7 @@ def test_changelog_config_flag_merge_prerelease(

util.run_cli("bump", "--changelog")

with changelog_path.open() as f:
out = f.read()
out = changelog_path.read_text()

file_regression.check(out, extension=".md")

Expand Down Expand Up @@ -1488,8 +1472,7 @@ def test_changelog_config_flag_merge_prerelease_only_prerelease_present(

util.run_cli("bump", "--changelog")

with changelog_path.open() as f:
out = f.read()
out = changelog_path.read_text()

file_regression.check(out, extension=".md")

Expand Down Expand Up @@ -1554,7 +1537,6 @@ def test_changelog_merge_preserves_header(
util.create_file_and_commit("feat: new feature right before the bump")
util.run_cli("bump", "--changelog")

with changelog_path.open() as f:
out = f.read()
out = changelog_path.read_text()

file_regression.check(out, extension=".md")
Loading