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

Tests for comitting files #221

Merged
merged 4 commits into from
Aug 7, 2024
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
4 changes: 4 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ repos:
- id: mypy
args: [--no-strict-optional, --ignore-missing-imports]
additional_dependencies: ["pydantic>2.0", "toml"]
exclude: |
(?x)^(
test.*
)$
- repo: https://github.com/jsh9/pydoclint
rev: 0.5.6
hooks:
Expand Down
2 changes: 1 addition & 1 deletion bumpversion/aliases.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class AliasedGroup(RichGroup):
If there were a command called ``push``, it would accept ``pus`` as an alias (so long as it was unique)
"""

def get_command(self, ctx: Context, cmd_name: str) -> Optional[click.Command]:
def get_command(self, ctx: Context, cmd_name: str) -> Optional[click.Command]: # type: ignore[return]
"""Given a context and a command name, this returns a Command object if it exists or returns None."""
rv = click.Group.get_command(self, ctx, cmd_name)
if rv is not None:
Expand Down
2 changes: 2 additions & 0 deletions bumpversion/scm.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ def path_in_repo(self, path: Union[Path, str]) -> bool:
"""Return whether a path is inside this repository."""
if self.repository_root is None:
return True
elif not Path(path).is_absolute():
return True

return str(path).startswith(str(self.repository_root))

Expand Down
93 changes: 93 additions & 0 deletions tests/test_bump.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from bumpversion.exceptions import ConfigurationError, VersionNotFoundError
from bumpversion.files import ConfiguredFile
from bumpversion.scm import Git, SCMInfo
from bumpversion.utils import run_command
from tests.conftest import get_config_data, inside_dir


Expand Down Expand Up @@ -353,3 +354,95 @@ def test_key_path_required_for_toml_change(tmp_path: Path, caplog):
ignore-words-list = "sugar, salt, flour"
"""
)


def test_changes_to_files_are_committed(git_repo: Path, caplog):
"""Any files changed during the bump are committed."""
from bumpversion import config

# Arrange
config_path = git_repo / ".bumpversion.toml"
config_path.write_text(
dedent(
"""
[tool.bumpversion]
current_version = "0.1.26"
tag_name = "{new_version}"
commit = true

[[tool.bumpversion.files]]
glob = "helm/charts/*/Chart.yaml"

"""
),
encoding="utf-8",
)
chart_contents = dedent(
"""
appVersion: 0.1.26
version: 0.1.26

"""
)
chart1_path = git_repo / "helm" / "charts" / "somechart" / "Chart.yaml"
chart2_path = git_repo / "helm" / "charts" / "otherchart" / "Chart.yaml"
chart1_path.parent.mkdir(parents=True, exist_ok=True)
chart1_path.write_text(chart_contents, encoding="utf-8")
chart2_path.parent.mkdir(parents=True, exist_ok=True)
chart2_path.write_text(chart_contents, encoding="utf-8")

with inside_dir(git_repo):
run_command(["git", "add", str(chart1_path), str(chart2_path), str(config_path)])
run_command(["git", "commit", "-m", "Initial commit"])
run_command(["git", "tag", "0.1.26"])

# Act
from click.testing import CliRunner, Result
from bumpversion import cli

runner: CliRunner = CliRunner()
with inside_dir(git_repo):
result: Result = runner.invoke(
cli.cli,
["bump", "-vv", "minor"],
)

if result.exit_code != 0:
print(caplog.text)
print("Here is the output:")
print(result.output)
import traceback

print(traceback.print_exception(result.exc_info[1]))

# Assert
assert result.exit_code == 0
assert config_path.read_text() == dedent(
"""
[tool.bumpversion]
current_version = "0.2.0"
tag_name = "{new_version}"
commit = true

[[tool.bumpversion.files]]
glob = "helm/charts/*/Chart.yaml"

"""
)
assert chart1_path.read_text() == dedent(
"""
appVersion: 0.2.0
version: 0.2.0

"""
)
assert chart2_path.read_text() == dedent(
"""
appVersion: 0.2.0
version: 0.2.0

"""
)
with inside_dir(git_repo):
status = run_command(["git", "status", "--porcelain"])
assert status.stdout == ""
Loading