Skip to content

Commit d660b00

Browse files
committed
[fix] Make virtual modules compatible with Windows.
Signed-off-by: Michael Seifert <[email protected]>
1 parent 00aa282 commit d660b00

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

pytest_asyncio/plugin.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import enum
55
import functools
66
import inspect
7+
import os
78
import socket
89
import sys
910
import warnings
@@ -659,9 +660,7 @@ def _patched_collect():
659660
# to all items in the package.
660661
# see also https://github.com/pytest-dev/pytest/issues/11662#issuecomment-1879310072 # noqa
661662
# Possibly related to https://github.com/pytest-dev/pytest/issues/4085
662-
fixture_id = (
663-
str(Path(pkg_nodeid).joinpath("__init__.py")) + "::<event_loop>"
664-
)
663+
fixture_id = f"{pkg_nodeid}/__init__.py::<event_loop>".lstrip("/")
665664
# When collector is a Package, collector.obj is the package's
666665
# __init__.py. Accessing the __init__.py to attach the fixture function
667666
# may trigger additional module imports or change the order of imports,
@@ -677,13 +676,18 @@ def _patched_collect():
677676
dir=pkgdir,
678677
prefix="pytest_asyncio_virtual_module_",
679678
suffix=".py",
679+
delete=False, # Required for Windows compatibility
680680
) as virtual_module_file:
681681
virtual_module = Module.from_parent(
682682
collector, path=Path(virtual_module_file.name)
683683
)
684684
virtual_module_file.write(
685685
dedent(
686686
f"""\
687+
# This is a temporary file created by pytest-asyncio
688+
# If you see this file, a pytest run has crashed and
689+
# wasn't able to clean up the file in time.
690+
# You can safely remove this file.
687691
import asyncio
688692
import pytest
689693
from pytest_asyncio.plugin \
@@ -715,6 +719,7 @@ def scoped_event_loop(
715719
# see also https://github.com/pytest-dev/pytest/issues/11662#issuecomment-1879310072 # noqa
716720
fixturemanager.parsefactories(virtual_module.obj, nodeid=pkg_nodeid)
717721
yield virtual_module
722+
os.unlink(virtual_module_file.name)
718723
yield from collector.__original_collect()
719724

720725
collector.__original_collect = collector.collect

0 commit comments

Comments
 (0)