From 150c7092e58cfe527e14b41909b685759c245459 Mon Sep 17 00:00:00 2001 From: Alexander Botello Date: Thu, 8 Nov 2018 03:33:20 -0600 Subject: [PATCH] StaticFiles will check if directory exists upon instantiation (#195) * Add check_dir parameter * Add test coverage for check_dir * Update documentation * linting * Remove extra space --- docs/staticfiles.md | 9 +++++++-- starlette/staticfiles.py | 4 +++- tests/test_staticfiles.py | 11 +++++++++-- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/docs/staticfiles.md b/docs/staticfiles.md index 448cc3b9..cc8db5e7 100644 --- a/docs/staticfiles.md +++ b/docs/staticfiles.md @@ -1,7 +1,12 @@ -Starlette also includes a `StaticFiles` class for serving a specific directory: +Starlette also includes a `StaticFiles` class for serving files in a given directory: -* `StaticFiles(directory)` - Serve any files in the given `directory`. +### StaticFiles + +Signature: `StaticFiles(directory, check_dir=True)` + +* `directory` - A string denoting the directory path +* `check_dir` - Ensure that the directory exists upon instantiation. Defaults to `True` You can combine this ASGI application with Starlette's routing to provide comprehensive static file serving. diff --git a/starlette/staticfiles.py b/starlette/staticfiles.py index 8a7527a6..ec2418ef 100644 --- a/starlette/staticfiles.py +++ b/starlette/staticfiles.py @@ -8,7 +8,9 @@ from starlette.types import ASGIInstance, Receive, Scope, Send class StaticFiles: - def __init__(self, *, directory: str) -> None: + def __init__(self, *, directory: str, check_dir: bool = True) -> None: + if check_dir and not os.path.isdir(directory): + raise RuntimeError("Directory '%s' does not exist" % directory) self.directory = directory self.config_checked = False diff --git a/tests/test_staticfiles.py b/tests/test_staticfiles.py index 9d12de89..84f33682 100644 --- a/tests/test_staticfiles.py +++ b/tests/test_staticfiles.py @@ -54,9 +54,16 @@ def test_staticfiles_with_missing_file_returns_404(tmpdir): assert response.text == "Not Found" +def test_staticfiles_instantiated_with_missing_directory(tmpdir): + with pytest.raises(RuntimeError) as exc: + path = os.path.join(tmpdir, "no_such_directory") + app = StaticFiles(directory=path) + assert "does not exist" in str(exc) + + def test_staticfiles_configured_with_missing_directory(tmpdir): path = os.path.join(tmpdir, "no_such_directory") - app = StaticFiles(directory=path) + app = StaticFiles(directory=path, check_dir=False) client = TestClient(app) with pytest.raises(RuntimeError) as exc: client.get("/example.txt") @@ -68,7 +75,7 @@ def test_staticfiles_configured_with_file_instead_of_directory(tmpdir): with open(path, "w") as file: file.write("") - app = StaticFiles(directory=path) + app = StaticFiles(directory=path, check_dir=False) client = TestClient(app) with pytest.raises(RuntimeError) as exc: client.get("/example.txt")