Fix race conditions with accessing working directory

This commit is contained in:
Yurii Motov
2026-02-24 18:09:40 +01:00
parent e13ffa2a21
commit 24b2144fa5
10 changed files with 34 additions and 3 deletions

View File

@@ -6,7 +6,7 @@ import pytest
from fastapi.testclient import TestClient
from inline_snapshot import snapshot
from tests.utils import needs_py310
from tests.utils import needs_py310, workdir_lock
@pytest.fixture(
@@ -29,6 +29,7 @@ def test_path_operation(client: TestClient):
assert response.json() == {"id": "foo", "value": "there goes my hero"}
@workdir_lock
def test_path_operation_img(client: TestClient):
shutil.copy("./docs/en/docs/img/favicon.png", "./image.png")
response = client.get("/items/foo?img=1")

View File

@@ -6,7 +6,7 @@ import pytest
from fastapi.testclient import TestClient
from inline_snapshot import snapshot
from tests.utils import needs_py310
from tests.utils import needs_py310, workdir_lock
@pytest.fixture(
@@ -29,6 +29,7 @@ def test_path_operation(client: TestClient):
assert response.json() == {"id": "foo", "value": "there goes my hero"}
@workdir_lock
def test_path_operation_img(client: TestClient):
shutil.copy("./docs/en/docs/img/favicon.png", "./image.png")
response = client.get("/items/foo?img=1")

View File

@@ -4,10 +4,12 @@ from pathlib import Path
from fastapi.testclient import TestClient
from docs_src.background_tasks.tutorial001_py310 import app
from tests.utils import workdir_lock
client = TestClient(app)
@workdir_lock
def test():
log = Path("log.txt")
if log.is_file():

View File

@@ -5,7 +5,7 @@ from pathlib import Path
import pytest
from fastapi.testclient import TestClient
from ...utils import needs_py310
from tests.utils import needs_py310, workdir_lock
@pytest.fixture(
@@ -22,6 +22,7 @@ def get_client(request: pytest.FixtureRequest):
return client
@workdir_lock
def test(client: TestClient):
log = Path("log.txt")
if log.is_file():

View File

@@ -4,6 +4,8 @@ from pathlib import Path
import pytest
from fastapi.testclient import TestClient
from tests.utils import workdir_lock
@pytest.fixture(scope="module")
def client():
@@ -17,6 +19,7 @@ def client():
static_dir.rmdir()
@workdir_lock
def test_swagger_ui_html(client: TestClient):
response = client.get("/docs")
assert response.status_code == 200, response.text
@@ -24,18 +27,21 @@ def test_swagger_ui_html(client: TestClient):
assert "https://unpkg.com/swagger-ui-dist@5/swagger-ui.css" in response.text
@workdir_lock
def test_swagger_ui_oauth2_redirect_html(client: TestClient):
response = client.get("/docs/oauth2-redirect")
assert response.status_code == 200, response.text
assert "window.opener.swaggerUIRedirectOauth2" in response.text
@workdir_lock
def test_redoc_html(client: TestClient):
response = client.get("/redoc")
assert response.status_code == 200, response.text
assert "https://unpkg.com/redoc@2/bundles/redoc.standalone.js" in response.text
@workdir_lock
def test_api(client: TestClient):
response = client.get("/users/john")
assert response.status_code == 200, response.text

View File

@@ -4,6 +4,8 @@ from pathlib import Path
import pytest
from fastapi.testclient import TestClient
from tests.utils import workdir_lock
@pytest.fixture(scope="module")
def client():
@@ -17,6 +19,7 @@ def client():
static_dir.rmdir()
@workdir_lock
def test_swagger_ui_html(client: TestClient):
response = client.get("/docs")
assert response.status_code == 200, response.text
@@ -24,18 +27,21 @@ def test_swagger_ui_html(client: TestClient):
assert "/static/swagger-ui.css" in response.text
@workdir_lock
def test_swagger_ui_oauth2_redirect_html(client: TestClient):
response = client.get("/docs/oauth2-redirect")
assert response.status_code == 200, response.text
assert "window.opener.swaggerUIRedirectOauth2" in response.text
@workdir_lock
def test_redoc_html(client: TestClient):
response = client.get("/redoc")
assert response.status_code == 200, response.text
assert "/static/redoc.standalone.js" in response.text
@workdir_lock
def test_api(client: TestClient):
response = client.get("/users/john")
assert response.status_code == 200, response.text

View File

@@ -3,6 +3,8 @@ from fastapi import FastAPI
from fastapi.testclient import TestClient
from inline_snapshot import snapshot
from tests.utils import workdir_lock
@pytest.fixture(name="app", scope="module")
def get_app():
@@ -11,6 +13,7 @@ def get_app():
yield app
@workdir_lock
def test_events(app: FastAPI):
with TestClient(app) as client:
response = client.get("/items/")
@@ -20,6 +23,7 @@ def test_events(app: FastAPI):
assert "Application shutdown" in log.read()
@workdir_lock
def test_openapi_schema(app: FastAPI):
with TestClient(app) as client:
response = client.get("/openapi.json")

View File

@@ -5,6 +5,8 @@ import pytest
from fastapi.testclient import TestClient
from inline_snapshot import snapshot
from tests.utils import workdir_lock
@pytest.fixture(scope="module")
def client():
@@ -20,17 +22,20 @@ def client():
static_dir.rmdir()
@workdir_lock
def test_static_files(client: TestClient):
response = client.get("/static/sample.txt")
assert response.status_code == 200, response.text
assert response.text == "This is a sample static file."
@workdir_lock
def test_static_files_not_found(client: TestClient):
response = client.get("/static/non_existent_file.txt")
assert response.status_code == 404, response.text
@workdir_lock
def test_openapi_schema(client: TestClient):
response = client.get("/openapi.json")
assert response.status_code == 200, response.text

View File

@@ -3,7 +3,10 @@ import shutil
from fastapi.testclient import TestClient
from tests.utils import workdir_lock
@workdir_lock
def test_main():
if os.path.isdir("./static"): # pragma: nocover
shutil.rmtree("./static")

View File

@@ -9,6 +9,8 @@ needs_py314 = pytest.mark.skipif(
sys.version_info < (3, 14), reason="requires python3.14+"
)
workdir_lock = pytest.mark.xdist_group("workdir_lock")
def skip_module_if_py_gte_314():
"""Skip entire module on Python 3.14+ at import time."""