Skip to content

Commit

Permalink
[fix] Make virtual modules compatible with Windows.
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Seifert <m.seifert@digitalernachschub.de>
  • Loading branch information
seifertm committed Jan 10, 2024
1 parent df487d5 commit 8ba9bd0
Showing 1 changed file with 8 additions and 3 deletions.
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

0 comments on commit 8ba9bd0

Please sign in to comment.