141 lines
4.7 KiB
Python
141 lines
4.7 KiB
Python
import pytest
|
|
|
|
|
|
@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
|
|
|
|
closed = []
|
|
error = RuntimeError(failure + ' failed')
|
|
|
|
class FakeJob:
|
|
def Close(self):
|
|
closed.append('job')
|
|
|
|
class FakeDesktop:
|
|
def CloseDesktop(self):
|
|
closed.append('desktop')
|
|
|
|
class FakeProcess:
|
|
def Close(self):
|
|
closed.append('process')
|
|
|
|
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: allocate('process', (process, FakeThread(), 321, 1)))
|
|
|
|
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 caught.value is error
|
|
assert closed == expected
|
|
|
|
|
|
def test_library_flag_loads_ui_dependencies_before_creating_private_desktop(monkeypatch, tmp_path):
|
|
import builtins
|
|
import win32service
|
|
import cbb_refresh_verify.mentor_adapter as module
|
|
|
|
created = []
|
|
original_import = builtins.__import__
|
|
executable = tmp_path / 'LibraryManager.exe'
|
|
executable.touch()
|
|
|
|
def deny_pywinauto(name, *args, **kwargs):
|
|
if name == 'pywinauto' or name.startswith('pywinauto.'):
|
|
raise ImportError('pywinauto unavailable')
|
|
return original_import(name, *args, **kwargs)
|
|
|
|
monkeypatch.setattr(builtins, '__import__', deny_pywinauto)
|
|
monkeypatch.setattr(module, 'library_manager_path', lambda: executable)
|
|
monkeypatch.setattr(win32service, 'CreateDesktop',
|
|
lambda *args: created.append(args))
|
|
|
|
with pytest.raises(ImportError, match='pywinauto unavailable'):
|
|
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']
|