Skip to content

Commit

Permalink
Added tests for utils.is_subpath
Browse files Browse the repository at this point in the history
  • Loading branch information
coordt committed Jan 26, 2025
1 parent 51a8b47 commit 4e993ed
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
1 change: 0 additions & 1 deletion bumpversion/scm/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,6 @@ def _update_from_latest_tag_info(self, latest_tag_info: LatestTagInfo):

def path_in_repo(self, path: Path | str) -> bool:
"""Return whether a path is inside this repository."""
print(path, self.repository_root)
if self.repository_root is None:
return True
elif not Path(path).is_absolute():
Expand Down
27 changes: 27 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
"""Tests for the utils module."""

from itertools import combinations
from pathlib import Path

import pytest
from pytest import param
from bumpversion import utils


Expand Down Expand Up @@ -131,3 +133,28 @@ def test_returns_empty_string_when_no_flags(self):

# Assert
assert actual_flags == expected


@pytest.mark.parametrize(
["parent", "path", "expected"],
[
param(Path("/parent"), Path("/parent/subpath"), True, id="absolute_path_within_parent_returns_true"),
param("/parent", Path("/parent/subpath"), True, id="parent_as_string_path_as_path_1"),
param(Path("/parent"), "/parent/subpath", True, id="parent_as_path_path_as_string_1"),
param(Path("/parent"), Path("/other/subpath"), False, id="absolute_path_outside_of_parent_returns_false"),
param("/parent", Path("/other/subpath"), False, id="parent_as_string_path_as_path_2"),
param(Path("/parent"), "/other/subpath", False, id="parent_as_path_path_as_string_2"),
param(Path("/parent"), Path("/parent"), True, id="identical_absolute_paths_return_true"),
param("/parent", Path("/parent"), True, id="parent_as_string_path_as_path_3"),
param(Path("/parent"), "/parent", True, id="parent_as_path_path_as_string_3"),
param(Path("/parent"), Path("relative/path"), True, id="relative_path_returns_true"),
param("/parent", Path("relative/path"), True, id="parent_as_string_path_as_path_4"),
param(Path("/parent"), "relative/path", True, id="parent_as_path_path_as_string_4"),
],
)
def test_is_subpath(parent: Path | str, path: Path | str, expected: bool) -> None:
"""Test the is_subpath function."""
# Act
result = utils.is_subpath(parent, path)
# Assert
assert result is expected

0 comments on commit 4e993ed

Please sign in to comment.