From a7bc5e9eeae7c5c90898411962e9a74bf10a9cef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anton=20Gr=C3=BCbel?= Date: Wed, 18 Oct 2023 16:47:23 +0200 Subject: [PATCH] replace freezegun with time_machine (#7656) --- requirements/constraints.txt | 16 +++++++++++++--- requirements/dev.txt | 16 +++++++++++++--- requirements/test.in | 2 +- requirements/test.txt | 6 +++--- tests/test_cookiejar.py | 27 ++++++++++++++++++++++----- 5 files changed, 52 insertions(+), 15 deletions(-) diff --git a/requirements/constraints.txt b/requirements/constraints.txt index 097ba6d236c..30d0a44a5fa 100644 --- a/requirements/constraints.txt +++ b/requirements/constraints.txt @@ -67,8 +67,6 @@ exceptiongroup==1.1.2 # via pytest filelock==3.12.2 # via virtualenv -freezegun==1.2.2 - # via -r requirements/test.in frozenlist==1.4.0 # via # -r requirements/runtime-deps.in @@ -88,6 +86,10 @@ idna==3.4 # yarl imagesize==1.4.1 # via sphinx +importlib-metadata==6.8.0 + # via sphinx +importlib-resources==6.1.0 + # via towncrier incremental==22.10.0 # via towncrier iniconfig==2.0.0 @@ -156,9 +158,11 @@ pytest-cov==4.1.0 pytest-mock==3.11.1 # via -r requirements/test.in python-dateutil==2.8.2 - # via freezegun + # via time-machine python-on-whales==0.65.0 # via -r requirements/test.in +pytz==2023.3.post1 + # via babel pyyaml==6.0.1 # via pre-commit re-assert==1.1.0 @@ -202,6 +206,8 @@ sphinxcontrib-spelling==8.0.0 ; platform_system != "Windows" # via -r requirements/doc-spelling.in sphinxcontrib-towncrier==0.3.2a0 # via -r requirements/doc.in +time-machine==2.13.0 ; implementation_name == "cpython" + # via -r requirements/test.in tomli==2.0.1 # via # build @@ -249,6 +255,10 @@ wheel==0.41.0 # via pip-tools yarl==1.9.2 # via -r requirements/runtime-deps.in +zipp==3.17.0 + # via + # importlib-metadata + # importlib-resources # The following packages are considered to be unsafe in a requirements file: pip==23.2.1 diff --git a/requirements/dev.txt b/requirements/dev.txt index 4275f6f7afe..eeef9bb6155 100644 --- a/requirements/dev.txt +++ b/requirements/dev.txt @@ -65,8 +65,6 @@ exceptiongroup==1.1.2 # via pytest filelock==3.12.2 # via virtualenv -freezegun==1.2.2 - # via -r requirements/test.in frozenlist==1.4.0 # via # -r requirements/runtime-deps.in @@ -86,6 +84,10 @@ idna==3.4 # yarl imagesize==1.4.1 # via sphinx +importlib-metadata==6.8.0 + # via sphinx +importlib-resources==6.1.0 + # via towncrier incremental==22.10.0 # via towncrier iniconfig==2.0.0 @@ -151,9 +153,11 @@ pytest-cov==4.1.0 pytest-mock==3.11.1 # via -r requirements/test.in python-dateutil==2.8.2 - # via freezegun + # via time-machine python-on-whales==0.65.0 # via -r requirements/test.in +pytz==2023.3.post1 + # via babel pyyaml==6.0.1 # via pre-commit re-assert==1.1.0 @@ -194,6 +198,8 @@ sphinxcontrib-serializinghtml==1.1.5 # via sphinx sphinxcontrib-towncrier==0.3.2a0 # via -r requirements/doc.in +time-machine==2.13.0 ; implementation_name == "cpython" + # via -r requirements/test.in tomli==2.0.1 # via # build @@ -241,6 +247,10 @@ wheel==0.41.0 # via pip-tools yarl==1.9.2 # via -r requirements/runtime-deps.in +zipp==3.17.0 + # via + # importlib-metadata + # importlib-resources # The following packages are considered to be unsafe in a requirements file: pip==23.2.1 diff --git a/requirements/test.in b/requirements/test.in index 9e9161272bf..417d45959be 100644 --- a/requirements/test.in +++ b/requirements/test.in @@ -2,7 +2,6 @@ -c broken-projects.in coverage -freezegun mypy; implementation_name == "cpython" proxy.py pytest @@ -11,5 +10,6 @@ pytest-mock python-on-whales re-assert setuptools-git +time-machine; implementation_name == "cpython" trustme; platform_machine != "i686" # no 32-bit wheels wait-for-it diff --git a/requirements/test.txt b/requirements/test.txt index b76e408c661..329e5e6f968 100644 --- a/requirements/test.txt +++ b/requirements/test.txt @@ -32,8 +32,6 @@ cryptography==41.0.3 # via trustme exceptiongroup==1.1.2 # via pytest -freezegun==1.2.2 - # via -r requirements/test.in frozenlist==1.4.0 # via # -r requirements/runtime-deps.in @@ -79,7 +77,7 @@ pytest-cov==4.1.0 pytest-mock==3.11.1 # via -r requirements/test.in python-dateutil==2.8.2 - # via freezegun + # via time-machine python-on-whales==0.65.0 # via -r requirements/test.in re-assert==1.1.0 @@ -92,6 +90,8 @@ setuptools-git==1.2 # via -r requirements/test.in six==1.16.0 # via python-dateutil +time-machine==2.13.0 ; implementation_name == "cpython" + # via -r requirements/test.in tomli==2.0.1 # via # coverage diff --git a/tests/test_cookiejar.py b/tests/test_cookiejar.py index 755838fbeac..00a32708756 100644 --- a/tests/test_cookiejar.py +++ b/tests/test_cookiejar.py @@ -4,17 +4,22 @@ import itertools import pathlib import pickle +import sys import unittest from http.cookies import BaseCookie, Morsel, SimpleCookie from typing import Any from unittest import mock import pytest -from freezegun import freeze_time from yarl import URL from aiohttp import CookieJar, DummyCookieJar +try: + from time_machine import travel +except ImportError: + travel = None # type: ignore[assignment] + def dump_cookiejar() -> bytes: # pragma: no cover """Create pickled data for test_pickle_format().""" @@ -418,10 +423,10 @@ def timed_request(self, url: Any, update_time: Any, send_time: Any): elif isinstance(send_time, float): send_time = datetime.datetime.fromtimestamp(send_time) - with freeze_time(update_time): + with travel(update_time, tick=False): self.jar.update_cookies(self.cookies_to_send) - with freeze_time(send_time): + with travel(send_time, tick=False): cookies_sent = self.jar.filter_cookies(URL(url)) self.jar.clear() @@ -608,6 +613,10 @@ def test_path_value(self) -> None: self.assertEqual(cookies_received["path-cookie"]["path"], "/somepath") self.assertEqual(cookies_received["wrong-path-cookie"]["path"], "/") + @unittest.skipIf( + sys.implementation.name != "cpython", + reason="time_machine leverages CPython specific pointers https://github.com/adamchainz/time-machine/issues/305", + ) def test_expires(self) -> None: ts_before = datetime.datetime( 1975, 1, 1, tzinfo=datetime.timezone.utc @@ -629,6 +638,10 @@ def test_expires(self) -> None: self.assertEqual(set(cookies_sent.keys()), {"shared-cookie"}) + @unittest.skipIf( + sys.implementation.name != "cpython", + reason="time_machine leverages CPython specific pointers https://github.com/adamchainz/time-machine/issues/305", + ) def test_max_age(self) -> None: cookies_sent = self.timed_request("http://maxagetest.com/", 1000, 1000) @@ -776,6 +789,10 @@ async def test_cookie_jar_clear_all() -> None: assert len(sut) == 0 +@pytest.mark.skipif( + sys.implementation.name != "cpython", + reason="time_machine leverages CPython specific pointers https://github.com/adamchainz/time-machine/issues/305", +) async def test_cookie_jar_clear_expired(): sut = CookieJar() @@ -784,11 +801,11 @@ async def test_cookie_jar_clear_expired(): cookie["foo"] = "bar" cookie["foo"]["expires"] = "Tue, 1 Jan 1990 12:00:00 GMT" - with freeze_time("1980-01-01"): + with travel("1980-01-01", tick=False): sut.update_cookies(cookie) sut.clear(lambda x: False) - with freeze_time("1980-01-01"): + with travel("1980-01-01", tick=False): assert len(sut) == 0