Files
dxdesigner-part-replacement/tests/v1_1/test_path_access.py
T

146 lines
5.8 KiB
Python

import pytest
def test_windows_extended_paths_keep_canonical_identity():
from dx_productivity.trial_task_center.path_access import windows_extended_path, strip_extended_prefix
slash = chr(92)
local = 'C:' + slash + ('nested' + slash) * 45 + 'file.json'
assert windows_extended_path(local) == slash * 2 + '?' + slash + local
assert strip_extended_prefix(windows_extended_path(local)) == local
unc = slash * 2 + 'server' + slash + 'share' + slash + 'file.json'
assert windows_extended_path(unc) == slash * 2 + '?' + slash + 'UNC' + unc[1:]
assert strip_extended_prefix(windows_extended_path(unc)) == unc
def test_sharing_retry_is_bounded_and_does_not_retry_permission_denied():
from dx_productivity.trial_task_center.path_access import retry_sharing_violation
calls = []
def operation():
calls.append(1)
error = PermissionError('sharing violation')
error.winerror = 32
raise error
with pytest.raises(PermissionError):
retry_sharing_violation(operation, sleep=lambda delay: None)
assert len(calls) == 3
calls.clear()
def denied():
calls.append(1)
raise PermissionError('access denied')
with pytest.raises(PermissionError):
retry_sharing_violation(denied, sleep=lambda delay: None)
assert len(calls) == 1
def test_discovery_uses_extended_filesystem_access_before_legacy_launch(tmp_path, monkeypatch):
import os
import subprocess
from pathlib import Path
from dx_productivity.trial_task_center.project_discovery import discover_project, DiscoveryRuntime
if os.name != 'nt':
pytest.skip('Windows filesystem boundary')
data = tmp_path / ('nested-' + 'a' * 90) / ('more-' + 'b' * 70)
project = tmp_path / 'board.prj'
project.write_text('project')
script = tmp_path / 'worker.vbs'
script.write_text('script')
real_mkdir = Path.mkdir
def strict_mkdir(path, *args, **kwargs):
if len(str(path)) >= 260 and not str(path).startswith(chr(92) * 2 + '?'):
raise OSError('legacy MAX_PATH filesystem access')
return real_mkdir(path, *args, **kwargs)
monkeypatch.setattr(Path, 'mkdir', strict_mkdir)
reached = []
def launch(*args):
reached.append(1)
raise subprocess.SubprocessError('stop at launch')
runtime = DiscoveryRuntime(launch, lambda: Path('cscript'), lambda s, r: (str(s), str(r)),
lambda: (), str, lambda *a: (), lambda *a: {}, 5)
with pytest.raises(Exception):
discover_project(project, ('OLD',), data_root=data, script_source=script, runtime=runtime)
assert reached, 'long-path filesystem preparation failed before worker boundary'
def test_native_short_path_never_passes_extended_prefix_to_wsh(tmp_path):
import os
from dx_productivity.trial_task_center.legacy_paths import _short_path
if os.name != 'nt':
pytest.skip('Windows short-path boundary')
result = _short_path(tmp_path)
if result is None:
pytest.skip('filesystem does not provide short paths')
assert not result.startswith(chr(92) * 2 + '?')
def test_batch_resource_record_retries_reader_denied_atomic_replace(tmp_path, monkeypatch):
import json
import os
from threading import Timer
if os.name != 'nt':
pytest.skip('Windows delete-sharing boundary')
import win32con
import win32file
from dx_productivity.trial_task_center import batch_resources
path = tmp_path / 'session.json'
path.write_text('{"stage": "before"}', encoding='utf-8')
reader = win32file.CreateFile(str(path), win32con.GENERIC_READ,
win32con.FILE_SHARE_READ | win32con.FILE_SHARE_WRITE, None,
win32con.OPEN_EXISTING, 0, None)
real_replace = os.replace
denials, timers = [], []
def replace(source, target):
try:
return real_replace(source, target)
except PermissionError as error:
denials.append(error.winerror)
timer = Timer(.02, reader.Close)
timers.append(timer)
timer.start()
raise
monkeypatch.setattr(batch_resources.os, 'replace', replace)
try:
batch_resources.write_record(path, {'stage': 'saved'})
assert denials == [5]
assert json.loads(path.read_text(encoding='utf-8')) == {'stage': 'saved'}
assert not list(tmp_path.glob('*.tmp'))
finally:
for timer in timers:
timer.join()
reader.Close()
def test_atomic_record_retries_a_delete_lock_released_before_the_error_is_observed(tmp_path, monkeypatch):
import json
import os
if os.name != 'nt':
pytest.skip('Windows delete-sharing race')
import win32con
import win32file
from dx_productivity.trial_task_center import batch_resources
path = tmp_path / 'supervisor.json'
path.write_text('{"state":"running"}', encoding='utf-8')
real_replace = os.replace
denials = []
def replace(source, target):
if not denials:
reader = win32file.CreateFile(str(path), win32con.GENERIC_READ,
win32con.FILE_SHARE_READ | win32con.FILE_SHARE_WRITE, None,
win32con.OPEN_EXISTING, 0, None)
try:
return real_replace(source, target)
except PermissionError as error:
denials.append(error.winerror)
raise
finally:
# The native rename failed while locked; a later DELETE probe
# cannot recover that fact after the reader has closed.
reader.Close()
return real_replace(source, target)
monkeypatch.setattr(batch_resources.os, 'replace', replace)
batch_resources.write_record(path, {'state': 'released', 'business': 'saved'})
assert denials == [5]
assert json.loads(path.read_text(encoding='utf-8')) == {'state': 'released', 'business': 'saved'}
assert not list(tmp_path.glob('*.tmp'))