Skip to content

Commit

Permalink
Coffee please (#2316)
Browse files Browse the repository at this point in the history
* Coffee please

* Add unit tests
  • Loading branch information
ahopkins committed Nov 18, 2021
1 parent 0860bfe commit 95631b9
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 3 deletions.
6 changes: 5 additions & 1 deletion sanic/app.py
Expand Up @@ -960,6 +960,10 @@ def asgi_client(self): # noqa
# Execution
# -------------------------------------------------------------------- #

def make_coffee(self, *args, **kwargs):
self.state.coffee = True
self.run(*args, **kwargs)

def run(
self,
host: Optional[str] = None,
Expand Down Expand Up @@ -1562,7 +1566,7 @@ def motd(self, serve_location):
extra.update(self.config.MOTD_DISPLAY)

logo = (
get_logo()
get_logo(coffee=self.state.coffee)
if self.config.LOGO == "" or self.config.LOGO is True
else self.config.LOGO
)
Expand Down
13 changes: 11 additions & 2 deletions sanic/application/logo.py
Expand Up @@ -10,6 +10,15 @@
Build Fast. Run Fast.
"""
COFFEE_LOGO = """\033[48;2;255;13;104m \033[0m
\033[38;2;255;255;255;48;2;255;13;104m ▄████████▄ \033[0m
\033[38;2;255;255;255;48;2;255;13;104m ██ ██▀▀▄ \033[0m
\033[38;2;255;255;255;48;2;255;13;104m ███████████ █ \033[0m
\033[38;2;255;255;255;48;2;255;13;104m ███████████▄▄▀ \033[0m
\033[38;2;255;255;255;48;2;255;13;104m ▀███████▀ \033[0m
\033[48;2;255;13;104m \033[0m
Dark roast. No sugar."""

COLOR_LOGO = """\033[48;2;255;13;104m \033[0m
\033[38;2;255;255;255;48;2;255;13;104m ▄███ █████ ██ \033[0m
\033[38;2;255;255;255;48;2;255;13;104m ██ \033[0m
Expand All @@ -32,9 +41,9 @@
ansi_pattern = re.compile(r"\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])")


def get_logo(full=False):
def get_logo(full=False, coffee=False):
logo = (
(FULL_COLOR_LOGO if full else COLOR_LOGO)
(FULL_COLOR_LOGO if full else (COFFEE_LOGO if coffee else COLOR_LOGO))
if sys.stdout.isatty()
else BASE_LOGO
)
Expand Down
1 change: 1 addition & 0 deletions sanic/application/state.py
Expand Up @@ -34,6 +34,7 @@ class Mode(StrEnum):
class ApplicationState:
app: Sanic
asgi: bool = field(default=False)
coffee: bool = field(default=False)
fast: bool = field(default=False)
host: str = field(default="")
mode: Mode = field(default=Mode.PRODUCTION)
Expand Down
48 changes: 48 additions & 0 deletions tests/test_coffee.py
@@ -0,0 +1,48 @@
import logging

from unittest.mock import patch

import pytest

from sanic.application.logo import COFFEE_LOGO, get_logo
from sanic.exceptions import SanicException


def has_sugar(value):
if value:
raise SanicException("I said no sugar please")

return False


@pytest.mark.parametrize("sugar", (True, False))
def test_no_sugar(sugar):
if sugar:
with pytest.raises(SanicException):
assert has_sugar(sugar)
else:
assert not has_sugar(sugar)


def test_get_logo_returns_expected_logo():
with patch("sys.stdout.isatty") as isatty:
isatty.return_value = True
logo = get_logo(coffee=True)
assert logo is COFFEE_LOGO


def test_logo_true(app, caplog):
@app.after_server_start
async def shutdown(*_):
app.stop()

with patch("sys.stdout.isatty") as isatty:
isatty.return_value = True
with caplog.at_level(logging.DEBUG):
app.make_coffee()

# Only in the regular logo
assert " ▄███ █████ ██ " not in caplog.text

# Only in the coffee logo
assert " ██ ██▀▀▄ " in caplog.text

0 comments on commit 95631b9

Please sign in to comment.