Repository navigation
Expand file tree
/
Copy pathtest_resolve_input.py
More file actions
345 lines (281 loc) · 12.6 KB
/
Copy pathtest_resolve_input.py
File metadata and controls
345 lines (281 loc) · 12.6 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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Tests for resolve_input node."""
from __future__ import annotations
import io
import tempfile
import zipfile
from pathlib import Path
import httpx
import pytest
from skillspector.input_handler import TransitiveIngestTruncatedError
from skillspector.nodes.resolve_input import resolve_input
def test_resolve_input_with_input_path_directory(tmp_path: Path) -> None:
"""When input_path is a local directory, skill_path is set; temp_dir_for_cleanup is None."""
(tmp_path / "SKILL.md").write_text("# Test", encoding="utf-8")
state = {"input_path": str(tmp_path)}
update = resolve_input(state)
assert update["skill_path"] == str(tmp_path.resolve())
assert update.get("temp_dir_for_cleanup") is None
assert update.get("selected_source_identity") == tmp_path.name
def test_resolve_input_with_skill_path_only(tmp_path: Path) -> None:
"""When only skill_path is set, it is normalized; temp_dir_for_cleanup is None."""
(tmp_path / "SKILL.md").write_text("# Test", encoding="utf-8")
state = {"skill_path": str(tmp_path)}
update = resolve_input(state)
assert update["skill_path"] == str(tmp_path.resolve())
assert update.get("temp_dir_for_cleanup") is None
assert update.get("selected_source_identity") == tmp_path.name
def test_resolve_input_rejects_skill_path_with_symlinked_parent(tmp_path: Path) -> None:
"""The skill_path-only route must enforce the same symlink policy as input_path."""
external_skill = tmp_path / "external" / "skill"
external_skill.mkdir(parents=True)
(external_skill / "SKILL.md").write_text("# External skill", encoding="utf-8")
symlinked_parent = tmp_path / "linked"
try:
symlinked_parent.symlink_to(external_skill.parent, target_is_directory=True)
except OSError:
pytest.skip("symlinks are not supported on this filesystem")
with pytest.raises(ValueError, match="symlinked parent"):
resolve_input({"skill_path": str(symlinked_parent / external_skill.name)})
def test_resolve_input_prefers_input_path_over_skill_path(tmp_path: Path) -> None:
"""When both are set, input_path wins."""
(tmp_path / "SKILL.md").write_text("# Test", encoding="utf-8")
state = {"input_path": str(tmp_path), "skill_path": "/other/path"}
update = resolve_input(state)
assert update["skill_path"] == str(tmp_path.resolve())
def test_resolve_input_empty_input_returns_none_skill_path() -> None:
"""When neither input_path nor skill_path is set (or empty), skill_path becomes None."""
update = resolve_input({})
assert update["skill_path"] is None
assert update.get("temp_dir_for_cleanup") is None
update2 = resolve_input({"input_path": " ", "skill_path": ""})
assert update2["skill_path"] is None
def test_workflow_budget_starts_before_input_materialization(
monkeypatch: pytest.MonkeyPatch, tmp_path: Path
) -> None:
"""Resolve, context construction, and analyzers reuse one already-started clock."""
captured: list[object] = []
class CapturingHandler:
primary_file_path = None
primary_file_parent_path = None
def __init__(
self, transitive_budget: object | None = None, *, allow_git_credentials: bool = True
) -> None:
assert allow_git_credentials is True
assert transitive_budget is not None
assert getattr(transitive_budget, "started_at", None) is not None
captured.append(transitive_budget)
def resolve(self, _input_path: str) -> tuple[Path, str]:
return tmp_path, "directory"
def temp_dir_for_cleanup(self) -> None:
return None
def cleanup(self) -> None:
pass
monkeypatch.setattr("skillspector.nodes.resolve_input.InputHandler", CapturingHandler)
update = resolve_input({"input_path": str(tmp_path)})
assert update["workflow_resource_budget"] is captured[0]
def test_transitive_truncation_is_typed_sanitized_and_cleaned(
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""The node never converts a truncated remote child into a clean empty input."""
cleaned: list[bool] = []
class TruncatedHandler:
def __init__(
self, transitive_budget: object | None = None, *, allow_git_credentials: bool = True
) -> None:
assert allow_git_credentials is True
assert transitive_budget is not None
def resolve(self, _input_path: str) -> tuple[Path, str]:
raise TransitiveIngestTruncatedError("byte_budget_exhausted", "download")
def cleanup(self) -> None:
cleaned.append(True)
monkeypatch.setattr("skillspector.nodes.resolve_input.InputHandler", TruncatedHandler)
with pytest.raises(TransitiveIngestTruncatedError) as raised:
resolve_input(
{
"input_path": "https://raw.githubusercontent.com/private/source",
"transitive_traversal_state": object(),
}
)
assert raised.value.truncation.as_dict() == {
"code": "byte_budget_exhausted",
"source_type": "download",
"message": "Transitive download ingest truncated (byte_budget_exhausted)",
}
assert cleaned == [True]
assert "private/source" not in str(raised.value)
def test_selected_source_identity_from_resolved_git_and_archive_inputs() -> None:
"""Identity follows successful materialization, including preserved archive roots."""
from skillspector.input_handler import selected_source_identity_for_input
temp_dir = Path("/tmp/skillspector_abc")
clone_root = temp_dir / "repo"
assert (
selected_source_identity_for_input(
"https://github.com/acme/example-skill.git",
source_type="git",
resolved_path=clone_root,
temp_dir=temp_dir,
)
== "example-skill"
)
assert (
selected_source_identity_for_input(
"git@github.com:acme/example-skill.git",
source_type="git",
resolved_path=clone_root,
temp_dir=temp_dir,
)
== "example-skill"
)
assert (
selected_source_identity_for_input(
"/tmp/packs/example-skill.zip",
source_type="zip",
resolved_path=temp_dir / "extracted",
temp_dir=temp_dir,
)
== "example-skill"
)
assert (
selected_source_identity_for_input(
"/tmp/packs/peer-skill.zip",
source_type="zip",
resolved_path=temp_dir / "extracted" / "example-skill",
temp_dir=temp_dir,
)
== "example-skill"
)
assert (
selected_source_identity_for_input(
"/tmp/skillspector_abc/repo",
source_type="directory",
resolved_path=clone_root,
temp_dir=None,
)
== "repo"
)
@pytest.mark.parametrize("source", ["local-zip", "downloaded-zip"])
def test_failed_materialization_removes_the_handler_temp_dir(
monkeypatch: pytest.MonkeyPatch, tmp_path: Path, source: str
) -> None:
"""An input that fails to materialize leaves no temp directory or download behind."""
archive = io.BytesIO()
with zipfile.ZipFile(archive, "w") as bundle:
bundle.writestr("SKILL.md", "# Skill\n")
bundle.writestr("../outside.md", "escape\n")
created: list[Path] = []
real_mkdtemp = tempfile.mkdtemp
def mkdtemp(*args: str, **kwargs: str) -> str:
path = real_mkdtemp(*args, dir=tmp_path, **kwargs)
created.append(Path(path))
return path
monkeypatch.setattr("skillspector.input_handler.tempfile.mkdtemp", mkdtemp)
if source == "local-zip":
local_zip = tmp_path / "slip.zip"
local_zip.write_bytes(archive.getvalue())
input_path = str(local_zip)
else:
real_client = httpx.Client
transport = httpx.MockTransport(
lambda _request: httpx.Response(200, content=archive.getvalue())
)
monkeypatch.setattr(
"skillspector.input_handler.httpx.Client",
lambda *args, **kwargs: real_client(*args, transport=transport, **kwargs),
)
monkeypatch.setattr("skillspector.input_handler._is_private_ip", lambda _host: False)
input_path = "https://raw.githubusercontent.com/org/repo/main/skill.zip"
with pytest.raises(ValueError, match="zip-slip"):
resolve_input({"input_path": input_path})
assert created
assert [path for path in created if path.exists()] == []
def test_interrupted_extraction_removes_the_handler_temp_dir(
monkeypatch: pytest.MonkeyPatch, tmp_path: Path
) -> None:
"""An interrupt after the download landed in the temp directory leaves nothing behind."""
archive = io.BytesIO()
with zipfile.ZipFile(archive, "w") as bundle:
bundle.writestr("SKILL.md", "# Skill\n")
created: list[Path] = []
real_mkdtemp = tempfile.mkdtemp
def mkdtemp(*args: str, **kwargs: str) -> str:
path = real_mkdtemp(*args, dir=tmp_path, **kwargs)
created.append(Path(path))
return path
def interrupted_extract(_self: object, zip_path: Path) -> Path:
assert zip_path.exists()
raise KeyboardInterrupt
real_client = httpx.Client
transport = httpx.MockTransport(
lambda _request: httpx.Response(200, content=archive.getvalue())
)
monkeypatch.setattr("skillspector.input_handler.tempfile.mkdtemp", mkdtemp)
monkeypatch.setattr(
"skillspector.input_handler.httpx.Client",
lambda *args, **kwargs: real_client(*args, transport=transport, **kwargs),
)
monkeypatch.setattr("skillspector.input_handler._is_private_ip", lambda _host: False)
monkeypatch.setattr("skillspector.input_handler.InputHandler._extract_zip", interrupted_extract)
with pytest.raises(KeyboardInterrupt):
resolve_input({"input_path": "https://raw.githubusercontent.com/org/repo/main/skill.zip"})
assert created
assert [path for path in created if path.exists()] == []
def test_interrupted_clone_stops_git_and_removes_the_handler_temp_dir(
monkeypatch: pytest.MonkeyPatch, tmp_path: Path
) -> None:
"""An interrupt while Git is cloning terminates the child before the tree is removed."""
created: list[Path] = []
real_mkdtemp = tempfile.mkdtemp
def mkdtemp(*args: str, **kwargs: str) -> str:
path = real_mkdtemp(*args, dir=tmp_path, **kwargs)
created.append(Path(path))
return path
class InterruptedClone:
instances: list[InterruptedClone] = []
def __init__(self, command: list[str], **_kwargs: object) -> None:
self.clone_dir = Path(command[-1])
self.polls = 0
self.terminated = False
InterruptedClone.instances.append(self)
def poll(self) -> int | None:
self.polls += 1
if self.polls == 1:
# Git has started writing when the interrupt arrives.
(self.clone_dir / ".git").mkdir(parents=True)
raise KeyboardInterrupt
return None
def terminate(self) -> None:
self.terminated = True
def kill(self) -> None:
self.terminated = True
def wait(self, timeout: float | None = None) -> int:
return 0
monkeypatch.setattr("skillspector.input_handler.tempfile.mkdtemp", mkdtemp)
monkeypatch.setattr("skillspector.input_handler.subprocess.Popen", InterruptedClone)
monkeypatch.setattr("skillspector.input_handler._is_private_ip", lambda _host: False)
with pytest.raises(KeyboardInterrupt):
resolve_input({"input_path": "https://github.com/org/repo.git"})
assert [clone.terminated for clone in InterruptedClone.instances] == [True]
assert created
assert [path for path in created if path.exists()] == []
def test_resolve_input_preserves_anonymous_git_policy() -> None:
with pytest.raises(ValueError, match="unauthenticated HTTPS"):
resolve_input(
{
"input_path": "git@github.com:org/private.git",
"allow_git_credentials": False,
}
)