From 206992fe40e27e30ab3ba0bf9f77f6a70ff11f4d Mon Sep 17 00:00:00 2001 From: Adam Hopkins Date: Tue, 5 Jan 2021 16:57:35 +0200 Subject: [PATCH] #1989 Add disable app registry (#1993) * Bump to v20.12 * Update Changelog * Add disable app registry * squash --- sanic/app.py | 7 ++++++- sanic/config.py | 1 + tests/test_app.py | 16 ++++++++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/sanic/app.py b/sanic/app.py index 81cd66d1..7ddbbdbc 100644 --- a/sanic/app.py +++ b/sanic/app.py @@ -50,6 +50,7 @@ class Sanic: strict_slashes=False, log_config=None, configure_logging=True, + register=None, ): # Get name from previous stack frame @@ -88,7 +89,11 @@ class Sanic: # Register alternative method names self.go_fast = self.run - self.__class__.register_app(self) + if register is not None: + self.config.REGISTER = register + + if self.config.REGISTER: + self.__class__.register_app(self) @property def loop(self): diff --git a/sanic/config.py b/sanic/config.py index 55ab7b51..e3d50227 100644 --- a/sanic/config.py +++ b/sanic/config.py @@ -40,6 +40,7 @@ DEFAULT_CONFIG = { "PROXIES_COUNT": None, "FORWARDED_FOR_HEADER": "X-Forwarded-For", "FALLBACK_ERROR_FORMAT": "html", + "REGISTER": True, } diff --git a/tests/test_app.py b/tests/test_app.py index 7527b1c2..36f30375 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -3,6 +3,7 @@ import logging import sys from inspect import isawaitable +from os import environ from unittest.mock import patch import pytest @@ -290,6 +291,7 @@ def test_app_registry_name_reuse(): with pytest.raises(SanicException): Sanic("test") Sanic.test_mode = True + Sanic("test") def test_app_registry_retrieval(): @@ -306,3 +308,17 @@ def test_get_app_does_not_exist_force_create(): assert isinstance( Sanic.get_app("does-not-exist", force_create=True), Sanic ) + + +def test_app_no_registry(): + Sanic("no-register", register=False) + with pytest.raises(SanicException): + Sanic.get_app("no-register") + + +def test_app_no_registry_env(): + environ["SANIC_REGISTER"] = "False" + Sanic("no-register") + with pytest.raises(SanicException): + Sanic.get_app("no-register") + del environ["SANIC_REGISTER"]