Skip to content

Commit

Permalink
chore: replace deprecated custom_predicates (pypi#14864)
Browse files Browse the repository at this point in the history
  • Loading branch information
miketheman authored Nov 6, 2023
1 parent 6d0ff7b commit 179040a
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 46 deletions.
11 changes: 4 additions & 7 deletions tests/unit/forklift/test_action_routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
25 changes: 4 additions & 21 deletions tests/unit/legacy/test_action_routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 1 addition & 6 deletions warehouse/forklift/action_routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
25 changes: 13 additions & 12 deletions warehouse/legacy/action_routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down

0 comments on commit 179040a

Please sign in to comment.