]> git.ipfire.org Git - thirdparty/starlette.git/commitdiff
Add type hints to `test_staticfiles.py` (#2491)
authorScirlat Danut <danut.scirlat@gmail.com>
Fri, 9 Feb 2024 08:58:52 +0000 (10:58 +0200)
committerGitHub <noreply@github.com>
Fri, 9 Feb 2024 08:58:52 +0000 (08:58 +0000)
* Add type hints to test_staticfiles.py

* Remove any

* Remove ParamSpec

* Remove useless conditional

---------

Co-authored-by: Scirlat Danut <scirlatdanut@scirlats-mini.lan>
Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
tests/conftest.py
tests/test_staticfiles.py

index 224f4ac52dc8604bdacc1e1b9fae785177112f44..1a61664d17c58cbcc75a2992b4eb3f7b74a0c754 100644 (file)
@@ -1,5 +1,5 @@
 import functools
-from typing import Any, Callable, Dict
+from typing import Any, Callable, Dict, Literal
 
 import pytest
 
@@ -10,7 +10,7 @@ TestClientFactory = Callable[..., TestClient]
 
 @pytest.fixture
 def test_client_factory(
-    anyio_backend_name: str,
+    anyio_backend_name: Literal["asyncio", "trio"],
     anyio_backend_options: Dict[str, Any],
 ) -> TestClientFactory:
     # anyio_backend_name defined by:
index 2bb730566f3382300eac6a1ca541d7e16502b445..d20bb7ef7a0408d9d726b4d244ef2d7625df35ff 100644 (file)
@@ -2,6 +2,7 @@ import os
 import stat
 import tempfile
 import time
+import typing
 from pathlib import Path
 
 import anyio
@@ -10,13 +11,17 @@ import pytest
 from starlette.applications import Starlette
 from starlette.exceptions import HTTPException
 from starlette.middleware import Middleware
-from starlette.middleware.base import BaseHTTPMiddleware
+from starlette.middleware.base import BaseHTTPMiddleware, RequestResponseEndpoint
 from starlette.requests import Request
+from starlette.responses import Response
 from starlette.routing import Mount
 from starlette.staticfiles import StaticFiles
+from starlette.testclient import TestClient
 
+TestClientFactory = typing.Callable[..., TestClient]
 
-def test_staticfiles(tmpdir, test_client_factory):
+
+def test_staticfiles(tmpdir: Path, test_client_factory: TestClientFactory) -> None:
     path = os.path.join(tmpdir, "example.txt")
     with open(path, "w") as file:
         file.write("<file content>")
@@ -28,7 +33,9 @@ def test_staticfiles(tmpdir, test_client_factory):
     assert response.text == "<file content>"
 
 
-def test_staticfiles_with_pathlib(tmp_path: Path, test_client_factory):
+def test_staticfiles_with_pathlib(
+    tmp_path: Path, test_client_factory: TestClientFactory
+) -> None:
     path = tmp_path / "example.txt"
     with open(path, "w") as file:
         file.write("<file content>")
@@ -40,7 +47,9 @@ def test_staticfiles_with_pathlib(tmp_path: Path, test_client_factory):
     assert response.text == "<file content>"
 
 
-def test_staticfiles_head_with_middleware(tmpdir, test_client_factory):
+def test_staticfiles_head_with_middleware(
+    tmpdir: Path, test_client_factory: TestClientFactory
+) -> None:
     """
     see https://github.com/encode/starlette/pull/935
     """
@@ -48,7 +57,9 @@ def test_staticfiles_head_with_middleware(tmpdir, test_client_factory):
     with open(path, "w") as file:
         file.write("x" * 100)
 
-    async def does_nothing_middleware(request: Request, call_next):
+    async def does_nothing_middleware(
+        request: Request, call_next: RequestResponseEndpoint
+    ) -> Response:
         response = await call_next(request)
         return response
 
@@ -62,7 +73,7 @@ def test_staticfiles_head_with_middleware(tmpdir, test_client_factory):
     assert response.headers.get("content-length") == "100"
 
 
-def test_staticfiles_with_package(test_client_factory):
+def test_staticfiles_with_package(test_client_factory: TestClientFactory) -> None:
     app = StaticFiles(packages=["tests"])
     client = test_client_factory(app)
     response = client.get("/example.txt")
@@ -76,7 +87,7 @@ def test_staticfiles_with_package(test_client_factory):
     assert response.text == "123\n"
 
 
-def test_staticfiles_post(tmpdir, test_client_factory):
+def test_staticfiles_post(tmpdir: Path, test_client_factory: TestClientFactory) -> None:
     path = os.path.join(tmpdir, "example.txt")
     with open(path, "w") as file:
         file.write("<file content>")
@@ -90,7 +101,9 @@ def test_staticfiles_post(tmpdir, test_client_factory):
     assert response.text == "Method Not Allowed"
 
 
-def test_staticfiles_with_directory_returns_404(tmpdir, test_client_factory):
+def test_staticfiles_with_directory_returns_404(
+    tmpdir: Path, test_client_factory: TestClientFactory
+) -> None:
     path = os.path.join(tmpdir, "example.txt")
     with open(path, "w") as file:
         file.write("<file content>")
@@ -104,7 +117,9 @@ def test_staticfiles_with_directory_returns_404(tmpdir, test_client_factory):
     assert response.text == "Not Found"
 
 
-def test_staticfiles_with_missing_file_returns_404(tmpdir, test_client_factory):
+def test_staticfiles_with_missing_file_returns_404(
+    tmpdir: Path, test_client_factory: TestClientFactory
+) -> None:
     path = os.path.join(tmpdir, "example.txt")
     with open(path, "w") as file:
         file.write("<file content>")
@@ -118,14 +133,16 @@ def test_staticfiles_with_missing_file_returns_404(tmpdir, test_client_factory):
     assert response.text == "Not Found"
 
 
-def test_staticfiles_instantiated_with_missing_directory(tmpdir):
+def test_staticfiles_instantiated_with_missing_directory(tmpdir: Path) -> None:
     with pytest.raises(RuntimeError) as exc_info:
         path = os.path.join(tmpdir, "no_such_directory")
         StaticFiles(directory=path)
     assert "does not exist" in str(exc_info.value)
 
 
-def test_staticfiles_configured_with_missing_directory(tmpdir, test_client_factory):
+def test_staticfiles_configured_with_missing_directory(
+    tmpdir: Path, test_client_factory: TestClientFactory
+) -> None:
     path = os.path.join(tmpdir, "no_such_directory")
     app = StaticFiles(directory=path, check_dir=False)
     client = test_client_factory(app)
@@ -135,8 +152,8 @@ def test_staticfiles_configured_with_missing_directory(tmpdir, test_client_facto
 
 
 def test_staticfiles_configured_with_file_instead_of_directory(
-    tmpdir, test_client_factory
-):
+    tmpdir: Path, test_client_factory: TestClientFactory
+) -> None:
     path = os.path.join(tmpdir, "example.txt")
     with open(path, "w") as file:
         file.write("<file content>")
@@ -148,7 +165,9 @@ def test_staticfiles_configured_with_file_instead_of_directory(
     assert "is not a directory" in str(exc_info.value)
 
 
-def test_staticfiles_config_check_occurs_only_once(tmpdir, test_client_factory):
+def test_staticfiles_config_check_occurs_only_once(
+    tmpdir: Path, test_client_factory: TestClientFactory
+) -> None:
     app = StaticFiles(directory=tmpdir)
     client = test_client_factory(app)
     assert not app.config_checked
@@ -162,7 +181,7 @@ def test_staticfiles_config_check_occurs_only_once(tmpdir, test_client_factory):
         client.get("/")
 
 
-def test_staticfiles_prevents_breaking_out_of_directory(tmpdir):
+def test_staticfiles_prevents_breaking_out_of_directory(tmpdir: Path) -> None:
     directory = os.path.join(tmpdir, "foo")
     os.mkdir(directory)
 
@@ -182,7 +201,9 @@ def test_staticfiles_prevents_breaking_out_of_directory(tmpdir):
     assert exc_info.value.detail == "Not Found"
 
 
-def test_staticfiles_never_read_file_for_head_method(tmpdir, test_client_factory):
+def test_staticfiles_never_read_file_for_head_method(
+    tmpdir: Path, test_client_factory: TestClientFactory
+) -> None:
     path = os.path.join(tmpdir, "example.txt")
     with open(path, "w") as file:
         file.write("<file content>")
@@ -195,7 +216,9 @@ def test_staticfiles_never_read_file_for_head_method(tmpdir, test_client_factory
     assert response.headers["content-length"] == "14"
 
 
-def test_staticfiles_304_with_etag_match(tmpdir, test_client_factory):
+def test_staticfiles_304_with_etag_match(
+    tmpdir: Path, test_client_factory: TestClientFactory
+) -> None:
     path = os.path.join(tmpdir, "example.txt")
     with open(path, "w") as file:
         file.write("<file content>")
@@ -216,8 +239,8 @@ def test_staticfiles_304_with_etag_match(tmpdir, test_client_factory):
 
 
 def test_staticfiles_304_with_last_modified_compare_last_req(
-    tmpdir, test_client_factory
-):
+    tmpdir: Path, test_client_factory: TestClientFactory
+) -> None:
     path = os.path.join(tmpdir, "example.txt")
     file_last_modified_time = time.mktime(
         time.strptime("2013-10-10 23:40:00", "%Y-%m-%d %H:%M:%S")
@@ -242,7 +265,9 @@ def test_staticfiles_304_with_last_modified_compare_last_req(
     assert response.content == b"<file content>"
 
 
-def test_staticfiles_html_normal(tmpdir, test_client_factory):
+def test_staticfiles_html_normal(
+    tmpdir: Path, test_client_factory: TestClientFactory
+) -> None:
     path = os.path.join(tmpdir, "404.html")
     with open(path, "w") as file:
         file.write("<h1>Custom not found page</h1>")
@@ -275,7 +300,9 @@ def test_staticfiles_html_normal(tmpdir, test_client_factory):
     assert response.text == "<h1>Custom not found page</h1>"
 
 
-def test_staticfiles_html_without_index(tmpdir, test_client_factory):
+def test_staticfiles_html_without_index(
+    tmpdir: Path, test_client_factory: TestClientFactory
+) -> None:
     path = os.path.join(tmpdir, "404.html")
     with open(path, "w") as file:
         file.write("<h1>Custom not found page</h1>")
@@ -300,7 +327,9 @@ def test_staticfiles_html_without_index(tmpdir, test_client_factory):
     assert response.text == "<h1>Custom not found page</h1>"
 
 
-def test_staticfiles_html_without_404(tmpdir, test_client_factory):
+def test_staticfiles_html_without_404(
+    tmpdir: Path, test_client_factory: TestClientFactory
+) -> None:
     path = os.path.join(tmpdir, "dir")
     os.mkdir(path)
     path = os.path.join(path, "index.html")
@@ -325,7 +354,9 @@ def test_staticfiles_html_without_404(tmpdir, test_client_factory):
     assert exc_info.value.status_code == 404
 
 
-def test_staticfiles_html_only_files(tmpdir, test_client_factory):
+def test_staticfiles_html_only_files(
+    tmpdir: Path, test_client_factory: TestClientFactory
+) -> None:
     path = os.path.join(tmpdir, "hello.html")
     with open(path, "w") as file:
         file.write("<h1>Hello</h1>")
@@ -343,8 +374,8 @@ def test_staticfiles_html_only_files(tmpdir, test_client_factory):
 
 
 def test_staticfiles_cache_invalidation_for_deleted_file_html_mode(
-    tmpdir, test_client_factory
-):
+    tmpdir: Path, test_client_factory: TestClientFactory
+) -> None:
     path_404 = os.path.join(tmpdir, "404.html")
     with open(path_404, "w") as file:
         file.write("<p>404 file</p>")
@@ -382,8 +413,8 @@ def test_staticfiles_cache_invalidation_for_deleted_file_html_mode(
 
 
 def test_staticfiles_with_invalid_dir_permissions_returns_401(
-    tmp_path, test_client_factory
-):
+    tmp_path: Path, test_client_factory: TestClientFactory
+) -> None:
     (tmp_path / "example.txt").write_bytes(b"<file content>")
 
     original_mode = tmp_path.stat().st_mode
@@ -406,7 +437,9 @@ def test_staticfiles_with_invalid_dir_permissions_returns_401(
         tmp_path.chmod(original_mode)
 
 
-def test_staticfiles_with_missing_dir_returns_404(tmpdir, test_client_factory):
+def test_staticfiles_with_missing_dir_returns_404(
+    tmpdir: Path, test_client_factory: TestClientFactory
+) -> None:
     path = os.path.join(tmpdir, "example.txt")
     with open(path, "w") as file:
         file.write("<file content>")
@@ -420,7 +453,9 @@ def test_staticfiles_with_missing_dir_returns_404(tmpdir, test_client_factory):
     assert response.text == "Not Found"
 
 
-def test_staticfiles_access_file_as_dir_returns_404(tmpdir, test_client_factory):
+def test_staticfiles_access_file_as_dir_returns_404(
+    tmpdir: Path, test_client_factory: TestClientFactory
+) -> None:
     path = os.path.join(tmpdir, "example.txt")
     with open(path, "w") as file:
         file.write("<file content>")
@@ -435,9 +470,11 @@ def test_staticfiles_access_file_as_dir_returns_404(tmpdir, test_client_factory)
 
 
 def test_staticfiles_unhandled_os_error_returns_500(
-    tmpdir, test_client_factory, monkeypatch
-):
-    def mock_timeout(*args, **kwargs):
+    tmpdir: Path,
+    test_client_factory: TestClientFactory,
+    monkeypatch: pytest.MonkeyPatch,
+) -> None:
+    def mock_timeout(*args: typing.Any, **kwargs: typing.Any) -> None:
         raise TimeoutError
 
     path = os.path.join(tmpdir, "example.txt")
@@ -455,7 +492,9 @@ def test_staticfiles_unhandled_os_error_returns_500(
     assert response.text == "Internal Server Error"
 
 
-def test_staticfiles_follows_symlinks(tmpdir, test_client_factory):
+def test_staticfiles_follows_symlinks(
+    tmpdir: Path, test_client_factory: TestClientFactory
+) -> None:
     statics_path = os.path.join(tmpdir, "statics")
     os.mkdir(statics_path)
 
@@ -476,7 +515,9 @@ def test_staticfiles_follows_symlinks(tmpdir, test_client_factory):
     assert response.text == "<h1>Hello</h1>"
 
 
-def test_staticfiles_follows_symlink_directories(tmpdir, test_client_factory):
+def test_staticfiles_follows_symlink_directories(
+    tmpdir: Path, test_client_factory: TestClientFactory
+) -> None:
     statics_path = os.path.join(tmpdir, "statics")
     statics_html_path = os.path.join(statics_path, "html")
     os.mkdir(statics_path)
@@ -497,7 +538,7 @@ def test_staticfiles_follows_symlink_directories(tmpdir, test_client_factory):
     assert response.text == "<h1>Hello</h1>"
 
 
-def test_staticfiles_disallows_path_traversal_with_symlinks(tmpdir):
+def test_staticfiles_disallows_path_traversal_with_symlinks(tmpdir: Path) -> None:
     statics_path = os.path.join(tmpdir, "statics")
 
     root_source_path = tempfile.mkdtemp()
@@ -522,7 +563,7 @@ def test_staticfiles_disallows_path_traversal_with_symlinks(tmpdir):
     assert exc_info.value.detail == "Not Found"
 
 
-def test_staticfiles_avoids_path_traversal(tmp_path: Path):
+def test_staticfiles_avoids_path_traversal(tmp_path: Path) -> None:
     statics_path = tmp_path / "static"
     statics_disallow_path = tmp_path / "static_disallow"