working version
This commit is contained in:
@@ -1,80 +1,75 @@
|
||||
"""Tests for flow.commands.dotfiles — link/unlink/status logic."""
|
||||
|
||||
import json
|
||||
from pathlib import Path
|
||||
"""Tests for flow.commands.dotfiles discovery and path resolution."""
|
||||
|
||||
import pytest
|
||||
|
||||
from flow.commands.dotfiles import _discover_packages, _resolve_edit_target, _walk_package
|
||||
from flow.core.config import AppConfig, FlowContext
|
||||
from flow.core.console import ConsoleLogger
|
||||
from flow.core.platform import PlatformInfo
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def dotfiles_tree(tmp_path):
|
||||
"""Create a sample dotfiles directory structure."""
|
||||
common = tmp_path / "common"
|
||||
(common / "zsh").mkdir(parents=True)
|
||||
(common / "zsh" / ".zshrc").write_text("# zshrc")
|
||||
(common / "zsh" / ".zshenv").write_text("# zshenv")
|
||||
(common / "tmux").mkdir(parents=True)
|
||||
(common / "tmux" / ".tmux.conf").write_text("# tmux")
|
||||
def _make_tree(tmp_path):
|
||||
flow_root = tmp_path
|
||||
shared = flow_root / "_shared"
|
||||
(shared / "zsh").mkdir(parents=True)
|
||||
(shared / "zsh" / ".zshrc").write_text("# zsh")
|
||||
(shared / "tmux").mkdir(parents=True)
|
||||
(shared / "tmux" / ".tmux.conf").write_text("# tmux")
|
||||
|
||||
profiles = tmp_path / "profiles" / "work"
|
||||
(profiles / "git").mkdir(parents=True)
|
||||
(profiles / "git" / ".gitconfig").write_text("[user]\nname = Work")
|
||||
profile = flow_root / "work"
|
||||
(profile / "git").mkdir(parents=True)
|
||||
(profile / "git" / ".gitconfig").write_text("[user]\nname = Work")
|
||||
|
||||
return tmp_path
|
||||
|
||||
|
||||
def test_discover_packages_common(dotfiles_tree):
|
||||
packages = _discover_packages(dotfiles_tree)
|
||||
def test_discover_packages_shared_only(tmp_path):
|
||||
tree = _make_tree(tmp_path)
|
||||
packages = _discover_packages(tree)
|
||||
assert "zsh" in packages
|
||||
assert "tmux" in packages
|
||||
assert "git" not in packages # git is only in profiles
|
||||
assert "git" not in packages
|
||||
|
||||
|
||||
def test_discover_packages_with_profile(dotfiles_tree):
|
||||
packages = _discover_packages(dotfiles_tree, profile="work")
|
||||
def test_discover_packages_with_profile(tmp_path):
|
||||
tree = _make_tree(tmp_path)
|
||||
packages = _discover_packages(tree, profile="work")
|
||||
assert "zsh" in packages
|
||||
assert "tmux" in packages
|
||||
assert "git" in packages
|
||||
|
||||
|
||||
def test_discover_packages_profile_overrides(dotfiles_tree):
|
||||
# Add zsh to work profile
|
||||
work_zsh = dotfiles_tree / "profiles" / "work" / "zsh"
|
||||
work_zsh.mkdir(parents=True)
|
||||
(work_zsh / ".zshrc").write_text("# work zshrc")
|
||||
def test_discover_packages_profile_overrides_shared(tmp_path):
|
||||
tree = _make_tree(tmp_path)
|
||||
profile_zsh = tree / "work" / "zsh"
|
||||
profile_zsh.mkdir(parents=True)
|
||||
(profile_zsh / ".zshrc").write_text("# work zsh")
|
||||
|
||||
packages = _discover_packages(dotfiles_tree, profile="work")
|
||||
# Profile should override common
|
||||
assert packages["zsh"] == work_zsh
|
||||
with pytest.raises(RuntimeError, match="Conflicting dotfile targets"):
|
||||
from flow.commands.dotfiles import _collect_home_specs
|
||||
_collect_home_specs(tree, tmp_path / "home", "work", set(), None)
|
||||
|
||||
|
||||
def test_walk_package(dotfiles_tree):
|
||||
home = Path("/tmp/fakehome")
|
||||
source = dotfiles_tree / "common" / "zsh"
|
||||
pairs = list(_walk_package(source, home))
|
||||
assert len(pairs) == 2
|
||||
sources = {str(s.name) for s, _ in pairs}
|
||||
assert ".zshrc" in sources
|
||||
assert ".zshenv" in sources
|
||||
targets = {str(t) for _, t in pairs}
|
||||
assert str(home / ".zshrc") in targets
|
||||
assert str(home / ".zshenv") in targets
|
||||
def test_walk_package_returns_relative_paths(tmp_path):
|
||||
tree = _make_tree(tmp_path)
|
||||
source = tree / "_shared" / "zsh"
|
||||
|
||||
pairs = list(_walk_package(source))
|
||||
assert len(pairs) == 1
|
||||
src, rel = pairs[0]
|
||||
assert src.name == ".zshrc"
|
||||
assert str(rel) == ".zshrc"
|
||||
|
||||
|
||||
def test_resolve_edit_target_package(dotfiles_tree):
|
||||
target = _resolve_edit_target("zsh", dotfiles_dir=dotfiles_tree)
|
||||
assert target == dotfiles_tree / "common" / "zsh"
|
||||
def test_resolve_edit_target_package(tmp_path):
|
||||
tree = _make_tree(tmp_path)
|
||||
target = _resolve_edit_target("zsh", dotfiles_dir=tree)
|
||||
assert target == tree / "_shared" / "zsh"
|
||||
|
||||
|
||||
def test_resolve_edit_target_repo_path(dotfiles_tree):
|
||||
target = _resolve_edit_target("common/zsh/.zshrc", dotfiles_dir=dotfiles_tree)
|
||||
assert target == dotfiles_tree / "common" / "zsh" / ".zshrc"
|
||||
def test_resolve_edit_target_repo_path(tmp_path):
|
||||
tree = _make_tree(tmp_path)
|
||||
target = _resolve_edit_target("_shared/zsh/.zshrc", dotfiles_dir=tree)
|
||||
assert target == tree / "_shared" / "zsh" / ".zshrc"
|
||||
|
||||
|
||||
def test_resolve_edit_target_missing_returns_none(dotfiles_tree):
|
||||
assert _resolve_edit_target("does-not-exist", dotfiles_dir=dotfiles_tree) is None
|
||||
def test_resolve_edit_target_missing_returns_none(tmp_path):
|
||||
tree = _make_tree(tmp_path)
|
||||
assert _resolve_edit_target("does-not-exist", dotfiles_dir=tree) is None
|
||||
|
||||
Reference in New Issue
Block a user