forked from hyphen-2025/cyber-pilot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_init_inject.py
More file actions
158 lines (137 loc) · 6.51 KB
/
test_init_inject.py
File metadata and controls
158 lines (137 loc) · 6.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
"""
Focused unit tests for _inject_managed_block and its wrappers in commands/init.py.
Covers:
A) Valid in-root write succeeds
B) Sibling-prefix escape is rejected
C) Clearly outside-root path is rejected
D) _inject_root_agents / _inject_root_claude wrappers still work
"""
import sys
import unittest
from pathlib import Path
from tempfile import TemporaryDirectory
sys.path.insert(0, str(Path(__file__).parent.parent / "skills" / "cypilot" / "scripts"))
class TestInjectManagedBlockContainment(unittest.TestCase):
"""Path containment checks in _inject_managed_block."""
def _fn(self):
from cypilot.commands.init import _inject_managed_block
return _inject_managed_block
def test_valid_in_root_write_succeeds(self):
"""A: target directly inside project_root succeeds."""
_inject_managed_block = self._fn()
with TemporaryDirectory() as td:
project_root = Path(td) / "project"
project_root.mkdir()
target = project_root / "AGENTS.md"
result = _inject_managed_block(target, "cypilot", project_root=project_root)
self.assertEqual(result, "created")
self.assertTrue(target.is_file())
def test_sibling_prefix_escape_is_rejected(self):
"""B: /tmp/project2/AGENTS.md must not be accepted as inside /tmp/project."""
_inject_managed_block = self._fn()
with TemporaryDirectory() as td:
project_root = Path(td) / "project"
project_root.mkdir()
sibling = Path(td) / "project2"
sibling.mkdir()
target = sibling / "AGENTS.md"
with self.assertRaises(ValueError) as ctx:
_inject_managed_block(target, "cypilot", project_root=project_root)
self.assertIn("Refusing to write outside project root", str(ctx.exception))
def test_clearly_outside_root_is_rejected(self):
"""C: path in an unrelated directory must raise ValueError."""
_inject_managed_block = self._fn()
with TemporaryDirectory() as td:
project_root = Path(td) / "project"
project_root.mkdir()
other = Path(td) / "other"
other.mkdir()
target = other / "AGENTS.md"
with self.assertRaises(ValueError) as ctx:
_inject_managed_block(target, "cypilot", project_root=project_root)
self.assertIn("Refusing to write outside project root", str(ctx.exception))
def test_no_project_root_skips_validation(self):
"""project_root=None disables the check entirely."""
_inject_managed_block = self._fn()
with TemporaryDirectory() as td:
target = Path(td) / "anywhere" / "AGENTS.md"
target.parent.mkdir()
result = _inject_managed_block(target, "cypilot")
self.assertEqual(result, "created")
def test_nested_subdir_write_succeeds(self):
"""Target in a subdirectory under project_root is accepted."""
_inject_managed_block = self._fn()
with TemporaryDirectory() as td:
project_root = Path(td) / "project"
subdir = project_root / "subdir"
subdir.mkdir(parents=True)
target = subdir / "AGENTS.md"
result = _inject_managed_block(target, "cypilot", project_root=project_root)
self.assertEqual(result, "created")
class TestInjectRootWrappers(unittest.TestCase):
"""D: _inject_root_agents and _inject_root_claude still work correctly."""
def test_inject_root_agents_creates(self):
from cypilot.commands.init import _inject_root_agents
with TemporaryDirectory() as td:
root = Path(td) / "proj"
root.mkdir()
result = _inject_root_agents(root, "cypilot")
self.assertEqual(result, "created")
agents = root / "AGENTS.md"
self.assertTrue(agents.is_file())
self.assertIn('cypilot_path = "cypilot"', agents.read_text())
def test_inject_root_agents_updates(self):
from cypilot.commands.init import _inject_root_agents, MARKER_START, MARKER_END
with TemporaryDirectory() as td:
root = Path(td) / "proj"
root.mkdir()
agents = root / "AGENTS.md"
agents.write_text(
f"{MARKER_START}\n```toml\ncypilot_path = \"old\"\n```\n{MARKER_END}\n",
encoding="utf-8",
)
result = _inject_root_agents(root, "newdir")
self.assertEqual(result, "updated")
self.assertIn('cypilot_path = "newdir"', agents.read_text())
def test_inject_root_agents_unchanged(self):
from cypilot.commands.init import _inject_root_agents, _compute_managed_block
with TemporaryDirectory() as td:
root = Path(td) / "proj"
root.mkdir()
agents = root / "AGENTS.md"
agents.write_text(_compute_managed_block("cypilot") + "\n", encoding="utf-8")
result = _inject_root_agents(root, "cypilot")
self.assertEqual(result, "unchanged")
def test_inject_root_claude_creates(self):
from cypilot.commands.init import _inject_root_claude
with TemporaryDirectory() as td:
root = Path(td) / "proj"
root.mkdir()
result = _inject_root_claude(root, "cypilot")
self.assertEqual(result, "created")
claude = root / "CLAUDE.md"
self.assertTrue(claude.is_file())
self.assertIn('cypilot_path = "cypilot"', claude.read_text())
def test_inject_root_claude_updates(self):
from cypilot.commands.init import _inject_root_claude, MARKER_START, MARKER_END
with TemporaryDirectory() as td:
root = Path(td) / "proj"
root.mkdir()
claude = root / "CLAUDE.md"
claude.write_text(
f"{MARKER_START}\n```toml\ncypilot_path = \"old\"\n```\n{MARKER_END}\n",
encoding="utf-8",
)
result = _inject_root_claude(root, "newdir")
self.assertEqual(result, "updated")
self.assertIn('cypilot_path = "newdir"', claude.read_text())
def test_inject_root_agents_dry_run(self):
from cypilot.commands.init import _inject_root_agents
with TemporaryDirectory() as td:
root = Path(td) / "proj"
root.mkdir()
result = _inject_root_agents(root, "cypilot", dry_run=True)
self.assertEqual(result, "created")
self.assertFalse((root / "AGENTS.md").exists())
if __name__ == "__main__":
unittest.main()