Files
cbb-refresh-verify-795/tests/test_native_inspection_release_regressions.py
T

103 lines
4.4 KiB
Python

import json
from pathlib import Path
import sys
from uuid import uuid4
import pytest
def test_empty_private_desktop_does_not_inherit_stale_windows_error(tmp_path):
from cbb_refresh_verify.desktop_worker import DesktopWorker
script = tmp_path/'enumerate.py'
result = tmp_path/'result.json'
script.write_text(
'import sys,json,win32api,win32gui,win32process\nfrom pathlib import Path\n'
f'sys.path.insert(0,{str(Path("src").resolve())!r})\n'
'from cbb_refresh_verify.native_repair_ui import _windows\n'
'win32api.SetLastError(2)\n'
'try:\n record={"windows":_windows(999999)}\n'
'except Exception as error:\n record={"error":str(error)}\n'
f'Path({str(result)!r}).write_text(json.dumps(record))\n', encoding='utf-8')
worker = DesktopWorker([sys.executable, str(script)], 'CbbProbe_' + uuid4().hex[:12])
try:
assert worker.wait(timeout=20) == 0
finally:
worker.close()
assert json.loads(result.read_text()) == {'windows': []}
def test_real_window_enumeration_errors_still_propagate(monkeypatch):
import win32gui, pywintypes
from cbb_refresh_verify.native_repair_ui import _windows
def denied(*args): raise pywintypes.error(5, 'EnumWindows', 'denied')
monkeypatch.setattr(win32gui, 'EnumWindows', denied)
with pytest.raises(pywintypes.error): _windows(123)
def test_cancel_reaches_prelaunch_scan(tmp_path, monkeypatch):
from contextlib import contextmanager
from test_native_repair import repair_worker_fixture
from cbb_refresh_verify.native_repair import RepairTarget
ui, root, job, actions = repair_worker_fixture(tmp_path, monkeypatch, 'active')
data = json.loads(job.read_text('utf-8'))
data['maintenance_context'] = {'library': str(tmp_path/'lib.lmc'), 'block': 'CBB1'}
job.write_text(json.dumps(data), encoding='utf-8')
calls, interrupted = [], []
@contextmanager
def guard(*args, stop=None, **kwargs):
calls.append(1)
assert stop is not None, 'worker scan has no cancellation input'
if len(calls) == 1:
Path(data['cancel_signal']).touch()
assert stop.is_set()
interrupted.append(1)
raise ValueError('MAINTENANCE_CANCELLED')
yield {'target': RepairTarget(Path(data['project']), Path(data['database']))}
monkeypatch.setattr('cbb_refresh_verify.maintenance_entry.maintenance_target_guard', guard)
monkeypatch.setattr(ui.subprocess, 'Popen', lambda *a, **kw: pytest.fail('cancelled native launch'))
assert ui.repair_worker_main(job) == 1
assert interrupted == [1] and not actions
def test_worker_uses_one_complete_guard_immediately_before_native_launch(tmp_path, monkeypatch):
from contextlib import contextmanager
from test_native_repair import repair_worker_fixture
from cbb_refresh_verify.native_repair import RepairTarget
ui, root, job, actions = repair_worker_fixture(tmp_path, monkeypatch, 'active')
data = json.loads(job.read_text('utf-8'))
data['maintenance_context'] = {'library': str(tmp_path/'lib.lmc'), 'block': 'CBB1'}
job.write_text(json.dumps(data), encoding='utf-8')
order = []
@contextmanager
def guard(*args, stop=None, **kwargs):
assert stop is not None
order.append('guard')
yield {'target': RepairTarget(Path(data['project']), Path(data['database']))}
order.append('guard_complete')
monkeypatch.setattr('cbb_refresh_verify.maintenance_entry.maintenance_target_guard', guard)
popen = ui.subprocess.Popen
def launch(*args, **kwargs):
order.append('launch')
return popen(*args, **kwargs)
monkeypatch.setattr(ui.subprocess, 'Popen', launch)
ui.repair_worker_main(job)
assert order == ['guard', 'guard_complete', 'launch']
def test_file_cancellation_polling_is_bounded_and_latches_cancel(tmp_path, monkeypatch):
from types import SimpleNamespace
import cbb_refresh_verify.native_repair_ui as ui
now = [1.0]
monkeypatch.setattr(ui, 'time', SimpleNamespace(monotonic=lambda: now[0]))
signal = ui._CancellationFile(tmp_path/'cancel.signal')
reads = []
signal.path = SimpleNamespace(exists=lambda: reads.append(1) or len(reads) > 1)
for _ in range(50000):
assert not signal.is_set()
assert len(reads) == 1
now[0] += .11
assert signal.is_set()
signal.path = SimpleNamespace(exists=lambda: pytest.fail('cancel must stay latched'))
now[0] += 1
assert signal.is_set()