Redirecting stdout to both devtools and logfile

This commit is contained in:
Darren Burns
2022-04-14 11:10:47 +01:00
parent ee19daf6a6
commit 3b40eb828b
6 changed files with 102 additions and 62 deletions

View File

@@ -1,4 +1,5 @@
import json
import types
from asyncio import Queue
from datetime import datetime
@@ -11,6 +12,9 @@ from tests.utilities.render import wait_for_predicate
from textual.devtools.client import DevtoolsClient
from textual.devtools.redirect_output import DevtoolsLog
CALLER_LINENO = 123
CALLER_PATH = "a/b/c.py"
CALLER = types.SimpleNamespace(filename=CALLER_PATH, lineno=CALLER_LINENO)
TIMESTAMP = 1649166819
@@ -26,15 +30,15 @@ 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(DevtoolsLog("Hello, world!"))
devtools.log(DevtoolsLog("Hello, world!", CALLER))
queued_log = await devtools.log_queue.get()
queued_log_json = json.loads(queued_log)
assert queued_log_json == {
"type": "client_log",
"payload": {
"timestamp": TIMESTAMP,
"path": "",
"line_number": 0,
"path": CALLER_PATH,
"line_number": CALLER_LINENO,
"encoded_segments": "gANdcQAoY3JpY2guc2VnbWVudApTZWdtZW50CnEBWA0AAABIZWxsbywgd29ybGQhcQJOTodxA4FxBGgBWAEAAAAKcQVOTodxBoFxB2Uu",
},
}
@@ -43,15 +47,15 @@ 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(DevtoolsLog(("hello", "world")))
devtools.log(DevtoolsLog(("hello", "world"), CALLER))
queued_log = await devtools.log_queue.get()
queued_log_json = json.loads(queued_log)
assert queued_log_json == {
"type": "client_log",
"payload": {
"timestamp": TIMESTAMP,
"path": "",
"line_number": 0,
"path": CALLER_PATH,
"line_number": CALLER_LINENO,
"encoded_segments": "gANdcQAoY3JpY2guc2VnbWVudApTZWdtZW50CnEBWAsAAABoZWxsbyB3b3JsZHECTk6HcQOBcQRoAVgBAAAACnEFTk6HcQaBcQdlLg==",
},
}
@@ -63,10 +67,10 @@ async def test_devtools_log_spillover(devtools):
devtools.log_queue = Queue(maxsize=2)
# Force spillover of 2
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
devtools.log(DevtoolsLog((Panel("hello, world"),), CALLER))
devtools.log(DevtoolsLog("second message", CALLER))
devtools.log(DevtoolsLog("third message", CALLER)) # Discarded by rate-limiting
devtools.log(DevtoolsLog("fourth message", CALLER)) # Discarded by rate-limiting
assert devtools.spillover == 2
@@ -75,7 +79,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(DevtoolsLog("another message"))
devtools.log(DevtoolsLog("another message", CALLER))
await devtools.log_queue.get()
# Ensure we're informing the server of spillover rate-limiting