Update tests, ensure caller is passed through in another delegated app.log call

This commit is contained in:
Darren Burns
2022-04-12 14:29:59 +01:00
parent 7f96b650b8
commit ec0ce30b49
5 changed files with 24 additions and 18 deletions

View File

@@ -46,12 +46,11 @@ class BasicApp(App):
self.log(self.screen.tree)
def action_print(self):
print("Hello, world!", "another", self.screen.tree)
print("A new print statement")
print(
"Printed using builtin [b blue]print[/] function:",
self.screen.tree,
sep=" - ",
)
BasicApp.run(css_file="uber.css", log="textual.log", log_verbosity=1)
# if __name__ == '__main__':
# print(b"Hello, world!", "another", end="")
# time.sleep(5)

View File

@@ -1,3 +1,4 @@
import inspect
from typing import Any
from rich.console import RenderableType
@@ -9,7 +10,8 @@ def log(*args: object, verbosity: int = 0, **kwargs) -> None:
from ._context import active_app
app = active_app.get()
app.log(*args, verbosity=verbosity, **kwargs)
caller = inspect.stack()[1]
app.log(*args, verbosity=verbosity, caller=caller, **kwargs)
def panic(*args: RenderableType) -> None:

View File

@@ -192,8 +192,8 @@ class DevtoolsClient:
"type": "client_log",
"payload": {
"timestamp": int(datetime.datetime.utcnow().timestamp()),
"path": getattr(log.caller, "filename", None),
"line_number": getattr(log.caller, "lineno", None),
"path": getattr(log.caller, "filename", ""),
"line_number": getattr(log.caller, "lineno", 0),
"encoded_segments": encoded_segments,
},
}

View File

@@ -1,7 +1,10 @@
from __future__ import annotations
import inspect
from typing import NamedTuple, Any
from typing import NamedTuple, Any, TYPE_CHECKING
if TYPE_CHECKING:
from textual.app import App
class DevtoolsLog(NamedTuple):
@@ -10,7 +13,7 @@ class DevtoolsLog(NamedTuple):
class DevtoolsWritable:
def __init__(self, app):
def __init__(self, app: App):
self.app = app
self._buffer: list[DevtoolsLog] = []
@@ -46,5 +49,6 @@ class DevtoolsWritable:
def _log_batched(self, log_batch: list[DevtoolsLog]) -> None:
batched_log = "".join(log.objects_or_string for log in log_batch)
batched_log = batched_log.rstrip()
if self.app.devtools.is_connected:
self.app.devtools.log(DevtoolsLog(batched_log, caller=log_batch[-1].caller))

View File

@@ -9,6 +9,7 @@ from rich.panel import Panel
from tests.utilities.render import wait_for_predicate
from textual.devtools.client import DevtoolsClient
from textual.devtools.redirect_output import DevtoolsLog
TIMESTAMP = 1649166819
@@ -25,7 +26,7 @@ async def test_devtools_client_is_connected(devtools):
@time_machine.travel(datetime.fromtimestamp(TIMESTAMP))
async def test_devtools_log_places_encodes_and_queues_message(devtools):
await devtools._stop_log_queue_processing()
devtools.log("Hello, world!")
devtools.log(DevtoolsLog("Hello, world!"))
queued_log = await devtools.log_queue.get()
queued_log_json = json.loads(queued_log)
assert queued_log_json == {
@@ -42,7 +43,7 @@ async def test_devtools_log_places_encodes_and_queues_message(devtools):
@time_machine.travel(datetime.fromtimestamp(TIMESTAMP))
async def test_devtools_log_places_encodes_and_queues_many_logs_as_string(devtools):
await devtools._stop_log_queue_processing()
devtools.log("hello", "world")
devtools.log(DevtoolsLog(("hello", "world")))
queued_log = await devtools.log_queue.get()
queued_log_json = json.loads(queued_log)
assert queued_log_json == {
@@ -62,10 +63,10 @@ async def test_devtools_log_spillover(devtools):
devtools.log_queue = Queue(maxsize=2)
# Force spillover of 2
devtools.log(Panel("hello, world"))
devtools.log("second message")
devtools.log("third message") # Discarded by rate-limiting
devtools.log("fourth message") # Discarded by rate-limiting
devtools.log(DevtoolsLog((Panel("hello, world"),)))
devtools.log(DevtoolsLog("second message"))
devtools.log(DevtoolsLog("third message")) # Discarded by rate-limiting
devtools.log(DevtoolsLog("fourth message")) # Discarded by rate-limiting
assert devtools.spillover == 2
@@ -74,7 +75,7 @@ async def test_devtools_log_spillover(devtools):
await devtools.log_queue.get()
# Add another message now that we're under spillover threshold
devtools.log("another message")
devtools.log(DevtoolsLog("another message"))
await devtools.log_queue.get()
# Ensure we're informing the server of spillover rate-limiting