|
| 1 | +"""Tests for the auto-gitignore behavior of `solo-mise init`.""" |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +from solo_mise import init as init_mod |
| 7 | + |
| 8 | + |
| 9 | +def _read_gi(target: Path) -> str: |
| 10 | + return (target / ".gitignore").read_text() |
| 11 | + |
| 12 | + |
| 13 | +def test_init_creates_gitignore_when_missing(tmp_target: Path): |
| 14 | + rc = init_mod.run(target=tmp_target, profile_id="repo") |
| 15 | + assert rc == 0 |
| 16 | + gi = _read_gi(tmp_target) |
| 17 | + assert init_mod.GITIGNORE_BEGIN in gi |
| 18 | + assert init_mod.GITIGNORE_END in gi |
| 19 | + assert ".claude/memory-handoffs/*" in gi |
| 20 | + assert "!.claude/memory-handoffs/TEMPLATE.md" in gi |
| 21 | + assert "memory/handoff-inbox/" in gi |
| 22 | + |
| 23 | + |
| 24 | +def test_init_appends_block_to_existing_gitignore(tmp_target: Path): |
| 25 | + tmp_target.mkdir() |
| 26 | + pre_existing = "# project rules\n*.log\n.env\n" |
| 27 | + (tmp_target / ".gitignore").write_text(pre_existing) |
| 28 | + rc = init_mod.run(target=tmp_target, profile_id="repo") |
| 29 | + assert rc == 0 |
| 30 | + gi = _read_gi(tmp_target) |
| 31 | + assert pre_existing.strip() in gi, "should preserve existing rules" |
| 32 | + assert init_mod.GITIGNORE_BEGIN in gi |
| 33 | + assert init_mod.GITIGNORE_END in gi |
| 34 | + # block appears exactly once |
| 35 | + assert gi.count(init_mod.GITIGNORE_BEGIN) == 1 |
| 36 | + assert gi.count(init_mod.GITIGNORE_END) == 1 |
| 37 | + |
| 38 | + |
| 39 | +def test_init_idempotent_replaces_block(tmp_target: Path): |
| 40 | + rc = init_mod.run(target=tmp_target, profile_id="repo") |
| 41 | + assert rc == 0 |
| 42 | + first = _read_gi(tmp_target) |
| 43 | + |
| 44 | + # Tamper with the block to confirm replacement happens, not append. |
| 45 | + tampered = first.replace(".claude/memory-handoffs/*", "GARBAGE_LINE") |
| 46 | + (tmp_target / ".gitignore").write_text(tampered) |
| 47 | + |
| 48 | + rc = init_mod.run(target=tmp_target, profile_id="repo", force=True) |
| 49 | + assert rc == 0 |
| 50 | + second = _read_gi(tmp_target) |
| 51 | + assert ".claude/memory-handoffs/*" in second |
| 52 | + assert "GARBAGE_LINE" not in second |
| 53 | + # still exactly one block |
| 54 | + assert second.count(init_mod.GITIGNORE_BEGIN) == 1 |
| 55 | + |
| 56 | + |
| 57 | +def test_init_preserves_user_edits_outside_block(tmp_target: Path): |
| 58 | + tmp_target.mkdir() |
| 59 | + pre = "node_modules/\n# user rules\n*.swp\n" |
| 60 | + (tmp_target / ".gitignore").write_text(pre) |
| 61 | + init_mod.run(target=tmp_target, profile_id="repo") |
| 62 | + # Add user content AFTER the block; re-run should keep it. |
| 63 | + gi_text = _read_gi(tmp_target) |
| 64 | + gi_text += "\n# after block\n.local-cache/\n" |
| 65 | + (tmp_target / ".gitignore").write_text(gi_text) |
| 66 | + |
| 67 | + init_mod.run(target=tmp_target, profile_id="repo", force=True) |
| 68 | + final = _read_gi(tmp_target) |
| 69 | + assert "node_modules/" in final |
| 70 | + assert "*.swp" in final |
| 71 | + assert ".local-cache/" in final |
| 72 | + |
| 73 | + |
| 74 | +def test_no_gitignore_flag_skips_creation(tmp_target: Path): |
| 75 | + rc = init_mod.run(target=tmp_target, profile_id="repo", update_gitignore=False) |
| 76 | + assert rc == 0 |
| 77 | + assert not (tmp_target / ".gitignore").exists() |
| 78 | + |
| 79 | + |
| 80 | +def test_no_gitignore_flag_leaves_existing_alone(tmp_target: Path): |
| 81 | + tmp_target.mkdir() |
| 82 | + pre = "# only my rules\n*.tmp\n" |
| 83 | + (tmp_target / ".gitignore").write_text(pre) |
| 84 | + rc = init_mod.run( |
| 85 | + target=tmp_target, profile_id="repo", update_gitignore=False |
| 86 | + ) |
| 87 | + assert rc == 0 |
| 88 | + assert _read_gi(tmp_target) == pre |
| 89 | + |
| 90 | + |
| 91 | +def test_dry_run_does_not_write_gitignore(tmp_target: Path): |
| 92 | + rc = init_mod.run(target=tmp_target, profile_id="repo", dry_run=True) |
| 93 | + assert rc == 0 |
| 94 | + assert not (tmp_target / ".gitignore").exists() |
| 95 | + assert not tmp_target.exists() |
| 96 | + |
| 97 | + |
| 98 | +def test_workspace_profile_also_adds_block(tmp_target: Path): |
| 99 | + rc = init_mod.run(target=tmp_target, profile_id="workspace") |
| 100 | + assert rc == 0 |
| 101 | + gi = _read_gi(tmp_target) |
| 102 | + assert init_mod.GITIGNORE_BEGIN in gi |
| 103 | + assert "memory/handoff-inbox/" in gi |
0 commit comments