Skip to content

Commit 1ec2e25

Browse files
badGarnetclaude
andcommitted
test: assert orphan cleanup against the path production created
The orphan checks asserted that a directory held no matching files, which only proves cleanup if the file landed in that directory to begin with. That held solely because the helpers route through the global tempfile.tempdir the tests patch -- an assumption the tests never checked. A change that passed an explicit dir would have made them pass while a partial file leaked into the real temp dir. A shared `_record_created_paths` spy now wraps the temp-file factory, delegating to the real one and recording each path it hands out. The tests assert on that path: one file was created, it was where the test expected, and it is gone. Verified by breaking it two ways. With the unlink removed the tests fail, as before. With the unlink removed AND the destination pinned to an explicit dir that ignores the patched tempdir -- the vacuous case, which the old directory-glob assertion passed -- they now fail too. Applied to the spill test as well. Review only raised the two copy-failure tests, but that one asserted on an empty directory for the same reason and could go vacuous the same way if `dir_` stopped being honored. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent 3080cf6 commit 1ec2e25

1 file changed

Lines changed: 38 additions & 4 deletions

File tree

‎_test_unstructured_client/unit/test_ndjson_elements_file.py‎

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import httpx
2222

23+
from unstructured_client import general
2324
from unstructured_client._hooks.custom import request_utils
2425
from unstructured_client._hooks.custom.request_utils import (
2526
ELEMENTS_FILE_EXTENSION_KEY,
@@ -178,7 +179,27 @@ def test_write_chunk_body_to_temp_roundtrips(tmp_path):
178179
assert _read_ndjson(path) == elements
179180

180181

181-
def test_spill_failure_leaves_no_orphan_file(tmp_path):
182+
def _record_created_paths(monkeypatch, module, attr):
183+
"""Spy on a temp-file factory, recording every path it hands out.
184+
185+
Asserting on an empty directory only proves cleanup if the file landed in that
186+
directory in the first place. Recording what production actually created keeps the
187+
assertion honest even if the destination moves.
188+
"""
189+
created: list[str] = []
190+
real = getattr(module, attr)
191+
192+
def _spy(*args, **kwargs):
193+
result = real(*args, **kwargs)
194+
# mkstemp returns (fd, path); NamedTemporaryFile returns a handle with .name.
195+
created.append(result[1] if isinstance(result, tuple) else result.name)
196+
return result
197+
198+
monkeypatch.setattr(module, attr, _spy)
199+
return created
200+
201+
202+
def test_spill_failure_leaves_no_orphan_file(tmp_path, monkeypatch):
182203
"""A write that fails partway must take its own temp file with it.
183204
184205
The caller only registers the returned path for cleanup once this function returns,
@@ -207,13 +228,18 @@ def _failing_fdopen(fd, mode):
207228
return _FailingWriter(real_fdopen(fd, mode))
208229

209230
response = httpx.Response(status_code=200, content=b'{"type": "Table"}\n')
231+
created = _record_created_paths(monkeypatch, request_utils.tempfile, "mkstemp")
210232

211233
with mock.patch.object(request_utils.os, "fdopen", _failing_fdopen):
212234
with pytest.raises(OSError) as excinfo:
213235
write_chunk_body_to_temp(response, str(tmp_path))
214236

215237
assert excinfo.value.errno == errno.ENOSPC
216-
assert list(tmp_path.iterdir()) == []
238+
# Assert against the path production actually created, so the check cannot pass
239+
# vacuously if the file ever stops landing where the test expects.
240+
assert len(created) == 1
241+
assert Path(created[0]).parent == tmp_path
242+
assert not os.path.exists(created[0])
217243

218244

219245
def test_elements_file_copy_failure_leaves_no_orphan(tmp_path, monkeypatch):
@@ -223,6 +249,7 @@ def test_elements_file_copy_failure_leaves_no_orphan(tmp_path, monkeypatch):
223249
exactly what makes an interrupted copy leak.
224250
"""
225251
monkeypatch.setattr(tempfile, "tempdir", str(tmp_path))
252+
created = _record_created_paths(monkeypatch, general, "_new_elements_file")
226253

227254
class _FailingResponse:
228255
extensions: dict = {}
@@ -234,13 +261,18 @@ def iter_bytes(self):
234261
with pytest.raises(httpx.ReadError):
235262
_ndjson_elements_file(_FailingResponse())
236263

237-
assert list(tmp_path.glob("unst_elements_*")) == []
264+
# The recorded path is the one production created, so this cannot pass vacuously if
265+
# the helper ever stops routing through the global tempdir.
266+
assert len(created) == 1
267+
assert Path(created[0]).parent == tmp_path
268+
assert not os.path.exists(created[0])
238269

239270

240271
@pytest.mark.asyncio
241272
async def test_elements_file_copy_failure_leaves_no_orphan_async(tmp_path, monkeypatch):
242273
"""Async counterpart of `test_elements_file_copy_failure_leaves_no_orphan`."""
243274
monkeypatch.setattr(tempfile, "tempdir", str(tmp_path))
275+
created = _record_created_paths(monkeypatch, general, "_new_elements_file")
244276

245277
class _FailingResponse:
246278
extensions: dict = {}
@@ -252,7 +284,9 @@ async def aiter_bytes(self):
252284
with pytest.raises(httpx.ReadError):
253285
await _ndjson_elements_file_async(_FailingResponse())
254286

255-
assert list(tmp_path.glob("unst_elements_*")) == []
287+
assert len(created) == 1
288+
assert Path(created[0]).parent == tmp_path
289+
assert not os.path.exists(created[0])
256290

257291

258292
def test_combine_accepts_bodies_spilled_without_caching(tmp_path):

0 commit comments

Comments
 (0)