de368ea810
CI / Lint, Typecheck & Test (push) Successful in 1m11s
1. 删除 13 个已有 YAML 配置的 cli .py 入口脚本, 统一通过 pf 调用 2. gittool.yaml 用 CLEAN_EXCLUDES 数组变量配置 git clean 的 -e 参数, 保留 .venv/.tox/node_modules/.idea 等目录避免误删 3. run_cli 执行前打印调用信息: [gittool] 执行: c 4. 更新 pyproject.toml 移除 13 个冗余 entry points, 仅保留 pf 5. 清理测试文件中的 TestMain 类 (测 _ops 模块的测试保留)
95 lines
3.3 KiB
Python
95 lines
3.3 KiB
Python
"""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
|