Browse Source

Fix race conditions with accessing working directory

pull/14992/head
Yurii Motov 5 months ago
parent
commit
24b2144fa5
  1. 3
      tests/test_tutorial/test_additional_responses/test_tutorial002.py
  2. 3
      tests/test_tutorial/test_additional_responses/test_tutorial004.py
  3. 2
      tests/test_tutorial/test_background_tasks/test_tutorial001.py
  4. 3
      tests/test_tutorial/test_background_tasks/test_tutorial002.py
  5. 6
      tests/test_tutorial/test_custom_docs_ui/test_tutorial001.py
  6. 6
      tests/test_tutorial/test_custom_docs_ui/test_tutorial002.py
  7. 4
      tests/test_tutorial/test_events/test_tutorial002.py
  8. 5
      tests/test_tutorial/test_static_files/test_tutorial001.py
  9. 3
      tests/test_tutorial/test_templates/test_tutorial001.py
  10. 2
      tests/utils.py

3
tests/test_tutorial/test_additional_responses/test_tutorial002.py

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

3
tests/test_tutorial/test_additional_responses/test_tutorial004.py

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

2
tests/test_tutorial/test_background_tasks/test_tutorial001.py

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

3
tests/test_tutorial/test_background_tasks/test_tutorial002.py

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

6
tests/test_tutorial/test_custom_docs_ui/test_tutorial001.py

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

6
tests/test_tutorial/test_custom_docs_ui/test_tutorial002.py

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

4
tests/test_tutorial/test_events/test_tutorial002.py

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

5
tests/test_tutorial/test_static_files/test_tutorial001.py

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

3
tests/test_tutorial/test_templates/test_tutorial001.py

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

2
tests/utils.py

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

Loading…
Cancel
Save