From 179040aab0b47c441ecb1abd2487ae6d281ae92d Mon Sep 17 00:00:00 2001 From: Mike Fiedler Date: Mon, 6 Nov 2023 16:21:16 -0500 Subject: [PATCH] chore: replace deprecated custom_predicates (#14864) --- tests/unit/forklift/test_action_routing.py | 11 ++++------ tests/unit/legacy/test_action_routing.py | 25 ++++------------------ warehouse/forklift/action_routing.py | 7 +----- warehouse/legacy/action_routing.py | 25 +++++++++++----------- 4 files changed, 22 insertions(+), 46 deletions(-) diff --git a/tests/unit/forklift/test_action_routing.py b/tests/unit/forklift/test_action_routing.py index e4200a405e0d..7662a483a3e8 100644 --- a/tests/unit/forklift/test_action_routing.py +++ b/tests/unit/forklift/test_action_routing.py @@ -15,23 +15,20 @@ from warehouse.forklift import action_routing -def test_add_legacy_action_route(monkeypatch): - pred = pretend.stub() - pypi_action = pretend.call_recorder(lambda name: pred) - monkeypatch.setattr(action_routing, "pypi_action", pypi_action) - +def test_add_legacy_action_route(): config = pretend.stub(add_route=pretend.call_recorder(lambda *a, **k: None)) action_routing.add_legacy_action_route(config, "the name", "the action") assert config.add_route.calls == [ - pretend.call("the name", "/legacy/", custom_predicates=[pred]) + pretend.call("the name", "/legacy/", pypi_action="the action") ] def test_includeme(): config = pretend.stub( - add_directive=pretend.call_recorder(lambda name, f, action_wrap: None) + add_route_predicate=pretend.call_recorder(lambda name, pred: None), + add_directive=pretend.call_recorder(lambda name, f, action_wrap: None), ) action_routing.includeme(config) diff --git a/tests/unit/legacy/test_action_routing.py b/tests/unit/legacy/test_action_routing.py index 978587d2a5aa..bd9697252454 100644 --- a/tests/unit/legacy/test_action_routing.py +++ b/tests/unit/legacy/test_action_routing.py @@ -11,41 +11,24 @@ # limitations under the License. import pretend -import pytest from warehouse.legacy import action_routing -@pytest.mark.parametrize( - ("action", "params", "expected"), - [ - ("foo", {":action": "foo"}, True), - ("foo", {":action": "bar"}, False), - ("bar", {}, False), - ], -) -def test_pypi_action(action, params, expected): - res = action_routing.pypi_action(action)({}, pretend.stub(params=params)) - assert res == expected - - -def test_add_pypi_action_route(monkeypatch): - pred = pretend.stub() - pypi_action = pretend.call_recorder(lambda name: pred) - monkeypatch.setattr(action_routing, "pypi_action", pypi_action) - +def test_add_pypi_action_route(): config = pretend.stub(add_route=pretend.call_recorder(lambda *a, **k: None)) action_routing.add_pypi_action_route(config, "the name", "the action") assert config.add_route.calls == [ - pretend.call("the name", "/pypi", custom_predicates=[pred]) + pretend.call("the name", "/pypi", pypi_action="the action") ] def test_includeme(): config = pretend.stub( - add_directive=pretend.call_recorder(lambda name, f, action_wrap: None) + add_route_predicate=pretend.call_recorder(lambda name, pred: None), + add_directive=pretend.call_recorder(lambda name, f, action_wrap: None), ) action_routing.includeme(config) diff --git a/warehouse/forklift/action_routing.py b/warehouse/forklift/action_routing.py index 406a94cd3304..a550031be2bd 100644 --- a/warehouse/forklift/action_routing.py +++ b/warehouse/forklift/action_routing.py @@ -10,14 +10,9 @@ # See the License for the specific language governing permissions and # limitations under the License. -from warehouse.legacy.action_routing import pypi_action - def add_legacy_action_route(config, name, action, **kwargs): - custom_predicates = kwargs.pop("custom_predicates", []) - custom_predicates += [pypi_action(action)] - - config.add_route(name, "/legacy/", custom_predicates=custom_predicates, **kwargs) + config.add_route(name, "/legacy/", pypi_action=action, **kwargs) def includeme(config): diff --git a/warehouse/legacy/action_routing.py b/warehouse/legacy/action_routing.py index f273375af62b..6da2a17c71a4 100644 --- a/warehouse/legacy/action_routing.py +++ b/warehouse/legacy/action_routing.py @@ -11,28 +11,29 @@ # limitations under the License. -def pypi_action(action): - def predicate(info, request): - return action == request.params.get(":action", None) +class PyPIActionPredicate: + def __init__(self, action: str, info): + self.action_name = action - return predicate + def text(self) -> str: + return f"pypi_action = {self.action_name}" + phash = text + + def __call__(self, context, request) -> bool: + return self.action_name == request.params.get(":action", None) -def add_pypi_action_route(config, name, action, **kwargs): - custom_predicates = kwargs.pop("custom_predicates", []) - custom_predicates += [pypi_action(action)] - config.add_route(name, "/pypi", custom_predicates=custom_predicates, **kwargs) +def add_pypi_action_route(config, name, action, **kwargs): + config.add_route(name, "/pypi", pypi_action=action, **kwargs) def add_pypi_action_redirect(config, action, target, **kwargs): - custom_predicates = kwargs.pop("custom_predicates", []) - custom_predicates += [pypi_action(action)] - - config.add_redirect("/pypi", target, custom_predicates=custom_predicates, **kwargs) + config.add_redirect("/pypi", target, pypi_action=action, **kwargs) def includeme(config): + config.add_route_predicate("pypi_action", PyPIActionPredicate) config.add_directive( "add_pypi_action_route", add_pypi_action_route, action_wrap=False )