Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Coffee please #2316

Merged
merged 3 commits into from Nov 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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