-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.py
More file actions
executable file
·254 lines (211 loc) · 10.4 KB
/
Copy pathtest.py
File metadata and controls
executable file
·254 lines (211 loc) · 10.4 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import os
import tempfile
import unittest
from pathlib import Path
from unittest import mock
from dcsm import (
GITIGNORE_BEGIN,
GITIGNORE_END,
DCSMTemplate,
Files,
find_proximate_gitignore,
gitignore,
update_gitignore,
)
class TestDCSMTemplate(unittest.TestCase):
def test_braced_pattern(self) -> None:
template = DCSMTemplate("Value: $DCSM{var}")
self.assertEqual(template.substitute(var="123"), "Value: 123")
def test_named_pattern(self) -> None:
template = DCSMTemplate("Name: $DCSM_NAME")
self.assertEqual(template.substitute(NAME="John"), "Name: John")
def test_escaped_braced(self) -> None:
template = DCSMTemplate("Escaped: $$DCSM{VAR}")
self.assertEqual(template.substitute(), "Escaped: $DCSM{VAR}")
def test_notpattern(self) -> None:
template = DCSMTemplate("Not a pattern: $DCSMVAR")
self.assertEqual(template.substitute(), "Not a pattern: $DCSMVAR")
def test_escaped_named(self) -> None:
template = DCSMTemplate("Escaped: $$DCSM_VAR")
self.assertEqual(template.substitute(), "Escaped: $DCSM_VAR")
def test_invalid_braced(self) -> None:
template = DCSMTemplate("Invalid: $DCSM{}")
self.assertEqual(template.safe_substitute(), "Invalid: $DCSM{}")
def test_invalid_named(self) -> None:
template = DCSMTemplate("Invalid: $DCSM_name")
self.assertEqual(template.safe_substitute(), "Invalid: $DCSM_name")
class TestGitignore(unittest.TestCase):
def _touch(self, path: Path, content: str = "") -> None:
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(content)
def test_find_proximate_uses_existing_root_gitignore(self) -> None:
with tempfile.TemporaryDirectory() as td:
root = Path(td)
self._touch(root / ".gitignore", "something\n")
template = root / "sub" / "dir" / "file.env.template"
self._touch(template)
found = find_proximate_gitignore(template, root)
self.assertEqual(found, root / ".gitignore")
def test_find_proximate_prefers_closest(self) -> None:
with tempfile.TemporaryDirectory() as td:
root = Path(td)
self._touch(root / ".gitignore")
self._touch(root / "sub" / ".gitignore")
template = root / "sub" / "dir" / "file.env.template"
self._touch(template)
found = find_proximate_gitignore(template, root)
self.assertEqual(found, root / "sub" / ".gitignore")
def test_find_proximate_rejects_template_outside_root(self) -> None:
with tempfile.TemporaryDirectory() as td:
root = Path(td) / "inside"
root.mkdir()
outside = Path(td) / "elsewhere" / "file.env.template"
self._touch(outside)
with self.assertRaises(ValueError):
find_proximate_gitignore(outside, root)
def test_find_proximate_falls_back_to_template_root(self) -> None:
with tempfile.TemporaryDirectory() as td:
root = Path(td)
template = root / "sub" / "dir" / "file.env.template"
self._touch(template)
found = find_proximate_gitignore(template, root)
self.assertEqual(found, root / ".gitignore")
self.assertFalse(found.exists())
def test_find_proximate_when_template_is_in_template_root(self) -> None:
with tempfile.TemporaryDirectory() as td:
root = Path(td)
template = root / "app.env.template"
self._touch(template)
# no gitignore present -> falls back to template_root/.gitignore
self.assertEqual(
find_proximate_gitignore(template, root),
root / ".gitignore",
)
# with a gitignore present at the root, that's also what's found
self._touch(root / ".gitignore")
self.assertEqual(
find_proximate_gitignore(template, root),
root / ".gitignore",
)
def test_gitignore_command_renders_paths_relative_to_each_gitignore(self) -> None:
with tempfile.TemporaryDirectory() as td:
root = Path(td)
# root .gitignore handles templates with no closer ancestor
self._touch(root / ".gitignore")
self._touch(root / "config" / "postgres" / "init.sh.template")
# synapse has its own .gitignore, so its template stays local to it
self._touch(root / "config" / "synapse" / ".gitignore")
self._touch(root / "config" / "synapse" / "homeserver.yaml.template")
self._touch(root / "config" / "synapse" / "log.config.template")
with mock.patch.dict(
os.environ, {"DCSM_TEMPLATE_DIR": str(root)}, clear=True
):
gitignore(Files())
root_lines = (root / ".gitignore").read_text().splitlines()
synapse_lines = (
(root / "config" / "synapse" / ".gitignore").read_text().splitlines()
)
# exactly one entry in root .gitignore, with full path from root
self.assertIn("config/postgres/init.sh", root_lines)
self.assertNotIn("init.sh", root_lines)
self.assertNotIn("homeserver.yaml", root_lines)
self.assertNotIn("config/synapse/homeserver.yaml", root_lines)
# synapse entries are bare filenames (relative to that gitignore)
self.assertIn("homeserver.yaml", synapse_lines)
self.assertIn("log.config", synapse_lines)
self.assertNotIn("config/synapse/homeserver.yaml", synapse_lines)
def test_gitignore_command_collects_into_one_file_when_none_exist(self) -> None:
with tempfile.TemporaryDirectory() as td:
root = Path(td)
self._touch(root / "a" / "one.env.template")
self._touch(root / "b" / "c" / "two.yaml.template")
with mock.patch.dict(
os.environ, {"DCSM_TEMPLATE_DIR": str(root)}, clear=True
):
gitignore(Files())
# only one gitignore should be created, at the template root
self.assertTrue((root / ".gitignore").is_file())
self.assertFalse((root / "a" / ".gitignore").exists())
self.assertFalse((root / "b" / "c" / ".gitignore").exists())
content = (root / ".gitignore").read_text()
self.assertIn("a/one.env", content)
self.assertIn("b/c/two.yaml", content)
def test_update_gitignore_creates_file(self) -> None:
with tempfile.TemporaryDirectory() as td:
path = Path(td) / ".gitignore"
wrote = update_gitignore(path, ["foo.env", "bar.yaml"])
self.assertTrue(wrote)
content = path.read_text()
self.assertIn(GITIGNORE_BEGIN, content)
self.assertIn(GITIGNORE_END, content)
self.assertIn("bar.yaml", content)
self.assertIn("foo.env", content)
# entries are sorted
self.assertLess(content.index("bar.yaml"), content.index("foo.env"))
def test_update_gitignore_appends_block_to_existing(self) -> None:
with tempfile.TemporaryDirectory() as td:
path = Path(td) / ".gitignore"
path.write_text("node_modules/\n*.log\n")
update_gitignore(path, ["app.env"])
content = path.read_text()
# original content preserved
self.assertTrue(content.startswith("node_modules/\n*.log\n"))
self.assertIn(GITIGNORE_BEGIN, content)
self.assertIn("app.env", content)
def test_update_gitignore_replaces_existing_block(self) -> None:
with tempfile.TemporaryDirectory() as td:
path = Path(td) / ".gitignore"
path.write_text(f"keep-me\n\n{GITIGNORE_BEGIN}\nold.env\n{GITIGNORE_END}\n")
update_gitignore(path, ["new.env"])
content = path.read_text()
self.assertIn("keep-me", content)
self.assertNotIn("old.env", content)
self.assertIn("new.env", content)
# only one managed block
self.assertEqual(content.count(GITIGNORE_BEGIN), 1)
def test_update_gitignore_collapses_multiple_existing_blocks(self) -> None:
with tempfile.TemporaryDirectory() as td:
path = Path(td) / ".gitignore"
path.write_text(
"keep-me\n\n"
f"{GITIGNORE_BEGIN}\nstale1.env\n{GITIGNORE_END}\n"
"\n"
f"{GITIGNORE_BEGIN}\nstale2.env\n{GITIGNORE_END}\n"
"\n"
"tail-content\n"
)
update_gitignore(path, ["fresh.env"])
content = path.read_text()
self.assertIn("keep-me", content)
self.assertIn("tail-content", content)
self.assertNotIn("stale1.env", content)
self.assertNotIn("stale2.env", content)
self.assertIn("fresh.env", content)
# exactly one managed block remains
self.assertEqual(content.count(GITIGNORE_BEGIN), 1)
self.assertEqual(content.count(GITIGNORE_END), 1)
def test_update_gitignore_idempotent(self) -> None:
with tempfile.TemporaryDirectory() as td:
path = Path(td) / ".gitignore"
self.assertTrue(update_gitignore(path, ["a", "b"]))
self.assertFalse(update_gitignore(path, ["a", "b"]))
self.assertFalse(update_gitignore(path, ["b", "a"]))
def test_gitignore_command_end_to_end(self) -> None:
with tempfile.TemporaryDirectory() as td:
root = Path(td)
self._touch(root / ".gitignore", "pre-existing\n")
self._touch(root / "config" / "postgres" / "init.sh.template")
self._touch(root / "config" / "synapse" / "homeserver.yaml.template")
self._touch(root / "config" / "synapse" / ".gitignore")
with mock.patch.dict(
os.environ, {"DCSM_TEMPLATE_DIR": str(root)}, clear=True
):
gitignore(Files())
root_ignore = (root / ".gitignore").read_text()
self.assertIn("pre-existing", root_ignore)
self.assertIn("config/postgres/init.sh", root_ignore)
self.assertNotIn("homeserver.yaml", root_ignore)
synapse_ignore = (root / "config" / "synapse" / ".gitignore").read_text()
self.assertIn("homeserver.yaml", synapse_ignore)
if __name__ == "__main__":
unittest.main()