diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 42c20ab1..601dec38 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -46,3 +46,35 @@ jobs: - name: Run tests run: tox -e py + + import-mode-importlib: + name: import-mode=importlib + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@master + + - name: Set up Python + uses: actions/setup-python@v2 + + - name: Install tox + run: pip install tox + + - name: Run tests + run: tox -e import-mode-importlib + + import-mode-prepend: + name: import-mode=prepend + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@master + + - name: Set up Python + uses: actions/setup-python@v2 + + - name: Install tox + run: pip install tox + + - name: Run tests + run: tox -e import-mode-prepend diff --git a/tests/test_importlib.py b/tests/test_importlib.py new file mode 100644 index 00000000..749433d2 --- /dev/null +++ b/tests/test_importlib.py @@ -0,0 +1,17 @@ +from datetime import datetime as from_datetime +import datetime as toplevel_datetime + +from freezegun import freeze_time + + +def test_from_datetime(): + # Importing the datetime class doesn't work when pytest is using import-mode=importlib. + # This is supposed to become the default, according to the documentation: https://docs.pytest.org/en/latest/changelog.html?highlight=importlib#pytest-6-0-0rc1-2020-07-08 + with freeze_time("2019-01-01"): + assert from_datetime.utcnow().year == 2019 + + +def test_top_level_datetime(): + # freezegun works as expected when we're importing the datetime module + with freeze_time("2019-01-01"): + assert toplevel_datetime.datetime.utcnow().year == 2019 diff --git a/tox.ini b/tox.ini index 917e9368..4778eae0 100644 --- a/tox.ini +++ b/tox.ini @@ -4,12 +4,20 @@ # and then run "tox" from this directory. [tox] -envlist = py36, py37, py38, py39, pypy3, mypy +envlist = py36, py37, py38, py39, pypy3, mypy, import-mode-importlib, import-mode-prepend [testenv] commands = pytest --cov {posargs} deps = -rrequirements.txt +[testenv:import-mode-importlib] +commands = pytest --import-mode=importlib tests/test_importlib.py +deps = -rrequirements.txt + +[testenv:import-mode-prepend] +commands = pytest --import-mode=prepend tests/test_importlib.py +deps = -rrequirements.txt + [testenv:mypy] deps = mypy