From 8ba9bd03c44584929c7251cf1383df23f5e14f44 Mon Sep 17 00:00:00 2001 From: Michael Seifert Date: Wed, 10 Jan 2024 19:15:09 +0100 Subject: [PATCH] [fix] Make virtual modules compatible with Windows. Signed-off-by: Michael Seifert --- pytest_asyncio/plugin.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/pytest_asyncio/plugin.py b/pytest_asyncio/plugin.py index cfc28e8d..a1bdbd91 100644 --- a/pytest_asyncio/plugin.py +++ b/pytest_asyncio/plugin.py @@ -4,6 +4,7 @@ import enum import functools import inspect +import os import socket import sys import warnings @@ -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")) + "::" - ) + fixture_id = f"{pkg_nodeid}/__init__.py::".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, @@ -677,6 +676,7 @@ 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) @@ -684,6 +684,10 @@ def _patched_collect(): 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 \ @@ -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