Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a bug where cancelling site generation in the Graphical User Interface would cause a fatal error #1299

Merged
merged 1 commit into from
Feb 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 18 additions & 11 deletions betty/gui/project.py
Original file line number Diff line number Diff line change
@@ -6,13 +6,13 @@
import asyncio
import copy
import re
from asyncio import Task
from asyncio import Task, CancelledError
from contextlib import suppress
from pathlib import Path
from urllib.parse import urlparse

from PyQt6.QtCore import Qt, QThread, QObject
from PyQt6.QtGui import QAction
from PyQt6.QtGui import QAction, QCloseEvent
from PyQt6.QtWidgets import QFileDialog, QPushButton, QWidget, QVBoxLayout, QHBoxLayout, QMenu, QStackedLayout, \
QGridLayout, QCheckBox, QFormLayout, QLabel, QLineEdit, QButtonGroup, QRadioButton, QFrame, QScrollArea, QSizePolicy
from babel import Locale
@@ -598,7 +598,9 @@ def __init__(self, project: Project, generate_window: _GenerateWindow):

@sync
async def run(self) -> None:
self._task = asyncio.create_task(self._generate())
with suppress(CancelledError):
self._task = asyncio.create_task(self._generate())
await self._task

async def _generate(self) -> None:
with ExceptionCatcher(self._generate_window, close_parent=True):
@@ -638,6 +640,7 @@ def __init__(
central_layout.addLayout(button_layout)

self._cancel_button = QPushButton()
self._cancel_button.released.connect(self.close)
button_layout.addWidget(self._cancel_button)

self._serve_button = QPushButton()
@@ -646,29 +649,33 @@ def __init__(
button_layout.addWidget(self._serve_button)

self._logging_handler = LogRecordViewerHandler(self._log_record_viewer)
load.getLogger().addHandler(self._logging_handler)
generate.getLogger().addHandler(self._logging_handler)

self._thread = _GenerateThread(self._app.project, self)
self._thread.finished.connect(self._finish_generate)
self._cancel_button.released.connect(self._thread.cancel)
self._thread.start()

@property
def window_title(self) -> Localizable:
return Str._('Generating your site...')

def _serve(self) -> None:
with ExceptionCatcher(self):
serve_window = ServeProjectWindow(self._app, parent=self)
serve_window = ServeProjectWindow(self._app, parent=self.parent())
serve_window.show()

def show(self) -> None:
super().show()
load.getLogger().addHandler(self._logging_handler)
generate.getLogger().addHandler(self._logging_handler)
self._thread.start()
def closeEvent(self, a0: QCloseEvent | None) -> None:
super().closeEvent(a0)
self._thread.cancel()
self._finalize()

def _finish_generate(self) -> None:
self._cancel_button.setDisabled(True)
self._serve_button.setDisabled(False)
self.setWindowFlags(self.windowFlags() | Qt.WindowType.WindowCloseButtonHint)
self._finalize()

def _finalize(self) -> None:
load.getLogger().removeHandler(self._logging_handler)
generate.getLogger().removeHandler(self._logging_handler)

18 changes: 10 additions & 8 deletions betty/tests/gui/test_project.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
from asyncio import sleep
from pathlib import Path

import aiofiles
@@ -202,18 +203,19 @@ async def test_project_window_autowrite(self, betty_qtbot: BettyQtBot) -> None:


class TestGenerateWindow:
async def test_cancel_button_should_close_window(
self,
betty_qtbot: BettyQtBot,
) -> None:
async def test_cancel_button_should_close_window(self, mocker: MockerFixture, betty_qtbot: BettyQtBot) -> None:
async def _generate(app: App) -> None:
# Ensure this takes a very long time, longer than any timeout. That way,
# if cancellation fails, this test may never pass.
await sleep(999999999)
mocker.patch('betty.generate.generate', new_callable=lambda: _generate)

async with App() as app:
sut = _GenerateWindow(app)
betty_qtbot.qtbot.addWidget(sut)

with betty_qtbot.qtbot.waitSignal(sut._thread.finished):
sut.show()
betty_qtbot.qtbot.mouseClick(sut._cancel_button, Qt.MouseButton.LeftButton)

sut.show()
betty_qtbot.mouse_click(sut._cancel_button)
betty_qtbot.assert_not_window(sut)

async def test_serve_button_should_open_serve_window(
Loading