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

153 lines
5.8 KiB
Python

from pathlib import Path
from contextlib import closing
from threading import Event
import pytest
from dx_productivity.trial_task_center import project_tasks
from dx_productivity.trial_task_center.errors import TaskCenterError
from dx_productivity.trial_task_center.store import TaskStore
def project(path):
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text('KEY FrontEndSnapshot "DxD"\nKEY iCDBDir "database"\n')
return path
def test_scan_cancel_discards_partial_results(tmp_path):
project(tmp_path / 'a' / 'a.prj')
project(tmp_path / 'b' / 'b.prj')
stop = Event()
updates = []
def progress(directory, visited, found):
updates.append((directory, visited, found))
if found:
stop.set()
with pytest.raises(TaskCenterError, match='cancel'):
project_tasks.scan_project_directory(tmp_path, stop_event=stop, on_progress=progress)
assert updates and updates[-1][2] == 1
def test_selected_creation_does_not_walk_unrelated_subtree(tmp_path, monkeypatch):
root = tmp_path / 'root'
chosen = project(root / 'a' / 'b' / 'c' / 'board.prj')
project(root / 'unrelated' / 'board.prj')
original = project_tasks.os.scandir
def guarded(path):
assert Path(path) != root / 'unrelated'
return original(path)
monkeypatch.setattr(project_tasks.os, 'scandir', guarded)
with closing(TaskStore(tmp_path / 'tasks.db')) as store:
assert len(project_tasks.create_project_tasks(store, root, selected_paths=(chosen,))) == 1
@pytest.mark.parametrize('kind', ['ancestor', 'excluded', 'invalid', 'outside'])
def test_selected_validation_is_atomic(tmp_path, kind):
root = tmp_path / 'root'
good = project(root / 'good' / 'board.prj')
bad = project(root / 'a' / 'deep' / 'bad.prj')
excluded = ()
if kind == 'ancestor':
project(root / 'a' / 'ancestor.prj')
elif kind == 'excluded':
excluded = (root / 'a',)
elif kind == 'invalid':
bad.write_text('not a schematic')
else:
bad = project(tmp_path / 'outside' / 'bad.prj')
with closing(TaskStore(tmp_path / 'tasks.db')) as store:
with pytest.raises(TaskCenterError):
project_tasks.create_project_tasks(store, root, selected_paths=(good, bad), excluded_roots=excluded)
assert store.list_tasks() == ()
def test_excluded_root_cannot_be_scanned(tmp_path):
project(tmp_path / 'board.prj')
with pytest.raises(TaskCenterError):
project_tasks.scan_project_directory(tmp_path, excluded_roots=(tmp_path,))
def test_dialog_cancel_closes_and_signals_worker(qtbot, tmp_path):
from threading import Event
from types import SimpleNamespace
from dx_productivity.trial_task_center.ui.project_plan import DirectoryTaskDialog
started = Event()
stopped = Event()
def scan(root, *, stop_event, on_progress):
on_progress(root, 3, 1)
started.set()
stop_event.wait(3)
stopped.set()
raise TaskCenterError('discovery_cancelled', 'cancelled')
dialog = DirectoryTaskDialog(SimpleNamespace(scan_project_directory=scan))
qtbot.addWidget(dialog)
dialog.directory.setText(str(tmp_path))
dialog.show()
dialog.read_projects()
qtbot.waitUntil(started.is_set)
qtbot.waitUntil(lambda: '3' in dialog.summary.text())
dialog.reject()
assert not dialog.isVisible()
qtbot.waitUntil(stopped.is_set)
qtbot.waitUntil(lambda: dialog._worker is None)
assert dialog.projects.count() == 0
def test_dialog_cancel_ignores_partial_success(qtbot, tmp_path):
from types import SimpleNamespace
from dx_productivity.trial_task_center.ui.project_plan import DirectoryTaskDialog
dialog = DirectoryTaskDialog(SimpleNamespace())
qtbot.addWidget(dialog)
dialog.directory.setText(str(tmp_path))
dialog._stop_event.set()
dialog._received((tmp_path / 'partial.prj',))
assert dialog.projects.count() == 0
def test_selected_rechecks_each_ancestor_for_reparse(tmp_path, monkeypatch):
root = tmp_path / 'root'
chosen = project(root / 'a' / 'b' / 'c' / 'board.prj')
original = project_tasks._reject_reparse
def reject(details, path):
if path == root / 'a' / 'b':
raise TaskCenterError('discovery_reparse', 'reparse ancestor')
original(details, path)
monkeypatch.setattr(project_tasks, '_reject_reparse', reject)
with closing(TaskStore(tmp_path / 'tasks.db')) as store:
with pytest.raises(TaskCenterError, match='reparse'):
project_tasks.create_project_tasks(store, root, selected_paths=(chosen,))
assert store.list_tasks() == ()
def test_scan_pre_cancelled_does_not_touch_directory(tmp_path, monkeypatch):
stop = Event()
stop.set()
def unexpected(*args):
pytest.fail('cancelled scan touched disk')
monkeypatch.setattr(project_tasks.os, 'scandir', unexpected)
with pytest.raises(TaskCenterError, match='cancel'):
project_tasks.scan_project_directory(tmp_path, stop_event=stop)
def test_scan_worker_can_finish_after_dialog_is_deleted(qtbot, tmp_path):
from types import SimpleNamespace
from shiboken6 import delete
from dx_productivity.trial_task_center.ui.project_plan import DirectoryTaskDialog
started, release, finished = Event(), Event(), Event()
def scan(root, *, stop_event, on_progress):
started.set()
release.wait(3)
on_progress(root, 1, 0)
finished.set()
return ()
dialog = DirectoryTaskDialog(SimpleNamespace(scan_project_directory=scan))
dialog.directory.setText(str(tmp_path))
dialog.read_projects()
worker = dialog._worker
qtbot.waitUntil(started.is_set)
dialog.reject()
delete(dialog)
release.set()
qtbot.waitUntil(finished.is_set)
# Keep the worker alive while its queued completion signals are delivered.
qtbot.wait(20)
assert worker is not None