Coffee please (#2316)

* Coffee please

* Add unit tests
This commit is contained in:
Adam Hopkins 2021-11-18 14:53:06 +02:00 committed by GitHub
parent 0860bfe1f1
commit 95631b9686
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 65 additions and 3 deletions

View File

@ -960,6 +960,10 @@ class Sanic(BaseSanic, metaclass=TouchUpMeta):
# Execution
# -------------------------------------------------------------------- #
def make_coffee(self, *args, **kwargs):
self.state.coffee = True
self.run(*args, **kwargs)
def run(
self,
host: Optional[str] = None,
@ -1562,7 +1566,7 @@ class Sanic(BaseSanic, metaclass=TouchUpMeta):
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
)

View File

@ -10,6 +10,15 @@ BASE_LOGO = """
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
@ -32,9 +41,9 @@ FULL_COLOR_LOGO = """
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
)

View File

@ -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)

48
tests/test_coffee.py Normal file
View File

@ -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