Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes a bug that breaks virtual modules on Windows #746

Merged
merged 4 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,12 @@ jobs:
path: dist

test:
name: Python ${{ matrix.python-version }}
runs-on: ubuntu-latest
name: ${{ matrix.os }} - Python ${{ matrix.python-version }}
runs-on: ${{ matrix.os }}-latest

strategy:
matrix:
os: [ubuntu, windows]
python-version: ['3.8', '3.9', '3.10', '3.11', '3.12']

steps:
Expand All @@ -75,7 +76,6 @@ jobs:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
set -xe
python -VV
python -m site
python -m pip install --upgrade pip
Expand All @@ -85,6 +85,7 @@ jobs:

- name: Store coverage data
uses: actions/upload-artifact@v3
if: "!endsWith(matrix.os, 'windows')"
with:
name: coverage-per-interpreter
path: .coverage.*
Expand Down
11 changes: 8 additions & 3 deletions pytest_asyncio/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import enum
import functools
import inspect
import os
import socket
import sys
import warnings
Expand Down Expand Up @@ -659,9 +660,7 @@ def _patched_collect():
# to all items in the package.
# see also https://github.com/pytest-dev/pytest/issues/11662#issuecomment-1879310072 # noqa
# Possibly related to https://github.com/pytest-dev/pytest/issues/4085
fixture_id = (
str(Path(pkg_nodeid).joinpath("__init__.py")) + "::<event_loop>"
)
fixture_id = f"{pkg_nodeid}/__init__.py::<event_loop>".lstrip("/")
# When collector is a Package, collector.obj is the package's
# __init__.py. Accessing the __init__.py to attach the fixture function
# may trigger additional module imports or change the order of imports,
Expand All @@ -677,13 +676,18 @@ def _patched_collect():
dir=pkgdir,
prefix="pytest_asyncio_virtual_module_",
suffix=".py",
delete=False, # Required for Windows compatibility
) as virtual_module_file:
virtual_module = Module.from_parent(
collector, path=Path(virtual_module_file.name)
)
virtual_module_file.write(
dedent(
f"""\
# This is a temporary file created by pytest-asyncio
# If you see this file, a pytest run has crashed and
# wasn't able to clean up the file in time.
# You can safely remove this file.
import asyncio
import pytest
from pytest_asyncio.plugin \
Expand Down Expand Up @@ -715,6 +719,7 @@ def scoped_event_loop(
# see also https://github.com/pytest-dev/pytest/issues/11662#issuecomment-1879310072 # noqa
fixturemanager.parsefactories(virtual_module.obj, nodeid=pkg_nodeid)
yield virtual_module
os.unlink(virtual_module_file.name)
yield from collector.__original_collect()

collector.__original_collect = collector.collect
Expand Down
2 changes: 1 addition & 1 deletion tests/test_subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
# The default asyncio event loop implementation on Windows does not
# support subprocesses. Subprocesses are available for Windows if a
# ProactorEventLoop is used.
@pytest.yield_fixture()
@pytest.fixture()
def event_loop():
loop = asyncio.ProactorEventLoop()
yield loop
Expand Down