Skip to content

Commit 37be50c

Browse files
thrixclaude
andauthored
Fix handling of '.' as tree_path in get_tree function (#20)
When tree_path is '.', the current code constructs invalid paths like '/path/to/repo./' which causes "Invalid directory path" errors. This fix adds special handling for '.' (current directory) to avoid path modification while still handling .git suffix removal properly. Fixes #19 🤖 Generated with [Claude Code](https://claude.ai/code) --------- Signed-off-by: Miroslav Vadkerti <mvadkert@redhat.com> Co-authored-by: Claude <noreply@anthropic.com>
1 parent 357007a commit 37be50c

2 files changed

Lines changed: 38 additions & 4 deletions

File tree

src/tmt_web/service.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,13 @@ def get_tree(url: str, name: str, ref: str | None, tree_path: str | None) -> tmt
4242
except Exception as exc:
4343
raise GeneralError(f"Failed to clone repository: {exc}") from exc
4444

45-
if tree_path is not None:
45+
# Remove .git suffix if present
46+
if path.suffix == ".git":
47+
path = path.with_suffix("")
48+
49+
# If path is set, construct a path to the tmt Tree
50+
if tree_path is not None and tree_path != ".":
4651
tree_path += "/"
47-
# If path is set, construct a path to the tmt Tree
48-
if path.suffix == ".git":
49-
path = path.with_suffix("")
5052
path = Path(path.as_posix() + tree_path)
5153

5254
logger.debug(f"Looking for tree in {path}")

tests/unit/test_service.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,38 @@ def test_get_tree_with_git_suffix(mocker):
100100
mock_path.with_suffix.assert_called_once_with("")
101101

102102

103+
def test_get_tree_with_dot_path(mocker):
104+
"""Test get_tree with '.' as tree_path (current directory)."""
105+
mock_path = mocker.Mock()
106+
mock_path.suffix = ""
107+
mock_path.with_suffix.return_value = mock_path
108+
mock_path.as_posix.return_value = "/path/to/repo"
109+
mocker.patch("tmt_web.utils.git_handler.get_git_repository", return_value=mock_path)
110+
mocker.patch("tmt.base.Tree")
111+
mocker.patch("tmt.plugins.explore")
112+
113+
get_tree("url", "test", None, ".")
114+
# Should not call as_posix() when tree_path is "."
115+
mock_path.as_posix.assert_not_called()
116+
117+
118+
def test_get_tree_with_dot_path_and_git_suffix(mocker):
119+
"""Test get_tree with '.' as tree_path and .git suffix."""
120+
mock_path = mocker.Mock()
121+
mock_path.suffix = ".git"
122+
mock_path.with_suffix.return_value = mock_path
123+
mock_path.as_posix.return_value = "/path/to/repo"
124+
mocker.patch("tmt_web.utils.git_handler.get_git_repository", return_value=mock_path)
125+
mocker.patch("tmt.base.Tree")
126+
mocker.patch("tmt.plugins.explore")
127+
128+
get_tree("url", "test", None, ".")
129+
# Should still strip .git suffix when tree_path is "."
130+
mock_path.with_suffix.assert_called_once_with("")
131+
# Should not call as_posix() when tree_path is "."
132+
mock_path.as_posix.assert_not_called()
133+
134+
103135
def test_format_data_unsupported_format(mocker, logger):
104136
"""Test format_data with unsupported format."""
105137
test_data = TestData(name="test")

0 commit comments

Comments
 (0)