Skip to content

Commit

Permalink
Fix MOTD for extra data (#2803)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahopkins authored Nov 28, 2023
1 parent 7d93c10 commit a0adf36
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 9 deletions.
10 changes: 9 additions & 1 deletion sanic/application/motd.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ def set_variables(self): # no cov
self.value_width = min(
max(map(len, self.data.values())), self.max_value_width
)
if self.extra:
self.key_width = max(
self.key_width, max(map(len, self.extra.keys()))
)
self.value_width = min(
max((*map(len, self.extra.values()), self.value_width)),
self.max_value_width,
)
self.logo_lines = self.logo.split("\n") if self.logo else []
self.logo_line_length = 24
self.centering_length = (
Expand Down Expand Up @@ -131,7 +139,7 @@ def display(self, version=True, action="Goin' Fast", out=None):
self._render_data(lines, self.data, 0)
if self.extra:
logo_part = self._get_logo_part(len(lines) - 4)
lines.append(f"| {logo_part}{display_filler}┤")
lines.append(f" {logo_part}{display_filler}┤")
self._render_data(lines, self.extra, len(lines) - 4)

self._render_fill(lines)
Expand Down
3 changes: 2 additions & 1 deletion sanic/mixins/startup.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@

class StartupMixin(metaclass=SanicMeta):
_app_registry: ClassVar[Dict[str, Sanic]]

name: str
asgi: bool
config: Config
listeners: Dict[str, List[ListenerType[Any]]]
Expand Down Expand Up @@ -790,6 +790,7 @@ def get_motd_data(
server = "ASGI" if self.asgi else "unknown" # type: ignore

display = {
"app": self.name,
"mode": " ".join(mode),
"server": server,
"python": platform.python_version(),
Expand Down
11 changes: 6 additions & 5 deletions tests/test_motd.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ def test_motd_with_expected_info(app, run_startup):
logs = run_startup(app)

assert logs[1][2] == f"Sanic v{__version__}"
assert logs[3][2] == "mode: debug, single worker"
assert logs[4][2] == "server: sanic, HTTP/1.1"
assert logs[5][2] == f"python: {platform.python_version()}"
assert logs[6][2] == f"platform: {platform.platform()}"
assert logs[3][2] == "app: test_motd_with_expected_info"
assert logs[4][2] == "mode: debug, single worker"
assert logs[5][2] == "server: sanic, HTTP/1.1"
assert logs[6][2] == f"python: {platform.python_version()}"
assert logs[7][2] == f"platform: {platform.platform()}"


def test_motd_init():
Expand All @@ -61,7 +62,7 @@ def test_motd_display(caplog):
│ │
├───────────────────────┬────────┤
│ foobar │ one: 1 │
| ├────────┤
├────────┤
│ │ two: 2 │
└───────────────────────┴────────┘
"""
Expand Down
4 changes: 2 additions & 2 deletions tests/test_static.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ def test_stack_trace_on_not_found(app, static_file_directory, caplog):
counter = Counter([(r[0], r[1]) for r in caplog.record_tuples])

assert response.status == 404
assert counter[("sanic.root", logging.INFO)] == 9
assert counter[("sanic.root", logging.INFO)] == 10
assert counter[("sanic.root", logging.ERROR)] == 0
assert counter[("sanic.error", logging.ERROR)] == 0
assert counter[("sanic.server", logging.INFO)] == 2
Expand All @@ -536,7 +536,7 @@ async def file_not_found(request, exception):
counter = Counter([(r[0], r[1]) for r in caplog.record_tuples])

assert response.status == 404
assert counter[("sanic.root", logging.INFO)] == 9
assert counter[("sanic.root", logging.INFO)] == 10
assert counter[("sanic.root", logging.ERROR)] == 0
assert counter[("sanic.error", logging.ERROR)] == 0
assert counter[("sanic.server", logging.INFO)] == 2
Expand Down

0 comments on commit a0adf36

Please sign in to comment.