test: enforce desktop cleanup interfaces and guard reader setup

This commit is contained in:
阿白叔 committed 2026-11-05 14:59:19 +08:00
1 parent 311d0dd9a4
commit 766d67cb57
3 files changed
+123 -24

No files matched your search

+98 -14
View File
@@ -1,33 +1,60 @@
import pytest
def test_constructor_releases_private_desktop_when_process_launch_fails(monkeypatch):
@pytest.mark.parametrize('failure, expected', [
('desktop', []),
('job', ['desktop']),
('process', ['job', 'desktop']),
('thread', ['thread', 'terminate', 'process', 'job', 'desktop']),
])
def test_constructor_rolls_back_allocated_resources(monkeypatch, failure, expected):
import cbb_refresh_verify.desktop_worker as module
class Handle:
def __init__(self):
self.closed = False
closed = []
error = RuntimeError(failure + ' failed')
class FakeJob:
def Close(self):
self.closed = True
closed.append('job')
class FakeDesktop:
def CloseDesktop(self):
self.closed = True
closed.append('desktop')
desktop = Handle()
job = Handle()
class FakeProcess:
def Close(self):
closed.append('process')
monkeypatch.setattr(module.win32service, 'CreateDesktop', lambda *args: desktop)
monkeypatch.setattr(module.win32job, 'CreateJobObject', lambda *args: job)
class FakeThread:
def Close(self):
closed.append('thread')
raise error
def allocate(stage, value):
if failure == stage:
raise error
return value
process = FakeProcess()
monkeypatch.setattr(module.win32service, 'CreateDesktop',
lambda *args: allocate('desktop', FakeDesktop()))
monkeypatch.setattr(module.win32job, 'CreateJobObject',
lambda *args: allocate('job', FakeJob()))
monkeypatch.setattr(module.win32process, 'STARTUPINFO', lambda: type('Startup', (), {})())
monkeypatch.setattr(module.win32process, 'CreateProcess',
lambda *args: (_ for _ in ()).throw(RuntimeError('launch failed')))
lambda *args: allocate('process', (process, FakeThread(), 321, 1)))
with pytest.raises(RuntimeError, match='launch failed'):
def terminate(handle, code):
assert handle is process
closed.append('terminate')
monkeypatch.setattr(module.win32process, 'TerminateProcess', terminate)
with pytest.raises(RuntimeError) as caught:
module.DesktopWorker(['worker.exe'], 'PrivateDesk')
assert desktop.closed is True
assert job.closed is True
assert caught.value is error
assert closed == expected
def test_library_flag_loads_ui_dependencies_before_creating_private_desktop(monkeypatch, tmp_path):
@@ -54,3 +81,60 @@ def test_library_flag_loads_ui_dependencies_before_creating_private_desktop(monk
module.read_library_flag('library.lmc', 'Block')
assert created == []
@pytest.mark.parametrize('reader', ['read_library_flag', 'read_library_tree_image_map'])
def test_reader_does_not_create_desktop_before_preparation_finishes(monkeypatch, tmp_path, reader):
import win32service
import cbb_refresh_verify.mentor_adapter as module
closed = []
class FakeDesktop:
def CloseDesktop(self):
closed.append('closed')
class InvalidLibrary:
def __fspath__(self):
raise ValueError('invalid library path')
executable = tmp_path / 'LibraryManager.exe'
executable.touch()
monkeypatch.setattr(module, 'library_manager_path', lambda: executable)
monkeypatch.setattr(win32service, 'CreateDesktop',
lambda *args: closed.append('created') or FakeDesktop())
with pytest.raises(ValueError, match='invalid library path'):
getattr(module, reader)(InvalidLibrary(), 'Block')
assert closed == []
@pytest.mark.parametrize('reader', ['read_library_flag', 'read_library_tree_image_map'])
def test_reader_closes_desktop_when_process_launch_fails(monkeypatch, tmp_path, reader):
import win32service
import win32process
import cbb_refresh_verify.mentor_adapter as module
closed = []
class FakeDesktop:
def CloseDesktop(self):
closed.append('desktop')
def fail(*args):
raise RuntimeError('launch failed')
executable = tmp_path / 'LibraryManager.exe'
executable.touch()
monkeypatch.setattr(module, 'library_manager_path', lambda: executable)
monkeypatch.setattr(win32service, 'CreateDesktop', lambda *args: FakeDesktop())
monkeypatch.setattr(win32process, 'CreateProcess', fail)
if reader == 'read_library_flag':
flag, detail = module.read_library_flag(tmp_path / 'library.lmc', 'Block')
assert flag == module.FLAG_UNKNOWN
assert 'launch failed' in detail
else:
with pytest.raises(RuntimeError, match='launch failed'):
module.read_library_tree_image_map(tmp_path / 'library.lmc', ['Block'])
assert closed == ['desktop']