This commit is contained in:
Will McGugan
2022-05-13 16:38:09 +01:00
parent 673cadaf85
commit f7fb6f613a
3 changed files with 35 additions and 32 deletions

View File

@@ -1,4 +1,4 @@
from datetime import datetime, timezone
from datetime import datetime
import pytest
import time_machine
@@ -6,22 +6,23 @@ from rich.align import Align
from rich.console import Console
from rich.segment import Segment
import msgpack
from tests.utilities.render import wait_for_predicate
from textual.devtools.renderables import DevConsoleLog, DevConsoleNotice
TIMESTAMP = 1649166819
WIDTH = 40
# The string "Hello, world!" is encoded in the payload below
EXAMPLE_LOG = {
_EXAMPLE_LOG = {
"type": "client_log",
"payload": {
"encoded_segments": "gASVQgAAAAAAAABdlCiMDHJpY2guc2VnbWVudJSMB1NlZ"
"21lbnSUk5SMDUhlbGxvLCB3b3JsZCGUTk6HlIGUaAOMAQqUTk6HlIGUZS4=",
"segments": b"\x80\x04\x955\x00\x00\x00\x00\x00\x00\x00]\x94\x8c\x0crich.segment\x94\x8c\x07Segment\x94\x93\x94\x8c\rHello, world!\x94NN\x87\x94\x81\x94a.",
"line_number": 123,
"path": "abc/hello.py",
"timestamp": TIMESTAMP,
},
}
EXAMPLE_LOG = msgpack.packb(_EXAMPLE_LOG)
@pytest.fixture(scope="module")
@@ -48,15 +49,10 @@ def test_log_message_render(console):
right: Align = right_cells[0]
# Since we can't guarantee the timezone the tests will run in...
local_time = (
datetime.fromtimestamp(TIMESTAMP)
.replace(tzinfo=timezone.utc)
.astimezone(tz=datetime.now().astimezone().tzinfo)
)
timezone_name = local_time.tzname()
local_time = datetime.fromtimestamp(TIMESTAMP)
string_timestamp = local_time.time()
assert left == f"[dim]{string_timestamp} {timezone_name}"
assert left == f"[dim]{string_timestamp}"
assert right.align == "right"
assert "hello.py:123" in right.renderable
@@ -69,7 +65,7 @@ def test_internal_message_render(console):
async def test_devtools_valid_client_log(devtools):
await devtools.websocket.send_json(EXAMPLE_LOG)
await devtools.websocket.send_bytes(EXAMPLE_LOG)
assert devtools.is_connected

View File

@@ -7,6 +7,7 @@ import time_machine
from aiohttp.web_ws import WebSocketResponse
from rich.console import ConsoleDimensions
from rich.panel import Panel
import msgpack
from tests.utilities.render import wait_for_predicate
from textual.devtools.client import DevtoolsClient
@@ -32,14 +33,15 @@ async def test_devtools_log_places_encodes_and_queues_message(devtools):
await devtools._stop_log_queue_processing()
devtools.log(DevtoolsLog("Hello, world!", CALLER))
queued_log = await devtools.log_queue.get()
queued_log_json = json.loads(queued_log)
assert queued_log_json == {
queued_log_data = msgpack.unpackb(queued_log)
print(repr(queued_log_data))
assert queued_log_data == {
"type": "client_log",
"payload": {
"timestamp": TIMESTAMP,
"path": CALLER_PATH,
"line_number": CALLER_LINENO,
"encoded_segments": "gANdcQAoY3JpY2guc2VnbWVudApTZWdtZW50CnEBWA0AAABIZWxsbywgd29ybGQhcQJOTodxA4FxBGgBWAEAAAAKcQVOTodxBoFxB2Uu",
"timestamp": 1649170419,
"path": "a/b/c.py",
"line_number": 123,
"segments": b"\x80\x05\x95B\x00\x00\x00\x00\x00\x00\x00]\x94(\x8c\x0crich.segment\x94\x8c\x07Segment\x94\x93\x94\x8c\rHello, world!\x94NN\x87\x94\x81\x94h\x03\x8c\x01\n\x94NN\x87\x94\x81\x94e.",
},
}
@@ -49,14 +51,15 @@ async def test_devtools_log_places_encodes_and_queues_many_logs_as_string(devtoo
await devtools._stop_log_queue_processing()
devtools.log(DevtoolsLog(("hello", "world"), CALLER))
queued_log = await devtools.log_queue.get()
queued_log_json = json.loads(queued_log)
assert queued_log_json == {
queued_log_data = msgpack.unpackb(queued_log)
print(repr(queued_log_data))
assert queued_log_data == {
"type": "client_log",
"payload": {
"timestamp": TIMESTAMP,
"path": CALLER_PATH,
"line_number": CALLER_LINENO,
"encoded_segments": "gANdcQAoY3JpY2guc2VnbWVudApTZWdtZW50CnEBWAsAAABoZWxsbyB3b3JsZHECTk6HcQOBcQRoAVgBAAAACnEFTk6HcQaBcQdlLg==",
"timestamp": 1649170419,
"path": "a/b/c.py",
"line_number": 123,
"segments": b"\x80\x05\x95@\x00\x00\x00\x00\x00\x00\x00]\x94(\x8c\x0crich.segment\x94\x8c\x07Segment\x94\x93\x94\x8c\x0bhello world\x94NN\x87\x94\x81\x94h\x03\x8c\x01\n\x94NN\x87\x94\x81\x94e.",
},
}

View File

@@ -4,12 +4,13 @@ from datetime import datetime
import time_machine
import msgpack
from textual.devtools.redirect_output import StdoutRedirector
TIMESTAMP = 1649166819
@time_machine.travel(datetime.fromtimestamp(TIMESTAMP))
@time_machine.travel(datetime.utcfromtimestamp(TIMESTAMP))
async def test_print_redirect_to_devtools_only(devtools):
await devtools._stop_log_queue_processing()
@@ -19,14 +20,15 @@ async def test_print_redirect_to_devtools_only(devtools):
assert devtools.log_queue.qsize() == 1
queued_log = await devtools.log_queue.get()
queued_log_json = json.loads(queued_log)
payload = queued_log_json["payload"]
queued_log_data = msgpack.unpackb(queued_log)
print(repr(queued_log_data))
payload = queued_log_data["payload"]
assert queued_log_json["type"] == "client_log"
assert queued_log_data["type"] == "client_log"
assert payload["timestamp"] == TIMESTAMP
assert (
payload["encoded_segments"]
== "gANdcQAoY3JpY2guc2VnbWVudApTZWdtZW50CnEBWA0AAABIZWxsbywgd29ybGQhcQJOTodxA4FxBGgBWAEAAAAKcQVOTodxBoFxB2Uu"
payload["segments"]
== b"\x80\x05\x95B\x00\x00\x00\x00\x00\x00\x00]\x94(\x8c\x0crich.segment\x94\x8c\x07Segment\x94\x93\x94\x8c\rHello, world!\x94NN\x87\x94\x81\x94h\x03\x8c\x01\n\x94NN\x87\x94\x81\x94e."
)
@@ -86,8 +88,10 @@ async def test_print_multiple_args_batched_as_one_log(devtools, in_memory_logfil
assert queued_log_json["type"] == "client_log"
assert payload["timestamp"] == TIMESTAMP
assert payload[
"encoded_segments"] == "gANdcQAoY3JpY2guc2VnbWVudApTZWdtZW50CnEBWBQAAABIZWxsbyB3b3JsZCBtdWx0aXBsZXECTk6HcQOBcQRoAVgBAAAACnEFTk6HcQaBcQdlLg=="
assert (
payload["encoded_segments"]
== "gANdcQAoY3JpY2guc2VnbWVudApTZWdtZW50CnEBWBQAAABIZWxsbyB3b3JsZCBtdWx0aXBsZXECTk6HcQOBcQRoAVgBAAAACnEFTk6HcQaBcQdlLg=="
)
assert len(payload["path"]) > 0
assert payload["line_number"] != 0