"""Tests for cli.gittool module.""" from __future__ import annotations from pathlib import Path from unittest.mock import patch import pytest import pyflowx as px from pyflowx.cli._ops import dev # ---------------------------------------------------------------------- # # not_has_git_repo # ---------------------------------------------------------------------- # class TestNotHasGitRepo: """Test not_has_git_repo function.""" def test_not_has_git_repo_true(self, tmp_path: Path) -> None: """Should return True when no .git directory.""" with patch.object(Path, "cwd", return_value=tmp_path): result = dev.not_has_git_repo() assert result is True def test_not_has_git_repo_false(self, tmp_path: Path) -> None: """Should return False when .git directory exists.""" git_dir = tmp_path / ".git" git_dir.mkdir() with patch.object(Path, "cwd", return_value=tmp_path): result = dev.not_has_git_repo() assert result is False def test_not_has_git_repo_cwd_not_exists(self, tmp_path: Path) -> None: """Should return True when cwd doesn't exist.""" nonexistent = tmp_path / "nonexistent" with patch.object(Path, "cwd", return_value=nonexistent): result = dev.not_has_git_repo() assert result is True # ---------------------------------------------------------------------- # # has_files # ---------------------------------------------------------------------- # class TestHasFiles: """Test has_files function.""" def test_has_files_true(self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: """Should return True when there are uncommitted changes.""" class _FakeResult: stdout = " M test.txt\n" monkeypatch.setattr("subprocess.run", lambda *_, **__: _FakeResult()) result = dev.has_files() assert result is True def test_has_files_false(self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: """Should return False when no uncommitted changes.""" class _FakeResult: stdout = "" monkeypatch.setattr("subprocess.run", lambda *_, **__: _FakeResult()) result = dev.has_files() assert result is False # ---------------------------------------------------------------------- # # init_sub_dirs # ---------------------------------------------------------------------- # class TestInitSubDirs: """Test init_sub_dirs function.""" def test_init_sub_dirs_with_subdirectories(self, tmp_path: Path) -> None: """Should initialize git in subdirectories.""" subdir1 = tmp_path / "subdir1" subdir1.mkdir() subdir2 = tmp_path / "subdir2" subdir2.mkdir() with patch.object(Path, "cwd", return_value=tmp_path), patch.object(px, "run") as mock_run: dev.init_sub_dirs() # Should call px.run for each subdirectory assert mock_run.call_count == 2 def test_init_sub_dirs_no_subdirectories(self, tmp_path: Path) -> None: """Should handle no subdirectories.""" with patch.object(Path, "cwd", return_value=tmp_path), patch.object(px, "run") as mock_run: dev.init_sub_dirs() # Should not call px.run assert mock_run.call_count == 0