Skip to content

Commit

Permalink
Merge pull request #4333 from Textualize/fix-4282
Browse files Browse the repository at this point in the history
Tests re-raise exceptions that happen inside `Widget.compose`
  • Loading branch information
willmcgugan authored Mar 26, 2024
2 parents 5cc4ee3 + 809f383 commit ff77cf7
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Fixed issue with flickering scrollbars https://github.com/Textualize/textual/pull/4315
- Fixed issue where narrow TextArea would repeatedly wrap due to scrollbar appearing/disappearing https://github.com/Textualize/textual/pull/4334
- Fix progress bar ETA not updating when setting `total` reactive https://github.com/Textualize/textual/pull/4316
- Exceptions inside `Widget.compose` or workers weren't bubbling up in tests https://github.com/Textualize/textual/issues/4282

### Changed

Expand Down
6 changes: 2 additions & 4 deletions src/textual/widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -3618,10 +3618,8 @@ async def _compose(self) -> None:
raise TypeError(
f"{self!r} compose() method returned an invalid result; {error}"
) from error
except Exception:
from rich.traceback import Traceback

self.app.panic(Traceback())
except Exception as error:
self.app._handle_exception(error)
else:
self._extend_compose(widgets)
await self.mount_composed_widgets(widgets)
Expand Down
3 changes: 2 additions & 1 deletion src/textual/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,8 @@ async def _run(self, app: App) -> None:

app.log.worker(Traceback())
if self.exit_on_error:
app._fatal_error()
worker_failed = WorkerFailed(self._error)
app._handle_exception(worker_failed)
else:
self.state = WorkerState.SUCCESS
app.log.worker(self)
Expand Down
36 changes: 34 additions & 2 deletions tests/test_pilot.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import pytest

from textual import events
from textual import events, work
from textual.app import App, ComposeResult
from textual.binding import Binding
from textual.containers import Center, Middle
from textual.pilot import OutOfBounds
from textual.screen import Screen
from textual.widgets import Button, Label
from textual.worker import WorkerFailed

KEY_CHARACTERS_TO_TEST = "akTW03" + punctuation
"""Test some "simple" characters (letters + digits) and all punctuation."""
Expand Down Expand Up @@ -56,7 +58,7 @@ def on_key(self, event: events.Key) -> None:
assert keys_pressed[-1] == char


async def test_pilot_exception_catching_compose():
async def test_pilot_exception_catching_app_compose():
"""Ensuring that test frameworks are aware of exceptions
inside compose methods when running via Pilot run_test()."""

Expand All @@ -70,6 +72,21 @@ def compose(self) -> ComposeResult:
pass


async def test_pilot_exception_catching_widget_compose():
class SomeScreen(Screen[None]):
def compose(self) -> ComposeResult:
1 / 0
yield Label("Beep")

class FailingApp(App[None]):
def on_mount(self) -> None:
self.push_screen(SomeScreen())

with pytest.raises(ZeroDivisionError):
async with FailingApp().run_test():
pass


async def test_pilot_exception_catching_action():
"""Ensure that exceptions inside action handlers are presented
to the test framework when running via Pilot run_test()."""
Expand All @@ -85,6 +102,21 @@ def action_beep(self) -> None:
await pilot.press("b")


async def test_pilot_exception_catching_worker():
class SimpleAppThatCrashes(App[None]):
def on_mount(self) -> None:
self.crash()

@work(name="crash")
async def crash(self) -> None:
1 / 0

with pytest.raises(WorkerFailed) as exc:
async with SimpleAppThatCrashes().run_test():
pass
assert exc.type is ZeroDivisionError


async def test_pilot_click_screen():
"""Regression test for https://github.com/Textualize/textual/issues/3395.
Expand Down
18 changes: 9 additions & 9 deletions tests/workers/test_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,10 @@ class ErrorApp(App):
pass

app = ErrorApp()
async with app.run_test():
worker: Worker[str] = Worker(app, run_error)
worker._start(app)
with pytest.raises(WorkerFailed):
with pytest.raises(WorkerFailed):
async with app.run_test():
worker: Worker[str] = Worker(app, run_error)
worker._start(app)
await worker.wait()


Expand Down Expand Up @@ -218,12 +218,12 @@ async def self_referential_work():
await get_current_worker().wait()

app = App()
async with app.run_test():
worker = Worker(app, self_referential_work)
worker._start(app)
with pytest.raises(WorkerFailed) as exc:
with pytest.raises(WorkerFailed) as exc:
async with app.run_test():
worker = Worker(app, self_referential_work)
worker._start(app)
await worker.wait()
assert exc.type is DeadlockError
assert exc.type is DeadlockError


async def test_wait_without_start():
Expand Down

0 comments on commit ff77cf7

Please sign in to comment.